Re-factor the ORM tests to use randomized fake data

- create `*Factory` classes with fakerboy and faker that generate
  randomized instances of the ORM models
- add new pytest marker: "db" are the integration tests involving the
  database whereas "e2e" will be all other integration tests
- streamline the docstrings in the ORM models
This commit is contained in:
Alexander Hess 2020-12-29 14:37:37 +01:00
commit 78dba23d5d
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
19 changed files with 1092 additions and 721 deletions

View file

@ -1,80 +1,70 @@
"""Test the ORM's Restaurant model."""
"""Test the ORM's `Restaurant` model."""
# pylint:disable=no-self-use
import pytest
import sqlalchemy as sqla
from sqlalchemy import exc as sa_exc
from sqlalchemy.orm import exc as orm_exc
from urban_meal_delivery import db
class TestSpecialMethods:
"""Test special methods in Restaurant."""
"""Test special methods in `Restaurant`."""
# pylint:disable=no-self-use
def test_create_restaurant(self, restaurant_data):
"""Test instantiation of a new Restaurant object."""
result = db.Restaurant(**restaurant_data)
assert result is not None
def test_text_representation(self, restaurant_data):
"""Restaurant has a non-literal text representation."""
restaurant = db.Restaurant(**restaurant_data)
name = restaurant_data['name']
def test_create_restaurant(self, restaurant):
"""Test instantiation of a new `Restaurant` object."""
assert restaurant is not None
def test_text_representation(self, restaurant):
"""`Restaurant` has a non-literal text representation."""
result = repr(restaurant)
assert result == f'<Restaurant({name})>'
assert result == f'<Restaurant({restaurant.name})>'
@pytest.mark.e2e
@pytest.mark.db
@pytest.mark.no_cover
class TestConstraints:
"""Test the database constraints defined in Restaurant."""
"""Test the database constraints defined in `Restaurant`."""
# pylint:disable=no-self-use
def test_insert_into_database(self, db_session, restaurant):
"""Insert an instance into the (empty) database."""
assert db_session.query(db.Restaurant).count() == 0
def test_insert_into_database(self, restaurant, db_session):
"""Insert an instance into the database."""
db_session.add(restaurant)
db_session.commit()
def test_dublicate_primary_key(self, restaurant, restaurant_data, db_session):
"""Can only add a record once."""
db_session.add(restaurant)
db_session.commit()
assert db_session.query(db.Restaurant).count() == 1
another_restaurant = db.Restaurant(**restaurant_data)
db_session.add(another_restaurant)
with pytest.raises(orm_exc.FlushError):
db_session.commit()
def test_delete_a_referenced_address(self, restaurant, address, db_session):
def test_delete_a_referenced_address(self, db_session, restaurant):
"""Remove a record that is referenced with a FK."""
db_session.add(restaurant)
db_session.commit()
with pytest.raises(sa_exc.IntegrityError):
db_session.execute(
db.Address.__table__.delete().where( # noqa:WPS609
db.Address.id == address.id,
),
)
# Must delete without ORM as otherwise an UPDATE statement is emitted.
stmt = sqla.delete(db.Address).where(db.Address.id == restaurant.address.id)
def test_negative_prep_duration(self, restaurant, db_session):
with pytest.raises(
sa_exc.IntegrityError, match='fk_restaurants_to_addresses_via_address_id',
):
db_session.execute(stmt)
def test_negative_prep_duration(self, db_session, restaurant):
"""Insert an instance with invalid data."""
restaurant.estimated_prep_duration = -1
db_session.add(restaurant)
with pytest.raises(sa_exc.IntegrityError):
with pytest.raises(
sa_exc.IntegrityError, match='realistic_estimated_prep_duration',
):
db_session.commit()
def test_too_high_prep_duration(self, restaurant, db_session):
def test_too_high_prep_duration(self, db_session, restaurant):
"""Insert an instance with invalid data."""
restaurant.estimated_prep_duration = 2500
db_session.add(restaurant)
with pytest.raises(sa_exc.IntegrityError):
with pytest.raises(
sa_exc.IntegrityError, match='realistic_estimated_prep_duration',
):
db_session.commit()