2020-12-29 14:37:37 +01:00
|
|
|
"""Test the ORM's `City` model."""
|
2020-08-09 03:45:19 +02:00
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
2026-11-23 18:06:53 +01:00
|
|
|
from velofy import db
|
|
|
|
|
from velofy.db import utils
|
2020-08-09 03:45:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
2026-11-28 03:08:55 +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
|
|
|
|
2021-01-04 20:33:10 +01:00
|
|
|
def test_center(self, city, city_data):
|
|
|
|
|
"""Test `City.center` property."""
|
|
|
|
|
result = city.center
|
2020-08-09 03:45:19 +02:00
|
|
|
|
2021-01-04 20:33:10 +01:00
|
|
|
assert isinstance(result, utils.Location)
|
2026-11-28 03:08:55 +01:00
|
|
|
assert result.latitude == pytest.approx(city_data["center_latitude"])
|
|
|
|
|
assert result.longitude == pytest.approx(city_data["center_longitude"])
|
2021-01-04 20:33:10 +01:00
|
|
|
|
|
|
|
|
def test_center_is_cached(self, city):
|
|
|
|
|
"""Test `City.center` property."""
|
|
|
|
|
result1 = city.center
|
|
|
|
|
result2 = city.center
|
2020-08-09 03:45:19 +02:00
|
|
|
|
2021-01-04 20:33:10 +01:00
|
|
|
assert result1 is result2
|
|
|
|
|
|
2021-01-05 18:58:48 +01:00
|
|
|
def test_northeast(self, city, city_data):
|
|
|
|
|
"""Test `City.northeast` property."""
|
|
|
|
|
result = city.northeast
|
2020-08-09 03:45:19 +02:00
|
|
|
|
2021-01-05 18:58:48 +01:00
|
|
|
assert isinstance(result, utils.Location)
|
2026-11-28 03:08:55 +01:00
|
|
|
assert result.latitude == pytest.approx(city_data["northeast_latitude"])
|
|
|
|
|
assert result.longitude == pytest.approx(city_data["northeast_longitude"])
|
2021-01-05 18:58:48 +01:00
|
|
|
|
|
|
|
|
def test_northeast_is_cached(self, city):
|
|
|
|
|
"""Test `City.northeast` property."""
|
|
|
|
|
result1 = city.northeast
|
|
|
|
|
result2 = city.northeast
|
|
|
|
|
|
|
|
|
|
assert result1 is result2
|
2020-08-09 03:45:19 +02:00
|
|
|
|
2021-01-05 18:58:48 +01:00
|
|
|
def test_southwest(self, city, city_data):
|
|
|
|
|
"""Test `City.southwest` property."""
|
|
|
|
|
result = city.southwest
|
2020-08-09 03:45:19 +02:00
|
|
|
|
2021-01-04 20:33:10 +01:00
|
|
|
assert isinstance(result, utils.Location)
|
2026-11-28 03:08:55 +01:00
|
|
|
assert result.latitude == pytest.approx(city_data["southwest_latitude"])
|
|
|
|
|
assert result.longitude == pytest.approx(city_data["southwest_longitude"])
|
2021-01-04 20:33:10 +01:00
|
|
|
|
2021-01-05 18:58:48 +01:00
|
|
|
def test_southwest_is_cached(self, city):
|
|
|
|
|
"""Test `City.southwest` property."""
|
|
|
|
|
result1 = city.southwest
|
|
|
|
|
result2 = city.southwest
|
2021-01-04 20:33:10 +01:00
|
|
|
|
|
|
|
|
assert result1 is result2
|
2021-01-02 16:29:50 +01:00
|
|
|
|
2021-01-05 18:58:48 +01:00
|
|
|
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
|
2021-01-02 16:29:50 +01:00
|
|
|
|
2021-01-05 18:58:48 +01:00
|
|
|
assert result > 9_000
|