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_clean_reference(self): reference = { 'bronwaarde': random_string(), 'other_col': random_string(), 'begin_geldigheid': random_string(), } expected_result = { 'bronwaarde': reference['bronwaarde'], 'begin_geldigheid': reference['begin_geldigheid'], 'broninfo': { 'other_col': reference['other_col'], } } result = _clean_references(reference) self.assertEqual(expected_result, result)
def test_store_events(self, mock): metadata = fixtures.get_metadata_fixture() event = fixtures.get_event_fixture(metadata) event['data']['_last_event'] = fixtures.random_string() last_events = {event['data']['_tid']: event['data']['_last_event']} mock.return_value = self.mock_storage stats = UpdateStatistics() _store_events(self.mock_storage, last_events, [event], stats)
def test_clean_reference_without_other_values(self): reference = { 'bronwaarde': random_string(), } expected_result = { 'bronwaarde': reference['bronwaarde'], } result = _clean_references(reference) self.assertEqual(expected_result, result)
def test_extract_references_json_force_list(self): row = { "id": random_string(), "name": random_string(), # Added space is important. Should result in one object with the added space in the attribute (as opposed to two separate objects) "col": random_string() + ' ' + random_string(), } field_source = { "someattr": "col", } field_type = "GOB.JSON" result = _extract_references(row, field_source, field_type, True) expected_result = [{'someattr': row['col']}] self.assertEqual(expected_result, result) # And now without force_list set to True result = _extract_references(row, field_source, field_type, False) expected_result = {'someattr': row['col']} self.assertEqual(expected_result, result)
def test_extract_references_singleref_objectref(self): row = { "id": random_string(), "name": random_string(), "ref_col": { "a": random_string(), "b": random_string(), "c": random_string(), } } field_source = {"bronwaarde": "ref_col.b"} field_type = "GOB.Reference" result = _extract_references(row, field_source, field_type) expected_value = { "a": row["ref_col"]["a"], "b": row["ref_col"]["b"], "c": row["ref_col"]["c"], "bronwaarde": row["ref_col"]["b"] } self.assertEqual(expected_value, result)
def test_annotated_method(self): self.session = None # assert annotated method raises Exception when called without session in class with self.assertRaises(GOBException): self.annotated_method({}) # assert annotated method runs when session is not None self.session = fixtures.random_string() params = fixtures.random_dict() self.assertIsNone(self.in_method) self.annotated_method(**params) self.assertEqual(params, self.in_method)
def test_clean_manyreference(self): reference = [{ 'bronwaarde': random_string(), 'other_col': random_string(), }, { 'bronwaarde': random_string(), 'other_col': random_string(), }] expected_result = [{ 'bronwaarde': reference[0]['bronwaarde'], 'broninfo': { 'other_col': reference[0]['other_col'], } }, { 'bronwaarde': reference[1]['bronwaarde'], 'broninfo': { 'other_col': reference[1]['other_col'], } }] result = _clean_references(reference) self.assertEqual(expected_result, result)
def test_extract_references_bronwaarde_object_ref(self): row = { "id": random_string(), "name": random_string(), "col": random_string(), "ref_col": [{ "ref_attr": random_string(), "other_col": random_string(), "another_col": random_string(), }, { "ref_attr": random_string(), "other_col": random_string(), "another_col": random_string(), }], } field_source = { "bronwaarde": "ref_col.ref_attr", } field_type = "GOB.ManyReference" result = _extract_references(row, field_source, field_type) expected_result = [{ "bronwaarde": row["ref_col"][0]["ref_attr"], "ref_attr": row["ref_col"][0]["ref_attr"], "other_col": row["ref_col"][0]["other_col"], "another_col": row["ref_col"][0]["another_col"], }, { "bronwaarde": row["ref_col"][1]["ref_attr"], "ref_attr": row["ref_col"][1]["ref_attr"], "other_col": row["ref_col"][1]["other_col"], "another_col": row["ref_col"][1]["another_col"], }] self.assertEqual(expected_result, result) field_source = { "bronwaarde": "=somevalue", } result = _extract_references(row, field_source, field_type) expected_result = [{"bronwaarde": 'somevalue'}] self.assertEqual(expected_result, result)
def test_session_context(self): mock_session = mock.MagicMock() handler.GOBStorageHandler.Session = mock_session storage = handler.GOBStorageHandler(fixtures.random_string()) # assert starting situation self.assertIsNone(storage.session) # prepare mock mock_session_instance = mock.MagicMock() mock_session.return_value = mock_session_instance # test session creation in context with storage.get_session(): mock_session.assert_called_with() self.assertEqual(storage.session, mock_session_instance) # test session creation after leaving context: mock_session_instance.close.assert_called() self.assertIsNone(storage.session)
def setUp(self): self.mock_dataset = { 'source': { 'entity_id': fixtures.random_string(), 'application': fixtures.random_string(), 'name': fixtures.random_string(), 'type': 'file', 'config': {}, 'query': fixtures.random_string(), }, 'version': 0.1, 'catalogue': fixtures.random_string(), 'entity': fixtures.random_string(), 'gob_mapping': {} } self.mock_msg = { 'header': {} }
def test_convert(self): row = { "id": random_string(), "name": random_string(), "col": random_string(), "ref_col": [{ "ref_attr": random_string(), "other_ref_attr": random_string(), "other_col": random_string(), "another_col": random_string(), }, { "ref_attr": random_string(), "other_ref_attr": random_string(), "other_col": random_string(), "another_col": random_string(), }], } converter = Converter("catalog", "entity", { "gob_mapping": {}, "source": { "entity_id": "any entity id" } }) result = converter.convert(row) self.assertEqual(result, {"_source_id": mock.ANY})