|
20 | 20 | import json |
21 | 21 | import uuid |
22 | 22 | import pytz |
23 | | -import datetime |
24 | 23 | import pytest |
| 24 | +import datetime |
25 | 25 | from unittest.mock import Mock, PropertyMock, patch, call |
26 | 26 |
|
27 | 27 | from btrdb.conn import BTrDB |
|
34 | 34 |
|
35 | 35 | RawPointProto = btrdb_pb2.RawPoint |
36 | 36 | StatPointProto = btrdb_pb2.StatPoint |
| 37 | +EST = pytz.timezone('America/New_York') |
37 | 38 |
|
38 | 39 |
|
39 | 40 | ########################################################################## |
@@ -116,8 +117,31 @@ def test_refresh_metadata_deserializes_annotations(self): |
116 | 117 | Assert refresh_metadata deserializes annotation values |
117 | 118 | """ |
118 | 119 | uu = uuid.uuid4() |
119 | | - serialized = {"parent": '{"child": 42}', "sentence": "the quick brown fox"} |
120 | | - expected = {"parent": {"child": 42}, "sentence": "the quick brown fox"} |
| 120 | + serialized = { |
| 121 | + 'acronym': 'VPHM', |
| 122 | + 'description': 'El Segundo PMU 42 Ean', |
| 123 | + 'devacronym': 'PMU!EL_SEG_PMU_42', |
| 124 | + 'enabled': 'true', |
| 125 | + 'id': '76932ae4-09bc-472c-8dc6-64fea68d2797', |
| 126 | + 'phase': 'A', |
| 127 | + 'label': 'null', |
| 128 | + 'frequency': '30', |
| 129 | + 'control': '2019-11-07 13:21:23.000000-0500', |
| 130 | + "calibrate": '{"racf": 1.8, "pacf": 0.005}', |
| 131 | + } |
| 132 | + expected = { |
| 133 | + 'acronym': 'VPHM', |
| 134 | + 'description': 'El Segundo PMU 42 Ean', |
| 135 | + 'devacronym': 'PMU!EL_SEG_PMU_42', |
| 136 | + 'enabled': True, |
| 137 | + 'id': '76932ae4-09bc-472c-8dc6-64fea68d2797', |
| 138 | + 'phase': 'A', |
| 139 | + 'label': None, |
| 140 | + 'frequency': 30, |
| 141 | + 'control': '2019-11-07 13:21:23.000000-0500', |
| 142 | + "calibrate": {"racf": 1.8, "pacf": 0.005}, |
| 143 | + } |
| 144 | + |
121 | 145 | endpoint = Mock(Endpoint) |
122 | 146 | endpoint.streamInfo = Mock(return_value=("koala", 42, {}, serialized, None)) |
123 | 147 | stream = Stream(btrdb=BTrDB(endpoint), uuid=uu) |
@@ -221,19 +245,43 @@ def test_update_annotations(self): |
221 | 245 | endpoint = Mock(Endpoint) |
222 | 246 | endpoint.streamInfo = Mock(return_value=("koala", 42, {}, {}, None)) |
223 | 247 | stream = Stream(btrdb=BTrDB(endpoint), uuid=uu) |
224 | | - annotations = {"owner": "rabbit"} |
| 248 | + |
| 249 | + # Test realistic annotations with multiple types |
| 250 | + annotations = { |
| 251 | + "acronym": "VPHM", |
| 252 | + "description": "El Segundo PMU 42 Ean", |
| 253 | + "devacronym": "PMU!EL_SEG_PMU_42", |
| 254 | + "enabled": True, |
| 255 | + "id": uuid.UUID('76932ae4-09bc-472c-8dc6-64fea68d2797'), |
| 256 | + "phase": "A", |
| 257 | + "label": None, |
| 258 | + "frequency": 30, |
| 259 | + "control": EST.localize(datetime.datetime(2019, 11, 7, 13, 21, 23)), |
| 260 | + "calibrate": {"racf": 1.8, "pacf": 0.005}, |
| 261 | + } |
225 | 262 |
|
226 | 263 | stream.refresh_metadata() |
227 | 264 | stream.update(annotations=annotations) |
228 | 265 | stream._btrdb.ep.setStreamAnnotations.assert_called_once_with( |
229 | 266 | uu=uu, |
230 | 267 | expected=42, |
231 | | - changes={"owner": '"rabbit"'} |
| 268 | + changes={ |
| 269 | + 'acronym': 'VPHM', |
| 270 | + 'description': 'El Segundo PMU 42 Ean', |
| 271 | + 'devacronym': 'PMU!EL_SEG_PMU_42', |
| 272 | + 'enabled': 'true', |
| 273 | + 'id': '76932ae4-09bc-472c-8dc6-64fea68d2797', |
| 274 | + 'phase': 'A', |
| 275 | + 'label': 'null', |
| 276 | + 'frequency': '30', |
| 277 | + 'control': '2019-11-07 13:21:23.000000-0500', |
| 278 | + "calibrate": '{"racf": 1.8, "pacf": 0.005}', |
| 279 | + } |
232 | 280 | ) |
233 | 281 | stream._btrdb.ep.setStreamTags.assert_not_called() |
234 | 282 |
|
235 | 283 |
|
236 | | - def test_nested_conversions(self): |
| 284 | + def test_update_annotations_nested_conversions(self): |
237 | 285 | """ |
238 | 286 | Assert update correctly encodes nested annotation data |
239 | 287 | """ |
@@ -278,7 +326,24 @@ def test_nested_conversions(self): |
278 | 326 | } |
279 | 327 | ) |
280 | 328 |
|
| 329 | + def test_update_annotations_no_encoder(self): |
| 330 | + uu = uuid.UUID('0d22a53b-e2ef-4e0a-ab89-b2d48fb2592a') |
| 331 | + endpoint = Mock(Endpoint) |
| 332 | + endpoint.streamInfo = Mock(return_value=("koala", 42, {}, {}, None)) |
| 333 | + stream = Stream(btrdb=BTrDB(endpoint), uuid=uu) |
| 334 | + |
| 335 | + annotations = {"foo": "this is a string", "bar": "3.14"} |
| 336 | + |
| 337 | + stream.refresh_metadata() |
| 338 | + stream.update(annotations=annotations, encoder=None) |
| 339 | + stream._btrdb.ep.setStreamAnnotations.assert_called_once_with( |
| 340 | + uu=uu, |
| 341 | + expected=42, |
| 342 | + changes=annotations, |
| 343 | + ) |
281 | 344 |
|
| 345 | + # TODO: mock json.dumps |
| 346 | + # assert mock_dumps.assert_not_called() |
282 | 347 |
|
283 | 348 | ########################################################################## |
284 | 349 | ## exists tests |
|
0 commit comments