-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client.py
More file actions
295 lines (243 loc) · 10.3 KB
/
Copy pathtest_client.py
File metadata and controls
295 lines (243 loc) · 10.3 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
"""Basic tests for MapFlow client."""
import unittest
from unittest.mock import Mock, patch, MagicMock
from uuid import UUID
from mapflow import (
MapFlowClient,
Customer,
DeliveryLocation,
Warehouse,
DeliveryItem,
Vehicle,
Tag,
AuthenticationError,
NotFoundError,
ValidationError
)
class TestMapFlowClient(unittest.TestCase):
"""Test MapFlow client initialization and basic methods."""
def setUp(self):
"""Set up test client."""
self.api_key = "test-api-key"
self.client = MapFlowClient(api_key=self.api_key)
def test_client_initialization(self):
"""Test client initialization."""
self.assertEqual(self.client.api_key, self.api_key)
self.assertEqual(self.client.base_url, "https://api.mapflow.co")
self.assertEqual(self.client.timeout, 30)
self.assertIn('X-API-Key', self.client.session.headers)
self.assertEqual(self.client.session.headers['X-API-Key'], self.api_key)
def test_client_custom_base_url(self):
"""Test client with custom base URL."""
custom_url = "https://api-test.mapflow.co"
client = MapFlowClient(api_key=self.api_key, base_url=custom_url)
self.assertEqual(client.base_url, custom_url)
@patch('mapflow.client.requests.Session.request')
def test_handle_response_200(self, mock_request):
"""Test successful response handling."""
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.return_value = {"id": "123", "name": "Test"}
mock_request.return_value = mock_response
result = self.client._request('GET', '/test/')
self.assertEqual(result, {"id": "123", "name": "Test"})
@patch('mapflow.client.requests.Session.request')
def test_handle_response_401(self, mock_request):
"""Test authentication error handling."""
mock_response = Mock()
mock_response.status_code = 401
mock_response.json.return_value = {"detail": "Invalid API key"}
mock_request.return_value = mock_response
with self.assertRaises(AuthenticationError) as context:
self.client._request('GET', '/test/')
self.assertEqual(context.exception.status_code, 401)
@patch('mapflow.client.requests.Session.request')
def test_handle_response_404(self, mock_request):
"""Test not found error handling."""
mock_response = Mock()
mock_response.status_code = 404
mock_response.json.return_value = {"detail": "Not found"}
mock_request.return_value = mock_response
with self.assertRaises(NotFoundError) as context:
self.client._request('GET', '/test/')
self.assertEqual(context.exception.status_code, 404)
@patch('mapflow.client.requests.Session.request')
def test_handle_response_400(self, mock_request):
"""Test validation error handling."""
mock_response = Mock()
mock_response.status_code = 400
mock_response.json.return_value = {"detail": "Validation error"}
mock_request.return_value = mock_response
with self.assertRaises(ValidationError) as context:
self.client._request('GET', '/test/')
self.assertEqual(context.exception.status_code, 400)
class TestCustomerMethods(unittest.TestCase):
"""Test customer-related methods."""
def setUp(self):
"""Set up test client."""
self.client = MapFlowClient(api_key="test-api-key")
@patch('mapflow.client.requests.Session.request')
def test_list_customers(self, mock_request):
"""Test listing customers."""
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.return_value = {
"count": 1,
"next": None,
"previous": None,
"results": [
{
"id": "123e4567-e89b-12d3-a456-426614174000",
"customer_type": "company",
"company_name": "Test Corp",
"organisation": "123e4567-e89b-12d3-a456-426614174001",
"is_active": True,
"created_at": "2024-01-01T00:00:00Z",
"updated_at": "2024-01-01T00:00:00Z"
}
]
}
mock_request.return_value = mock_response
result = self.client.list_customers()
self.assertEqual(result.count, 1)
self.assertEqual(len(result.results), 1)
mock_request.assert_called_once()
@patch('mapflow.client.requests.Session.request')
def test_create_customer(self, mock_request):
"""Test creating a customer."""
mock_response = Mock()
mock_response.status_code = 201
mock_response.json.return_value = {
"id": "123e4567-e89b-12d3-a456-426614174000",
"customer_type": "company",
"company_name": "New Corp",
"organisation": "123e4567-e89b-12d3-a456-426614174001",
"is_active": True,
"created_at": "2024-01-01T00:00:00Z",
"updated_at": "2024-01-01T00:00:00Z"
}
mock_request.return_value = mock_response
customer_data = {
"customer_type": "company",
"company_name": "New Corp"
}
result = self.client.create_customer(customer_data)
self.assertIsInstance(result, Customer)
self.assertEqual(result.company_name, "New Corp")
# Verify request was called
self.assertTrue(mock_request.called)
@patch('mapflow.client.requests.Session.request')
def test_get_customer(self, mock_request):
"""Test getting a customer."""
customer_id = "123e4567-e89b-12d3-a456-426614174000"
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.return_value = {
"id": customer_id,
"customer_type": "company",
"company_name": "Test Corp",
"organisation": "123e4567-e89b-12d3-a456-426614174001",
"is_active": True,
"created_at": "2024-01-01T00:00:00Z",
"updated_at": "2024-01-01T00:00:00Z"
}
mock_request.return_value = mock_response
result = self.client.get_customer(customer_id)
self.assertIsInstance(result, Customer)
self.assertEqual(str(result.id), customer_id)
mock_request.assert_called_once()
class TestDeliveryLocationMethods(unittest.TestCase):
"""Test delivery location methods."""
def setUp(self):
"""Set up test client."""
self.client = MapFlowClient(api_key="test-api-key")
@patch('mapflow.client.requests.Session.request')
def test_list_delivery_locations(self, mock_request):
"""Test listing delivery locations."""
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.return_value = {
"count": 1,
"next": None,
"previous": None,
"results": [
{
"id": "123e4567-e89b-12d3-a456-426614174000",
"customer": "123e4567-e89b-12d3-a456-426614174001",
"name": "Test Location",
"address": "123 Test St",
"zip_code": "75001",
"city": "Paris",
"is_active": True,
"created_at": "2024-01-01T00:00:00Z",
"updated_at": "2024-01-01T00:00:00Z"
}
]
}
mock_request.return_value = mock_response
result = self.client.list_delivery_locations()
self.assertEqual(result.count, 1)
mock_request.assert_called_once()
class TestWarehouseMethods(unittest.TestCase):
"""Test warehouse methods."""
def setUp(self):
"""Set up test client."""
self.client = MapFlowClient(api_key="test-api-key")
@patch('mapflow.client.requests.Session.request')
def test_list_warehouses(self, mock_request):
"""Test listing warehouses."""
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.return_value = {
"count": 1,
"next": None,
"previous": None,
"results": [
{
"id": "123e4567-e89b-12d3-a456-426614174000",
"organisation": "123e4567-e89b-12d3-a456-426614174001",
"name": "Test Warehouse",
"code": "TEST-01",
"address": "123 Test St",
"zip_code": "75001",
"city": "Paris",
"is_active": True,
"created_at": "2024-01-01T00:00:00Z",
"updated_at": "2024-01-01T00:00:00Z"
}
]
}
mock_request.return_value = mock_response
result = self.client.list_warehouses()
self.assertEqual(result.count, 1)
mock_request.assert_called_once()
class TestVehicleMethods(unittest.TestCase):
"""Test vehicle methods."""
def setUp(self):
"""Set up test client."""
self.client = MapFlowClient(api_key="test-api-key")
@patch('mapflow.client.requests.Session.request')
def test_list_vehicles(self, mock_request):
"""Test listing vehicles."""
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.return_value = [
{
"id": "123e4567-e89b-12d3-a456-426614174000",
"organisation": "123e4567-e89b-12d3-a456-426614174001",
"name": "Test Vehicle",
"license_plate": "AB-123-CD",
"vehicle_type": "van_medium",
"is_available": True,
"is_operational": True,
"created_at": "2024-01-01T00:00:00Z",
"updated_at": "2024-01-01T00:00:00Z"
}
]
mock_request.return_value = mock_response
result = self.client.list_vehicles()
self.assertEqual(len(result), 1)
self.assertIsInstance(result[0], Vehicle)
mock_request.assert_called_once()
if __name__ == '__main__':
unittest.main()