Example #1
0
def test_entity_inequality(aggregate):
    identity_a = Identity.create()
    identity_b = Identity.create()

    class Entity(DomainEntity):
        def mutate(self, event):
            pass

    a = Entity(identity=identity_a, aggregate=aggregate)
    b = Entity(identity=identity_b, aggregate=aggregate)

    assert a != b
    assert a != identity_b
    assert a != None
    assert a != {}
Example #2
0
def test_get(event_mapper, record_manager, event_store):
    event_mapper.register(DomainEvent)

    identity = Identity.create()

    class Aggregate(AggregateRoot):
        def mutate(self, event):
            self.proof_of_work(event)

        def proof_of_work(self, event):
            pass

    Aggregate.proof_of_work = mock.Mock()

    with record_manager.session() as session:
        session.append(
            EventRecord(stream_id=Aggregate.create_stream_id(identity),
                        number=1,
                        topic=DomainEvent.__name__,
                        version=1,
                        timestamp=0.0,
                        trace_id='tid',
                        message='domain_event',
                        context='ctx',
                        payload={}))
        session.commit()

    EventSourcedRpository = make_adapter(Aggregate, Identity)
    rep = EventSourcedRpository(event_store,
                                snapshot_configuration=SnapshotConfiguration(
                                    enabled=True,
                                    when_store_event=DomainEvent))

    aggregate = rep.get(identity)
    aggregate.proof_of_work.assert_called()
Example #3
0
def test_save(record_manager, event_store):
    class Aggregate(AggregateRoot):
        def proof_of_work(self):
            self.__apply__(
                self.__stamp__(DomainEvent)(__trace_id__='tid',
                                            __context__='ctx',
                                            __version__=1))

        def mutate(self, event):
            pass

    identity = Identity.create()
    aggregate = Aggregate(identity)
    aggregate.proof_of_work()

    EventSourcedRpository = make_adapter(Aggregate, Identity)
    rep = EventSourcedRpository(event_store)
    rep.save(aggregate)

    records = record_manager.get_records(Aggregate.create_stream_id(identity))
    assert len(list(records)) == 1
Example #4
0
def test_save_snapshot_on_store_event(record_manager, event_store):
    class Aggregate(AggregateRoot):
        def proof_of_work(self):
            self.__apply__(
                self.__stamp__(DomainEvent)(__trace_id__='tid',
                                            __context__='ctx',
                                            __version__=1))

        def mutate(self, event):
            pass

        def take_snapshot(self) -> DomainEvent:
            return DomainEvent(__stream_id__=self.create_snapshot_stream_id(
                self.__identity__),
                               __number__=self.__version__ + 1,
                               __timestamp__=datetime.datetime.timestamp(
                                   datetime.datetime.now()),
                               __trace_id__='tid',
                               __context__='ctx',
                               __version__=1)

    identity = Identity.create()
    aggregate = Aggregate(identity)
    aggregate.proof_of_work()

    EventSourcedRpository = make_adapter(Aggregate, Identity)
    rep = EventSourcedRpository(event_store,
                                snapshot_configuration=SnapshotConfiguration(
                                    enabled=True,
                                    when_store_event=DomainEvent))
    rep.save(aggregate)

    records = record_manager.get_records(Aggregate.create_stream_id(identity))
    snapshots = record_manager.get_records(
        Aggregate.create_snapshot_stream_id(identity))
    assert len(list(records)) == 1
    assert len(list(snapshots)) == 1
Example #5
0
def aggregate():
    class Aggregate(AggregateRoot):
        def mutate(self, event: DomainEvent) -> None:
            pass

    return Aggregate(Identity.create())
Example #6
0
def identity():
    return Identity.create()
Example #7
0
def aggregate(aggregate_id):
    return Aggregate(Identity.from_text(aggregate_id))