def test_get_with_snapshot_only_newer_events(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.append( EventRecord( stream_id=Aggregate.create_snapshot_stream_id(identity), number=1, topic=DomainEvent.__name__, version=1, timestamp=0.0, trace_id='tid', message='domain_event', context='ctx', payload={})) session.commit() aggregate = Aggregate(identity) EventSourcedRpository = make_adapter(Aggregate, Identity) rep = EventSourcedRpository( event_store, snapshot_configuration=SnapshotConfiguration(enabled=True)) aggregate = rep.get(identity) assert len(aggregate.proof_of_work.mock_calls) == 1
def test_deserialize_event(): class Attribute(ValueObject): some_property: str class Event(DomainEvent): some_property: Attribute r = EventRecord(stream_id='sid', number=1, topic='Event', version=1, timestamp=0.0, trace_id='tid', message=MessageType.DOMAIN_EVENT.value, context='some_context', payload={'some_property': { 'some_property': 'x' }}) t = Transcoder() m = t.deserialize(r, Event) assert isinstance(m, Event) assert m.__stream_id__ == r.stream_id assert m.__number__ == r.number assert m.__version__ == r.version assert m.__timestamp__ == r.timestamp assert m.__trace_id__ == r.trace_id assert m.__context__ == r.context assert m.some_property.some_property == 'x'
def event_record(stream_id): return EventRecord(stream_id=stream_id, number=0, topic='some-topic', version=1, timestamp=datetime.datetime.now(), trace_id=uuid.uuid4(), message='event', context='some-context', payload={})
def deserialize(cls, dct: dict) -> EventRecord: event_record = EventRecord( stream_id=deserialize(dct["stream_id"]), number=deserialize(dct["number"]), topic=deserialize(dct["topic"]), version=deserialize(dct["version"]), timestamp=deserialize(dct["timestamp"]), trace_id=deserialize(dct["trace_id"]), message=deserialize(dct["message"]), context=deserialize(dct["context"]), payload=deserialize(dct["payload"]), ) return event_record
def event_record(stream_id): return EventRecord(stream_id=stream_id, number=0, topic='some-topic', version=1, timestamp=datetime.datetime.timestamp( datetime.datetime.now()), trace_id=str(uuid.uuid4()), message='event', context='some-context', payload={'some_property': { 'some_other_property': 'x' }})
def record_fromdict( dct: dict, ) -> typing.Union[CommandRecord, IntegrationRecord, EventRecord]: message = dct["message"] if message == MessageType.APPLICATION_COMMAND.value: return CommandRecord(**dct) if message == MessageType.INTEGRATION_EVENT.value: return IntegrationRecord(**dct) if message == MessageType.DOMAIN_EVENT.value: return EventRecord(**dct) raise ValueError("dct[message] should be one of command, " "integration_event or domain_event")
def encode(self, obj: typing.Any, field_type: typing.Type) -> typing.Any: dct = self._encode(obj, field_type) msg: DomainEvent = obj if msg.__trace_id__ is None: raise TypeError("__trace_id__ should not be None") if msg.__context__ is None: raise TypeError("__context__ should not be None") return EventRecord( stream_id=msg.__stream_id__, number=msg.__number__, topic=field_type.__name__, version=msg.__version__, timestamp=msg.__timestamp__, trace_id=msg.__trace_id__, message=msg.__message__, context=msg.__context__, payload=dct["payload"], )