def test_apply_return_values(self): """This method tests that all applied events are correctly returned from apply :return: """ applicator = EventApplicator(self.storage) applicator.add_add_event = MagicMock( return_value=['applied event1', 'applied event2']) applicator.apply_add_events = MagicMock( return_value=['applied event3', 'applied event4']) applicator.add_other_event = MagicMock( return_value=['applied event5', 'applied event6']) # BULKCONFIRM self.set_contents({'confirms': [{'_tid': 'entity_source_id'}]}) self.mock_event['action'] = 'BULKCONFIRM' event = dict_to_object(self.mock_event) self.assertEqual((ANY, 1, []), applicator.apply(event, dict(), set())) # ADD self.set_contents({'_tid': 'entity_source_id', '_hash': '123'}) self.mock_event['action'] = 'ADD' event = dict_to_object(self.mock_event) self.assertEqual((ANY, 1, ['applied event1', 'applied event2']), applicator.apply(event, dict(), set())) # CONFIRM (other) self.mock_event["action"] = 'CONFIRM' self.set_contents({'_tid': 'entity_source_id', '_hash': '123'}) event = dict_to_object(self.mock_event) self.assertEqual((ANY, 1, [ 'applied event3', 'applied event4', 'applied event5', 'applied event6' ]), applicator.apply(event, dict(), set()))
def test_apply(self): applicator = EventApplicator(self.storage) self.mock_event["action"] = 'CONFIRM' self.set_contents({'_tid': 'entity_source_id', '_hash': '123'}) event = dict_to_object(self.mock_event) applicator.apply(event, {}, set()) self.assertEqual(len(applicator.add_events), 0) self.assertEqual(len(applicator.other_events), 1)
def test_apply_bulk(self): applicator = EventApplicator(self.storage) self.mock_event["action"] = 'BULKCONFIRM' self.set_contents({'confirms': [{'_tid': 'entity_source_id'}]}) event = dict_to_object(self.mock_event) applicator.apply(event, dict(), set()) self.assertEqual(len(applicator.add_events), 0) self.storage.bulk_update_confirms.assert_called()
def test_apply_new_add(self): self.set_contents({'_tid': 'entity_source_id', '_hash': '123'}) event = dict_to_object(self.mock_event) with EventApplicator(self.storage) as applicator: applicator.apply(event, dict(), set()) self.assertEqual(len(applicator.add_events), 1) applicator.apply_all() self.assertEqual(len(applicator.add_events), 0) self.storage.add_add_events.assert_called()
def test_apply_existing_add(self): # Expect add event for existing deleted entity leads to add other event self.set_contents({'_hash': '123'}) event = dict_to_object(self.mock_event) event.tid = 'existing_source_id' applicator = EventApplicator(self.storage) applicator.add_add_event = MagicMock() applicator.apply_add_events = MagicMock() applicator.add_other_event = MagicMock() applicator.apply(event, {'existing_source_id': 'any event id'}, set()) applicator.add_add_event.assert_not_called() applicator.apply_add_events.assert_called_once() applicator.add_other_event.assert_called_once()
def test_apply_event_batch_add_delete(self): """ Test if a batch of events with ADD -> DELETE -> ADD of the same entity is handled correctly. We expect the second ADD event to be handled as an 'other' event, because it needs to revive the deleted entity. """ applicator = EventApplicator(self.storage) test_events = [ { 'action': 'ADD', 'contents': { '_tid': 'any source id' } }, { 'action': 'DELETE', 'contents': { '_tid': 'any source id' } }, { 'action': 'ADD', 'contents': { '_tid': 'any source id' } }, ] test_gob_events = [] add_event_source_ids = set() for event in test_events: self.mock_event['action'] = event['action'] self.set_contents(event['contents']) event_object = dict_to_object(self.mock_event) gob_event, *_ = applicator.apply(event_object, dict(), add_event_source_ids) test_gob_events.append(gob_event) # Expect the first add event to be applied, and a DELETE and ADD event in other events self.storage.add_add_events.assert_called_with(test_gob_events[:1]) self.assertEqual(len(applicator.add_events), 0) print(applicator.other_events) self.assertEqual( sum([len(x) for x in applicator.other_events.values()]), 2)
def test_apply_event_batch_modifies(self): """ Test if a batch of events multiple MODIFY events of the same entity is handled correctly. We expect the all modify events to be applied """ applicator = EventApplicator(self.storage) test_events = [ { 'action': 'MODIFY', 'contents': { '_tid': 'any source id' } }, { 'action': 'MODIFY', 'contents': { '_tid': 'any source id' } }, { 'action': 'MODIFY', 'contents': { '_tid': 'any source id' } }, ] test_gob_events = [] add_event_source_ids = set() for event in test_events: self.mock_event['action'] = event['action'] self.set_contents(event['contents']) event_object = dict_to_object(self.mock_event) gob_event, *_ = applicator.apply(event_object, dict(), add_event_source_ids) test_gob_events.append(gob_event) # Expect all 3 modify events to be added to other events self.assertEqual(len(applicator.add_events), 0) self.assertEqual( sum([len(x) for x in applicator.other_events.values()]), 3)
def test_apply_new_add_exception(self): self.set_contents({'_tid': 'entity_source_id', '_hash': '123'}) event = dict_to_object(self.mock_event) with self.assertRaises(GOBException): with EventApplicator(self.storage) as applicator: applicator.apply(event, dict(), set())