def test_get_item(self): # Setup an event store. event_store = self.construct_event_store() # Put an event in the event store. entity_id = uuid4() event_store.append(Example.Created(entity_id=entity_id, a=1, b=2)) # Check ValueError is raised if repo doesn't have a mutator function... with self.assertRaises(ValueError): EventSourcedRepository(event_store=event_store, mutator=None) # ...and isn't if we pass a mutator function as a constructor arg. event_sourced_repo = EventSourcedRepository(event_store=event_store, mutator=Example.mutate) # Check the entity attributes. example = event_sourced_repo[entity_id] self.assertEqual(1, example.a) self.assertEqual(2, example.b) self.assertEqual(entity_id, example.id) # Setup an example repository, using the subclass ExampleRepository. example_repo = ExampleRepository(event_store=event_store) # Check the repo has the example. self.assertIn(entity_id, example_repo) self.assertNotIn(uuid4(), 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_get_item(self): # Setup an event store. event_store = self.construct_event_store() # Put an event in the event store. entity_id = uuid4() event_store.store( Example.Created( a=1, b=2, originator_id=entity_id, originator_topic=get_topic(Example), )) # Construct a repository. event_sourced_repo = EventSourcedRepository(event_store=event_store) # Check the entity attributes. example = event_sourced_repo[entity_id] self.assertEqual(1, example.a) self.assertEqual(2, example.b) self.assertEqual(entity_id, example.id) # Setup an example repository, using the subclass ExampleRepository. example_repo = ExampleRepository(event_store=event_store) # Check the repo has the example. self.assertIn(entity_id, example_repo) self.assertNotIn(uuid4(), 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 __init__(self, session, **kwargs): # Construct infrastructure objects for storing events with SQLAlchemy entity_active_record_strategy = SQLAlchemyActiveRecordStrategy( active_record_class=IntegerSequencedItemRecord, session=session) snapshot_active_record_strategy = SQLAlchemyActiveRecordStrategy( active_record_class=SnapshotRecord, session=session) # Initialize super(UnitOfWork, self).__init__( entity_active_record_strategy=entity_active_record_strategy, snapshot_active_record_strategy=snapshot_active_record_strategy, **kwargs) # Construct repositories self.snapshot_strategy = EventSourcedSnapshotStrategy( event_store=self.snapshot_event_store) # -- User self.users = EventSourcedRepository( event_store=self.entity_event_store, mutator=User._mutate, snapshot_strategy=self.snapshot_strategy) self.user_collections = CollectionRepository( event_store=self.entity_event_store) self.user_projection_policy = UserProjectionPolicy( user_collections=self.user_collections) self.user_snapshotting_policy = DomainSnapshottingPolicy( repository=self.users)
def test_get_item(self) -> None: # Setup an event store. event_store = self.construct_event_store() # Put an event in the event store. entity_id = uuid4() event_store.store_events([ Example.Created( a=1, b=2, originator_id=entity_id, originator_topic=get_topic(Example), ) ]) # Construct a repository. event_sourced_repo: EventSourcedRepository[ Example, Example.Event] = EventSourcedRepository(event_store=event_store) # Check the entity attributes. example = event_sourced_repo[entity_id] self.assertEqual(1, example.a) self.assertEqual(2, example.b) self.assertEqual(entity_id, example.id) # Check class-specific request. example = event_sourced_repo.get_instance_of(Example, entity_id) self.assertEqual(1, example.a) self.assertEqual(2, example.b) self.assertEqual(entity_id, example.id) # Check class-specific request fails on type. class SubExample(Example): pass self.assertIsNone( event_sourced_repo.get_instance_of(SubExample, entity_id)) # Setup an example repository, using the subclass ExampleRepository. example_repo = ExampleRepository(event_store=event_store) # Check the repo has the example. self.assertIn(entity_id, example_repo) self.assertNotIn(uuid4(), 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 __init__(self, datastore): event_store = EventStore( record_manager=SQLAlchemyRecordManager( session=datastore.session, record_class=IntegerSequencedNoIDRecord), event_mapper=SequencedItemMapper( sequence_id_attr_name="originator_id", position_attr_name="originator_version", ), ) self.repository = EventSourcedRepository(event_store=event_store) self.persistence_policy = PersistencePolicy( persist_event_type=ExampleAggregateRoot.Event, event_store=event_store)
def __init__(self): self.event_store = EventStore( record_manager=CassandraRecordManager( record_class=TimeuuidSequencedRecord, sequenced_item_class=SequencedItem), event_mapper=SequencedItemMapper( sequenced_item_class=SequencedItem, sequence_id_attr_name="originator_id", position_attr_name="event_id", ), ) self.repository = EventSourcedRepository(event_store=self.event_store) self.persistence_policy = PersistencePolicy( event_store=self.event_store, persist_event_type=DomainEvent)
def __init__(self): self.event_store = EventStore( active_record_strategy=CassandraActiveRecordStrategy( active_record_class=CqlTimeuuidSequencedItem, sequenced_item_class=SequencedItem, ), sequenced_item_mapper=SequencedItemMapper( sequenced_item_class=SequencedItem, event_sequence_id_attr='entity_id', event_position_attr='event_id', )) self.repository = EventSourcedRepository( mutator=ExampleEntity.mutate, event_store=self.event_store, ) self.persistence_policy = PersistencePolicy(self.event_store)
def __init__(self): self.event_store = EventStore( record_manager=CassandraRecordManager( record_class=TimeuuidSequencedRecord, sequenced_item_class=SequencedItem, ), sequenced_item_mapper=SequencedItemMapper( sequenced_item_class=SequencedItem, sequence_id_attr_name='originator_id', position_attr_name='event_id', ) ) self.repository = EventSourcedRepository( event_store=self.event_store, ) self.persistence_policy = PersistencePolicy(self.event_store)
def __init__(self, datastore): event_store = EventStore( active_record_strategy=SQLAlchemyActiveRecordStrategy( session=datastore.session, active_record_class=IntegerSequencedItemRecord, ), sequenced_item_mapper=SequencedItemMapper( sequence_id_attr_name='originator_id', position_attr_name='originator_version', )) self.aggregate_repository = EventSourcedRepository( mutator=ExampleAggregateRoot._mutate, event_store=event_store, ) self.persistence_policy = PersistencePolicy( event_type=ExampleAggregateRoot.Event, event_store=event_store, )
def __init__(self, **kwargs): session = get_session().session entity_active_record_strategy = SQLAlchemyActiveRecordStrategy( active_record_class=IntegerSequencedItemRecord, session=session) snapshot_active_record_strategy = SQLAlchemyActiveRecordStrategy( active_record_class=SnapshotRecord, session=session) super(UnitOfWork, self).__init__( entity_active_record_strategy=entity_active_record_strategy, snapshot_active_record_strategy=snapshot_active_record_strategy, **kwargs) self.snapshot_strategy = EventSourcedSnapshotStrategy( event_store=self.snapshot_event_store) self.users = EventSourcedRepository( event_store=self.entity_event_store, mutator=User._mutate, snapshot_strategy=self.snapshot_strategy) self.snapshotting_policy = KanbanSnapshottingPolicy( repository=self.users)
# set up the event store database = construct_sqlalchemy_db() event_store = create_event_store(database) # add the old global events to the event store event_store.store_events(events) # manually add a new event to the event store newco.add_new_shareholder(shareholder_name="Mars Investments", share_class=newco.share_classes[0], number_of_shares=1000) newco.__save__() domain_event_as_stored_item = event_store.event_mapper.item_from_event( events[-1]) event_store.record_manager.record_item(domain_event_as_stored_item) # verify that our event store contains the newest event assert event_store.get_most_recent_event( newco.id).shareholder_name == "Mars Investments" assert len(event_store.list_events(newco.id)) == 7 print("Event store tests passed") # create and test a repository from eventsourcing.infrastructure.eventsourcedrepository import EventSourcedRepository repo = EventSourcedRepository(event_store) newco_before_investment = repo.get_entity(newco.id, at=5) assert 'Mars Investments' not in [ sh.name for sh in newco_before_investment.shareholders ] print("Event sourced repository tests passed")
import uuid from eventsourcing.infrastructure.sequenceditemmapper import SequencedItemMapper from eventsourcing.infrastructure.eventsourcedrepository import EventSourcedRepository from eventsourcing.infrastructure.eventstore import EventStore from eventsourcing.infrastructure.sequenceditem import StoredEvent from eventsourcing.infrastructure.sqlalchemy.datastore import SQLAlchemyDatastore, SQLAlchemySettings from eventsourcing.infrastructure.sqlalchemy.manager import SQLAlchemyRecordManager from eventsourcing.infrastructure.sqlalchemy.records import StoredEventRecord datastore = SQLAlchemyDatastore( tables=(StoredEventRecord, ), settings=SQLAlchemySettings(uri='sqlite:///mydatabase')) datastore.setup_connection() datastore.setup_tables() recordmanager = SQLAlchemyRecordManager(session=datastore.session, record_class=StoredEventRecord, application_name=uuid.uuid4().hex, contiguous_record_ids=True, sequenced_item_class=StoredEvent) sequenceitemmapper = SequencedItemMapper(sequenced_item_class=StoredEvent) eventstore = EventStore(record_manager=recordmanager, sequenced_item_mapper=sequenceitemmapper) repository = EventSourcedRepository(event_store=eventstore)
def construct_repository(self, event_store, **kwargs): return EventSourcedRepository(event_store=event_store, **kwargs)
def setup_repository(self, **kwargs): self.repository = EventSourcedRepository(event_store=self.event_store, **kwargs)