How to use the eventsourcing.domain.model.example.Example function in eventsourcing

To help you get started, we’ve selected a few eventsourcing examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github johnbywater / eventsourcing / eventsourcingtests / test_events.py View on Github external
def test_event_attributes(self):
        event = Example.Created(entity_id='entity1', a=1, b=2)

        # Check constructor keyword args lead to read-only attributes.
        self.assertEqual(1, event.a)
        self.assertEqual(2, event.b)
        self.assertRaises(AttributeError, getattr, event, 'c')
        self.assertRaises(AttributeError, setattr, event, 'c', 3)

        # Check domain event has auto-generated timestamp.
        self.assertIsInstance(event.timestamp, float)

        # Check timestamp value can be given to domain events.
        self.assertEqual(3, Example.Created(entity_id='entity1', a=1, b=2, domain_event_id=3).domain_event_id)
        domain_event_id = uuid1().hex
        self.assertEqual(timestamp_from_uuid(domain_event_id), Example.Created(entity_id='entity1', a=1, b=2, domain_event_id=domain_event_id).timestamp)
github johnbywater / eventsourcing / eventsourcingtests / test_entity.py View on Github external
entity2._validate_originator,
                          DomainEvent(entity_id=entity2.id, entity_version=0)
                          )
        # Should validate event with correct entity ID and version.
        entity2._validate_originator(
            DomainEvent(entity_id=entity2.id, entity_version=entity2.version)
        )

        # Check an entity can be reregistered with the same ID.
        replacement_event = Example.Created(entity_id=entity1.id, a=11, b=12)
        # replacement = Example.mutate(event=replacement_event)
        publish(event=replacement_event)

        # Check the replacement entity can be retrieved from the example repository.
        replacement = repo[entity1.id]
        assert isinstance(replacement, Example)
        self.assertEqual(replacement.a, 11)
        self.assertEqual(replacement.b, 12)
github johnbywater / eventsourcing / eventsourcingtests / test_event_player.py View on Github external
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)
github johnbywater / eventsourcing / eventsourcingtests / test_stored_events.py View on Github external
def test_resolve_event_topic(self):
        example_topic = 'eventsourcing.domain.model.example#Example.Created'
        actual = resolve_domain_topic(example_topic)
        self.assertEqual(Example.Created, actual)
        example_topic = 'xxxxxxxxxxxxx#Example.Event'
        self.assertRaises(TopicResolutionError, resolve_domain_topic, example_topic)
        example_topic = 'eventsourcing.domain.model.example#Xxxxxxxx.Xxxxxxxx'
        self.assertRaises(TopicResolutionError, resolve_domain_topic, example_topic)
github johnbywater / eventsourcing / eventsourcingtests / test_performance.py View on Github external
last_n_stored_events = repo.get_most_recent_events(stored_entity_id, limit=n)
                time_last_n = (utc_now() - start_last_n) / repetitions

                num_retrieved_events = len(list(last_n_stored_events))
                events_per_second = num_retrieved_events / time_last_n
                print(("Time to get last {:>"+str(i+1)+"} events after {} events: {:.6f}s ({:.0f} events/s)"
                      "").format(n, num_beats + 1, time_last_n, events_per_second))

            for j in range(0, i+1):
                last_n(10**j)

            # Get the entity by replaying all events (which it must since there isn't a snapshot).
            start_replay = utc_now()
            for _ in six.moves.range(repetitions):
                example = self.app.example_repo[example.id]
                assert isinstance(example, Example)
                heartbeats = example.count_heartbeats()
                assert heartbeats == num_beats, (heartbeats, num_beats)

            time_replaying = (utc_now() - start_replay) / repetitions
            print("Time to replay {} beats: {:.2f}s ({:.0f} beats/s, {:.6f}s each)"
                  "".format(num_beats, time_replaying, num_beats / time_replaying, time_replaying / num_beats))

            # Take snapshot, and beat heart a few more times.
            self.app.example_repo.event_player.take_snapshot(example.id, until=uuid1().hex)

            extra_beats = 4
            for _ in six.moves.range(extra_beats):
                example.beat_heart()
            num_beats += extra_beats

            # Get the entity using snapshot and replaying events since the snapshot.
github johnbywater / eventsourcing / eventsourcingtests / example_application_testcase.py View on Github external
self.assertIsInstance(self.app.stored_event_repo, StoredEventRepository)

        # Check there's an event store.
        self.assertIsInstance(self.app.event_store, EventStore)
        self.assertEqual(self.app.event_store.stored_event_repo, self.app.stored_event_repo)

        # Check there's a persistence subscriber.
        self.assertIsInstance(self.app.persistence_subscriber, PersistenceSubscriber)
        self.assertEqual(self.app.persistence_subscriber.event_store, self.app.event_store)

        # Check there's an example repository.
        self.assertIsInstance(self.app.example_repo, ExampleRepo)

        # Register a new example.
        example1 = self.app.register_new_example(a=10, b=20)
        self.assertIsInstance(example1, Example)

        # Check the example is available in the repo.
        entity1 = self.app.example_repo[example1.id]
        self.assertEqual(10, entity1.a)
        self.assertEqual(20, entity1.b)
        self.assertEqual(example1, entity1)

        # Change attribute values.
        entity1.a = 100

        # Check the new value is available in the repo.
        entity1 = self.app.example_repo[example1.id]
        self.assertEqual(100, entity1.a)
github johnbywater / eventsourcing / eventsourcingtests / test_event_player.py View on Github external
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)
github johnbywater / eventsourcing / eventsourcing / domain / model / example.py View on Github external
def register_new_example(a, b):
    """
    Factory method for example entities.

    :rtype: Example
    """
    entity_id = uuid.uuid4().hex
    event = Example.Created(entity_id=entity_id, a=a, b=b)
    entity = Example.mutate(event=event)
    publish(event=event)
    return entity
github johnbywater / eventsourcing / eventsourcing / domain / model / example.py View on Github external
def register_new_example(a, b):
    """
    Factory method for example entities.

    :rtype: Example
    """
    entity_id = uuid.uuid4().hex
    event = Example.Created(entity_id=entity_id, a=a, b=b)
    entity = Example.mutate(event=event)
    publish(event=event)
    return entity
github johnbywater / eventsourcing / eventsourcing / domain / model / example.py View on Github external
def heartbeat_mutator(event, self):
    self._validate_originator(event)
    assert isinstance(self, Example), self
    self._count_heartbeats += 1
    self._increment_version()
    return self