Exemple #1
0
 def setup_repository(self, **kwargs):
     # Setup repository with a snapshot strategy.
     self.snapshot_strategy = EventSourcedSnapshotStrategy(
         event_store=self.snapshot_store)
     super(SnapshottingApplication,
           self).setup_repository(snapshot_strategy=self.snapshot_strategy,
                                  **kwargs)
 def construct_repository(self, **kwargs: Any) -> None:
     # Setup repository with a snapshot strategy.
     assert self.snapshot_store
     self.snapshot_strategy = EventSourcedSnapshotStrategy(
         snapshot_store=self.snapshot_store)
     super(SnapshottingApplication, self).construct_repository(
         snapshot_strategy=self.snapshot_strategy, **kwargs)
Exemple #3
0
 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)
Exemple #4
0
 def __init__(self, **kwargs):
     super(ExampleApplication, self).__init__(**kwargs)
     self.snapshot_strategy = EventSourcedSnapshotStrategy(
         event_store=self.timestamp_entity_event_store, )
     self.example_repo = ExampleRepository(
         event_store=self.version_entity_event_store,
         snapshot_strategy=self.snapshot_strategy,
     )
Exemple #5
0
 def __init__(self, **kwargs):
     super(KanbanApplication, self).__init__(**kwargs)
     self.snapshot_strategy = None
     if self.snapshot_event_store:
         self.snapshot_strategy = EventSourcedSnapshotStrategy(
             event_store=self.snapshot_event_store)
     assert self.entity_event_store is not None
     self.user_repository = UserRepository(
         event_store=self.entity_event_store,
         snapshot_strategy=self.snapshot_strategy,
     )
 def __init__(self, **kwargs):
     super(ExampleApplication, self).__init__(**kwargs)
     self.snapshot_strategy = None
     if self.snapshot_event_store:
         self.snapshot_strategy = EventSourcedSnapshotStrategy(
             snapshot_store=self.snapshot_event_store, )
     assert self.entity_event_store is not None
     self.example_repository = ExampleRepository(
         event_store=self.entity_event_store,
         snapshot_strategy=self.snapshot_strategy,
     )
Exemple #7
0
    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)
    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 test_snapshots(self):
        self.policy = CombinedPersistencePolicy(
            versioned_entity_event_store=self.version_entity_event_store,
            timestamped_entity_event_store=self.timestamp_entity_event_store,

        )
        event_player = EventPlayer(
            event_store=self.version_entity_event_store,
            mutator=Example.mutate,
            snapshot_strategy=EventSourcedSnapshotStrategy(
                event_store=self.timestamp_entity_event_store
            )
        )

        # Take a snapshot with a non-existent ID.
        self.assertIsNone(event_player.take_snapshot(uuid4()))

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

        # Take a snapshot.
        snapshot1 = event_player.take_snapshot(registered_example.id)

        # Replay from this snapshot.
        initial_state = entity_from_snapshot(snapshot1)
        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, 123)

        # Remember the version now.
        version1 = retrieved_example._version

        # Change attribute value.
        retrieved_example.a = 999

        # Remember the version now.
        version2 = retrieved_example._version

        # Change attribute value.
        retrieved_example.a = 9999

        # Remember the version now.
        version3 = retrieved_example._version

        # 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)

        # Check we can 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)