Skip to content

Commit 8a50ace

Browse files
committed
test: add testing to coordinates component
1 parent 38ffa12 commit 8a50ace

2 files changed

Lines changed: 100 additions & 0 deletions

File tree

data/tests/test_cities.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,3 +546,34 @@ def test_update_city_sanitizes_country_code(
546546

547547
# Verify sanitization occurred
548548
assert update_data[cities.COUNTRY_CODE] == 'CA'
549+
550+
def test_add_city_invalid_coordinates(self, sample_city_with_state):
551+
"""Test that add_city rejects out-of-range coordinates."""
552+
sample_city_with_state[cities.COORDINATES] = {
553+
cities.LATITUDE: 91,
554+
cities.LONGITUDE: -89.64,
555+
}
556+
557+
with patch('data.db_connect.create') as mock_create, \
558+
patch('data.states.state_exists', return_value=True), \
559+
patch('data.countries.country_exists', return_value=True):
560+
with pytest.raises(ValueError, match="latitude"):
561+
cities.add_city(sample_city_with_state)
562+
563+
mock_create.assert_not_called()
564+
565+
def test_update_city_invalid_coordinates(self, sample_city_with_state):
566+
"""Test that update_city rejects out-of-range coordinates."""
567+
update_data = {
568+
cities.COORDINATES: {
569+
cities.LATITUDE: 39.78,
570+
cities.LONGITUDE: -181,
571+
}
572+
}
573+
574+
with patch('data.cities.get_city_by_name_and_state', return_value=sample_city_with_state), \
575+
patch('data.db_connect.update') as mock_update:
576+
with pytest.raises(ValueError, match="longitude"):
577+
cities.update_city('Springfield', 'IL', update_data)
578+
579+
mock_update.assert_not_called()

data/tests/test_coordinates.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""
2+
Tests for the coordinate validation types.
3+
"""
4+
5+
import pytest
6+
7+
from data.coordinates import Coordinate, Coordinates, Latitude, Longitude
8+
9+
10+
class TestCoordinates:
11+
def test_coordinate_base_is_abstract(self):
12+
with pytest.raises(TypeError):
13+
Coordinate(10)
14+
15+
def test_construct_latitude(self):
16+
latitude = Latitude(39.78)
17+
assert isinstance(latitude, Latitude)
18+
19+
def test_construct_longitude(self):
20+
longitude = Longitude(-89.64)
21+
assert isinstance(longitude, Longitude)
22+
23+
def test_construct_latitude_bad_type(self):
24+
with pytest.raises(TypeError):
25+
Latitude("39.78")
26+
27+
def test_construct_longitude_bad_type(self):
28+
with pytest.raises(TypeError):
29+
Longitude("-89.64")
30+
31+
def test_construct_latitude_too_small(self):
32+
with pytest.raises(ValueError, match="latitude"):
33+
Latitude(-91)
34+
35+
def test_construct_latitude_too_large(self):
36+
with pytest.raises(ValueError, match="latitude"):
37+
Latitude(91)
38+
39+
def test_construct_longitude_too_small(self):
40+
with pytest.raises(ValueError, match="longitude"):
41+
Longitude(-181)
42+
43+
def test_construct_longitude_too_large(self):
44+
with pytest.raises(ValueError, match="longitude"):
45+
Longitude(181)
46+
47+
def test_coordinate_string_conversion(self):
48+
assert str(Latitude(39.78)) == "39.78"
49+
50+
def test_coordinate_float_conversion(self):
51+
assert float(Longitude(-89.64)) == -89.64
52+
53+
def test_coordinates_from_dict(self):
54+
coordinates = Coordinates.from_dict(
55+
{"latitude": 39.78, "longitude": -89.64}
56+
)
57+
assert coordinates.to_dict() == {"latitude": 39.78, "longitude": -89.64}
58+
59+
def test_coordinates_bad_container_type(self):
60+
with pytest.raises(TypeError):
61+
Coordinates.from_dict("39.78,-89.64")
62+
63+
def test_coordinates_missing_latitude(self):
64+
with pytest.raises(ValueError, match="Missing latitude"):
65+
Coordinates.from_dict({"longitude": -89.64})
66+
67+
def test_coordinates_missing_longitude(self):
68+
with pytest.raises(ValueError, match="Missing longitude"):
69+
Coordinates.from_dict({"latitude": 39.78})

0 commit comments

Comments
 (0)