-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_dendra_api_client.py
More file actions
357 lines (289 loc) · 14.9 KB
/
test_dendra_api_client.py
File metadata and controls
357 lines (289 loc) · 14.9 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
353
354
355
356
357
"""Unit tests for dendra_api_client.py"""
import unittest
from unittest.mock import patch, MagicMock
import datetime as dt
import pytz
import dendra_api_client as dendra
def _mock_response(data, status_code=200):
mock_resp = MagicMock()
mock_resp.status_code = status_code
mock_resp.json.return_value = {'data': data}
return mock_resp
class TestTimeHelpers(unittest.TestCase):
def test_time_utc_empty_returns_utc_datetime(self):
result = dendra.time_utc()
self.assertIsInstance(result, dt.datetime)
self.assertEqual(result.tzinfo, pytz.utc)
def test_time_utc_parses_utc_string(self):
result = dendra.time_utc('2019-03-01T08:00:00Z')
self.assertEqual(result.year, 2019)
self.assertEqual(result.month, 3)
self.assertEqual(result.day, 1)
self.assertEqual(result.hour, 8)
def test_time_utc_converts_offset_to_utc(self):
result = dendra.time_utc('2019-03-01T08:00:00-0400')
self.assertEqual(result.hour, 12) # 8 AM EDT = 12 PM UTC
def test_time_format_default_is_local_style(self):
result = dendra.time_format()
self.assertRegex(result, r'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$')
self.assertFalse(result.endswith('Z'))
def test_time_format_utc_appends_z(self):
dt_time = dt.datetime(2019, 3, 1, 8, 0, 0)
result = dendra.time_format(dt_time, 'utc')
self.assertEqual(result, '2019-03-01T08:00:00Z')
def test_time_format_local_no_z(self):
dt_time = dt.datetime(2019, 3, 1, 8, 0, 0)
result = dendra.time_format(dt_time, 'local')
self.assertEqual(result, '2019-03-01T08:00:00')
def test_time_format_with_explicit_datetime(self):
dt_time = dt.datetime(2020, 6, 15, 12, 30, 45)
result = dendra.time_format(dt_time)
self.assertEqual(result, '2020-06-15T12:30:45')
class TestInternalHelpers(unittest.TestCase):
def setUp(self):
self._saved = dendra.headers.copy()
def tearDown(self):
dendra.headers.clear()
dendra.headers.update(self._saved)
def test_validate_mongo_id_valid(self):
dendra._validate_mongo_id('5ae8793efe27f424f9102b87') # no exception
def test_validate_mongo_id_bad_type(self):
with self.assertRaises(TypeError):
dendra._validate_mongo_id(12345)
def test_validate_mongo_id_too_short(self):
with self.assertRaises(ValueError):
dendra._validate_mongo_id('tooshort')
def test_validate_mongo_id_too_long(self):
with self.assertRaises(ValueError):
dendra._validate_mongo_id('a' * 25)
def test_check_auth_raises_without_token(self):
dendra.headers.pop('Authorization', None)
with self.assertRaises(RuntimeError):
dendra._check_auth()
def test_check_auth_passes_with_token(self):
dendra.headers['Authorization'] = 'test_token'
dendra._check_auth() # should not raise
class TestListFunctions(unittest.TestCase):
def setUp(self):
self._saved = dendra.headers.copy()
dendra.headers.pop('Authorization', None)
def tearDown(self):
dendra.headers.clear()
dendra.headers.update(self._saved)
@patch('dendra_api_client.requests.get')
def test_get_organization_id(self, mock_get):
mock_get.return_value = _mock_response([{'_id': 'abc123def456abc123def456'}])
result = dendra.get_organization_id('erczo')
self.assertEqual(result, 'abc123def456abc123def456')
mock_get.assert_called_once()
@patch('dendra_api_client.requests.get')
def test_list_organizations_all(self, mock_get):
data = [
{'_id': 'abc123def456abc123def456', 'name': 'ERCZO', 'slug': 'erczo'},
{'_id': 'def456abc123def456abc123', 'name': 'UCNRS', 'slug': 'ucnrs'},
]
mock_get.return_value = _mock_response(data)
result = dendra.list_organizations()
self.assertEqual(len(result), 2)
@patch('dendra_api_client.requests.get')
def test_list_organizations_filtered(self, mock_get):
data = [{'_id': 'abc123def456abc123def456', 'name': 'ERCZO', 'slug': 'erczo'}]
mock_get.return_value = _mock_response(data)
result = dendra.list_organizations('erczo')
self.assertEqual(len(result), 1)
self.assertEqual(mock_get.call_args[1]['params']['slug'], 'erczo')
@patch('dendra_api_client.requests.get')
def test_list_stations_all(self, mock_get):
data = [{'_id': 'aaa111bbb222ccc333ddd444', 'name': 'Station A', 'slug': 'station-a'}]
mock_get.return_value = _mock_response(data)
result = dendra.list_stations()
self.assertEqual(len(result), 1)
@patch('dendra_api_client.requests.get')
def test_list_stations_by_org(self, mock_get):
org_data = [{'_id': 'abc123def456abc123def456', 'name': 'UCNRS', 'slug': 'ucnrs'}]
stn_data = [{'_id': 'aaa111bbb222ccc333ddd444', 'name': 'Stunt Ranch', 'slug': 'stunt-ranch'}]
mock_get.side_effect = [_mock_response(org_data), _mock_response(stn_data)]
result = dendra.list_stations('ucnrs')
self.assertEqual(len(result), 1)
self.assertEqual(result[0]['name'], 'Stunt Ranch')
@patch('dendra_api_client.requests.get')
def test_list_stations_bad_org_returns_error(self, mock_get):
mock_get.return_value = _mock_response([])
result = dendra.list_stations('nonexistent')
self.assertIn('ERROR', result)
@patch('dendra_api_client.requests.get')
def test_list_datastreams_by_station_id(self, mock_get):
data = [{'_id': 'aaa111bbb222ccc333ddd444', 'name': 'Air Temp Avg'}]
mock_get.return_value = _mock_response(data)
result = dendra.list_datastreams_by_station_id('aaa111bbb222ccc333ddd444')
self.assertEqual(len(result), 1)
self.assertEqual(mock_get.call_args[1]['params']['station_id'], 'aaa111bbb222ccc333ddd444')
@patch('dendra_api_client.requests.get')
def test_list_datastreams_by_query(self, mock_get):
data = [{'_id': 'aaa111bbb222ccc333ddd444', 'name': 'Air Temp Avg'}]
mock_get.return_value = _mock_response(data)
result = dendra.list_datastreams_by_query(station_id='aaa111bbb222ccc333ddd444')
self.assertEqual(len(result), 1)
@patch('dendra_api_client.requests.get')
def test_needs_auth_blocks_without_token(self, mock_get):
with self.assertRaises(RuntimeError):
dendra.list_organizations(needs_auth=True)
mock_get.assert_not_called()
@patch('dendra_api_client.requests.get')
def test_needs_auth_passes_with_token(self, mock_get):
dendra.headers['Authorization'] = 'test_token'
mock_get.return_value = _mock_response([])
dendra.list_organizations(needs_auth=True)
mock_get.assert_called_once()
class TestGetMeta(unittest.TestCase):
def setUp(self):
self._saved = dendra.headers.copy()
def tearDown(self):
dendra.headers.clear()
dendra.headers.update(self._saved)
def test_get_meta_station_bad_type_raises(self):
with self.assertRaises(TypeError):
dendra.get_meta_station_by_id(12345)
def test_get_meta_station_bad_length_raises(self):
with self.assertRaises(ValueError):
dendra.get_meta_station_by_id('short')
def test_get_meta_datastream_bad_type_raises(self):
with self.assertRaises(TypeError):
dendra.get_meta_datastream_by_id(12345)
def test_get_meta_datastream_bad_length_raises(self):
with self.assertRaises(ValueError):
dendra.get_meta_datastream_by_id('short')
def test_get_meta_annotation_bad_type_raises(self):
with self.assertRaises(TypeError):
dendra.get_meta_annotation(12345)
@patch('dendra_api_client.requests.get')
def test_get_meta_station_by_id(self, mock_get):
station_id = 'aaa111bbb222ccc333ddd444'
mock_get.return_value = _mock_response([{'_id': station_id, 'name': 'Test Station', 'slug': 'test-station'}])
result = dendra.get_meta_station_by_id(station_id)
self.assertEqual(result['name'], 'Test Station')
self.assertEqual(mock_get.call_args[1]['params']['_id'], station_id)
@patch('dendra_api_client.requests.get')
def test_get_meta_datastream_by_id(self, mock_get):
ds_id = 'aaa111bbb222ccc333ddd444'
mock_get.return_value = _mock_response([{'_id': ds_id, 'name': 'Air Temp Avg', 'station_id': 'bbb222ccc333ddd444eee555'}])
result = dendra.get_meta_datastream_by_id(ds_id)
self.assertEqual(result['name'], 'Air Temp Avg')
@patch('dendra_api_client.requests.get')
def test_get_meta_annotation(self, mock_get):
ann_id = 'aaa111bbb222ccc333ddd444'
mock_get.return_value = _mock_response([{'_id': ann_id, 'title': 'Test Annotation'}])
result = dendra.get_meta_annotation(ann_id)
self.assertEqual(result['title'], 'Test Annotation')
def test_get_meta_organization_no_args_raises(self):
with self.assertRaises(ValueError):
dendra.get_meta_organization()
@patch('dendra_api_client.requests.get')
def test_get_meta_organization_by_slug(self, mock_get):
org_data = [{'_id': 'abc123def456abc123def456', 'name': 'ERCZO'}]
mock_get.side_effect = [
_mock_response([{'_id': 'abc123def456abc123def456'}]), # get_organization_id
_mock_response(org_data), # get_meta_organization
]
result = dendra.get_meta_organization('erczo')
self.assertEqual(result['name'], 'ERCZO')
def test_get_datastream_by_id_delegates(self):
ds_id = 'aaa111bbb222ccc333ddd444'
with patch('dendra_api_client.get_meta_datastream_by_id') as mock_meta:
mock_meta.return_value = {'_id': ds_id}
result = dendra.get_datastream_by_id(ds_id)
mock_meta.assert_called_once_with(ds_id, None)
self.assertEqual(result['_id'], ds_id)
class TestGetDatapoints(unittest.TestCase):
DS_ID = 'aaa111bbb222ccc333ddd444'
STN_ID = 'bbb222ccc333ddd444eee555'
def setUp(self):
self._saved = dendra.headers.copy()
def tearDown(self):
dendra.headers.clear()
dendra.headers.update(self._saved)
def test_invalid_datastream_id_type_raises(self):
with self.assertRaises(TypeError):
dendra.get_datapoints(12345, '2019-01-01T00:00:00')
def test_invalid_datastream_id_length_raises(self):
with self.assertRaises(ValueError):
dendra.get_datapoints('short', '2019-01-01T00:00:00')
@patch('dendra_api_client.requests.get')
def test_get_datapoints_with_explicit_name(self, mock_get):
dp_data = [
{'lt': '2019-02-01T08:00:00', 't': '2019-02-01T16:00:00.000Z', 'v': 15.2},
{'lt': '2019-02-01T08:05:00', 't': '2019-02-01T16:05:00.000Z', 'v': 15.4},
]
mock_get.side_effect = [
_mock_response(dp_data), # page 1
_mock_response([]), # page 2 (empty, stops paging)
]
result = dendra.get_datapoints(self.DS_ID, '2019-02-01T00:00:00', '2019-03-01T00:00:00', name='MyStream')
self.assertEqual(len(result), 2)
self.assertIn('MyStream', result.columns)
@patch('dendra_api_client.requests.get')
def test_get_datapoints_default_name_uses_meta(self, mock_get):
dp_data = [
{'lt': '2019-02-01T08:00:00', 't': '2019-02-01T16:00:00.000Z', 'v': 15.2},
]
ds_meta = [{'_id': self.DS_ID, 'name': 'Air Temp Avg', 'station_id': self.STN_ID}]
stn_meta = [{'_id': self.STN_ID, 'slug': 'stunt-ranch'}]
mock_get.side_effect = [
_mock_response(dp_data), # datapoints page 1
_mock_response([]), # datapoints page 2 (empty)
_mock_response(ds_meta), # get_meta_datastream_by_id
_mock_response(stn_meta), # get_meta_station_by_id
]
result = dendra.get_datapoints(self.DS_ID, '2019-02-01T00:00:00', '2019-03-01T00:00:00')
self.assertIn('StuntRanch_Air_Temp_Avg', result.columns)
@patch('dendra_api_client.requests.get')
def test_get_datapoints_empty_result(self, mock_get):
ds_meta = [{'_id': self.DS_ID, 'name': 'Air Temp Avg', 'station_id': self.STN_ID}]
stn_meta = [{'_id': self.STN_ID, 'slug': 'test-station'}]
mock_get.side_effect = [
_mock_response([]), # empty datapoints
_mock_response(ds_meta), # get_meta_datastream_by_id
_mock_response(stn_meta), # get_meta_station_by_id
]
result = dendra.get_datapoints(self.DS_ID, '2019-02-01T00:00:00', '2019-03-01T00:00:00')
self.assertTrue(result.empty)
@patch('dendra_api_client.requests.get')
def test_get_datapoints_returns_status_code_on_http_error(self, mock_get):
mock_get.return_value = _mock_response([], status_code=401)
result = dendra.get_datapoints(self.DS_ID, '2019-02-01T00:00:00', '2019-03-01T00:00:00', name='Test')
self.assertEqual(result, 401)
@patch('dendra_api_client.requests.get')
def test_get_datapoints_pages_through_results(self, mock_get):
page1 = [{'lt': '2019-02-01T08:00:00', 't': '2019-02-01T16:00:00.000Z', 'v': i} for i in range(5)]
page2 = [{'lt': '2019-02-01T09:00:00', 't': '2019-02-01T17:00:00.000Z', 'v': i} for i in range(3)]
mock_get.side_effect = [
_mock_response(page1),
_mock_response(page2),
_mock_response([]), # empty final page
]
result = dendra.get_datapoints(self.DS_ID, '2019-02-01T00:00:00', '2019-03-01T00:00:00', name='Test')
self.assertEqual(len(result), 8)
@patch('dendra_api_client.requests.get')
def test_get_datapoints_needs_auth_blocks(self, mock_get):
dendra.headers.pop('Authorization', None)
with self.assertRaises(RuntimeError):
dendra.get_datapoints(self.DS_ID, '2019-02-01T00:00:00', needs_auth=True)
mock_get.assert_not_called()
@patch('dendra_api_client.requests.get')
def test_get_datapoints_from_station_id(self, mock_get):
ds_list = [{'_id': self.DS_ID, 'name': 'Air Temp Avg'}]
dp_data = [{'lt': '2019-02-01T08:00:00', 't': '2019-02-01T16:00:00.000Z', 'v': 15.2}]
stn_meta = [{'_id': self.STN_ID, 'slug': 'test-station'}]
ds_meta = [{'_id': self.DS_ID, 'name': 'Air Temp Avg', 'station_id': self.STN_ID}]
mock_get.side_effect = [
_mock_response(ds_list), # list_datastreams_by_station_id
_mock_response(dp_data), # get_datapoints page 1
_mock_response([]), # get_datapoints page 2 (empty)
_mock_response(ds_meta), # get_meta_datastream_by_id
_mock_response(stn_meta), # get_meta_station_by_id
]
result = dendra.get_datapoints_from_station_id(self.STN_ID, '2019-02-01T00:00:00', '2019-03-01T00:00:00')
self.assertIsNotNone(result)
self.assertEqual(len(result), 1)
if __name__ == '__main__':
unittest.main()