Esempio n. 1
0
    def setUp(self):
        super(WithPersistencePolicies, self).setUp()
        # Setup the persistence subscriber.
        self.entity_event_store = EventStore(
            record_manager=self.entity_record_manager,
            sequenced_item_mapper=SequencedItemMapper(
                sequenced_item_class=SequencedItem,
                sequence_id_attr_name='originator_id',
                position_attr_name='originator_version'
            )
        )
        self.log_event_store = EventStore(
            record_manager=self.log_record_manager,
            sequenced_item_mapper=SequencedItemMapper(
                sequenced_item_class=SequencedItem,
                sequence_id_attr_name='originator_id',
                position_attr_name='timestamp'
            )
        )
        self.snapshot_store = EventStore(
            record_manager=self.snapshot_record_manager,
            sequenced_item_mapper=SequencedItemMapper(
                sequenced_item_class=SequencedItem,
                sequence_id_attr_name='originator_id',
                position_attr_name='originator_version'
            )
        )

        self.integer_sequenced_event_policy = None
        if self.entity_event_store is not None:
            self.integer_sequenced_event_policy = PersistencePolicy(
                event_store=self.entity_event_store,
                event_type=VersionedEntity.Event,
            )

        self.timestamp_sequenced_event_policy = None
        if self.log_event_store is not None:
            self.timestamp_sequenced_event_policy = PersistencePolicy(
                event_store=self.log_event_store,
                event_type=Logged,
            )

        self.snapshot_policy = None
        if self.snapshot_store is not None:
            self.snapshot_policy = PersistencePolicy(
                event_store=self.snapshot_store,
                event_type=Snapshot,
            )
Esempio n. 2
0
 def construct_persistence_policy(self) -> None:
     """
     Constructs persistence policy object.
     """
     self._persistence_policy = PersistencePolicy(
         event_store=self.event_store,
         persist_event_type=self.persist_event_type)
Esempio n. 3
0
 def setup_persistence_policy(self, persist_event_type):
     persist_event_type = persist_event_type or DomainEntity.Event
     super(SnapshottingApplication,
           self).setup_persistence_policy(persist_event_type)
     self.snapshotting_policy = SnapshottingPolicy(self.repository,
                                                   self.period)
     self.snapshot_persistence_policy = PersistencePolicy(
         event_store=self.snapshot_store, event_type=Snapshot)
Esempio n. 4
0
 def __init__(self):
     self.event_store = EventStore(
         record_manager=CassandraRecordManager(
             record_class=StoredEventRecord,
             sequenced_item_class=StoredEvent),
         event_mapper=SequencedItemMapper(sequenced_item_class=StoredEvent,
                                          other_attr_names=()),
     )
     self.repository = ExampleRepository(event_store=self.event_store)
     self.persistence_policy = PersistencePolicy(
         event_store=self.event_store, persist_event_type=DomainEvent)
Esempio n. 5
0
 def __init__(self):
     self.event_store = EventStore(
         active_record_strategy=CassandraActiveRecordStrategy(
             active_record_class=StoredEventRecord,
             sequenced_item_class=StoredEvent,
         ),
         sequenced_item_mapper=SequencedItemMapper(
             sequenced_item_class=StoredEvent,
             other_attr_names=(),
         ))
     self.repository = ExampleRepository(event_store=self.event_store, )
     self.persistence_policy = PersistencePolicy(self.event_store)
Esempio n. 6
0
 def __init__(self, datastore):
     self.event_store = EventStore(
         active_record_strategy=SQLAlchemyActiveRecordStrategy(
             datastore=datastore,
             active_record_class=SqlExtendedIntegerSequencedItem,
             sequenced_item_class=ExtendedSequencedItem,
         ),
         sequenced_item_mapper=ExtendedSequencedItemMapper(
             sequenced_item_class=ExtendedSequencedItem,
             event_sequence_id_attr='entity_id',
             event_position_attr='entity_version',
         ))
     self.repository = ExampleRepository(event_store=self.event_store, )
     self.persistence_policy = PersistencePolicy(self.event_store)
 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)
