Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<int:id>`, `zookeeper/<int:id>`, and `enclosure/<int:id>`,
Expand Down
Empty file removed server/instance/app.db
Empty file.
15 changes: 13 additions & 2 deletions server/testing/app_test.py
Original file line number Diff line number Diff line change
@@ -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/<id>".'''
Expand Down
2 changes: 1 addition & 1 deletion server/testing/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
36 changes: 33 additions & 3 deletions server/testing/models/animal_test.py
Original file line number Diff line number Diff line change
@@ -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'''
Expand All @@ -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'
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
40 changes: 40 additions & 0 deletions server/testing/models/enclosure_test.py
Original file line number Diff line number Diff line change
@@ -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]
40 changes: 40 additions & 0 deletions server/testing/models/zookeeper_test.py
Original file line number Diff line number Diff line change
@@ -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]