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,65 +1,45 @@
"""Test the ORM's City model."""
"""Test the ORM's `City` model."""
# pylint:disable=no-self-use
import pytest
from sqlalchemy.orm import exc as orm_exc
from urban_meal_delivery import db
class TestSpecialMethods:
"""Test special methods in City."""
"""Test special methods in `City`."""
# pylint:disable=no-self-use
def test_create_city(self, city_data):
"""Test instantiation of a new City object."""
result = db.City(**city_data)
assert result is not None
def test_text_representation(self, city_data):
"""City has a non-literal text representation."""
city = db.City(**city_data)
name = city_data['name']
def test_create_city(self, city):
"""Test instantiation of a new `City` object."""
assert city is not None
def test_text_representation(self, city):
"""`City` has a non-literal text representation."""
result = repr(city)
assert result == f'<City({name})>'
assert result == f'<City({city.name})>'
@pytest.mark.e2e
@pytest.mark.db
@pytest.mark.no_cover
class TestConstraints:
"""Test the database constraints defined in City."""
"""Test the database constraints defined in `City`."""
# pylint:disable=no-self-use
def test_insert_into_database(self, db_session, city):
"""Insert an instance into the (empty) database."""
assert db_session.query(db.City).count() == 0
def test_insert_into_database(self, city, db_session):
"""Insert an instance into the database."""
db_session.add(city)
db_session.commit()
def test_dublicate_primary_key(self, city, city_data, db_session):
"""Can only add a record once."""
db_session.add(city)
db_session.commit()
another_city = db.City(**city_data)
db_session.add(another_city)
with pytest.raises(orm_exc.FlushError):
db_session.commit()
assert db_session.query(db.City).count() == 1
class TestProperties:
"""Test properties in City."""
# pylint:disable=no-self-use
def test_location_data(self, city_data):
"""Test City.location property."""
city = db.City(**city_data)
"""Test properties in `City`."""
def test_location_data(self, city, city_data):
"""Test `City.location` property."""
result = city.location
assert isinstance(result, dict)
@ -67,33 +47,19 @@ class TestProperties:
assert result['latitude'] == pytest.approx(city_data['_center_latitude'])
assert result['longitude'] == pytest.approx(city_data['_center_longitude'])
def test_viewport_data_overall(self, city_data):
"""Test City.viewport property."""
city = db.City(**city_data)
def test_viewport_data_overall(self, city):
"""Test `City.viewport` property."""
result = city.viewport
assert isinstance(result, dict)
assert len(result) == 2
def test_viewport_data_northeast(self, city_data):
"""Test City.viewport property."""
city = db.City(**city_data)
result = city.viewport['northeast']
@pytest.mark.parametrize('corner', ['northeast', 'southwest'])
def test_viewport_data_corners(self, city, city_data, corner):
"""Test `City.viewport` property."""
result = city.viewport[corner]
assert isinstance(result, dict)
assert len(result) == 2
assert result['latitude'] == pytest.approx(city_data['_northeast_latitude'])
assert result['longitude'] == pytest.approx(city_data['_northeast_longitude'])
def test_viewport_data_southwest(self, city_data):
"""Test City.viewport property."""
city = db.City(**city_data)
result = city.viewport['southwest']
assert isinstance(result, dict)
assert len(result) == 2
assert result['latitude'] == pytest.approx(city_data['_southwest_latitude'])
assert result['longitude'] == pytest.approx(city_data['_southwest_longitude'])
assert result['latitude'] == pytest.approx(city_data[f'_{corner}_latitude'])
assert result['longitude'] == pytest.approx(city_data[f'_{corner}_longitude'])