def test_persist_only_notifications(mock_messenger, mock_config, create_indexes): from history.subscriber.persister import start_dojot_messenger p = Persister() mock_config.dojot = { "management": { "user": "******", "tenant": "dojot-management" }, "subjects": { "tenancy": "dojot.tenancy", "devices": "dojot.device-manager.device", "device_data": "device-data" } } start_dojot_messenger(mock_config, p, True) mock_messenger.assert_called_once_with("Persister", mock_config) mock_messenger().create_channel.assert_called_once_with( "dojot.notifications", "r") assert mock_messenger().create_channel.call_count == 1 mock_messenger().on.assert_any_call( mock_config.dojot['subjects']['tenancy'], "message", p.handle_new_tenant) mock_messenger().on.assert_any_call("dojot.notifications", "message", p.handle_notification) assert mock_messenger().on.call_count == 2
def test_handle_event_devices_configure(self, mock_event_data, mock_parse_message): message = json.dumps({ "event": "configure" }) p = Persister() p.handle_event_devices('admin', message) assert mock_event_data.called
def test_handle_notification_no_timestamp(self): # raises exception because does not have "timestamp" with pytest.raises(Exception): p = Persister() message = json.dumps( {"ts": 1567704621, "metaAttrsFilter": {"shouldPersist": False}}) p.handle_notification('admin', message)
def create__device(transaction): _create_device = { "event": "create", "meta": { "service": "admin" }, "data": { "id": "labtemp", "label": "Device 1", "templates": [1, 2, 3], "attrs": {}, "created": "2018-02-06T10:43:40.890330+00:00" } } persister = Persister() persister.init_mongodb() persister.handle_event_devices("admin", json.dumps(_create_device)) _update_data = { "metadata": { "deviceid": "labtemp", "protocol": "mqtt", "payload": "json" }, "attrs": { "temperature": "22.12" } } persister.handle_event_data("admin", json.dumps(_update_data)) _update_data['attrs']['temperature'] = "23.12" persister.handle_event_data("admin", json.dumps(_update_data))
def test_handle_event_data_attrs_no_dict(self): message = json.dumps({ "attrs": 80, "metadata": {"deviceid": "labtemp", "timestamp": 1567704621, "tenant": "admin"} }) p = Persister() p.handle_event_data('admin', message)
def test_handle_event_devices_create(self, mock_create_indexes): message = json.dumps({ "event": "create", "data": {"id": "e51k"}, "meta": {"service": "admin"} }) p = Persister() p.handle_event_devices('admin', message) assert mock_create_indexes.called
def test_handle_notification_persisting(self): p = Persister() message = json.dumps({ "timestamp": 1567704621, "metaAttrsFilter": { "shouldPersist": True } }) p.handle_notification('admin', message)
def setup(transaction): _create_device = { "event": "create", "meta": { "service": "admin" }, "data": { "id": "labtemp", "label": "Device 1", "templates": [1, 2, 3], "attrs": {}, "created": "2018-02-06T10:43:40.890330+00:00" } } persister = Persister() persister.init_mongodb() persister.handle_event_devices("admin", json.dumps(_create_device)) _update_data = { "metadata": { "deviceid": "labtemp", "protocol": "mqtt", "payload": "json", "timestamp": 1528226137452 }, "attrs": { "temperature": "22.12" } } persister.handle_event_data("admin", json.dumps(_update_data)) _update_data['attrs']['temperature'] = "23.12" persister.handle_event_data("admin", json.dumps(_update_data)) _new_tenant = {"tenant": "admin"} persister.handle_new_tenant("admin", json.dumps(_new_tenant)) _notification = { "msgID": "12345", "timestamp": 1551111524, "metaAttrsFilter": { "level": 1, "shouldPersist": "True" }, "message": "DEU ALGUMA COISA MUITO ERRADO", "subject": "debug" } persister.handle_notification("admin", json.dumps(_notification))
def test_handle_event_data_invalid_json(self): p = Persister() message = "Test_Message" assert p.handle_event_data('admin', message) is None
def test_handle_new_tenant(self, mock_index_for_tenant): message = json.dumps({"tenant": "normal"}) p = Persister() p.handle_new_tenant('admin', message) assert mock_index_for_tenant.called
def test_handle_notification_invalid_json(self): p = Persister() message = "This is a string" assert p.handle_notification('admin', message) is None
def test_enable_collection_sharding(self, mock_command, mock_create_index): p = Persister() p.init_mongodb() p.enable_collection_sharding('admin_notifications') mock_create_index.assert_called_once() assert mock_command.call_count == 2
def test_init_mongodb(self, mock_create_indexes): p = Persister() p.init_mongodb('admin_notifications') assert mock_create_indexes.called
def test_create_index_for_notifications(self, mock_create_index): p = Persister() p.create_indexes_for_notifications('admin') assert mock_create_index.called
def test_create_index_for_tenant(self, mock_create_indexes): p = Persister() p.create_index_for_tenant('admin') assert mock_create_indexes.called
def test_parse_datetime_error(self): with pytest.raises(TypeError): p = Persister() p.parse_datetime('1567704621')
def test_init_mongodb_create_index(self, mock_create_index): p = Persister() p.init_mongodb('admin_notifications') assert mock_create_index.call_count == 3
def setup(transaction): _create_device = { "event": "create", "meta": { "service": "admin" }, "data": { "id": "b374a5", "label": "Device 1", "templates": [1, 2, 3], "attrs": {}, "created": "2018-02-06T10:43:40.890330+00:00" } } persister = Persister() persister.init_mongodb() persister.handle_event_devices("admin", json.dumps(_create_device)) _update_data = { "metadata": { "deviceid": "b374a5", "protocol": "mqtt", "payload": "json", "timestamp": 1528226137452 }, "attrs": { "temperature": "22.12", "pressure": "1013" } } persister.handle_event_data("admin", json.dumps(_update_data)) _update_data['attrs']['temperature'] = "23.12" persister.handle_event_data("admin", json.dumps(_update_data)) _update_data['attrs']['temperature'] = "24.10" persister.handle_event_data("admin", json.dumps(_update_data)) _update_data['attrs']['pressure'] = "1033" persister.handle_event_data("admin", json.dumps(_update_data)) _update_data['attrs']['pressure'] = "1053" persister.handle_event_data("admin", json.dumps(_update_data)) _new_tenant = {"tenant": "admin"} persister.handle_new_tenant("admin", json.dumps(_new_tenant)) _notification = { "msgID": "12345", "timestamp": 1550693872863, "metaAttrsFilter": { "level": 3, "shouldPersist": True }, "message": "Something went wrong.", "subject": "user_notification", } persister.handle_notification("admin", json.dumps(_notification))