def test_modify_apply_to(self): source_id = fixtures.random_string() data = { 'identificatie': source_id, '_hash': fixtures.random_string(), 'modifications': [{ 'key': 'identificatie', 'new_value': 'new identificatie', 'old_value': 'old identificatie', }] } event = import_events.MODIFY.create_event(source_id, source_id, data) entity = fixtures.get_entity_fixture({ 'identificatie': 'old identificatie', }) gob_event = import_events.MODIFY(event['data'], fixtures.get_metadata_fixture()) gob_event._extract_modifications = MagicMock( return_value={'identificatie': 'new identificatie'}) gob_event.apply_to(entity) self.assertEqual(entity.identificatie, 'new identificatie')
def test_modify_create_event(self): source_id = fixtures.random_string() event = import_events.MODIFY.create_event( source_id, source_id, { 'modifications': [], '_hash': fixtures.random_string() }) self.assertEqual(event['event'], 'MODIFY')
def test_modify_create_event(self): tid = fixtures.random_string() event = import_events.MODIFY.create_event( tid, { 'modifications': [], '_hash': fixtures.random_string() }, '0.9') self.assertEqual(event['event'], 'MODIFY') self.assertEqual(event['version'], '0.9')
def test_json(self): GobType = get_gob_type("GOB.JSON") self.assertEqual(GobType.name, "JSON") self.assertEqual('null', GobType.from_value(None).json) key = fixtures.random_string() value = fixtures.random_string() generated_json = GobType.from_value({key: value}).json self.assertEqual(f'{{"{key}": "{value}"}}', GobType.from_value({ key: value }).json) self.assertEqual(f'["{key}", "{value}"]', GobType.from_value([key, value]).json) value = random.randint(9, 33) self.assertEqual(f'{{"{key}": {value}}}', GobType.from_value({key: value})) value = False self.assertEqual(f'{{"{key}": false}}', GobType.from_value({key: value})) value = None self.assertEqual(f'{{"{key}": null}}', GobType.from_value({key: value})) self.assertEqual(generated_json, GobType.from_value(generated_json)) self.assertEqual(generated_json, GobType(generated_json)) out_of_order = '{"b": "c", "a": "d"}' in_order = '{"a": "d", "b": "c"}' self.assertEqual(in_order, GobType(out_of_order)) self.assertEqual(in_order, GobType.from_value(GobType(out_of_order))) # Test unknown format with self.assertRaises(GOBTypeException): GobType.from_value('{"test" = "test"}') # DB ouptut is json self.assertIsInstance( GobType.from_value('{"key": "value"}').to_db, dict) # Python value is dict self.assertIsInstance( GobType.from_value('{"key": "value"}').to_value, dict)
def test_catalog_collection_names_from_ref(self): model = GOBModel() model.split_ref = MagicMock() ref = random_string() result = model.get_catalog_collection_names_from_ref(ref) model.split_ref.assert_called_with(ref) self.assertEqual(model.split_ref.return_value, result)
def test_on_message(self): global return_message # setup mocks and fixtures mocked_handler = mock.Mock(wraps=handler) service = fixtures.get_service_fixture(mocked_handler) single_service = [v for v in service.values()][0] message = {} queue = {'name': single_service['queue']} key = {'name': single_service['key']} connection = AsyncConnection({}) # setup expectations return_message = fixtures.random_string() return_queue = single_service['report'] with mock.patch.object(connection, "publish") as mocked_publish: # on_message = messagedriven_service._get_on_message(single_service) result = messagedriven_service._on_message(connection, single_service, message) # The result should be True self.assertTrue(result) # The message handler should be called with the message mocked_handler.assert_called_with(message) # The return message should be published on the return queue mocked_publish.assert_called_with(return_queue, return_queue['key'], return_message)
def test_messagedriven_service(self, mocked_connection, mock_init_broker): global return_method return_method = fixtures.random_string() service_definition = fixtures.get_service_fixture(handler) single_service = [v for v in service_definition.values()][0] expected_queue = single_service['queue'] messagedriven_service = MessagedrivenService(service_definition, 'Any name') messagedriven_service._init = MagicMock() messagedriven_service._start_threads = MagicMock() messagedriven_service._heartbeat_loop = MagicMock() messagedriven_service.keep_running = False messagedriven_service.start() mock_init_broker.assert_called_with() mocked_connection.assert_called_with(CONNECTION_PARAMS, {}) mocked_connection.return_value.__enter__.return_value.subscribe\ .assert_called_with([expected_queue], mock.ANY) # Inner function messagedriven_service._heartbeat_loop.assert_called_once() messagedriven_service._start_threads.assert_not_called()
def test_add_apply_to(self): source_id = fixtures.random_string() event = import_events.ADD.create_event(source_id, source_id, {'identificatie': source_id}) metadata = fixtures.get_metadata_fixture() entity = fixtures.get_entity_fixture(event['data']) gob_event = import_events.ADD(event['data'], metadata) gob_event.apply_to(entity) # Assert entity has the attribute with the value self.assertEqual(entity.identificatie, source_id)
def test_add_apply_to(self): tid = fixtures.random_string() event = import_events.ADD.create_event(tid, {'identificatie': tid}, '0.9') self.assertEqual(event['version'], '0.9') metadata = fixtures.get_metadata_fixture() entity = fixtures.get_entity_fixture(event['data']) gob_event = import_events.ADD("tid", event['data'], metadata) gob_event.apply_to(entity) # Assert entity has the attribute with the value self.assertEqual(entity.identificatie, tid)
def test_gob_type_and_string(self): GobType = get_gob_type("GOB.String") self.assertEqual(GobType.name, "String") self.assertEqual('None', str(GobType(None))) fixture = fixtures.random_string() # Gobtype can be instantiated with a string gob_type = GobType(fixture) # Gobtype can be compared with string self.assertTrue(gob_type == fixture) # Gobtype has a json representation self.assertEqual(gob_type.json, f'"{fixture}"') # Gobtype can be constructed from a value gob_type1 = GobType.from_value(fixture) # Gobtype can be compared with other gob_type self.assertTrue(gob_type == gob_type1) # DB ouptut is string self.assertIsInstance(gob_type.to_db, str) # Gobtype cannot be instantiated with something different than a string with self.assertRaises(GOBException): GobType(fixtures.random_bool()) with self.assertRaises(GOBException): GobType(random.randint(0, 10)) # Gobtype can be constructed from a value gob_type1 = GobType.from_value(int) gob_type2 = GobType.from_value(bool) # JSON values are String representations self.assertEqual('"1"', GobType.from_value(1).json) self.assertEqual('"True"', GobType.from_value(True).json) # Gobtype can be constructed from None self.assertNotEqual(GobType.from_value(None), GobType.from_value("None")) self.assertEqual(GobType.from_value(None), GobType.from_value(None)) self.assertEqual('null', GobType.from_value(None).json) self.assertEqual('"None"', GobType.from_value("None").json) self.assertEqual('null', GobType.from_value(float('nan')).json)
def test_messagedriven_service(self, mocked_connection, mocked_init, mocked_send, mocked_heartbeat): global return_method return_method = fixtures.random_string() service_definition = fixtures.get_service_fixture(handler) single_service = [v for v in service_definition.values()][0] expected_key = single_service['key'] expected_queue = single_service['queue'] expected_exchange = single_service['exchange'] messagedriven_service.keep_running = False messagedriven_service.messagedriven_service(service_definition, "Any name") mocked_init.assert_called_with() mocked_connection.assert_called_with(CONNECTION_PARAMS, {}) mocked_connection.return_value.__enter__.return_value.subscribe\ .assert_called_with([{'exchange': expected_exchange, 'name': expected_queue, 'key': expected_key}], mock.ANY) # Inner function mocked_heartbeat.asssert_not_called() mocked_send.assert_not_called()
def test_bulkconfirm_create_event(self): source_id = fixtures.random_string() event = import_events.BULKCONFIRM.create_event([]) self.assertEqual(event['event'], 'BULKCONFIRM')
def test_confirm_create_event(self): source_id = fixtures.random_string() event = import_events.CONFIRM.create_event(source_id, source_id, {}) self.assertEqual(event['event'], 'CONFIRM')
def test_delete_create_event(self): source_id = fixtures.random_string() event = import_events.DELETE.create_event(source_id, source_id, {}) self.assertEqual(event['event'], 'DELETE')
def test_delete_create_event(self): tid = fixtures.random_string() event = import_events.DELETE.create_event(tid, {}, '0.9') self.assertEqual(event['event'], 'DELETE') self.assertEqual(event['version'], '0.9')
def test_add_create_event(self): source_id = fixtures.random_string() event = import_events.ADD.create_event(source_id, source_id, {}) self.assertEqual(event['event'], 'ADD')
def test_get_event(self): for event in events.GOB_EVENTS: self.assertEqual(events._get_event(f"{event.name}"), event) with self.assertRaises(GOBException): events._get_event(fixtures.random_string())
def test_confirm_create_event(self): tid = fixtures.random_string() event = import_events.CONFIRM.create_event(tid, {}, '0.9') self.assertEqual(event['event'], 'CONFIRM') self.assertEqual(event['version'], '0.9')
def test_modify_create_event_without_modifications(self): source_id = fixtures.random_string() with self.assertRaises(GOBException): event = import_events.MODIFY.create_event( source_id, source_id, {'_hash': fixtures.random_string()})
def test_add_create_event(self): tid = fixtures.random_string() event = import_events.ADD.create_event(tid, {}, '0.9') self.assertEqual(event['event'], 'ADD') self.assertEqual(event['version'], '0.9')