Esempio n. 8
0
 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, session):
     self.event_store = EventStore(
         record_manager=SQLAlchemyRecordManager(
             session=session,
             record_class=StoredEventRecord,
             sequenced_item_class=StoredEvent,
         ),
         sequenced_item_mapper=SequencedItemMapper(
             sequenced_item_class=StoredEvent,
             sequence_id_attr_name='originator_id',
             position_attr_name='originator_version',
         ))
     self.repository = ExampleRepository(event_store=self.event_store, )
     self.persistence_policy = PersistencePolicy(self.event_store)
Esempio n. 10
0
 def __init__(self, session):
     self.event_store = EventStore(
         active_record_strategy=SQLAlchemyActiveRecordStrategy(
             session=session,
             active_record_class=ExtendedIntegerSequencedItemRecord,
             sequenced_item_class=ExtendedSequencedItem,
         ),
         sequenced_item_mapper=ExtendedSequencedItemMapper(
             sequenced_item_class=ExtendedSequencedItem,
             sequence_id_attr_name='originator_id',
             position_attr_name='originator_version',
             other_attr_names=('timestamp', ),
         ))
     self.repository = ExampleRepository(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(
         record_manager=SQLAlchemyRecordManager(
             session=datastore.session, record_class=IntegerSequencedNoIDRecord
         ),
         event_mapper=SequencedItemMapper(
             sequence_id_attr_name="originator_id",
             position_attr_name="originator_version",
         ),
     )
     # Todo: Remove having two repositories, because they are identical.
     self.aggregate1_repository = AggregateRepository(event_store=event_store)
     self.aggregate2_repository = AggregateRepository(event_store=event_store)
     self.persistence_policy = PersistencePolicy(
         persist_event_type=ExampleAggregateRoot.Event, event_store=event_store
     )
 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, session):
     self.event_store = EventStore(
         record_manager=SQLAlchemyRecordManager(
             session=session,
             record_class=ExtendedIntegerSequencedRecord,
             sequenced_item_class=ExtendedSequencedItem,
         ),
         event_mapper=ExtendedSequencedItemMapper(
             sequenced_item_class=ExtendedSequencedItem,
             sequence_id_attr_name="originator_id",
             position_attr_name="originator_version",
             other_attr_names=("timestamp", ),
         ),
     )
     self.repository = ExampleRepository(event_store=self.event_store)
     self.persistence_policy = PersistencePolicy(
         event_store=self.event_store, persist_event_type=DomainEvent)
Esempio n. 15
0
 def __init__(self, datastore):
     event_store = EventStore(record_manager=SQLAlchemyRecordManager(
         session=datastore.session,
         record_class=IntegerSequencedNoIDRecord,
     ),
                              sequenced_item_mapper=SequencedItemMapper(
                                  sequence_id_attr_name='originator_id',
                                  position_attr_name='originator_version',
                              ))
     self.aggregate1_repository = AggregateRepository(
         event_store=event_store, )
     self.aggregate2_repository = AggregateRepository(
         event_store=event_store, )
     self.persistence_policy = PersistencePolicy(
         event_type=ExampleAggregateRoot.Event,
         event_store=event_store,
     )
Esempio n. 16
0
 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,
     )
