-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.py
228 lines (190 loc) · 6.89 KB
/
test.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
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
import asyncio
import functools
import logging
import unittest
from unittest.mock import MagicMock
import aiohttp
import osrm
logging.basicConfig(level=logging.DEBUG)
OSRM_HOST = 'http://127.0.0.1:5000'
SAMPLE_ROUTE = [[-74.005482, 40.679220], [-74.005389, 40.679495],
[-74.005014, 40.679746], [-74.004927, 40.679718],
[-74.004694, 40.679651]]
class TestClient(unittest.TestCase):
def setUp(self):
self.client = osrm.Client(host=OSRM_HOST, timeout=1)
self.mock_session = MagicMock()
self.mock_client = osrm.Client(
host=OSRM_HOST, session=self.mock_session)
def test_server_error(self):
self.mock_session.get.return_value = MagicMock(
status_code=500,
text='unexpected error')
with self.assertRaises(osrm.OSRMServerException):
self.mock_client.nearest(
coordinates=[[-74.0056, 40.6197]],
number=10
)
def test_nearest(self):
response = self.client.nearest(
coordinates=[[-74.00578245683002, 40.60600816104437]],
radiuses=[70],
number=20
)
assert response['code'] == 'Ok'
assert len(response['waypoints']) == 12
response = self.client.nearest(
coordinates=[[-74.00578245683002, 40.60600816104437]],
radiuses=[70],
bearings=[[45, 30]],
number=20
)
assert response['code'] == 'Ok'
assert len(response['waypoints']) == 3
with self.assertRaises(AssertionError):
self.client.nearest(
coordinates=[[-200, 100]]
)
with self.assertRaises(AssertionError):
self.client.nearest(
coordinates=[[-74.00578245683002, 40.60600816104437]],
bearings=[[720, 360]],
)
with self.assertRaises(osrm.OSRMClientException) as cm:
self.client.nearest(
coordinates=[[-74.00578245683002, 40.60600816104437]],
radiuses=[10],
number=10
)
assert cm.exception.args[0]['code'] == 'NoSegment'
def test_route(self):
response = self.client.route(
coordinates=[[-74.0056, 40.6197], [-74.0034, 40.6333]],
overview=osrm.overview.full
)
assert response['code'] == 'Ok'
response = self.client.route(
coordinates=[[-74.0056, 40.6197], [-74.0034, 40.6333]],
overview=osrm.overview.full,
continue_straight=osrm.continue_straight.true
)
assert response['code'] == 'Ok'
def test_match(self):
response = self.client.match(
coordinates=SAMPLE_ROUTE,
radiuses=[9, 9, 10, 3, 3],
overview=osrm.overview.full
)
assert response['code'] == 'Ok'
response = self.client.match(
coordinates=SAMPLE_ROUTE,
radiuses=[9, 9, 10, 3, 3],
timestamps=[10, 11, 1000, 1001, 1002],
gaps=osrm.gaps.split
)
assert response['code'] == 'Ok'
assert len(response['matchings']) == 2
response = self.client.match(
coordinates=SAMPLE_ROUTE,
radiuses=[9, 9, 10, 3, 3],
timestamps=[10, 11, 1000, 1001, 1002],
gaps=osrm.gaps.ignore
)
assert response['code'] == 'Ok'
assert len(response['matchings']) == 1
def run_in_loop(f):
@functools.wraps(f)
def wrapper(testcase, *args, **kwargs):
coro = asyncio.coroutine(f)
future = asyncio.wait_for(coro(testcase, *args, **kwargs), timeout=15)
return testcase.loop.run_until_complete(future)
return wrapper
class TestAioHTTPClient(unittest.TestCase):
def setUp(self):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
async def _setUp():
self.session = aiohttp.ClientSession(loop=self.loop)
self.client = osrm.AioHTTPClient(
host=OSRM_HOST, session=self.session, timeout=0.2
)
self.mock_session = MagicMock()
self.mock_client = osrm.AioHTTPClient(
host=OSRM_HOST, session=self.mock_session,
timeout=0.2, max_retries=3
)
self.loop.run_until_complete(_setUp())
def tearDown(self):
self.loop.run_until_complete(self.session.close())
self.loop.close()
@run_in_loop
async def test_nearest(self):
response = await self.client.nearest(
coordinates=[[-74.0056, 40.6197]],
number=10
)
assert response['code'] == 'Ok'
@run_in_loop
async def test_match(self):
response = await self.client.match(
coordinates=SAMPLE_ROUTE,
radiuses=[9, 9, 10, 3, 3],
overview=osrm.overview.full,
tidy=False,
gaps=osrm.gaps.split
)
assert response['code'] == 'Ok'
@run_in_loop
async def test_route(self):
response = await self.client.route(
coordinates=[[-74.0056, 40.6197], [-74.0034, 40.6333]],
overview=osrm.overview.full
)
assert response['code'] == 'Ok'
@run_in_loop
async def test_retry(self):
counter = 0
def mock_get(url, **kwargs):
nonlocal counter
if counter < 2:
counter += 1
raise asyncio.TimeoutError('timeout')
return aiohttp.client._RequestContextManager(
asyncio.coroutine(
lambda: MagicMock(
status=200,
url=url,
text=asyncio.coroutine(lambda: '{}')
)
)()
)
self.mock_session.get = mock_get
await self.mock_client.nearest(
coordinates=[[-74.0056, 40.6197]], number=10
)
assert counter == 2
@run_in_loop
async def test_exceeded_max_retry(self):
counter = 0
def mock_get(url, **kwargs):
nonlocal counter
counter += 1
raise asyncio.TimeoutError('timeout')
self.mock_session.get = mock_get
with self.assertRaises(osrm.OSRMServerException) as cm:
await self.mock_client.nearest(
coordinates=[[-74.0056, 40.6197]], number=10
)
assert cm.exception.args[1] == 'server timeout'
assert counter == self.mock_client.max_retries
@run_in_loop
async def test_real_timeout(self):
client = osrm.AioHTTPClient(
host=OSRM_HOST, timeout=0.01, max_retries=1)
with self.assertRaises(osrm.OSRMServerException) as cm:
await client.match(
coordinates=[[-73.999, 40.724], [-73.994, 40.728]],
radiuses=[150, 150]
)
await client.close()
assert cm.exception.args[1] == 'server timeout'