def test_compare_creates_modify(self, storage_mock, model_mock): storage_mock.return_value = self.mock_storage model_mock.return_value = mock_model # setup: message and database have entity with same id but different data field_name = fixtures.random_string() old_value = fixtures.random_string() new_value = fixtures.random_string() # field moet ook in model! message = fixtures.get_message_fixture(contents=None, **{field_name: new_value}) data_object = message['contents'][0] entity = fixtures.get_entity_fixture(**data_object) setattr(entity, field_name, old_value) self.mock_storage.get_current_ids.return_value = [entity] self.mock_storage.get_current_entity.return_value = entity # Add the field to the model as well mock_model.get_collection.return_value = { "entity_id": "identificatie", "version": '0.9', "has_states": False, "all_fields": { field_name: { "type": "GOB.String" } } } original_value = { "_last_event": 123, "_tid": data_object['_tid'], "_hash": "1234", field_name: new_value } self.mock_storage.compare_temporary_data.return_value = [ {'_original_value': original_value, '_tid': data_object['_tid'], 'type': 'MODIFY', '_last_event': 1, '_hash': '1234567890'}] result = compare(message) # expectations: modify event is generated self.assertIsNotNone(result["contents_ref"]) mock_writer.return_value.__enter__().write.assert_called_once() mock_writer.return_value.__enter__().write.assert_called_with( {'event': 'MODIFY', 'data': ANY, 'version': '0.9'}) result = mock_writer.return_value.__enter__().write.call_args_list[0][0][0] # modificatinos dict has correct modifications. modifications = result['data']['modifications'] self.assertEqual(len(modifications), 1) self.assertEqual(modifications[0]['key'], field_name) self.assertEqual(modifications[0]['old_value'], old_value) self.assertEqual(modifications[0]['new_value'], new_value)
def test_compare_fails_on_missing_dependencies(self, storage_mock, model_mock): storage_mock.return_value = self.mock_storage self.mock_storage.has_any_event.return_value = False message = fixtures.get_message_fixture(contents=[]) message["header"]["depends_on"] = { "xyz": "abc" } result = compare(message) self.assertEqual(result, {'header': mock.ANY, 'summary': mock.ANY, 'contents': None})
def test_compare_succeeds_on_found_dependencies(self, storage_mock, model_mock): storage_mock.return_value = self.mock_storage # setup: one entity in db, none in message self.mock_storage.has_any_event.return_value = True message = fixtures.get_message_fixture(contents=[]) message["header"]["depends_on"] = { "xyz": "abc" } result = compare(message) self.assertNotEqual(result, None)
def test_compare_creates_delete(self, storage_mock, model_mock): storage_mock.return_value = self.mock_storage model_mock.return_value = mock_model # setup: message and database have the same entity original_value = { "_last_event": 123 } self.mock_storage.compare_temporary_data.return_value = [ {'_original_value': original_value, '_tid': 1, 'type': 'DELETE', '_entity_tid': 2, '_last_event': 1, '_hash': '1234567890'}] message = fixtures.get_message_fixture() result = compare(message) # expectations: confirm event is generated self.assertIsNotNone(result["contents_ref"]) mock_writer.return_value.__enter__().write.assert_called_once() mock_writer.return_value.__enter__().write.assert_called_with( {'event': 'DELETE', 'data': ANY, 'version': '0.9'})
def test_compare_creates_add(self, storage_mock, model_mock): storage_mock.return_value = self.mock_storage model_mock.return_value = mock_model # setup: no entity in db, one in message message = fixtures.get_message_fixture() data = message["contents"][0] self.mock_storage.has_any_event.return_value = True original_value = { "_last_event": 123 } self.mock_storage.compare_temporary_data.return_value = [ {'_original_value': original_value, '_tid': data['_tid'], 'type': 'ADD', '_last_event': 1, '_hash': '1234567890'}] result = compare(message) # expectations: add event is generated self.assertIsNotNone(result["contents_ref"]) mock_writer.return_value.__enter__().write.assert_called_once() mock_writer.return_value.__enter__().write.assert_called_with({'event': 'ADD', 'data': ANY, 'version': '0.9'})
def setUp(self): self.mock_model = MagicMock(spec=GOBModel) self.msg = fixtures.get_message_fixture() model = { "entity_id": "identificatie", "version": "1", "has_states": False, } # Add the hash to the message populator = Populator(model, self.msg) self.msg['header']['source'] = 'any source' for content in self.msg['contents']: populator.populate(content) message = ImportMessage(self.msg) metadata = message.metadata GOBStorageHandler.base = MagicMock() self.storage = GOBStorageHandler(metadata) GOBStorageHandler.engine = MagicMock() GOBStorageHandler.engine.__enter__ = lambda self: None GOBStorageHandler.engine.__exit__ = lambda *args: None GOBStorageHandler.engine.begin = lambda: GOBStorageHandler.engine