Esempio n. 17
0
    def test_take_snapshot(self):
        self.entity_persistence_policy = PersistencePolicy(
            event_store=self.entity_event_store,
            event_type=VersionedEntity.Event,
        )
        self.snapshot_persistence_policy = PersistencePolicy(
            event_store=self.snapshot_store,
            event_type=Snapshot,
        )
        snapshot_strategy = EventSourcedSnapshotStrategy(
            event_store=self.snapshot_store)
        event_player = EventPlayer(event_store=self.entity_event_store,
                                   mutator=Example._mutate,
                                   snapshot_strategy=snapshot_strategy)

        # Take a snapshot with a non-existent ID.
        unregistered_id = uuid4()
        # Check no snapshot is taken.
        self.assertIsNone(event_player.take_snapshot(unregistered_id))
        # Check no snapshot is available.
        self.assertIsNone(event_player.get_snapshot(unregistered_id))

        # Create a new entity.
        registered_example = create_new_example(a=123, b=234)

        # Take a snapshot of the new entity (no previous snapshots).
        snapshot1 = event_player.take_snapshot(registered_example.id,
                                               lt=registered_example.version)

        # Check the snapshot is pegged to the last applied version.
        self.assertEqual(snapshot1.originator_version, 0)

        # Replay from this snapshot.
        entity_from_snapshot1 = entity_from_snapshot(snapshot1)
        retrieved_example = event_player.replay_entity(
            registered_example.id,
            initial_state=entity_from_snapshot1,
            gte=entity_from_snapshot1._version)

        # Check the attributes are correct.
        self.assertEqual(retrieved_example.a, 123)

        # Remember the version now.
        version1 = retrieved_example._version
        self.assertEqual(version1, 1)

        # Change attribute value.
        retrieved_example.a = 999

        # Remember the version now.
        version2 = retrieved_example._version
        self.assertEqual(version2, 2)

        # Change attribute value.
        retrieved_example.a = 9999

        # Remember the version now.
        version3 = retrieved_example._version
        self.assertEqual(version3, 3)

        # Check the event sourced entities are correct.
        retrieved_example = event_player.replay_entity(registered_example.id)
        self.assertEqual(retrieved_example.a, 9999)

        # Take another snapshot.
        snapshot2 = event_player.take_snapshot(retrieved_example.id,
                                               lt=retrieved_example.version)

        # Replay from this snapshot.
        initial_state = entity_from_snapshot(snapshot2)
        retrieved_example = event_player.replay_entity(
            registered_example.id,
            initial_state=initial_state,
            gte=initial_state._version,
        )
        # Check the attributes are correct.
        self.assertEqual(retrieved_example.a, 9999)

        # Check we can get historical state at version1.
        retrieved_example = event_player.replay_entity(registered_example.id,
                                                       lt=version1)
        self.assertEqual(retrieved_example.a, 123)

        # Check we can get historical state at version2.
        retrieved_example = event_player.replay_entity(registered_example.id,
                                                       lt=version2)
        self.assertEqual(retrieved_example.a, 999)

        # Check we can get historical state at version3.
        retrieved_example = event_player.replay_entity(registered_example.id,
                                                       lt=version3)
        self.assertEqual(retrieved_example.a, 9999)

        # Similarly, check we can get historical state using a snapshot
        initial_state = entity_from_snapshot(snapshot1)
        retrieved_example = event_player.replay_entity(
            registered_example.id,
            initial_state=initial_state,
            gte=initial_state._version,
            lt=version2,
        )
        self.assertEqual(retrieved_example.a, 999)

        # Discard the entity.
        registered_example = event_player.replay_entity(registered_example.id)
        registered_example.discard()

        # Take snapshot of discarded entity.
        snapshot3 = event_player.take_snapshot(registered_example.id)
        self.assertIsNone(snapshot3.state)
        self.assertIsNone(entity_from_snapshot(snapshot3))
 def setUp(self):
     self.event_store = mock.Mock(spec=AbstractEventStore)
     self.persistence_policy = PersistencePolicy(
         event_store=self.event_store,
         persist_event_type=VersionedEntity.Event
     )
Esempio n. 19
0
 def setup_persistence_policy(self, persist_event_type):
     self.persistence_policy = PersistencePolicy(
         event_store=self.event_store, event_type=persist_event_type)
Esempio n. 20
0
 def construct_log_persistence_policy(self):
     if self.log_event_store:
         return PersistencePolicy(
             event_store=self.log_event_store,
             event_type=Logged,
         )
Esempio n. 21
0
 def construct_snapshot_persistence_policy(self):
     if self.snapshot_event_store:
         return PersistencePolicy(
             event_store=self.snapshot_event_store,
             event_type=Snapshot,
         )
Esempio n. 22
0
 def construct_entity_persistence_policy(self):
     if self.entity_event_store:
         return PersistencePolicy(
             event_store=self.entity_event_store,
             event_type=VersionedEntity.Event,
         )
Esempio n. 23
0
 def construct_persistence_policy(self):
     self.persistence_policy = PersistencePolicy(
         event_store=self.event_store,
         persist_event_type=self.persist_event_type)