Пример #1
0
    def test_get_entity_events(self):
        repo = PythonObjectsStoredEventRepository()
        event_store = EventStore(stored_event_repo=repo)

        # Check there are zero stored events in the repo.
        entity_events = event_store.get_entity_events(stored_entity_id='Example::entity1')
        entity_events = list(entity_events)
        self.assertEqual(0, len(entity_events))

        # Store a domain event.
        event1 = Example.Created(entity_id='entity1', a=1, b=2)
        event_store.append(event1)

        # Check there is one event in the event store.
        entity_events = event_store.get_entity_events(stored_entity_id='Example::entity1')
        entity_events = list(entity_events)
        self.assertEqual(1, len(entity_events))

        # Store another domain event.
        event1 = Example.AttributeChanged(entity_id='entity1', a=1, b=2, entity_version=1)
        event_store.append(event1)

        # Check there are two events in the event store.
        entity_events = event_store.get_entity_events(stored_entity_id='Example::entity1')
        entity_events = list(entity_events)
        self.assertEqual(2, len(entity_events))

        # Check there are two events in the event store.
        entity_events = event_store.get_entity_events(stored_entity_id='Example::entity1')
        entity_events = list(entity_events)
        self.assertEqual(2, len(entity_events))
    def test_get_entity(self):
        # Setup an event store, using Python objects.
        event_store = EventStore(stored_event_repo=PythonObjectsStoredEventRepository())

        # Store Instance events.
        event1 = Instance.Created(entity_id='entity1', atmo_id=27216, name='Ubuntu 14.04.2 XFCE Base', username='******')
        event_store.append(event1)
        event2 = Instance.Created(entity_id='entity2', atmo_id=27217, name='Ubuntu 15.04.2 XFCE Base', username='******')
        event_store.append(event2)
        event3 = Instance.Created(entity_id='entity3', atmo_id=27218, name='Ubuntu 16.04.2 XFCE Base', username='******')
        event_store.append(event3)
        event4 = Instance.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 'Instance'.
        event_player = EventPlayer(event_store=event_store, id_prefix='Instance',
                                   mutate_func=Instance.mutate)

        # The the reconstituted entity has correct attribute values.
        self.assertEqual('entity1', event_player.replay_events('entity1').id)
        self.assertEqual(27216, event_player.replay_events('entity1').atmo_id)
        self.assertEqual(27217, event_player.replay_events('entity2').atmo_id)
        self.assertEqual(None, event_player.replay_events('entity3'))

        # Check entity3 raises KeyError.
        self.assertEqual(event_player.replay_events('entity3'), None)

        # Check it works for "short" entities (should be faster, but the main thing is that it still works).
        # - just use a trivial mutate that always instantiates the 'Instance'.
        # TODO: Fix this. This is broken.
        # It does not throw an error, even though `a` is not an attribute off `Instance`.
        # See: eventsourcing/domain/model/entity.py:138
        event5 = Instance.AttributeChanged(entity_id='entity1', entity_version=1, name='a', value=10)
        event_store.append(event5)

        event_player = EventPlayer(event_store=event_store, id_prefix='Instance',
                                   mutate_func=Instance.mutate)

        self.assertEqual(10, event_player.replay_events('entity1').a)

        event_player = EventPlayer(
            event_store=event_store,
            id_prefix='Instance',
            mutate_func=Instance.mutate,
            is_short=True,
        )
        self.assertEqual(10, event_player.replay_events('entity1').a)
    def test_get_entity(self):
        # Setup an event store, using Python objects.
        event_store = EventStore(stored_event_repo=PythonObjectsStoredEventRepository())

        # Store Allocation Source events.
        event1 = AllocationSource.Created(entity_id='entity1', a=1, b=2)
        event_store.append(event1)
        event2 = AllocationSource.Created(entity_id='entity2', a=2, b=4)
        event_store.append(event2)
        event3 = AllocationSource.Created(entity_id='entity3', a=3, b=6)
        event_store.append(event3)
        event4 = AllocationSource.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 'AllocationSource'.
        event_player = EventPlayer(event_store=event_store, id_prefix='AllocationSource',
                                   mutate_func=AllocationSource.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)

        # Check it works for "short" entities (should be faster, but the main thing is that it still works).
        # - just use a trivial mutate that always instantiates the 'AllocationSource'.
        event5 = AllocationSource.AttributeChanged(entity_id='entity1', entity_version=1, name='a', value=10)
        event_store.append(event5)

        event_player = EventPlayer(event_store=event_store, id_prefix='AllocationSource',
                                   mutate_func=AllocationSource.mutate)

        self.assertEqual(10, event_player.replay_events('entity1').a)

        event_player = EventPlayer(
            event_store=event_store,
            id_prefix='AllocationSource',
            mutate_func=AllocationSource.mutate,
            is_short=True,
        )
        self.assertEqual(10, event_player.replay_events('entity1').a)
