def test_all_domain_events(self): event_store = self.construct_event_store() # Check there are zero domain events in total. domain_events = event_store.all_domain_events() domain_events = list(domain_events) self.assertEqual(len(domain_events), 0) # Store a domain event. entity_id1 = uuid4() event1 = Example.Created(originator_id=entity_id1, a=1, b=2) event_store.append(event1) # Store another domain event for the same entity. event1 = Example.AttributeChanged(originator_id=entity_id1, a=1, b=2, originator_version=1) event_store.append(event1) # Store a domain event for a different entity. entity_id2 = uuid4() event1 = Example.Created(originator_id=entity_id2, a=1, b=2) event_store.append(event1) # Check there are three domain events in total. domain_events = event_store.all_domain_events() domain_events = list(domain_events) self.assertEqual(len(domain_events), 3)
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_get_domain_events(self): event_store = self.construct_event_store() # Check there are zero stored events in the repo. entity_id1 = uuid4() entity_events = event_store.get_domain_events(originator_id=entity_id1) entity_events = list(entity_events) self.assertEqual(0, len(entity_events)) # Check there are zero events in the event store, using iterator. entity_events = event_store.get_domain_events(originator_id=entity_id1, page_size=1) entity_events = list(entity_events) self.assertEqual(0, len(entity_events)) # Store a domain event. event1 = Example.Created(a=1, b=2, originator_id=entity_id1, originator_topic=get_topic(Example)) event_store.store(event1) # Check there is one event in the event store. entity_events = event_store.get_domain_events(originator_id=entity_id1) entity_events = list(entity_events) self.assertEqual(1, len(entity_events)) # Check there are two events in the event store, using iterator. entity_events = event_store.get_domain_events(originator_id=entity_id1, page_size=1) entity_events = list(entity_events) self.assertEqual(1, len(entity_events)) # Store another domain event. event1 = Example.AttributeChanged( a=1, b=2, originator_id=entity_id1, originator_version=1, ) event_store.store(event1) # Check there are two events in the event store. entity_events = event_store.get_domain_events(originator_id=entity_id1) entity_events = list(entity_events) self.assertEqual(2, len(entity_events)) # Check there are two events in the event store, using iterator. entity_events = event_store.get_domain_events(originator_id=entity_id1, page_size=1) entity_events = list(entity_events) self.assertEqual(2, len(entity_events))
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_all_domain_events(self): event_store = self.construct_event_store() # Check there are zero domain events in total. domain_events = event_store.all_events() domain_events = list(domain_events) self.assertEqual(len(domain_events), 0) # Store a domain event. entity_id1 = uuid4() event1 = Example.Created(a=1, b=2, originator_id=entity_id1, originator_topic=get_topic(Example)) event_store.store_events([event1]) # Store another domain event for the same entity. event1 = Example.AttributeChanged( a=1, b=2, originator_id=entity_id1, originator_version=1, __previous_hash__=event1.__event_hash__, ) event_store.store_events([event1]) # Store a domain event for a different entity. entity_id2 = uuid4() event1 = Example.Created(originator_topic=get_topic(Example), originator_id=entity_id2, a=1, b=2) event_store.store_events([event1]) # Check there are three domain events in total. domain_events = event_store.all_events() domain_events = list(domain_events) self.assertEqual(len(domain_events), 3)