Add Grid.gridify() constructor

- the purpose of this constructor method is to generate all `Pixel`s
  for a `Grid` that have at least one `Address` assigned to them
- fix missing `UniqueConstraint` in `Grid` class => it was not possible
  to create two `Grid`s with the same `.side_length` in different cities
- change the `City.viewport` property into two separate `City.southwest`
  and `City.northeast` properties; also add `City.total_x` and
  `City.total_y` properties for convenience
This commit is contained in:
Alexander Hess 2021-01-05 18:58:48 +01:00
commit 776112d609
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
10 changed files with 224 additions and 57 deletions

View file

@ -3,7 +3,6 @@
import pytest
from tests.db.utils import test_locations as consts
from urban_meal_delivery import db
from urban_meal_delivery.db import utils
@ -55,33 +54,44 @@ class TestProperties:
assert result1 is result2
def test_viewport_overall(self, city):
"""Test `City.viewport` property."""
result = city.viewport
assert isinstance(result, dict)
assert len(result) == 2
@pytest.mark.parametrize('corner', ['northeast', 'southwest'])
def test_viewport_corners(self, city, city_data, corner):
"""Test `City.viewport` property."""
result = city.viewport[corner]
def test_northeast(self, city, city_data):
"""Test `City.northeast` property."""
result = city.northeast
assert isinstance(result, utils.Location)
assert result.latitude == pytest.approx(city_data[f'_{corner}_latitude'])
assert result.longitude == pytest.approx(city_data[f'_{corner}_longitude'])
assert result.latitude == pytest.approx(city_data['_northeast_latitude'])
assert result.longitude == pytest.approx(city_data['_northeast_longitude'])
def test_viewport_is_cached(self, city):
"""Test `City.viewport` property."""
result1 = city.viewport
result2 = city.viewport
def test_northeast_is_cached(self, city):
"""Test `City.northeast` property."""
result1 = city.northeast
result2 = city.northeast
assert result1 is result2
def test_city_as_xy_origin(self, city):
"""Test `City.as_xy_origin` property."""
result = city.as_xy_origin
def test_southwest(self, city, city_data):
"""Test `City.southwest` property."""
result = city.southwest
assert result.zone == consts.ZONE
assert consts.MIN_EASTING < result.easting < consts.MAX_EASTING
assert consts.MIN_NORTHING < result.northing < consts.MAX_NORTHING
assert isinstance(result, utils.Location)
assert result.latitude == pytest.approx(city_data['_southwest_latitude'])
assert result.longitude == pytest.approx(city_data['_southwest_longitude'])
def test_southwest_is_cached(self, city):
"""Test `City.southwest` property."""
result1 = city.southwest
result2 = city.southwest
assert result1 is result2
def test_total_x(self, city):
"""Test `City.total_x` property."""
result = city.total_x
assert result > 18_000
def test_total_y(self, city):
"""Test `City.total_y` property."""
result = city.total_y
assert result > 9_000

View file

@ -49,6 +49,18 @@ class TestConstraints:
):
db_session.execute(stmt)
def test_two_grids_with_identical_side_length(self, db_session, grid):
"""Insert a record that violates a unique constraint."""
db_session.add(grid)
db_session.commit()
# Create a `Grid` with the same `.side_length` in the same `.city`.
another_grid = db.Grid(city=grid.city, side_length=grid.side_length)
db_session.add(another_grid)
with pytest.raises(sa_exc.IntegrityError, match='duplicate key value'):
db_session.commit()
class TestProperties:
"""Test properties in `Grid`."""
@ -58,3 +70,86 @@ class TestProperties:
result = grid.pixel_area
assert result == 1.0
class TestGridification:
"""Test the `Grid.gridify()` constructor."""
def test_one_pixel_covering_entire_city_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.
"""
city.addresses = []
# `+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
def test_one_pixel_covering_entire_city_with_one_address(self, city, 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.
"""
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) == 1
def test_four_pixels_with_two_addresses(self, city, make_address):
"""Two `Address` objects in distinct `Pixel` objects."""
# Create two `Address` objects in distinct `Pixel`s.
city.addresses = [
# One `Address` in the lower-left `Pixel`, ...
make_address(latitude=48.8357377, longitude=2.2517412),
# ... and another one in the upper-right one.
make_address(latitude=48.8898312, longitude=2.4357622),
]
side_length = max(city.total_x // 2, city.total_y // 2) + 1
# By assumption of the test data.
n_pixels_x = (city.total_x // side_length) + 1
n_pixels_y = (city.total_y // side_length) + 1
assert n_pixels_x * n_pixels_y == 4
# Create a `Grid` with at most four `Pixel`s.
result = db.Grid.gridify(city=city, side_length=side_length)
assert isinstance(result, db.Grid)
assert len(result.pixels) == 2
@pytest.mark.db
@pytest.mark.no_cover
@pytest.mark.parametrize('side_length', [250, 500, 1_000, 2_000, 4_000, 8_000])
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.
This test creates confidence that the created `Grid`
objects adhere to the database constraints.
"""
city.addresses = [make_address() for _ in range(100)]
n_pixels_x = (city.total_x // side_length) + 1
n_pixels_y = (city.total_y // side_length) + 1
result = db.Grid.gridify(city=city, side_length=side_length)
assert isinstance(result, db.Grid)
assert 1 <= len(result.pixels) <= n_pixels_x * n_pixels_y
db_session.add(result)
db_session.commit()

View file

@ -425,7 +425,7 @@ class TestProperties:
@pytest.mark.db
@pytest.mark.no_cover
def test_make_random_orders( # noqa:C901,WPS211,WPS210,WPS213,WPS231
def test_make_random_orders( # noqa:C901,WPS211,WPS213,WPS231
db_session, make_address, make_courier, make_restaurant, make_order,
):
"""Sanity check the all the `make_*` fixtures.

View file

@ -35,7 +35,7 @@ def faraway_location():
@pytest.fixture
def origin(city):
"""A `Location` object based off the one and only `city`."""
obj = city.as_xy_origin
obj = city.southwest
assert obj.zone == ZONE # sanity check