|
| 1 | +from re import Pattern |
| 2 | +from typing import Any, Optional |
| 3 | + |
| 4 | +import pytest |
| 5 | +from pydantic import BaseModel, ValidationError |
| 6 | +from pydantic_core._pydantic_core import ArgsKwargs |
| 7 | + |
| 8 | +from pydantic_extra_types.coordinate import Coordinate, Latitude, Longitude |
| 9 | + |
| 10 | + |
| 11 | +class Coord(BaseModel): |
| 12 | + coord: Coordinate |
| 13 | + |
| 14 | + |
| 15 | +class Lat(BaseModel): |
| 16 | + lat: Latitude |
| 17 | + |
| 18 | + |
| 19 | +class Lng(BaseModel): |
| 20 | + lng: Longitude |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.parametrize( |
| 24 | + 'coord, result, error', |
| 25 | + [ |
| 26 | + # Valid coordinates |
| 27 | + ((20.0, 10.0), (20.0, 10.0), None), |
| 28 | + ((-90.0, 0.0), (-90.0, 0.0), None), |
| 29 | + (('20.0', 10.0), (20.0, 10.0), None), |
| 30 | + ((20.0, '10.0'), (20.0, 10.0), None), |
| 31 | + ((45.678, -123.456), (45.678, -123.456), None), |
| 32 | + (('45.678, -123.456'), (45.678, -123.456), None), |
| 33 | + (Coordinate(20.0, 10.0), (20.0, 10.0), None), |
| 34 | + (Coordinate(latitude=0, longitude=0), (0, 0), None), |
| 35 | + (ArgsKwargs(args=()), (0, 0), None), |
| 36 | + (ArgsKwargs(args=(1, 0.0)), (1.0, 0), None), |
| 37 | + # # Invalid coordinates |
| 38 | + ((), None, 'Field required'), # Empty tuple |
| 39 | + ((10.0,), None, 'Field required'), # Tuple with only one value |
| 40 | + (('ten, '), None, 'string is not recognized as a valid coordinate'), |
| 41 | + ((20.0, 10.0, 30.0), None, 'Tuple should have at most 2 items'), # Tuple with more than 2 values |
| 42 | + (ArgsKwargs(args=(1.0,)), None, 'Input should be a dictionary or an instance of Coordinate'), |
| 43 | + ( |
| 44 | + '20.0, 10.0, 30.0', |
| 45 | + None, |
| 46 | + 'Input should be a dictionary or an instance of Coordinate ', |
| 47 | + ), # Str with more than 2 values |
| 48 | + ('20.0, 10.0, 30.0', None, 'Unexpected positional argument'), # Str with more than 2 values |
| 49 | + (2, None, 'Input should be a dictionary or an instance of Coordinate'), # Wrong type |
| 50 | + ], |
| 51 | +) |
| 52 | +def test_format_for_coordinate(coord: (Any, Any), result: (float, float), error: Optional[Pattern]): |
| 53 | + if error is None: |
| 54 | + _coord: Coordinate = Coord(coord=coord).coord |
| 55 | + print('vars(_coord)', vars(_coord)) |
| 56 | + assert _coord.latitude == result[0] |
| 57 | + assert _coord.longitude == result[1] |
| 58 | + else: |
| 59 | + with pytest.raises(ValidationError, match=error): |
| 60 | + Coord(coord=coord).coord |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.parametrize( |
| 64 | + 'coord, error', |
| 65 | + [ |
| 66 | + # Valid coordinates |
| 67 | + ((-90.0, 0.0), None), |
| 68 | + ((50.0, 180.0), None), |
| 69 | + # Invalid coordinates |
| 70 | + ((-91.0, 0.0), 'Input should be greater than or equal to -90'), |
| 71 | + ((50.0, 181.0), 'Input should be less than or equal to 180'), |
| 72 | + ], |
| 73 | +) |
| 74 | +def test_limit_for_coordinate(coord: (Any, Any), error: Optional[Pattern]): |
| 75 | + if error is None: |
| 76 | + _coord: Coordinate = Coord(coord=coord).coord |
| 77 | + assert _coord.latitude == coord[0] |
| 78 | + assert _coord.longitude == coord[1] |
| 79 | + else: |
| 80 | + with pytest.raises(ValidationError, match=error): |
| 81 | + Coord(coord=coord).coord |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.parametrize( |
| 85 | + 'latitude, valid', |
| 86 | + [ |
| 87 | + # Valid latitude |
| 88 | + (20.0, True), |
| 89 | + (3.0000000000000000000000, True), |
| 90 | + (90.0, True), |
| 91 | + ('90.0', True), |
| 92 | + (-90.0, True), |
| 93 | + ('-90.0', True), |
| 94 | + # Unvalid latitude |
| 95 | + (91.0, False), |
| 96 | + (-91.0, False), |
| 97 | + ], |
| 98 | +) |
| 99 | +def test_format_latitude(latitude: float, valid: bool): |
| 100 | + if valid: |
| 101 | + _lat = Lat(lat=latitude).lat |
| 102 | + assert _lat == float(latitude) |
| 103 | + else: |
| 104 | + with pytest.raises(ValidationError, match='1 validation error for Lat'): |
| 105 | + Lat(lat=latitude) |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.parametrize( |
| 109 | + 'longitude, valid', |
| 110 | + [ |
| 111 | + # Valid latitude |
| 112 | + (20.0, True), |
| 113 | + (3.0000000000000000000000, True), |
| 114 | + (90.0, True), |
| 115 | + ('90.0', True), |
| 116 | + (-90.0, True), |
| 117 | + ('-90.0', True), |
| 118 | + (91.0, True), |
| 119 | + (-91.0, True), |
| 120 | + (180.0, True), |
| 121 | + (-180.0, True), |
| 122 | + # Unvalid latitude |
| 123 | + (181.0, False), |
| 124 | + (-181.0, False), |
| 125 | + ], |
| 126 | +) |
| 127 | +def test_format_longitude(longitude: float, valid: bool): |
| 128 | + if valid: |
| 129 | + _lng = Lng(lng=longitude).lng |
| 130 | + assert _lng == float(longitude) |
| 131 | + else: |
| 132 | + with pytest.raises(ValidationError, match='1 validation error for Lng'): |
| 133 | + Lng(lng=longitude) |
| 134 | + |
| 135 | + |
| 136 | +def test_str_repr(): |
| 137 | + assert str(Coord(coord=(20.0, 10.0)).coord) == '20.0,10.0' |
| 138 | + assert str(Coord(coord=('20.0, 10.0')).coord) == '20.0,10.0' |
| 139 | + assert repr(Coord(coord=(20.0, 10.0)).coord) == 'Coordinate(latitude=20.0, longitude=10.0)' |
| 140 | + |
| 141 | + |
| 142 | +def test_eq(): |
| 143 | + assert Coord(coord=(20.0, 10.0)).coord != Coord(coord='20.0,11.0').coord |
| 144 | + assert Coord(coord=('20.0, 10.0')).coord != Coord(coord='20.0,11.0').coord |
| 145 | + assert Coord(coord=('20.0, 10.0')).coord != Coord(coord='20.0,11.0').coord |
| 146 | + assert Coord(coord=(20.0, 10.0)).coord == Coord(coord='20.0,10.0').coord |
| 147 | + |
| 148 | + |
| 149 | +def test_hashable(): |
| 150 | + assert hash(Coord(coord=(20.0, 10.0)).coord) == hash(Coord(coord=(20.0, 10.0)).coord) |
| 151 | + assert hash(Coord(coord=(20.0, 11.0)).coord) != hash(Coord(coord=(20.0, 10.0)).coord) |
| 152 | + |
| 153 | + |
| 154 | +def test_json_schema(): |
| 155 | + class Model(BaseModel): |
| 156 | + value: Coordinate |
| 157 | + |
| 158 | + assert Model.model_json_schema(mode='validation')['$defs']['Coordinate'] == { |
| 159 | + 'properties': { |
| 160 | + 'latitude': {'maximum': 90.0, 'minimum': -90.0, 'title': 'Latitude', 'type': 'number'}, |
| 161 | + 'longitude': {'maximum': 180.0, 'minimum': -180.0, 'title': 'Longitude', 'type': 'number'}, |
| 162 | + }, |
| 163 | + 'required': ['latitude', 'longitude'], |
| 164 | + 'title': 'Coordinate', |
| 165 | + 'type': 'object', |
| 166 | + } |
| 167 | + assert Model.model_json_schema(mode='validation')['properties']['value'] == { |
| 168 | + 'anyOf': [ |
| 169 | + {'$ref': '#/$defs/Coordinate'}, |
| 170 | + { |
| 171 | + 'maxItems': 2, |
| 172 | + 'minItems': 2, |
| 173 | + 'prefixItems': [{'type': 'number'}, {'type': 'number'}], |
| 174 | + 'type': 'array', |
| 175 | + }, |
| 176 | + {'type': 'string'}, |
| 177 | + ], |
| 178 | + 'title': 'Value', |
| 179 | + } |
| 180 | + assert Model.model_json_schema(mode='serialization') == { |
| 181 | + '$defs': { |
| 182 | + 'Coordinate': { |
| 183 | + 'properties': { |
| 184 | + 'latitude': {'maximum': 90.0, 'minimum': -90.0, 'title': 'Latitude', 'type': 'number'}, |
| 185 | + 'longitude': {'maximum': 180.0, 'minimum': -180.0, 'title': 'Longitude', 'type': 'number'}, |
| 186 | + }, |
| 187 | + 'required': ['latitude', 'longitude'], |
| 188 | + 'title': 'Coordinate', |
| 189 | + 'type': 'object', |
| 190 | + } |
| 191 | + }, |
| 192 | + 'properties': {'value': {'allOf': [{'$ref': '#/$defs/Coordinate'}], 'title': 'Value'}}, |
| 193 | + 'required': ['value'], |
| 194 | + 'title': 'Model', |
| 195 | + 'type': 'object', |
| 196 | + } |
0 commit comments