Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.assertRaises(EntityIDConsistencyError,
entity2._validate_originator,
DomainEvent(entity_id=entity2.id+'wrong', entity_version=0)
)
# Should fail to validate event with wrong entity version.
self.assertRaises(EntityVersionConsistencyError,
entity2._validate_originator,
DomainEvent(entity_id=entity2.id, entity_version=0)
)
# Should validate event with correct entity ID and version.
entity2._validate_originator(
DomainEvent(entity_id=entity2.id, entity_version=entity2.version)
)
# Check an entity can be reregistered with the same ID.
replacement_event = Example.Created(entity_id=entity1.id, a=11, b=12)
# replacement = Example.mutate(event=replacement_event)
publish(event=replacement_event)
# Check the replacement entity can be retrieved from the example repository.
replacement = repo[entity1.id]
assert isinstance(replacement, Example)
self.assertEqual(replacement.a, 11)
self.assertEqual(replacement.b, 12)
def test_get_item(self):
# Setup an event store.
stored_event_repo = PythonObjectsStoredEventRepository()
event_store = EventStore(stored_event_repo=stored_event_repo)
# Put an event in the event store.
entity_id = 'entity1'
event_store.append(Example.Created(entity_id=entity_id, a=1, b=2))
# Setup an example repository.
example_repo = ExampleRepo(event_store=event_store)
# Check the repo has the example.
self.assertIn(entity_id, example_repo)
self.assertNotIn('xxxxxxxx', example_repo)
# Check the entity attributes.
example = example_repo[entity_id]
self.assertEqual(1, example.a)
self.assertEqual(2, example.b)
self.assertEqual(entity_id, example.id)
def test_hash(self):
event1 = Example.Created(entity_id='entity1', a=1, b=2, domain_event_id=3)
event2 = Example.Created(entity_id='entity1', a=1, b=2, domain_event_id=3)
self.assertEqual(hash(event1), hash(event2))
def test_get_entity(self):
# Setup an event store, using Python objects.
event_store = EventStore(stored_event_repo=PythonObjectsStoredEventRepository())
# Store example events.
event1 = Example.Created(entity_id='entity1', a=1, b=2)
event_store.append(event1)
event2 = Example.Created(entity_id='entity2', a=2, b=4)
event_store.append(event2)
event3 = Example.Created(entity_id='entity3', a=3, b=6)
event_store.append(event3)
event4 = Example.Discarded(entity_id='entity3', entity_version=1)
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=event_store, id_prefix='Example', mutate_func=Example.mutate)
# The the reconstituted entity has correct attribute values.
self.assertEqual('entity1', event_player.replay_events('entity1').id)
self.assertEqual(1, event_player.replay_events('entity1').a)
self.assertEqual(2, event_player.replay_events('entity2').a)
self.assertEqual(None, event_player.replay_events('entity3'))
# Check entity3 raises KeyError.
self.assertEqual(event_player.replay_events('entity3'), None)
def test_equality_comparison(self):
event1 = Example.Created(entity_id='entity1', a=1, b=2, domain_event_id=3)
event2 = Example.Created(entity_id='entity1', a=1, b=2, domain_event_id=3)
event3 = Example.Created(entity_id='entity1', a=3, b=2, domain_event_id=3)
self.assertEqual(event1, event2)
self.assertNotEqual(event1, event3)
self.assertNotEqual(event2, event3)
self.assertNotEqual(event2, None)
def test_event_attributes(self):
event = Example.Created(entity_id='entity1', a=1, b=2)
# Check constructor keyword args lead to read-only attributes.
self.assertEqual(1, event.a)
self.assertEqual(2, event.b)
self.assertRaises(AttributeError, getattr, event, 'c')
self.assertRaises(AttributeError, setattr, event, 'c', 3)
# Check domain event has auto-generated timestamp.
self.assertIsInstance(event.timestamp, float)
# Check timestamp value can be given to domain events.
self.assertEqual(3, Example.Created(entity_id='entity1', a=1, b=2, domain_event_id=3).domain_event_id)
domain_event_id = uuid1().hex
self.assertEqual(timestamp_from_uuid(domain_event_id), Example.Created(entity_id='entity1', a=1, b=2, domain_event_id=domain_event_id).timestamp)