Пример #4
0
    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)

        # Check it works for "short" entities (should be faster, but the main thing is that it still works).
        # - just use a trivial mutate that always instantiates the 'Example'.
        event5 = Example.AttributeChanged(entity_id='entity1', entity_version=1, name='a', value=10)
        event_store.append(event5)

        event_player = EventPlayer(event_store=event_store, id_prefix='Example', mutate_func=Example.mutate)
        self.assertEqual(10, event_player.replay_events('entity1').a)

        event_player = EventPlayer(
            event_store=event_store,
            id_prefix='Example',
            mutate_func=Example.mutate,
            is_short=True,
        )
        self.assertEqual(10, event_player.replay_events('entity1').a)
    def test(self):
        # Setup an event store.
        stored_event_repo = InMemoryStoredEventRepository()
        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 = ExampleRepository(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)
Пример #6
0
    def test(self):
        stored_event_repo = InMemoryStoredEventRepository()
        event_store = EventStore(stored_event_repo)
        # Store example events.
        event1 = Example.Created(entity_id='entity1', timestamp=3, a=1, b=2)
        event2 = Example.Created(entity_id='entity2', timestamp=4, a=2, b=4)
        event3 = Example.Created(entity_id='entity3', timestamp=5, a=3, b=6)
        event4 = Example.Discarded(entity_id='entity3', timestamp=6, entity_version=1)
        event_store.append(event1)
        event_store.append(event2)
        event_store.append(event3)
        event_store.append(event4)
        # Check the event sourced entities are correct.
        # - just use a trivial mutator that always instantiates the 'Example'.
        event_player = EventPlayer(event_store, Example.mutator, id_prefix='Example')
        self.assertEqual('entity1', event_player['entity1'].id)
        self.assertEqual(1, event_player['entity1'].a)
        self.assertEqual(2, event_player['entity2'].a)

        # Check entity3 is not found.
        self.assertRaises(KeyError, event_player.__getitem__, 'entity3')
    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 = ExampleRepository(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)
Пример #8
0
    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)
Пример #9
0
    def test_get_entity_events(self):
        repo = PythonObjectsStoredEventRepository()
        event_store = EventStore(stored_event_repo=repo)

        # Check there are zero stored events in the repo.
        entity_events = repo.get_entity_events(stored_entity_id='Example::entity1')
        self.assertEqual(0, len(entity_events))

        # Store a domain event.
        event1 = Example.Created(entity_id='entity1', a=1, b=2)
        event_store.append(event1)

        # Check there is one stored event in the repo.
        entity_events = repo.get_entity_events(stored_entity_id='Example::entity1')
        self.assertEqual(1, len(entity_events))

        # Store another domain event.
        event1 = Example.Created(entity_id='entity1', a=1, b=2)
        event_store.append(event1)

        # Check there are two stored events in the repo.
        entity_events = repo.get_entity_events(stored_entity_id='Example::entity1')
        self.assertEqual(2, len(entity_events))