|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
|
|
import os |
|
4
|
|
|
import json |
|
5
|
|
|
|
|
6
|
|
|
import pytest |
|
7
|
|
|
# import mock |
|
8
|
|
|
|
|
9
|
|
|
from route4me.sdk.self_test import MockerResourceWithNetworkClient |
|
10
|
|
|
from .optimizations import Optimizations |
|
11
|
|
|
import route4me.sdk.resources.optimizations as M |
|
12
|
|
|
|
|
13
|
|
|
from ..models import Optimization |
|
14
|
|
|
from ..models import AlgorithmTypeEnum |
|
15
|
|
|
from ..models import OptimizationStateEnum |
|
16
|
|
|
from ..models import OptimizationFactorEnum |
|
17
|
|
|
|
|
18
|
|
|
from ..errors import Route4MeApiError |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
class TestOptimizations(object): |
|
22
|
|
|
def test_ctor(self): |
|
23
|
|
|
|
|
24
|
|
|
api_key = '11111111111111111111111111111111' |
|
25
|
|
|
|
|
26
|
|
|
ns = Optimizations(api_key=api_key) |
|
27
|
|
|
|
|
28
|
|
|
assert ns is not None |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
class TestOptimizationApi(MockerResourceWithNetworkClient): |
|
32
|
|
|
|
|
33
|
|
|
resource_module = M |
|
34
|
|
|
|
|
35
|
|
|
def test_create(self): |
|
36
|
|
|
|
|
37
|
|
|
with open(os.path.join( |
|
38
|
|
|
# '..', '..', '..', |
|
39
|
|
|
'submodules', 'route4me-api-data-examples', 'Optimizations', |
|
40
|
|
|
'create_response.json' |
|
41
|
|
|
)) as f: |
|
42
|
|
|
sample_response_data = json.load(f) |
|
43
|
|
|
|
|
44
|
|
|
self.set_response(data=sample_response_data) |
|
45
|
|
|
|
|
46
|
|
|
o = Optimization() |
|
47
|
|
|
o.algorithm_type = AlgorithmTypeEnum.TSP |
|
48
|
|
|
o.state = OptimizationStateEnum.MATRIX_PROCESSING |
|
49
|
|
|
o.optimization_factor = OptimizationFactorEnum.DISTANCE |
|
50
|
|
|
|
|
51
|
|
|
r = Optimizations(api_key='test') |
|
52
|
|
|
res = r.create(o) |
|
53
|
|
|
|
|
54
|
|
|
# print(self.mock_fluent_request_class.mock_calls) |
|
55
|
|
|
# call(), |
|
56
|
|
|
# call().method('POST'), |
|
57
|
|
|
# call().url('https://www.route4me.com//api.v4/optimization_problem.php'), |
|
58
|
|
|
# call().qs(None), |
|
59
|
|
|
# call().json({'links': {}, 'parameters': {'algorithm_type': 1}}), |
|
60
|
|
|
# call().user_agent('requests/2.18.3 (Linux 4.8.0-53-generic) Route4Me-Python-SDK/0.1.0 CPython/3.5.2'), |
|
61
|
|
|
# call().header('Route4Me-User-Agent', 'requests/2.18.3 (Linux 4.8.0-53-generic) ..'), |
|
62
|
|
|
# call().accept('application/json'), |
|
63
|
|
|
# call().header('Route4Me-Api-Key', 'test'), |
|
64
|
|
|
# call().qs({'format': 'json', 'api_key': 'test'}), |
|
65
|
|
|
# call().send(), |
|
66
|
|
|
# call().send().json() |
|
67
|
|
|
|
|
68
|
|
|
# ---------- |
|
69
|
|
|
# assertions |
|
70
|
|
|
mock_freq = self.last_request() |
|
71
|
|
|
mock_freq.method.assert_called_with('POST') |
|
72
|
|
|
mock_freq.url.assert_called_with( |
|
73
|
|
|
'https://www.route4me.com/api.v4/optimization_problem.php' |
|
74
|
|
|
) |
|
75
|
|
|
mock_freq.json.assert_called_with(dict(o)) |
|
76
|
|
|
|
|
77
|
|
|
# assertions on response |
|
78
|
|
|
assert isinstance(res, Optimization) |
|
79
|
|
|
assert res.ID == '1EDB78F63556D99336E06A13A34CF139' |
|
80
|
|
|
assert res.name == 'Fri, 17 Jun 2016 08:21:59 +0000 UTC' |
|
81
|
|
|
assert res.algorithm_type == AlgorithmTypeEnum.TSP |
|
82
|
|
|
assert res.state == OptimizationStateEnum.MATRIX_PROCESSING |
|
83
|
|
|
assert res.optimization_factor == OptimizationFactorEnum.DISTANCE |
|
84
|
|
|
assert res.member_id == '1' |
|
85
|
|
|
assert res.vehicle_id is None |
|
86
|
|
|
assert res.device_id is None |
|
87
|
|
|
|
|
88
|
|
|
def test_create_with_callback(self): |
|
89
|
|
|
|
|
90
|
|
|
with open(os.path.join( |
|
91
|
|
|
# '..', '..', '..', |
|
92
|
|
|
'submodules', 'route4me-api-data-examples', 'Optimizations', |
|
93
|
|
|
'create_response.json' |
|
94
|
|
|
)) as f: |
|
95
|
|
|
sample_response_data = json.load(f) |
|
96
|
|
|
|
|
97
|
|
|
self.set_response(data=sample_response_data) |
|
98
|
|
|
|
|
99
|
|
|
o = Optimization() |
|
100
|
|
|
o.algorithm_type = AlgorithmTypeEnum.TSP |
|
101
|
|
|
o.state = OptimizationStateEnum.MATRIX_PROCESSING |
|
102
|
|
|
o.optimization_factor = OptimizationFactorEnum.DISTANCE |
|
103
|
|
|
|
|
104
|
|
|
r = Optimizations(api_key='test') |
|
105
|
|
|
res = r.create( |
|
106
|
|
|
optimization_data=o, |
|
107
|
|
|
callback_url='https://callback.route4me.com/callback?q=1' |
|
108
|
|
|
) |
|
109
|
|
|
|
|
110
|
|
|
# ---------- |
|
111
|
|
|
# assertions |
|
112
|
|
|
|
|
113
|
|
|
print(self.mock_fluent_request_class.mock_calls) |
|
114
|
|
|
# call(), |
|
115
|
|
|
# call().method('POST'), |
|
116
|
|
|
# call().url('https://www.route4me.com/api.v4/optimization_problem.php'), |
|
117
|
|
|
# call().qs({'optimized_callback_url': 'https://callback.route4me.com/callback?q=1'}), |
|
118
|
|
|
# call().json({'links': {}, 'parameters': {'store_route': True, 'algorithm_type': 1, |
|
119
|
|
|
# 'route_max_duration': 86400, 'optimize': 'Distance'}, 'state': 2, 'addresses': []}), |
|
120
|
|
|
# call().user_agent('requests/2.18.3 (Linux 4.8.0-53-generic) Route4Me-Python-SDK/0.1.0 CPython/3.5.2'), |
|
121
|
|
|
# call().header('Route4Me-Agent', |
|
122
|
|
|
# 'requests/2.18.3 (Linux 4.8.0-53-generic) Route4Me-Python-SDK/0.1.0 CPython/3.5.2'), |
|
123
|
|
|
# call().header('Route4Me-Agent-Release', '0.1.0-dev.5'), |
|
124
|
|
|
# call().header('Route4Me-Agent-Commit', None), |
|
125
|
|
|
# call().header('Route4Me-Agent-Build', None), |
|
126
|
|
|
# call().accept('application/json'), |
|
127
|
|
|
# call().header('Route4Me-Api-Key', 'test'), |
|
128
|
|
|
# call().qs({'api_key': 'test', 'format': 'json'}), |
|
129
|
|
|
# call().send(), |
|
130
|
|
|
# call().send().json() |
|
131
|
|
|
|
|
132
|
|
|
mock_freq = self.last_request() |
|
133
|
|
|
mock_freq.method.assert_called_with('POST') |
|
134
|
|
|
mock_freq.url.assert_called_with( |
|
135
|
|
|
'https://www.route4me.com/api.v4/optimization_problem.php' |
|
136
|
|
|
) |
|
137
|
|
|
mock_freq.json.assert_called_with(dict(o)) |
|
138
|
|
|
mock_freq.qs.assert_any_call({ |
|
139
|
|
|
'optimized_callback_url': 'https://callback.route4me.com/callback?q=1', |
|
140
|
|
|
}) |
|
141
|
|
|
|
|
142
|
|
|
# assertions on response |
|
143
|
|
|
assert isinstance(res, Optimization) |
|
144
|
|
|
assert res.ID == '1EDB78F63556D99336E06A13A34CF139' |
|
145
|
|
|
|
|
146
|
|
|
def test_get(self): |
|
147
|
|
|
|
|
148
|
|
|
with open(os.path.join( |
|
149
|
|
|
# '..', '..', '..', |
|
150
|
|
|
'submodules', 'route4me-api-data-examples', 'Optimizations', |
|
151
|
|
|
'get_response.json' |
|
152
|
|
|
)) as f: |
|
153
|
|
|
sample_response_data = json.load(f) |
|
154
|
|
|
|
|
155
|
|
|
self.set_response(data=sample_response_data) |
|
156
|
|
|
|
|
157
|
|
|
r = Optimizations(api_key='test') |
|
158
|
|
|
res = r.get('07372F2CF3814EC6DFFAFE92E22771AA') |
|
159
|
|
|
|
|
160
|
|
|
print(self.mock_fluent_request_class.mock_calls) |
|
161
|
|
|
|
|
162
|
|
|
# ---------- |
|
163
|
|
|
# assertions |
|
164
|
|
|
mock_freq = self.last_request() |
|
165
|
|
|
mock_freq.method.assert_called_with('GET') |
|
166
|
|
|
mock_freq.url.assert_called_with( |
|
167
|
|
|
'https://www.route4me.com/api.v4/optimization_problem.php' |
|
168
|
|
|
) |
|
169
|
|
|
mock_freq.qs.assert_any_call({ |
|
170
|
|
|
'optimization_problem_id': '07372F2CF3814EC6DFFAFE92E22771AA' |
|
171
|
|
|
}) |
|
172
|
|
|
assert not mock_freq.json.called |
|
173
|
|
|
assert not mock_freq.data.called |
|
174
|
|
|
|
|
175
|
|
|
# assertions on response |
|
176
|
|
|
assert isinstance(res, Optimization) |
|
177
|
|
|
assert res.ID == '07372F2CF3814EC6DFFAFE92E22771AA' |
|
178
|
|
|
assert res.name == 'Sunday 10th of April 2016 01:20 AM (+03:00)' |
|
179
|
|
|
assert res.algorithm_type == AlgorithmTypeEnum.CVRP_TW_SD |
|
180
|
|
|
assert res.state == OptimizationStateEnum.OPTIMIZED |
|
181
|
|
|
assert res.optimization_factor == OptimizationFactorEnum.TIME |
|
182
|
|
|
assert res.member_id == '44143' |
|
183
|
|
|
assert res.vehicle_id is None |
|
184
|
|
|
assert res.device_id is None |
|
185
|
|
|
|
|
186
|
|
|
def test_remove(self): |
|
187
|
|
|
|
|
188
|
|
|
with open(os.path.join( |
|
189
|
|
|
# '..', '..', '..', |
|
190
|
|
|
'submodules', 'route4me-api-data-examples', 'Optimizations', |
|
191
|
|
|
'remove_response.json' |
|
192
|
|
|
)) as f: |
|
193
|
|
|
sample_response_data = json.load(f) |
|
194
|
|
|
|
|
195
|
|
|
self.set_response(data=sample_response_data) |
|
196
|
|
|
|
|
197
|
|
|
opt_id = 'DE62B03510AB5A6A876093F30F6C7BF5' |
|
198
|
|
|
r = Optimizations(api_key='test') |
|
199
|
|
|
res = r.remove(ID=opt_id) |
|
200
|
|
|
|
|
201
|
|
|
# print(self.mock_fluent_request_class.mock_calls) |
|
202
|
|
|
|
|
203
|
|
|
# ---------- |
|
204
|
|
|
# assertions |
|
205
|
|
|
mock_freq = self.last_request() |
|
206
|
|
|
mock_freq.method.assert_called_with('DELETE') |
|
207
|
|
|
mock_freq.url.assert_called_with( |
|
208
|
|
|
'https://www.route4me.com/api.v4/optimization_problem.php' |
|
209
|
|
|
) |
|
210
|
|
|
mock_freq.qs.assert_any_call({ |
|
211
|
|
|
'optimization_problem_id': opt_id |
|
212
|
|
|
}) |
|
213
|
|
|
mock_freq.json.assert_called_once_with(None) |
|
214
|
|
|
|
|
215
|
|
|
assert not mock_freq.data.called |
|
216
|
|
|
|
|
217
|
|
|
assert res is True |
|
218
|
|
|
# assertions on response |
|
219
|
|
|
|
|
220
|
|
|
def test_remove_failed(self): |
|
221
|
|
|
|
|
222
|
|
|
self.set_response(data=None) |
|
223
|
|
|
|
|
224
|
|
|
opt_id = 'DE62B03510AB5A6A876093F30F6C7BF5' |
|
225
|
|
|
r = Optimizations(api_key='test') |
|
226
|
|
|
|
|
227
|
|
|
with pytest.raises(Route4MeApiError) as exc_info: |
|
228
|
|
|
r.remove(ID=opt_id) |
|
229
|
|
|
|
|
230
|
|
|
print(self.mock_fluent_request_class.mock_calls) |
|
231
|
|
|
|
|
232
|
|
|
exc = exc_info.value |
|
233
|
|
|
assert exc is not None |
|
234
|
|
|
|
|
235
|
|
|
# TODO: implement this! |
|
236
|
|
|
# assert exc.method == 'DELETE' |
|
237
|
|
|
# assert exc.url == 'https://www.route4me.com/api.v4/optimization_problem.php' |
|
238
|
|
|
|