-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtest_route.py
203 lines (161 loc) · 6.11 KB
/
test_route.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import json
import pytest
from weconnect.elements.route import Address, GeoCoordinate, Destination, Route
def test_valid_coordinates():
geo = GeoCoordinate(latitude=52.52, longitude=13.405)
assert geo.to_dict() == {"latitude": 52.52, "longitude": 13.405}
def test_invalid_coordinate_types():
with pytest.raises(TypeError):
GeoCoordinate(latitude="invalid", longitude="invalid")
def test_invalid_coordinates():
with pytest.raises(ValueError):
GeoCoordinate(latitude=255.0, longitude=512.0)
def test_edge_case_coordinates():
# Test with edge values like 0.0
GeoCoordinate(latitude=0.0, longitude=0.0)
def test_address_to_dict():
address = Address(
country="Germany", street="Unter den Linden", zipCode="10117", city="Berlin"
)
expected_dict = {
"country": "Germany",
"street": "Unter den Linden",
"zipCode": "10117",
"city": "Berlin",
}
assert address.to_dict() == expected_dict
def test_valid_destination():
geo = GeoCoordinate(latitude=52.52, longitude=13.405)
dest = Destination(geoCoordinate=geo, name="Brandenburg Gate")
expected_dict = {
"geoCoordinate": {"latitude": 52.52, "longitude": 13.405},
"destinationName": "Brandenburg Gate",
"poiProvider": "unknown",
"destinationSource": "MobileApp",
}
assert dest.to_dict() == expected_dict
def test_destination_missing_geo():
with pytest.raises(ValueError):
Destination(geoCoordinate=None)
def test_route_with_single_destination():
geo = GeoCoordinate(latitude=52.52, longitude=13.405)
dest = Destination(geoCoordinate=geo)
route = Route(destinations=dest)
expected_list = [
{
"geoCoordinate": {"latitude": 52.52, "longitude": 13.405},
"destinationName": "Destination",
"poiProvider": "unknown",
"destinationSource": "MobileApp",
}
]
assert route.to_list() == expected_list
def test_route_with_multiple_destinations():
geo1 = GeoCoordinate(latitude=52.52, longitude=13.405)
dest1 = Destination(geoCoordinate=geo1)
geo2 = GeoCoordinate(latitude=48.8566, longitude=2.3522)
dest2 = Destination(geoCoordinate=geo2, name="Eiffel Tower")
route = Route(destinations=[dest1, dest2])
expected_list = [
{
"geoCoordinate": {"latitude": 52.52, "longitude": 13.405},
"destinationName": "Destination",
"poiProvider": "unknown",
"destinationSource": "MobileApp",
"destinationType": "stopover",
},
{
"geoCoordinate": {"latitude": 48.8566, "longitude": 2.3522},
"destinationName": "Eiffel Tower",
"poiProvider": "unknown",
"destinationSource": "MobileApp",
},
]
assert route.to_list() == expected_list
def test_route_from_collection():
data = [
{
"geoCoordinate": {"latitude": 52.52, "longitude": 13.405},
"name": "Brandenburg Gate",
},
{
"geoCoordinate": {"latitude": 48.8566, "longitude": 2.3522},
"name": "Eiffel Tower",
},
]
route = Route.from_collection(data)
assert len(route.destinations) == 2
def test_invalid_destination_geo():
with pytest.raises(ValueError):
Destination(geoCoordinate=None)
def test_route_with_invalid_destinations():
with pytest.raises(TypeError):
Route(destinations="not a list")
def test_route_from_invalid_collection():
with pytest.raises(ValueError):
Route.from_collection("invalid data")
def test_route_from_value_with_destination():
geo = GeoCoordinate(latitude=52.52, longitude=13.405)
dest = Destination(geoCoordinate=geo, name="Brandenburg Gate")
route = Route.from_value(dest)
assert isinstance(route, Route)
assert len(route.destinations) == 1
assert route.destinations[0].name == "Brandenburg Gate"
def test_route_from_value_with_list_of_destinations():
geo1 = GeoCoordinate(latitude=52.52, longitude=13.405)
dest1 = Destination(geoCoordinate=geo1)
geo2 = GeoCoordinate(latitude=48.8566, longitude=2.3522)
dest2 = Destination(geoCoordinate=geo2, name="Eiffel Tower")
route = Route.from_value([dest1, dest2])
assert isinstance(route, Route)
assert len(route.destinations) == 2
def test_route_from_value_with_dict():
data = {
"geoCoordinate": {"latitude": 52.52, "longitude": 13.405},
"name": "Brandenburg Gate",
}
route = Route.from_value(data)
assert isinstance(route, Route)
assert len(route.destinations) == 1
assert route.destinations[0].name == "Brandenburg Gate"
def test_route_from_value_with_list_of_dicts():
data = [
{
"geoCoordinate": {"latitude": 52.52, "longitude": 13.405},
"name": "Brandenburg Gate",
},
{
"geoCoordinate": {"latitude": 48.8566, "longitude": 2.3522},
"name": "Eiffel Tower",
},
]
route = Route.from_value(data)
assert isinstance(route, Route)
assert len(route.destinations) == 2
assert route.destinations[1].name == "Eiffel Tower"
def test_route_from_value_with_json_string():
data = [
{
"geoCoordinate": {"latitude": 52.52, "longitude": 13.405},
"name": "Brandenburg Gate",
},
{
"geoCoordinate": {"latitude": 48.8566, "longitude": 2.3522},
"name": "Eiffel Tower",
},
]
json_data = json.dumps(data)
route = Route.from_value(json_data)
assert isinstance(route, Route)
assert len(route.destinations) == 2
assert route.destinations[0].name == "Brandenburg Gate"
def test_route_from_value_with_invalid_json_string():
invalid_json = '{"geoCoordinate": {"latitude": 52.52, "longitude": 13.405}, "name": "Brandenburg Gate"' # Missing closing brace
with pytest.raises(json.JSONDecodeError):
Route.from_value(invalid_json)
def test_route_from_value_with_invalid_type():
with pytest.raises(TypeError):
Route.from_value(12345)
def test_route_from_value_with_none():
with pytest.raises(TypeError):
Route.from_value(None)