def test_replay_entity(self): # Store example events. # Create entity1. entity_id1 = uuid4() event1 = Example.Created(originator_id=entity_id1, a=1, b=2) self.entity_event_store.append(event1) # Create entity2. entity_id2 = uuid4() event2 = Example.Created(originator_id=entity_id2, a=2, b=4) self.entity_event_store.append(event2) # Create entity3. entity_id3 = uuid4() event3 = Example.Created(originator_id=entity_id3, a=3, b=6) self.entity_event_store.append(event3) # Discard entity3. event4 = Example.Discarded(originator_id=entity_id3, originator_version=1) self.entity_event_store.append(event4) # Check the entities can be replayed. event_player = EventPlayer(event_store=self.entity_event_store, mutator=Example._mutate) # Check recovered entities have correct attribute values. recovered1 = event_player.replay_entity(entity_id1) self.assertEqual(entity_id1, recovered1.id) self.assertEqual(1, recovered1.a) recovered2 = event_player.replay_entity(entity_id2) self.assertEqual(2, recovered2.a) recovered3 = event_player.replay_entity(entity_id3) self.assertEqual(None, recovered3) # Check it works for "short" entities (should be faster, but the main thing is that it still works). # - just use a trivial mutate that always instantiates the 'Example'. event5 = Example.AttributeChanged(originator_id=entity_id1, originator_version=1, name='a', value=10) self.entity_event_store.append(event5) recovered1 = event_player.replay_entity(entity_id1) self.assertEqual(10, recovered1.a) event_player = EventPlayer( event_store=self.entity_event_store, mutator=Example._mutate, is_short=True, ) self.assertEqual(10, event_player.replay_entity(entity_id1).a)
def test_subscribe_to_decorator(self): entity_id1 = uuid4() event1 = Example.Created( originator_id=entity_id1, originator_topic=get_topic(Example), a=1, b=2 ) event2 = Example.Discarded( originator_id=entity_id1, originator_version=1, ) handler = mock.Mock() # Check we can assert there are no event handlers subscribed. assert_event_handlers_empty() @subscribe_to(Example.Created) def test_handler(e): """Doc string""" handler(e) # Check the decorator doesn't mess with the function doc string. self.assertEqual('Doc string', test_handler.__doc__) # Check can fail to assert event handlers empty. self.assertRaises(EventHandlersNotEmptyError, assert_event_handlers_empty) # Check event is received when published individually. publish(event1) handler.assert_called_once_with(event1) # Check event of wrong type is not received. handler.reset_mock() publish(event2) self.assertFalse(handler.call_count) # Check a list of events can be filtered. handler.reset_mock() publish([event1, event2]) handler.assert_called_once_with(event1) handler.reset_mock() publish([event1, event1]) self.assertEqual(2, handler.call_count) handler.reset_mock() publish([event2, event2]) self.assertEqual(0, handler.call_count) _event_handlers.clear()
def test_get_entity(self): # Store example events. entity_id1 = uuid4() event1 = Example.Created(entity_id=entity_id1, a=1, b=2) self.version_entity_event_store.append(event1) entity_id2 = uuid4() event2 = Example.Created(entity_id=entity_id2, a=2, b=4) self.version_entity_event_store.append(event2) entity_id3 = uuid4() event3 = Example.Created(entity_id=entity_id3, a=3, b=6) self.version_entity_event_store.append(event3) event4 = Example.Discarded(entity_id=entity_id3, entity_version=1) self.version_entity_event_store.append(event4) # Check the event sourced entities are correct. # - just use a trivial mutate that always instantiates the 'Example'. event_player = EventPlayer(event_store=self.version_entity_event_store, mutator=Example.mutate) # The the reconstituted entity has correct attribute values. self.assertEqual(entity_id1, event_player.replay_entity(entity_id1).id) self.assertEqual(1, event_player.replay_entity(entity_id1).a) self.assertEqual(2, event_player.replay_entity(entity_id2).a) self.assertEqual(None, event_player.replay_entity(entity_id3)) # Check entity3 raises KeyError. self.assertEqual(event_player.replay_entity(entity_id3), None) # Check it works for "short" entities (should be faster, but the main thing is that it still works). # - just use a trivial mutate that always instantiates the 'Example'. event5 = Example.AttributeChanged(entity_id=entity_id1, entity_version=1, name='a', value=10) self.version_entity_event_store.append(event5) event_player = EventPlayer(event_store=self.version_entity_event_store, mutator=Example.mutate) self.assertEqual(10, event_player.replay_entity(entity_id1).a) event_player = EventPlayer( event_store=self.version_entity_event_store, mutator=Example.mutate, is_short=True, ) self.assertEqual(10, event_player.replay_entity(entity_id1).a)
def test_subscribe_to_decorator(self): entity_id1 = uuid4() event1 = Example.Created(originator_id=entity_id1, originator_topic=get_topic(Example), a=1, b=2) event2 = Example.Discarded(originator_id=entity_id1, originator_version=1) handler1 = mock.Mock() handler2 = mock.Mock() handler3 = mock.Mock() # Check we can assert there are no event handlers subscribed. assert_event_handlers_empty() # Original style (one event class arg). @subscribe_to(Example.Created) def test_handler1(e): """Doc string""" handler1(e) # Naked style (not called). @subscribe_to def test_handler2(e): """Doc string""" handler2(e) # Multi-event style (many event class args). @subscribe_to((Example.Created, Example.Discarded)) def test_handler3(e): """Doc string""" handler3(e) # Check the decorator doesn't mess with the function doc string. self.assertEqual("Doc string", test_handler1.__doc__) self.assertEqual("Doc string", test_handler2.__doc__) self.assertEqual("Doc string", test_handler3.__doc__) # Check can fail to assert event handlers empty. self.assertRaises(EventHandlersNotEmptyError, assert_event_handlers_empty) # Check event is received when published individually. publish([event1]) handler1.assert_called_once_with(event1) handler2.assert_called_once_with(event1) handler3.assert_called_once_with(event1) # Check event of wrong type is not received. handler1.reset_mock() handler2.reset_mock() handler3.reset_mock() publish([event2]) self.assertFalse(handler1.call_count) handler2.assert_called_once_with(event2) handler3.assert_called_once_with(event2) # Check a list of events can be filtered. handler1.reset_mock() handler2.reset_mock() handler3.reset_mock() publish([event1, event2]) handler1.assert_called_once_with(event1) self.assertEqual(handler2.call_count, 2) self.assertEqual(handler3.call_count, 2) handler1.reset_mock() handler2.reset_mock() handler3.reset_mock() publish([event1, event1]) self.assertEqual(2, handler1.call_count) self.assertEqual(2, handler2.call_count) self.assertEqual(2, handler3.call_count) handler1.reset_mock() handler2.reset_mock() handler3.reset_mock() publish([event2, event2]) self.assertEqual(0, handler1.call_count) self.assertEqual(2, handler2.call_count) self.assertEqual(2, handler3.call_count) clear_event_handlers()