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,125 +1,108 @@
"""Test the ORM's Courier model."""
"""Test the ORM's `Courier` model."""
# pylint:disable=no-self-use
import pytest
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 Courier."""
"""Test special methods in `Courier`."""
# pylint:disable=no-self-use
def test_create_courier(self, courier_data):
"""Test instantiation of a new Courier object."""
result = db.Courier(**courier_data)
assert result is not None
def test_text_representation(self, courier_data):
"""Courier has a non-literal text representation."""
courier_data['id'] = 1
courier = db.Courier(**courier_data)
id_ = courier_data['id']
def test_create_courier(self, courier):
"""Test instantiation of a new `Courier` object."""
assert courier is not None
def test_text_representation(self, courier):
"""`Courier` has a non-literal text representation."""
result = repr(courier)
assert result == f'<Courier(#{id_})>'
assert result == f'<Courier(#{courier.id})>'
@pytest.mark.e2e
@pytest.mark.db
@pytest.mark.no_cover
class TestConstraints:
"""Test the database constraints defined in Courier."""
"""Test the database constraints defined in `Courier`."""
# pylint:disable=no-self-use
def test_insert_into_database(self, db_session, courier):
"""Insert an instance into the (empty) database."""
assert db_session.query(db.Courier).count() == 0
def test_insert_into_database(self, courier, db_session):
"""Insert an instance into the database."""
db_session.add(courier)
db_session.commit()
def test_dublicate_primary_key(self, courier, courier_data, db_session):
"""Can only add a record once."""
db_session.add(courier)
db_session.commit()
assert db_session.query(db.Courier).count() == 1
another_courier = db.Courier(**courier_data)
db_session.add(another_courier)
with pytest.raises(orm_exc.FlushError):
db_session.commit()
def test_invalid_vehicle(self, courier, db_session):
def test_invalid_vehicle(self, db_session, courier):
"""Insert an instance with invalid data."""
courier.vehicle = 'invalid'
db_session.add(courier)
with pytest.raises(sa_exc.IntegrityError):
with pytest.raises(sa_exc.IntegrityError, match='available_vehicle_types'):
db_session.commit()
def test_negative_speed(self, courier, db_session):
def test_negative_speed(self, db_session, courier):
"""Insert an instance with invalid data."""
courier.historic_speed = -1
db_session.add(courier)
with pytest.raises(sa_exc.IntegrityError):
with pytest.raises(sa_exc.IntegrityError, match='realistic_speed'):
db_session.commit()
def test_unrealistic_speed(self, courier, db_session):
def test_unrealistic_speed(self, db_session, courier):
"""Insert an instance with invalid data."""
courier.historic_speed = 999
db_session.add(courier)
with pytest.raises(sa_exc.IntegrityError):
with pytest.raises(sa_exc.IntegrityError, match='realistic_speed'):
db_session.commit()
def test_negative_capacity(self, courier, db_session):
def test_negative_capacity(self, db_session, courier):
"""Insert an instance with invalid data."""
courier.capacity = -1
db_session.add(courier)
with pytest.raises(sa_exc.IntegrityError):
with pytest.raises(sa_exc.IntegrityError, match='capacity_under_200_liters'):
db_session.commit()
def test_too_much_capacity(self, courier, db_session):
def test_too_much_capacity(self, db_session, courier):
"""Insert an instance with invalid data."""
courier.capacity = 999
db_session.add(courier)
with pytest.raises(sa_exc.IntegrityError):
with pytest.raises(sa_exc.IntegrityError, match='capacity_under_200_liters'):
db_session.commit()
def test_negative_pay_per_hour(self, courier, db_session):
def test_negative_pay_per_hour(self, db_session, courier):
"""Insert an instance with invalid data."""
courier.pay_per_hour = -1
db_session.add(courier)
with pytest.raises(sa_exc.IntegrityError):
with pytest.raises(sa_exc.IntegrityError, match='realistic_pay_per_hour'):
db_session.commit()
def test_too_much_pay_per_hour(self, courier, db_session):
def test_too_much_pay_per_hour(self, db_session, courier):
"""Insert an instance with invalid data."""
courier.pay_per_hour = 9999
db_session.add(courier)
with pytest.raises(sa_exc.IntegrityError):
with pytest.raises(sa_exc.IntegrityError, match='realistic_pay_per_hour'):
db_session.commit()
def test_negative_pay_per_order(self, courier, db_session):
def test_negative_pay_per_order(self, db_session, courier):
"""Insert an instance with invalid data."""
courier.pay_per_order = -1
db_session.add(courier)
with pytest.raises(sa_exc.IntegrityError):
with pytest.raises(sa_exc.IntegrityError, match='realistic_pay_per_order'):
db_session.commit()
def test_too_much_pay_per_order(self, courier, db_session):
def test_too_much_pay_per_order(self, db_session, courier):
"""Insert an instance with invalid data."""
courier.pay_per_order = 999
db_session.add(courier)
with pytest.raises(sa_exc.IntegrityError):
with pytest.raises(sa_exc.IntegrityError, match='realistic_pay_per_order'):
db_session.commit()