2020-12-29 14:37:37 +01:00
|
|
|
"""Test the ORM's `City` model."""
|
|
|
|
# pylint:disable=no-self-use
|
2020-08-09 03:45:19 +02:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2021-01-02 16:29:50 +01:00
|
|
|
from tests.db.utils import test_coordinates as consts
|
2020-08-09 03:45:19 +02:00
|
|
|
from urban_meal_delivery import db
|
|
|
|
|
|
|
|
|
|
|
|
class TestSpecialMethods:
|
2020-12-29 14:37:37 +01:00
|
|
|
"""Test special methods in `City`."""
|
2020-08-09 03:45:19 +02:00
|
|
|
|
2020-12-29 14:37:37 +01:00
|
|
|
def test_create_city(self, city):
|
|
|
|
"""Test instantiation of a new `City` object."""
|
|
|
|
assert city is not None
|
2020-08-09 03:45:19 +02:00
|
|
|
|
2020-12-29 14:37:37 +01:00
|
|
|
def test_text_representation(self, city):
|
|
|
|
"""`City` has a non-literal text representation."""
|
2020-08-09 03:45:19 +02:00
|
|
|
result = repr(city)
|
|
|
|
|
2020-12-29 14:37:37 +01:00
|
|
|
assert result == f'<City({city.name})>'
|
2020-08-09 03:45:19 +02:00
|
|
|
|
|
|
|
|
2020-12-29 14:37:37 +01:00
|
|
|
@pytest.mark.db
|
2020-08-09 03:45:19 +02:00
|
|
|
@pytest.mark.no_cover
|
|
|
|
class TestConstraints:
|
2020-12-29 14:37:37 +01:00
|
|
|
"""Test the database constraints defined in `City`."""
|
2020-08-09 03:45:19 +02:00
|
|
|
|
2020-12-29 14:37:37 +01:00
|
|
|
def test_insert_into_database(self, db_session, city):
|
|
|
|
"""Insert an instance into the (empty) database."""
|
|
|
|
assert db_session.query(db.City).count() == 0
|
2020-08-09 03:45:19 +02:00
|
|
|
|
|
|
|
db_session.add(city)
|
|
|
|
db_session.commit()
|
|
|
|
|
2020-12-29 14:37:37 +01:00
|
|
|
assert db_session.query(db.City).count() == 1
|
2020-08-09 03:45:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestProperties:
|
2020-12-29 14:37:37 +01:00
|
|
|
"""Test properties in `City`."""
|
2020-08-09 03:45:19 +02:00
|
|
|
|
2020-12-29 14:37:37 +01:00
|
|
|
def test_location_data(self, city, city_data):
|
|
|
|
"""Test `City.location` property."""
|
2020-08-09 03:45:19 +02:00
|
|
|
result = city.location
|
|
|
|
|
|
|
|
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'])
|
|
|
|
|
2020-12-29 14:37:37 +01:00
|
|
|
def test_viewport_data_overall(self, city):
|
|
|
|
"""Test `City.viewport` property."""
|
2020-08-09 03:45:19 +02:00
|
|
|
result = city.viewport
|
|
|
|
|
|
|
|
assert isinstance(result, dict)
|
|
|
|
assert len(result) == 2
|
|
|
|
|
2020-12-29 14:37:37 +01:00
|
|
|
@pytest.mark.parametrize('corner', ['northeast', 'southwest'])
|
|
|
|
def test_viewport_data_corners(self, city, city_data, corner):
|
|
|
|
"""Test `City.viewport` property."""
|
|
|
|
result = city.viewport[corner]
|
2020-08-09 03:45:19 +02:00
|
|
|
|
|
|
|
assert isinstance(result, dict)
|
|
|
|
assert len(result) == 2
|
2020-12-29 14:37:37 +01:00
|
|
|
assert result['latitude'] == pytest.approx(city_data[f'_{corner}_latitude'])
|
|
|
|
assert result['longitude'] == pytest.approx(city_data[f'_{corner}_longitude'])
|
2021-01-02 16:29:50 +01:00
|
|
|
|
|
|
|
def test_city_in_utm_coordinates(self, city):
|
|
|
|
"""Test `City.as_origin` property."""
|
|
|
|
result = city.as_origin
|
|
|
|
|
|
|
|
assert result.zone == consts.ZONE
|
|
|
|
assert consts.MIN_EASTING < result.easting < consts.MAX_EASTING
|
|
|
|
assert consts.MIN_NORTHING < result.northing < consts.MAX_NORTHING
|