From c06c440e849b2bb6b8a1ff45c47009c30cfa2f47 Mon Sep 17 00:00:00 2001 From: Ben Botsford Date: Mon, 1 May 2023 11:13:26 -0500 Subject: [PATCH] fixes issue #5 --- README.md | 16 ++++++---- server/instance/app.db | 0 server/testing/app_test.py | 15 ++++++++-- server/testing/conftest.py | 2 +- server/testing/models/animal_test.py | 36 ++++++++++++++++++++-- server/testing/models/enclosure_test.py | 40 +++++++++++++++++++++++++ server/testing/models/zookeeper_test.py | 40 +++++++++++++++++++++++++ 7 files changed, 137 insertions(+), 12 deletions(-) delete mode 100644 server/instance/app.db create mode 100644 server/testing/models/enclosure_test.py create mode 100644 server/testing/models/zookeeper_test.py diff --git a/README.md b/README.md index 81143b73c..fe7e46b8c 100644 --- a/README.md +++ b/README.md @@ -45,12 +45,16 @@ Instructions begin here: `flask db upgrade`. - Your database should represent a zoo. There should be three tables: `animals`, `zookeepers`, and `enclosures`. -- The `Animal` model should contain a `name`, a `species`, a `zookeeper`, and - an `enclosure`. -- The `Zookeeper` model should contain a `name`, a `birthday`, and a list of - `animals` that they take care of. -- The `Enclosure` model should contain an `environment` (grass, sand, or water), - an `open_to_visitors` boolean, and a list of `animals`. +- The `Animal` model should contain a String `name`, a String `species`, a + `zookeeper_id`, and an `enclosure_id`. It should be related to zookeepers and + enclosures using `db.relationship()`. + - _Reminder: You can test any model by itself with + `pytest testing/models/{modelname}_test.py`._ +- The `Zookeeper` model should contain a String `name`, a String `birthday`, and + a list of `animals` that they take care of using `db.relationship()`. +- The `Enclosure` model should contain a String `environment` (grass, sand, or + water), a Boolean `open_to_visitors`, and a list of `animals` using + `db.relationship()`. - Your application should contain three views: `animal_by_id`, `zookeeper_by_id`, and `enclosure_by_id`. Their routes should be `animal/`, `zookeeper/`, and `enclosure/`, diff --git a/server/instance/app.db b/server/instance/app.db deleted file mode 100644 index e69de29bb..000000000 diff --git a/server/testing/app_test.py b/server/testing/app_test.py index 37cd0790a..7a57b5a0b 100644 --- a/server/testing/app_test.py +++ b/server/testing/app_test.py @@ -1,10 +1,21 @@ from os import environ import re -from app import app +from app import app, db +from server.models import Animal, Enclosure, Zookeeper class TestApp: - '''Flask application in flask_app.py''' + '''Flask application in app.py''' + + with app.app_context(): + a_1 = Animal() + a_2 = Animal() + e = Enclosure() + z = Zookeeper() + e.animals = [a_1, a_2] + z.animals = [a_1, a_2] + db.session.add_all([a_1, a_2, e, z]) + db.session.commit() def test_animal_route(self): '''has a resource available at "/animal/".''' diff --git a/server/testing/conftest.py b/server/testing/conftest.py index dad81d86b..7a12f155d 100644 --- a/server/testing/conftest.py +++ b/server/testing/conftest.py @@ -6,4 +6,4 @@ def pytest_itemcollected(item): pref = par.__doc__.strip() if par.__doc__ else par.__class__.__name__ suf = node.__doc__.strip() if node.__doc__ else node.__name__ if pref or suf: - item._nodeid = ' '.join((pref, suf)) + item._nodeid = ' '.join((pref, suf)) \ No newline at end of file diff --git a/server/testing/models/animal_test.py b/server/testing/models/animal_test.py index dfb685ea6..457f1280e 100644 --- a/server/testing/models/animal_test.py +++ b/server/testing/models/animal_test.py @@ -1,5 +1,5 @@ -from app import app -from server.models import db, Animal +from app import app, db +from server.models import Animal, Enclosure, Zookeeper class TestAnimal: '''Animal model in models.py''' @@ -14,4 +14,34 @@ def test_has_name_and_species(self): '''can be instantiated with a name and species.''' a = Animal(name='Phil', species='Rhinoceros') assert a.name == 'Phil' - assert a.species == 'Rhinoceros' \ No newline at end of file + assert a.species == 'Rhinoceros' + + def test_has_enclosure_id_and_zookeeper_id(self): + '''has foreign keys for enclosure_id and zookeeper_id.''' + a = Animal() + assert hasattr(a, 'enclosure_id') + assert hasattr(a, 'zookeeper_id') + + def test_can_be_saved_to_database(self): + '''can be added to a transaction and committed to create a database record.''' + with app.app_context(): + a = Animal() + db.session.add(a) + db.session.commit() + assert hasattr(a, 'id') + assert db.session.query(Animal).filter(Animal.id == a.id).one_or_none() + + def test_is_related_to_enclosures_and_zookeepers(self): + '''has access to its associated enclosure and zookeeper objects.''' + with app.app_context(): + a = Animal() + e = Enclosure() + z = Zookeeper() + db.session.add_all([a, e, z]) + db.session.commit() + a.enclosure_id = e.id + a.zookeeper_id = z.id + db.session.add(a) + db.session.commit() + assert a.enclosure == e + assert a.zookeeper == z diff --git a/server/testing/models/enclosure_test.py b/server/testing/models/enclosure_test.py new file mode 100644 index 000000000..d75e8e830 --- /dev/null +++ b/server/testing/models/enclosure_test.py @@ -0,0 +1,40 @@ +from app import app, db +from server.models import Animal, Enclosure + +class TestEnclosure: + '''Enclosure model in models.py''' + + def test_can_be_instantiated(self): + '''can be invoked to create a Python object.''' + e = Enclosure() + assert e + assert isinstance(e, Enclosure) + + def test_has_environment_and_open_to_visitors(self): + '''can be instantiated with an environment and open_to_visitors, a Boolean.''' + e = Enclosure(environment='Desert', open_to_visitors=False) + assert e.environment == 'Desert' + assert e.open_to_visitors == False + + def test_can_be_saved_to_database(self): + '''can be added to a transaction and committed to create a database record.''' + with app.app_context(): + e = Enclosure() + db.session.add(e) + db.session.commit() + assert hasattr(e, 'id') + assert db.session.query(Enclosure).filter(Enclosure.id == e.id).one_or_none() + + def test_is_related_to_animals(self): + '''has access to its associated animal objects.''' + with app.app_context(): + a_1 = Animal() + a_2 = Animal() + e = Enclosure() + db.session.add_all([a_1, a_2, e]) + db.session.commit() + e.animals.append(a_1) + e.animals.append(a_2) + db.session.add(e) + db.session.commit() + assert e.animals == [a_1, a_2] diff --git a/server/testing/models/zookeeper_test.py b/server/testing/models/zookeeper_test.py new file mode 100644 index 000000000..63c573aec --- /dev/null +++ b/server/testing/models/zookeeper_test.py @@ -0,0 +1,40 @@ +from app import app, db +from server.models import Animal, Zookeeper + +class TestZookeeper: + '''Zookeeper model in models.py''' + + def test_can_be_instantiated(self): + '''can be invoked to create a Python object.''' + z = Zookeeper() + assert z + assert isinstance(z, Zookeeper) + + def test_has_environment_and_open_to_visitors(self): + '''can be instantiated with a name and birthday.''' + z = Zookeeper(name='Steve Irwin', birthday='02/22/1962') + assert z.name == 'Steve Irwin' + assert z.birthday == '02/22/1962' + + def test_can_be_saved_to_database(self): + '''can be added to a transaction and committed to create a database record.''' + with app.app_context(): + z = Zookeeper() + db.session.add(z) + db.session.commit() + assert hasattr(z, 'id') + assert db.session.query(Zookeeper).filter(Zookeeper.id == z.id).one_or_none() + + def test_is_related_to_animals(self): + '''has access to its associated animal objects.''' + with app.app_context(): + a_1 = Animal() + a_2 = Animal() + z = Zookeeper() + db.session.add_all([a_1, a_2, z]) + db.session.commit() + z.animals.append(a_1) + z.animals.append(a_2) + db.session.add(z) + db.session.commit() + assert z.animals == [a_1, a_2]