|
| 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