forked from kovacsbalu/WazeRouteCalculator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
352 lines (322 loc) · 18.5 KB
/
tests.py
File metadata and controls
352 lines (322 loc) · 18.5 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# -*- coding: utf-8 -*-
import WazeRouteCalculator as wrc
import mock
import requests_mock
class TestWRC():
def setup_method(self, method):
self.waze_url = "https://www.waze.com/"
self.address_req = self.waze_url + "SearchServer/mozi"
self.routing_req = self.waze_url + "row-RoutingManager/routingRequest"
self.lat = 47.4979
self.lon = 19.0402
self.bounds = {"bottom": 47.4, "top": 47.5, "left": 19, "right": 19.3}
self.length = 400
self.time = 60
self.address_to_coords_response = '[{"location":{"lat":%s,"lon":%s},"bounds":%s}]' % (self.lat, self.lon, str(self.bounds).replace("'", '"'))
self.routing_response = '{"response":{"results":[{"length":%s,"crossTime":%s}]}}' % (self.length, self.time)
def test_address_to_coords(self):
from_address = 'From address'
to_address = 'To address'
test_address = "Testaddress"
with requests_mock.mock() as m:
m.get(self.address_req, text=self.address_to_coords_response)
route = wrc.WazeRouteCalculator(from_address, to_address)
coords = route.address_to_coords(test_address)
assert coords == {'lat': self.lat, 'lon': self.lon, 'bounds': self.bounds}
assert m.call_count == 3
def test_address_to_coords_reversed(self):
from_address = 'From address'
to_address = 'To address'
test_address = "Testaddress"
bounds = {"top": 47.4, "bottom": 47.5, "right": 19, "left": 19.3}
address_to_coords_response = '[{"location":{"lat":%s,"lon":%s},"bounds":%s}]' % (self.lat, self.lon, str(bounds).replace("'", '"'))
with requests_mock.mock() as m:
m.get(self.address_req, text=address_to_coords_response)
route = wrc.WazeRouteCalculator(from_address, to_address)
coords = route.address_to_coords(test_address)
assert coords == {'lat': self.lat, 'lon': self.lon, 'bounds': self.bounds}
assert m.call_count == 3
def test_get_route_eu(self):
self.routing_req = self.waze_url + "row-RoutingManager/routingRequest"
with requests_mock.mock() as m:
m.get(self.address_req, text=self.address_to_coords_response)
m.get(self.routing_req, text=self.routing_response)
route = wrc.WazeRouteCalculator("", "", "EU")
response = route.get_route()
assert response == {"results": [{"length": self.length, "crossTime": self.time}]}
assert self.routing_req in m.request_history[2].url
def test_get_route_us(self):
self.routing_req = self.waze_url + "RoutingManager/routingRequest"
with requests_mock.mock() as m:
m.get(self.address_req, text=self.address_to_coords_response)
m.get(self.routing_req, text=self.routing_response)
route = wrc.WazeRouteCalculator("", "", "US")
response = route.get_route()
assert response == {"results": [{"length": self.length, "crossTime": self.time}]}
assert self.routing_req in m.request_history[2].url
def test_get_route_na(self):
"""NA (North America) is an alias for US (United States)"""
self.routing_req = self.waze_url + "RoutingManager/routingRequest"
with requests_mock.mock() as m:
m.get(self.address_req, text=self.address_to_coords_response)
m.get(self.routing_req, text=self.routing_response)
route = wrc.WazeRouteCalculator("", "", "na")
response = route.get_route()
assert response == {"results": [{"length": self.length, "crossTime": self.time}]}
assert self.routing_req in m.request_history[2].url
def test_get_route_il(self):
self.routing_req = self.waze_url + "il-RoutingManager/routingRequest"
with requests_mock.mock() as m:
m.get(self.address_req, text=self.address_to_coords_response)
m.get(self.routing_req, text=self.routing_response)
route = wrc.WazeRouteCalculator("", "", "il")
response = route.get_route()
assert response == {"results": [{"length": self.length, "crossTime": self.time}]}
assert self.routing_req in m.request_history[2].url
def test_get_route(self):
with requests_mock.mock() as m:
m.get(self.address_req, text=self.address_to_coords_response)
m.get(self.routing_req, text=self.routing_response)
route = wrc.WazeRouteCalculator("", "")
response = route.get_route()
assert response == {"results": [{"length": self.length, "crossTime": self.time}]}
assert self.routing_req in m.request_history[2].url
def test_get_all_routes(self):
length2, time2 = (410, 62)
self.routing_response = '{"alternatives":[{"response":{"results":[{"length":%s,"crossTime":%s}]}},{"response":{"results":[{"length":%s,"crossTime":%s}]}}]}' % (self.length, self.time, length2, time2)
with requests_mock.mock() as m:
m.get(self.address_req, text=self.address_to_coords_response)
m.get(self.routing_req, text=self.routing_response)
route = wrc.WazeRouteCalculator("", "")
response = route.get_route(3)
assert response == [{"results": [{"length": self.length, "crossTime": self.time}]}, {"results": [{"length": length2, "crossTime": time2}]}]
assert self.routing_req in m.request_history[2].url
def test_get_route_only_one(self):
with requests_mock.mock() as m:
m.get(self.address_req, text=self.address_to_coords_response)
m.get(self.routing_req, text=self.routing_response)
route = wrc.WazeRouteCalculator("", "")
response = route.get_route(3)
assert response == [{"results": [{"length": self.length, "crossTime": self.time}]}]
assert self.routing_req in m.request_history[2].url
def test_add_up_route(self):
with requests_mock.mock() as m:
m.get(self.address_req, text=self.address_to_coords_response)
route = wrc.WazeRouteCalculator("", "")
results = [{"length": 1000, "crossTime": 120}, {"length": 1000, "crossTime": 120}]
time, dist = route._add_up_route(results)
assert time == 4.00
assert dist == 2.00
def test_add_up_route_real_time(self):
with requests_mock.mock() as m:
m.get(self.address_req, text=self.address_to_coords_response)
route = wrc.WazeRouteCalculator("", "")
results = [{"length": 1000, "crossTime": 120, "crossTimeWithoutRealTime": 110}, {"length": 1000, "crossTime": 120, "crossTimeWithoutRealTime": 115}]
time, dist = route._add_up_route(results, real_time=True)
assert time == 4.00
assert dist == 2.00
def test_add_up_route_no_real_time(self):
with requests_mock.mock() as m:
m.get(self.address_req, text=self.address_to_coords_response)
route = wrc.WazeRouteCalculator("", "")
results = [{"length": 1000, "crossTime": 120, "crossTimeWithoutRealTime": 110}, {"length": 1000, "crossTime": 120, "crossTimeWithoutRealTime": 115}]
time, dist = route._add_up_route(results, real_time=False)
assert time == 3.75
assert dist == 2.00
def test_calc_route_info(self):
with requests_mock.mock() as m:
m.get(self.address_req, text=self.address_to_coords_response)
route = wrc.WazeRouteCalculator("", "")
route_mock = mock.Mock(return_value={"results": [{"length": 1000, "crossTime": 120}]})
route.get_route = route_mock
time, dist = route.calc_route_info()
assert route_mock.called
assert time == 2.00
assert dist == 1.00
def test_calc_all_routes_info(self):
with requests_mock.mock() as m:
m.get(self.address_req, text=self.address_to_coords_response)
route = wrc.WazeRouteCalculator("", "")
route_mock = mock.Mock(return_value=[{"routeName": "1", "results": [{"length": 1000, "crossTime": 120}]}, {"routeName": "2", "results": [{"length": 1100, "crossTime": 150}]}])
route.get_route = route_mock
results = route.calc_all_routes_info()
assert route_mock.called
assert results == {"1": (2.0, 1.0), "2": (2.5, 1.1)}
def test_calc_route_info_nort(self):
with requests_mock.mock() as m:
m.get(self.address_req, text=self.address_to_coords_response)
route = wrc.WazeRouteCalculator("", "")
route_mock = mock.Mock(return_value={"results": [{"length": 1000, "crossTime": 140, "crossTimeWithoutRealTime": 120}]})
route.get_route = route_mock
time, dist = route.calc_route_info(real_time=False)
assert route_mock.called
assert time == 2.00
assert dist == 1.00
def test_calc_all_routes_info_nort(self):
with requests_mock.mock() as m:
m.get(self.address_req, text=self.address_to_coords_response)
route = wrc.WazeRouteCalculator("", "")
route_mock = mock.Mock(return_value=[{"routeName": "1", "results": [{"length": 1000, "crossTime": 130, "crossTimeWithoutRealTime": 120}]}, {"routeName": "2", "results": [{"length": 1100, "crossTime": 150, "crossTimeWithoutRealTime": 120}]}])
route.get_route = route_mock
results = route.calc_all_routes_info(real_time=False)
assert route_mock.called
assert results == {"1": (2.0, 1.0), "2": (2.0, 1.1)}
def test_calc_all_routes_info_only_one(self):
with requests_mock.mock() as m:
m.get(self.address_req, text=self.address_to_coords_response)
route = wrc.WazeRouteCalculator("", "")
route_mock = mock.Mock(return_value=[{"routeName": "1", "results": [{"length": 1000, "crossTime": 120}]}])
route.get_route = route_mock
results = route.calc_all_routes_info()
assert route_mock.called
assert results == {"1": (2.0, 1.0)}
def test_calc_route_info_with_path_not_ignored(self):
with requests_mock.mock() as m:
lat = [47.49, 47.612, 47.645]
lon = [19.04, 18.99, 18.82]
bounds = [{"bottom": 47.4, "top": 47.5, "left": 19, "right": 19.1}, None, {"bottom": 47.6, "top": 47.7, "left": 18.8, "right": 18.9}]
length = [400, 5000, 500]
time = [40, 300, 50]
address_to_coords_response = [
'[{"location":{"lat":%s,"lon":%s},"bounds":%s}]' % (lat[0], lon[0], str(bounds[0]).replace("'", '"')),
'[{"location":{"lat":%s,"lon":%s},"bounds":%s}]' % (lat[2], lon[2], str(bounds[2]).replace("'", '"'))
]
m.get(self.address_req, [{'text': address_to_coords_response[0]}, {'text': address_to_coords_response[1]}])
route = wrc.WazeRouteCalculator("", "")
route_mock = mock.Mock(return_value={"results": [
{"length": length[0], "crossTime": time[0], "path": {"x": lon[0], "y": lat[0]}},
{"length": length[1], "crossTime": time[1], "path": {"x": lon[1], "y": lat[1]}},
{"length": length[2], "crossTime": time[2], "path": {"x": lon[2], "y": lat[2]}}
]})
route.get_route = route_mock
time, dist = route.calc_route_info()
assert route_mock.called
assert time == 6.5
assert dist == 5.9
def test_calc_route_info_with_ignored(self):
with requests_mock.mock() as m:
lat = [47.49, 47.612, 47.645]
lon = [19.04, 18.99, 18.82]
bounds = [{"bottom": 47.4, "top": 47.5, "left": 19, "right": 19.1}, None, {"bottom": 47.6, "top": 47.7, "left": 18.8, "right": 18.9}]
length = [400, 5000, 500]
time = [40, 300, 50]
address_to_coords_response = [
'[{"location":{"lat":%s,"lon":%s},"bounds":%s}]' % (lat[0], lon[0], str(bounds[0]).replace("'", '"')),
'[{"location":{"lat":%s,"lon":%s},"bounds":%s}]' % (lat[2], lon[2], str(bounds[2]).replace("'", '"'))
]
m.get(self.address_req, [{'text': address_to_coords_response[0]}, {'text': address_to_coords_response[1]}])
route = wrc.WazeRouteCalculator("", "")
route_mock = mock.Mock(return_value={"results": [
{"length": length[0], "crossTime": time[0], "path": {"x": lon[0], "y": lat[0]}},
{"length": length[1], "crossTime": time[1], "path": {"x": lon[1], "y": lat[1]}},
{"length": length[2], "crossTime": time[2], "path": {"x": lon[2], "y": lat[2]}}
]})
route.get_route = route_mock
time, dist = route.calc_route_info(stop_at_bounds=True)
assert route_mock.called
assert time == 5.00
assert dist == 5.00
def test_calc_route_info_with_ignored_and_nort(self):
with requests_mock.mock() as m:
lat = [47.49, 47.612, 47.645]
lon = [19.04, 18.99, 18.82]
bounds = [{"bottom": 47.4, "top": 47.5, "left": 19, "right": 19.1}, None, {"bottom": 47.6, "top": 47.7, "left": 18.8, "right": 18.9}]
length = [400, 5000, 500]
time = [40, 360, 60]
nort_time = [40, 300, 50]
address_to_coords_response = [
'[{"location":{"lat":%s,"lon":%s},"bounds":%s}]' % (lat[0], lon[0], str(bounds[0]).replace("'", '"')),
'[{"location":{"lat":%s,"lon":%s},"bounds":%s}]' % (lat[2], lon[2], str(bounds[2]).replace("'", '"'))
]
m.get(self.address_req, [{'text': address_to_coords_response[0]}, {'text': address_to_coords_response[1]}])
route = wrc.WazeRouteCalculator("", "")
route_mock = mock.Mock(return_value={"results": [
{"length": length[0], "crossTime": time[0], "crossTimeWithoutRealTime": nort_time[0], "path": {"x": lon[0], "y": lat[0]}},
{"length": length[1], "crossTime": time[1], "crossTimeWithoutRealTime": nort_time[1], "path": {"x": lon[1], "y": lat[1]}},
{"length": length[2], "crossTime": time[2], "crossTimeWithoutRealTime": nort_time[2], "path": {"x": lon[2], "y": lat[2]}}
]})
route.get_route = route_mock
time, dist = route.calc_route_info(stop_at_bounds=True, real_time=False)
assert route_mock.called
assert time == 5.00
assert dist == 5.00
def test_calc_route_info_stopatbounds_missing_bounds(self):
with requests_mock.mock() as m:
lat = [47.49, 47.612, 47.645]
lon = [19.04, 18.99, 18.82]
bounds = [None, None, {"bottom": 47.6, "top": 47.7, "left": 18.8, "right": 18.9}]
length = [400, 5000, 500]
time = [45, 300, 60]
address_to_coords_response = [
'[{"location":{"lat":%s,"lon":%s},"bounds":null}]' % (lat[0], lon[0]),
'[{"location":{"lat":%s,"lon":%s},"bounds":%s}]' % (lat[2], lon[2], str(bounds[2]).replace("'", '"'))
]
m.get(self.address_req, [{'text': address_to_coords_response[0]}, {'text': address_to_coords_response[1]}])
route = wrc.WazeRouteCalculator("", "")
route_mock = mock.Mock(return_value={"results": [
{"length": length[0], "crossTime": time[0], "path": {"x": lon[0], "y": lat[0]}},
{"length": length[1], "crossTime": time[1], "path": {"x": lon[1], "y": lat[1]}},
{"length": length[2], "crossTime": time[2], "path": {"x": lon[2], "y": lat[2]}}
]})
route.get_route = route_mock
time, dist = route.calc_route_info(stop_at_bounds=True)
assert route_mock.called
assert time == 5.75
assert dist == 5.40
def test_calc_all_routes_info_with_ignored(self):
with requests_mock.mock() as m:
lat = [47.49, [47.612, 47.614, 47.56], 47.645]
lon = [19.04, [18.99, 18.99, 19.01], 18.82]
bounds = [{"bottom": 47.4, "top": 47.5, "left": 19, "right": 19.1}, None, {"bottom": 47.6, "top": 47.7, "left": 18.8, "right": 18.9}]
length = [400, [5000, 5100, 4500], 500]
time = [40, [300, 330, 345], 50]
address_to_coords_response = [
'[{"location":{"lat":%s,"lon":%s},"bounds":%s}]' % (lat[0], lon[0], str(bounds[0]).replace("'", '"')),
'[{"location":{"lat":%s,"lon":%s},"bounds":%s}]' % (lat[2], lon[2], str(bounds[2]).replace("'", '"'))
]
m.get(self.address_req, [{'text': address_to_coords_response[0]}, {'text': address_to_coords_response[1]}])
route = wrc.WazeRouteCalculator("", "")
route_mock = mock.Mock(return_value=[{
"routeName": "1",
"results": [
{"length": length[0], "crossTime": time[0], "path": {"x": lon[0], "y": lat[0]}},
{"length": length[1][0], "crossTime": time[1][0], "path": {"x": lon[1][0], "y": lat[1][0]}},
{"length": length[2], "crossTime": time[2], "path": {"x": lon[2], "y": lat[2]}}
]
}, {
"routeName": "2",
"results": [
{"length": length[0], "crossTime": time[0], "path": {"x": lon[0], "y": lat[0]}},
{"length": length[1][1], "crossTime": time[1][1], "path": {"x": lon[1][1], "y": lat[1][1]}},
{"length": length[2], "crossTime": time[2], "path": {"x": lon[2], "y": lat[2]}}
]
}, {
"routeName": "3",
"results": [
{"length": length[0], "crossTime": time[0], "path": {"x": lon[0], "y": lat[0]}},
{"length": length[1][2], "crossTime": time[1][2], "path": {"x": lon[1][2], "y": lat[1][2]}},
{"length": length[2], "crossTime": time[2], "path": {"x": lon[2], "y": lat[2]}}
]
}])
route.get_route = route_mock
results = route.calc_all_routes_info(stop_at_bounds=True)
assert route_mock.called
assert results == {"1": (5.0, 5.0), "2": (5.5, 5.1), "3": (5.75, 4.5)}
def xtest_full_route_calc(self):
from_address = 'From address'
to_address = 'To address'
with requests_mock.mock() as m:
m.get(self.address_req, text=self.address_to_coords_response)
m.get(self.routing_req, text=self.routing_response)
route = wrc.WazeRouteCalculator(from_address, to_address)
time, dist = route.calc_route_info()
assert len(self.url_mock.call_args_list) == 3
assert time == 1.00
assert dist == 0.40
def test_silent_logging(self):
from_address = 'From address'
to_address = 'To address'
with requests_mock.mock() as m:
m.get(self.address_req, text=self.address_to_coords_response)
route = wrc.WazeRouteCalculator(from_address, to_address, log_lvl=None)
assert route.log.getEffectiveLevel() == wrc.logging.WARNING