Пример #1
0
 def setUp(self):
     assert_event_handlers_empty()
     # Setup the repo, and a persistence subscriber.
     stored_event_repo = PythonObjectsStoredEventRepository()
     event_store = EventStore(stored_event_repo=stored_event_repo)
     self.repo = CollectionRepo(event_store=event_store)
     self.ps = PersistenceSubscriber(event_store=event_store)
Пример #2
0
    def setUp(self):
        super(LogTestCase, self).setUp()

        # Check we're starting clean, event handler-wise.
        assert_event_handlers_empty()

        # Setup the persistence subscriber.
        self.event_store = EventStore(self.stored_event_repo)
        self.persistence_subscriber = PersistenceSubscriber(event_store=self.event_store)

        self.log_repo = LogRepo(self.event_store)
Пример #3
0
    def test_with_snapshots(self):
        # Check the EventPlayer's take_snapshot() method.
        stored_event_repo = PythonObjectsStoredEventRepository()
        event_store = EventStore(stored_event_repo)
        self.ps = PersistenceSubscriber(event_store)
        event_player = EventPlayer(
            event_store=event_store,
            id_prefix='Example',
            mutate_func=Example.mutate,
            snapshot_strategy=EventSourcedSnapshotStrategy(
                event_store=event_store))

        # Check the method returns None when there are no events.
        snapshot = event_player.take_snapshot('wrong')
        self.assertIsNone(snapshot)

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

        # Take a snapshot with the entity.
        snapshot1 = event_player.take_snapshot(example.id)
        self.assertIsInstance(snapshot1, Snapshot)

        # Take another snapshot with the entity.
        snapshot2 = event_player.take_snapshot(example.id)
        # - should return the previous snapshot
        self.assertIsInstance(snapshot2, Snapshot)
        self.assertEqual(snapshot2.at_event_id, snapshot1.at_event_id)

        # Generate a domain event.
        example.beat_heart()

        # Take another snapshot with the entity.
        # - should use the previous snapshot and the heartbeat event
        snapshot3 = event_player.take_snapshot(example.id)
        self.assertNotEqual(snapshot3.at_event_id, snapshot1.at_event_id)
Пример #4
0
 def setUp(self):
     # Set up a persistence subscriber with a (mock) event store.
     self.event_store = mock.Mock(spec=AbstractEventStore)
     self.persistence_subscriber = PersistenceSubscriber(
         event_store=self.event_store)
Пример #5
0
    def test_snapshots(self):
        stored_event_repo = PythonObjectsStoredEventRepository()
        event_store = EventStore(stored_event_repo)
        self.ps = PersistenceSubscriber(event_store)
        event_player = EventPlayer(event_store=event_store,
                                   id_prefix='Example',
                                   mutate_func=Example.mutate)

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

        # Take a snapshot.
        snapshot = take_snapshot(registered_example, uuid1().hex)

        # Replay from this snapshot.
        after = snapshot.at_event_id
        initial_state = entity_from_snapshot(snapshot)
        retrieved_example = event_player.replay_events(
            registered_example.id, initial_state=initial_state, after=after)

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

        # Remember the time now.
        timecheck1 = uuid1().hex

        # Change attribute value.
        retrieved_example.a = 999

        # Check the initial state doesn't move.
        self.assertEqual(initial_state.a, 123)

        # Remember the time now.
        timecheck2 = uuid1().hex

        # Change attribute value.
        retrieved_example.a = 9999

        # Remember the time now.
        timecheck3 = uuid1().hex

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

        # Take another snapshot.
        snapshot2 = take_snapshot(retrieved_example, uuid1().hex)

        # Check we can replay from this snapshot.
        initial_state2 = entity_from_snapshot(snapshot2)
        after2 = snapshot2.domain_event_id
        retrieved_example = event_player.replay_events(
            registered_example.id, initial_state=initial_state2, after=after2)
        # Check the attributes are correct.
        self.assertEqual(retrieved_example.a, 9999)

        # Check we can get historical state at timecheck1.
        retrieved_example = event_player.replay_events(registered_example.id,
                                                       until=timecheck1)
        self.assertEqual(retrieved_example.a, 123)

        # Check we can get historical state at timecheck2.
        retrieved_example = event_player.replay_events(registered_example.id,
                                                       until=timecheck2)
        self.assertEqual(retrieved_example.a, 999)

        # Check we can get historical state at timecheck3.
        retrieved_example = event_player.replay_events(registered_example.id,
                                                       until=timecheck3)
        self.assertEqual(retrieved_example.a, 9999)

        # Similarly, check we can get historical state using a snapshot
        retrieved_example = event_player.replay_events(
            registered_example.id,
            initial_state=initial_state,
            after=after,
            until=timecheck2)
        self.assertEqual(retrieved_example.a, 999)
Пример #6
0
 def setUp(self):
     # Setup the persistence subscriber.
     self.event_store = EventStore(PythonObjectsStoredEventRepository())
     self.persistence_subscriber = PersistenceSubscriber(
         event_store=self.event_store)
Пример #7
0
 def create_persistence_subscriber(self):
     if self.persist_events and self.event_store:
         return PersistenceSubscriber(event_store=self.event_store)