Integrate the new Location class
- the old `UTMCoordinate` class becomes the new `Location` class - its main purpose is to represent locations in both lat-long coordinates as well as in the UTM system - remove `Address.__init__()` and `City.__init__()` methods as they are not executed for entries retrieved from the database - simplfiy the `Location.__init__()` => remove `relative_to` argument
This commit is contained in:
parent
2e3ccd14d5
commit
a1cbb808fd
8 changed files with 198 additions and 157 deletions
|
|
@ -6,6 +6,7 @@ import sqlalchemy as sqla
|
|||
from sqlalchemy import exc as sa_exc
|
||||
|
||||
from urban_meal_delivery import db
|
||||
from urban_meal_delivery.db import utils
|
||||
|
||||
|
||||
class TestSpecialMethods:
|
||||
|
|
@ -123,6 +124,24 @@ class TestProperties:
|
|||
|
||||
assert result is False
|
||||
|
||||
def test_location(self, address):
|
||||
"""Test `Address.location` property."""
|
||||
latitude = float(address.latitude)
|
||||
longitude = float(address.longitude)
|
||||
|
||||
result = address.location
|
||||
|
||||
assert isinstance(result, utils.Location)
|
||||
assert result.latitude == pytest.approx(latitude)
|
||||
assert result.longitude == pytest.approx(longitude)
|
||||
|
||||
def test_location_is_cached(self, address):
|
||||
"""Test `Address.location` property."""
|
||||
result1 = address.location
|
||||
result2 = address.location
|
||||
|
||||
assert result1 is result2
|
||||
|
||||
def test_x_is_positive(self, address):
|
||||
"""Test `Address.x` property."""
|
||||
result = address.x
|
||||
|
|
|
|||
|
|
@ -3,8 +3,9 @@
|
|||
|
||||
import pytest
|
||||
|
||||
from tests.db.utils import test_coordinates as consts
|
||||
from tests.db.utils import test_locations as consts
|
||||
from urban_meal_delivery import db
|
||||
from urban_meal_delivery.db import utils
|
||||
|
||||
|
||||
class TestSpecialMethods:
|
||||
|
|
@ -39,16 +40,22 @@ class TestConstraints:
|
|||
class TestProperties:
|
||||
"""Test properties in `City`."""
|
||||
|
||||
def test_location_data(self, city, city_data):
|
||||
"""Test `City.location` property."""
|
||||
result = city.location
|
||||
def test_center(self, city, city_data):
|
||||
"""Test `City.center` property."""
|
||||
result = city.center
|
||||
|
||||
assert isinstance(result, dict)
|
||||
assert len(result) == 2
|
||||
assert result['latitude'] == pytest.approx(city_data['_center_latitude'])
|
||||
assert result['longitude'] == pytest.approx(city_data['_center_longitude'])
|
||||
assert isinstance(result, utils.Location)
|
||||
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):
|
||||
def test_center_is_cached(self, city):
|
||||
"""Test `City.center` property."""
|
||||
result1 = city.center
|
||||
result2 = city.center
|
||||
|
||||
assert result1 is result2
|
||||
|
||||
def test_viewport_overall(self, city):
|
||||
"""Test `City.viewport` property."""
|
||||
result = city.viewport
|
||||
|
||||
|
|
@ -56,18 +63,24 @@ class TestProperties:
|
|||
assert len(result) == 2
|
||||
|
||||
@pytest.mark.parametrize('corner', ['northeast', 'southwest'])
|
||||
def test_viewport_data_corners(self, city, city_data, corner):
|
||||
def test_viewport_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[f'_{corner}_latitude'])
|
||||
assert result['longitude'] == pytest.approx(city_data[f'_{corner}_longitude'])
|
||||
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'])
|
||||
|
||||
def test_city_in_utm_coordinates(self, city):
|
||||
"""Test `City.as_origin` property."""
|
||||
result = city.as_origin
|
||||
def test_viewport_is_cached(self, city):
|
||||
"""Test `City.viewport` property."""
|
||||
result1 = city.viewport
|
||||
result2 = city.viewport
|
||||
|
||||
assert result1 is result2
|
||||
|
||||
def test_city_as_xy_origin(self, city):
|
||||
"""Test `City.as_xy_origin` property."""
|
||||
result = city.as_xy_origin
|
||||
|
||||
assert result.zone == consts.ZONE
|
||||
assert consts.MIN_EASTING < result.easting < consts.MAX_EASTING
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
"""Test the `UTMCoordinate` class."""
|
||||
"""Test the `Location` class."""
|
||||
# pylint:disable=no-self-use
|
||||
|
||||
import pytest
|
||||
|
|
@ -14,8 +14,8 @@ ZONE = '31U'
|
|||
|
||||
@pytest.fixture
|
||||
def location(address):
|
||||
"""A `UTMCoordinate` object based off the `address` fixture."""
|
||||
obj = utils.UTMCoordinate(address.latitude, address.longitude)
|
||||
"""A `Location` object based off the `address` fixture."""
|
||||
obj = utils.Location(address.latitude, address.longitude)
|
||||
|
||||
assert obj.zone == ZONE # sanity check
|
||||
|
||||
|
|
@ -24,8 +24,8 @@ def location(address):
|
|||
|
||||
@pytest.fixture
|
||||
def faraway_location():
|
||||
"""A `UTMCoordinate` object far away from the `location`."""
|
||||
obj = utils.UTMCoordinate(latitude=0, longitude=0)
|
||||
"""A `Location` object far away from the `location`."""
|
||||
obj = utils.Location(latitude=0, longitude=0)
|
||||
|
||||
assert obj.zone != ZONE # sanity check
|
||||
|
||||
|
|
@ -34,10 +34,8 @@ def faraway_location():
|
|||
|
||||
@pytest.fixture
|
||||
def origin(city):
|
||||
"""A `UTMCoordinate` object based off the one and only `city`."""
|
||||
# Use the `city`'s lower left viewport corner as the `(0, 0)` origin.
|
||||
lower_left = city.viewport['southwest']
|
||||
obj = utils.UTMCoordinate(lower_left['latitude'], lower_left['longitude'])
|
||||
"""A `Location` object based off the one and only `city`."""
|
||||
obj = city.as_xy_origin
|
||||
|
||||
assert obj.zone == ZONE # sanity check
|
||||
|
||||
|
|
@ -45,43 +43,17 @@ def origin(city):
|
|||
|
||||
|
||||
class TestSpecialMethods:
|
||||
"""Test special methods in `UTMCoordinate`."""
|
||||
"""Test special methods in `Location`."""
|
||||
|
||||
def test_create_utm_coordinates(self, location):
|
||||
"""Test instantiation of a new `UTMCoordinate` object."""
|
||||
"""Test instantiation of a new `Location` object."""
|
||||
assert location is not None
|
||||
|
||||
def test_create_utm_coordinates_with_origin(self, address, origin):
|
||||
"""Test instantiation with a `relate_to` argument."""
|
||||
result = utils.UTMCoordinate(
|
||||
latitude=address.latitude, longitude=address.longitude, relative_to=origin,
|
||||
)
|
||||
|
||||
assert result is not None
|
||||
|
||||
def test_create_utm_coordinates_with_non_utm_origin(self):
|
||||
"""Test instantiation with a `relate_to` argument of the wrong type."""
|
||||
with pytest.raises(TypeError, match='UTMCoordinate'):
|
||||
utils.UTMCoordinate(
|
||||
latitude=0, longitude=0, relative_to=object(),
|
||||
)
|
||||
|
||||
def test_create_utm_coordinates_with_invalid_origin(
|
||||
self, address, faraway_location,
|
||||
):
|
||||
"""Test instantiation with a `relate_to` argument at an invalid location."""
|
||||
with pytest.raises(ValueError, match='must be in the same UTM zone'):
|
||||
utils.UTMCoordinate(
|
||||
latitude=address.latitude,
|
||||
longitude=address.longitude,
|
||||
relative_to=faraway_location,
|
||||
)
|
||||
|
||||
def test_text_representation(self, location):
|
||||
"""The text representation is a non-literal."""
|
||||
result = repr(location)
|
||||
|
||||
assert result.startswith('<UTM:')
|
||||
assert result.startswith('<Location:')
|
||||
assert result.endswith('>')
|
||||
|
||||
@pytest.mark.e2e
|
||||
|
|
@ -103,7 +75,7 @@ class TestSpecialMethods:
|
|||
assert MIN_NORTHING < northing < MAX_NORTHING
|
||||
|
||||
def test_compare_utm_coordinates_to_different_data_type(self, location):
|
||||
"""Test `UTMCoordinate.__eq__()`."""
|
||||
"""Test `Location.__eq__()`."""
|
||||
result = location == object()
|
||||
|
||||
assert result is False
|
||||
|
|
@ -111,56 +83,68 @@ class TestSpecialMethods:
|
|||
def test_compare_utm_coordinates_to_far_away_coordinates(
|
||||
self, location, faraway_location,
|
||||
):
|
||||
"""Test `UTMCoordinate.__eq__()`."""
|
||||
"""Test `Location.__eq__()`."""
|
||||
with pytest.raises(ValueError, match='must be in the same zone'):
|
||||
bool(location == faraway_location)
|
||||
|
||||
def test_compare_utm_coordinates_to_equal_coordinates(self, location, address):
|
||||
"""Test `UTMCoordinate.__eq__()`."""
|
||||
same_location = utils.UTMCoordinate(address.latitude, address.longitude)
|
||||
"""Test `Location.__eq__()`."""
|
||||
same_location = utils.Location(address.latitude, address.longitude)
|
||||
|
||||
result = location == same_location
|
||||
|
||||
assert result is True
|
||||
|
||||
def test_compare_utm_coordinates_to_themselves(self, location):
|
||||
"""Test `UTMCoordinate.__eq__()`."""
|
||||
"""Test `Location.__eq__()`."""
|
||||
# pylint:disable=comparison-with-itself
|
||||
result = location == location # noqa:WPS312
|
||||
|
||||
assert result is True
|
||||
|
||||
def test_compare_utm_coordinates_to_different_coordinates(self, location, origin):
|
||||
"""Test `UTMCoordinate.__eq__()`."""
|
||||
"""Test `Location.__eq__()`."""
|
||||
result = location == origin
|
||||
|
||||
assert result is False
|
||||
|
||||
|
||||
class TestProperties:
|
||||
"""Test properties in `UTMCoordinate`."""
|
||||
"""Test properties in `Location`."""
|
||||
|
||||
def test_latitude(self, location, address):
|
||||
"""Test `Location.latitude` property."""
|
||||
result = location.latitude
|
||||
|
||||
assert result == pytest.approx(float(address.latitude))
|
||||
|
||||
def test_longitude(self, location, address):
|
||||
"""Test `Location.longitude` property."""
|
||||
result = location.longitude
|
||||
|
||||
assert result == pytest.approx(float(address.longitude))
|
||||
|
||||
def test_easting(self, location):
|
||||
"""Test `UTMCoordinate.easting` property."""
|
||||
"""Test `Location.easting` property."""
|
||||
result = location.easting
|
||||
|
||||
assert MIN_EASTING < result < MAX_EASTING
|
||||
|
||||
def test_northing(self, location):
|
||||
"""Test `UTMCoordinate.northing` property."""
|
||||
"""Test `Location.northing` property."""
|
||||
result = location.northing
|
||||
|
||||
assert MIN_NORTHING < result < MAX_NORTHING
|
||||
|
||||
def test_zone(self, location):
|
||||
"""Test `UTMCoordinate.zone` property."""
|
||||
"""Test `Location.zone` property."""
|
||||
result = location.zone
|
||||
|
||||
assert result == ZONE
|
||||
|
||||
|
||||
class TestRelateTo:
|
||||
"""Test the `UTMCoordinate.relate_to()` method and the `.x` and `.y` properties."""
|
||||
"""Test the `Location.relate_to()` method and the `.x` and `.y` properties."""
|
||||
|
||||
def test_run_relate_to_twice(self, location, origin):
|
||||
"""The `.relate_to()` method must only be run once."""
|
||||
|
|
@ -170,8 +154,8 @@ class TestRelateTo:
|
|||
location.relate_to(origin)
|
||||
|
||||
def test_call_relate_to_with_wrong_other_type(self, location):
|
||||
"""`other` must be another `UTMCoordinate`."""
|
||||
with pytest.raises(TypeError, match='UTMCoordinate'):
|
||||
"""`other` must be another `Location`."""
|
||||
with pytest.raises(TypeError, match='Location'):
|
||||
location.relate_to(object())
|
||||
|
||||
def test_call_relate_to_with_far_away_other(
|
||||
Loading…
Add table
Add a link
Reference in a new issue