Add CLI script to gridify all cities

- reorganize `urban_meal_delivery.console` into a sub-package
- move `tests.db.conftest` fixtures into `tests.conftest`
  => some integration tests regarding CLI scripts need a database
- add `urban_meal_delivery.console.decorators.db_revision` decorator
  to ensure the database is at a certain state before a CLI script runs
- refactor the `urban_meal_delivery.db.grids.Grid.gridify()` constructor:
  - bug fix: even empty `Pixel`s end up in the database temporarily
    => create `Pixel` objects only if an `Address` is to be assigned
       to it
  - streamline code and docstring
  - add further test cases
This commit is contained in:
Alexander Hess 2021-01-06 16:17:05 +01:00
commit 54ff377579
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
15 changed files with 372 additions and 160 deletions

View file

@ -1,101 +0,0 @@
"""Utils for testing the ORM layer."""
import pytest
import sqlalchemy as sa
from alembic import command as migrations_cmd
from alembic import config as migrations_config
from sqlalchemy import orm
from tests.db import fake_data
from urban_meal_delivery import config
from urban_meal_delivery import db
@pytest.fixture(scope='session', params=['all_at_once', 'sequentially'])
def db_connection(request):
"""Create all tables given the ORM models.
The tables are put into a distinct PostgreSQL schema
that is removed after all tests are over.
The database connection used to do that is yielded.
There are two modes for this fixture:
- "all_at_once": build up the tables all at once with MetaData.create_all()
- "sequentially": build up the tables sequentially with `alembic upgrade head`
This ensures that Alembic's migration files are consistent.
"""
# We need a fresh database connection for each of the two `params`.
# Otherwise, the first test of the parameter run second will fail.
engine = sa.create_engine(config.DATABASE_URI)
connection = engine.connect()
# Monkey patch the package's global `engine` and `connection` objects,
# just in case if it is used somewhere in the code base.
db.engine = engine
db.connection = connection
if request.param == 'all_at_once':
connection.execute(f'CREATE SCHEMA {config.CLEAN_SCHEMA};')
db.Base.metadata.create_all(connection)
else:
cfg = migrations_config.Config('alembic.ini')
migrations_cmd.upgrade(cfg, 'head')
try:
yield connection
finally:
connection.execute(f'DROP SCHEMA {config.CLEAN_SCHEMA} CASCADE;')
if request.param == 'sequentially':
tmp_alembic_version = f'{config.ALEMBIC_TABLE}_{config.CLEAN_SCHEMA}'
connection.execute(
f'DROP TABLE {config.ALEMBIC_TABLE_SCHEMA}.{tmp_alembic_version};',
)
connection.close()
@pytest.fixture
def db_session(db_connection):
"""A SQLAlchemy session that rolls back everything after a test case."""
# Begin the outermost transaction
# that is rolled back at the end of each test case.
transaction = db_connection.begin()
# Create a session bound to the same connection as the `transaction`.
# Using any other session would not result in the roll back.
session = orm.sessionmaker()(bind=db_connection)
# Monkey patch the package's global `session` object,
# which is used heavily in the code base.
db.session = session
try:
yield session
finally:
session.close()
transaction.rollback()
# Import the fixtures from the `fake_data` sub-package.
make_address = fake_data.make_address
make_courier = fake_data.make_courier
make_customer = fake_data.make_customer
make_order = fake_data.make_order
make_restaurant = fake_data.make_restaurant
address = fake_data.address
city = fake_data.city
city_data = fake_data.city_data
courier = fake_data.courier
customer = fake_data.customer
order = fake_data.order
restaurant = fake_data.restaurant
grid = fake_data.grid
pixel = fake_data.pixel

View file

@ -75,11 +75,15 @@ class TestProperties:
class TestGridification:
"""Test the `Grid.gridify()` constructor."""
def test_one_pixel_covering_entire_city_without_addresses(self, city):
@pytest.mark.no_cover
def test_one_pixel_without_addresses(self, city):
"""At the very least, there must be one `Pixel` ...
... if the `side_length` is greater than both the
horizontal and vertical distances of the viewport.
This test case skips the `for`-loop inside `Grid.gridify()`.
Interestingly, coverage.py does not see this.
"""
city.addresses = []
@ -91,7 +95,7 @@ class TestGridification:
assert isinstance(result, db.Grid)
assert len(result.pixels) == 0 # noqa:WPS507
def test_one_pixel_covering_entire_city_with_one_address(self, city, address):
def test_one_pixel_with_one_address(self, city, address):
"""At the very least, there must be one `Pixel` ...
... if the `side_length` is greater than both the
@ -107,8 +111,66 @@ class TestGridification:
assert isinstance(result, db.Grid)
assert len(result.pixels) == 1
def test_one_pixel_with_two_addresses(self, city, make_address):
"""At the very least, there must be one `Pixel` ...
... if the `side_length` is greater than both the
horizontal and vertical distances of the viewport.
This test case is necessary as `test_one_pixel_with_one_address`
does not have to re-use an already created `Pixel` object internally.
"""
city.addresses = [make_address(), make_address()]
# `+1` as otherwise there would be a second pixel in one direction.
side_length = max(city.total_x, city.total_y) + 1
result = db.Grid.gridify(city=city, side_length=side_length)
assert isinstance(result, db.Grid)
assert len(result.pixels) == 1
def test_one_pixel_with_address_too_far_south(self, city, address):
"""An `address` outside the `city`'s viewport is discarded."""
# Move the `address` just below `city.southwest`.
address.latitude = city.southwest.latitude - 0.1
city.addresses = [address]
# `+1` as otherwise there would be a second pixel in one direction.
side_length = max(city.total_x, city.total_y) + 1
result = db.Grid.gridify(city=city, side_length=side_length)
assert isinstance(result, db.Grid)
assert len(result.pixels) == 0 # noqa:WPS507
@pytest.mark.no_cover
def test_one_pixel_with_address_too_far_west(self, city, address):
"""An `address` outside the `city`'s viewport is discarded.
This test is a logical sibling to `test_one_pixel_with_address_too_far_south`
and therefore redundant.
"""
# Move the `address` just left to `city.southwest`.
address.longitude = city.southwest.longitude - 0.1
city.addresses = [address]
# `+1` as otherwise there would be a second pixel in one direction.
side_length = max(city.total_x, city.total_y) + 1
result = db.Grid.gridify(city=city, side_length=side_length)
assert isinstance(result, db.Grid)
assert len(result.pixels) == 0 # noqa:WPS507
@pytest.mark.no_cover
def test_four_pixels_with_two_addresses(self, city, make_address):
"""Two `Address` objects in distinct `Pixel` objects."""
"""Two `Address` objects in distinct `Pixel` objects.
This test is more of a sanity check.
"""
# Create two `Address` objects in distinct `Pixel`s.
city.addresses = [
# One `Address` in the lower-left `Pixel`, ...
@ -136,7 +198,7 @@ class TestGridification:
def test_make_random_grids(self, db_session, city, make_address, side_length):
"""With 100 random `Address` objects, a grid must have ...
... between 1 and a deterministic number of `Pixel` objects.
... between 1 and a deterministic upper bound of `Pixel` objects.
This test creates confidence that the created `Grid`
objects adhere to the database constraints.