def test_unknown_codec_raises(): class UnknownType: pass t = Transcoder() with pytest.raises(MissingCodecError): t.encode(None, UnknownType)
def test_serialize_event(): class Attribute(ValueObject): some_property: str class Event(DomainEvent): __stream_id__: str __number__: int __version__: int __timestamp__: float __trace_id__: str __context__: str some_property: Attribute m = Event(__stream_id__='sid', __number__=1, __version__=1, __timestamp__=0.0, __trace_id__='tid', __context__='some_context', some_property=Attribute(some_property='x')) t = Transcoder() r = t.serialize(m) assert isinstance(r, EventRecord) assert r.stream_id == m.__stream_id__ assert r.number == m.__number__ assert r.version == m.__version__ assert r.timestamp == m.__timestamp__ assert r.trace_id == m.__trace_id__ assert r.context == m.__context__ assert r.payload['some_property']['some_property'] == 'x'
def test_deserialize_command(): class Command(ApplicationCommand): class Struct(ApplicationCommand.Struct): some_property: str __trace_id__: str __version__: int = 1 __timestamp__: float some_property: Struct r = CommandRecord(trace_id='tid', topic='Command', version=1, timestamp=0.0, message=MessageType.APPLICATION_COMMAND.value, payload={'some_property': { 'some_property': 'x' }}) t = Transcoder() m = t.deserialize(r, Command) assert isinstance(m, Command) assert m.__trace_id__ == r.trace_id assert m.__version__ == r.version assert m.__timestamp__ == r.timestamp assert m.some_property.some_property == 'x'
def test_serialize_integration(): class Integration(IntegrationEvent): __trace_id__: str __context__: str __resolve__: str __error__: typing.Optional[str] __version__: int __timestamp__: float some_property: str m = Integration(__trace_id__='tid', __context__='some_context', __resolve__='success', __error__=None, __version__=1, __timestamp__=0.0, some_property='x') t = Transcoder() r = t.serialize(m) assert isinstance(r, IntegrationRecord) assert r.trace_id == m.__trace_id__ assert r.context == m.__context__ assert r.resolve == m.__resolve__ assert r.error == m.__error__ assert r.version == m.__version__ assert r.timestamp == m.__timestamp__ assert r.payload['some_property'] == 'x'
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 test_decode_optional(): t = Transcoder() r = t.decode('text', typing.Optional[str]) assert r == 'text' r = t.decode(None, typing.Optional[str]) assert r == None
def test_stepfunction_scheduler_publish(stepfunctions, state_machine_arn, region_name): integration = ScheduleIntegartionEvent( __publish_at_field__='publish_at', __timestamp__=0.0, __trace_id__='tid', __context__='ctx', __version__=1, publish_at=datetime.datetime.now().isoformat()) mapper = Mapper(transcoder=Transcoder()) mapper.register(ScheduleIntegartionEvent) pub = AwsStepFunctionSchedulerPublisher(state_machine_arn, mapper, region_name=region_name) pub.publish(integration) executions = stepfunctions.list_executions( stateMachineArn=state_machine_arn)['executions'] assert len(executions) == 1 execution = stepfunctions.describe_execution( executionArn=executions[0]['executionArn']) input = json.loads(execution['input']) assert tuple(input.keys()) == ('publish_at', 'payload')
def test_eventbridge_publish(bus_name): command = ApplicationCommand(__timestamp__=0.0, __trace_id__='tid', __version__=1) mapper = Mapper(transcoder=Transcoder()) mapper.register(ApplicationCommand) pub = AwsEventBridgePublisher(bus_name, 'some_context', mapper) pub.publish(command)
def test_serialize_command(): class Command(ApplicationCommand): class Struct(ApplicationCommand.Struct): some_property: str __trace_id__: str __version__: int = 1 __timestamp__: float some_property: Struct m = Command(__trace_id__='tid', __version__=1, __timestamp__=0.0, some_property=Command.Struct(some_property='x')) t = Transcoder() r = t.serialize(m) assert isinstance(r, CommandRecord) assert r.trace_id == m.__trace_id__ assert r.version == m.__version__ assert r.timestamp == m.__timestamp__ assert r.payload['some_property'] == {'some_property': 'x'}
def test_sns_publish(topic_arn, region_name): command = ApplicationCommand(__timestamp__=0.0, __trace_id__='tid', __version__=1) mapper = Mapper(transcoder=Transcoder()) mapper.register(ApplicationCommand) pub = AwsSimpleNotificationServicePublisher(topic_arn, 'default', mapper, region_name=region_name) pub.publish(command)
def test_mapper_deserializer_raises_if_not_registered(): record = CommandRecord( trace_id='tid', topic='ApplicationCommand', version=1, timestamp=0.0, message=MessageType.APPLICATION_COMMAND.value, payload={ } ) mapper = Mapper(transcoder=Transcoder()) with pytest.raises(MessageTypeNotFoundError): mapper.deserialize(record)
def test_deserialize_integration(): class Integration(IntegrationEvent): some_property: str r = IntegrationRecord(trace_id='tid', context='some_context', topic='Integration', resolve='success', error=None, version=1, timestamp=0.0, message=MessageType.INTEGRATION_EVENT.value, payload={'some_property': 'x'}) t = Transcoder() m = t.deserialize(r, Integration) assert isinstance(m, Integration) assert m.__trace_id__ == r.trace_id assert m.__context__ == r.context assert m.__resolve__ == r.resolve assert m.__error__ == r.error assert m.__version__ == r.version assert m.__timestamp__ == r.timestamp assert m.some_property == 'x'
def test_sqs_publish(sqs, queue_url, queue_name, region_name): command = ApplicationCommand( __timestamp__=0.0, __trace_id__='tid', __version__=1 ) mapper = Mapper(transcoder=Transcoder()) mapper.register(ApplicationCommand) pub = AwsSimpleQueueServicePublisher(queue_name, mapper, region_name=region_name) pub.publish(command) sqs_messages = sqs.receive_message(QueueUrl=queue_url) assert len(sqs_messages['Messages']) == 1
def test_mapper_deserialize(): record = CommandRecord( trace_id='tid', topic='ApplicationCommand', version=1, timestamp=0.0, message=MessageType.APPLICATION_COMMAND.value, payload={ } ) mapper = Mapper(transcoder=Transcoder()) mapper.register(ApplicationCommand) message = mapper.deserialize(record) assert isinstance(message, ApplicationCommand) assert message.__timestamp__ == 0.0 assert message.__version__ == 1
def test_mapper_serialize_command(): command = ApplicationCommand( __timestamp__=0.0, __trace_id__='tid', __version__ = 1 ) mapper = Mapper(transcoder=Transcoder()) record = mapper.serialize(command) assert isinstance(record, CommandRecord) assert record.trace_id == 'tid' assert record.topic == 'ApplicationCommand' assert record.version == 1 assert record.timestamp == 0.0 assert record.message == MessageType.APPLICATION_COMMAND.value assert record.payload == {}
def test_decode_primitive(): t = Transcoder() m = t.encode('x', str) assert m == 'x' m = t.encode(1, int) assert m == 1 m = t.encode(1.0, int) assert m == 1 m = t.encode(1.1, float) assert m == 1.1 m = t.encode(1, float) assert m == 1.0 m = t.encode(True, bool) assert m == True
def test_add_new_codec(): NewType = type('NewType', (), {}) class NewTypeCodec(ICodec): def can_handle(self, field_type: typing.Type) -> bool: return field_type is NewType def encode(self, obj: typing.Any, field_type: typing.Type) -> typing.Any: return True def decode(self, data: dict, field_type: typing.Type) -> typing.Any: return True t = Transcoder() t.add_codec(NewTypeCodec()) assert t.encode(NewType(), NewType) assert t.decode({}, NewType)
def test_encode_single_type_sequence_builtin(): t = Transcoder() r = t.encode(tuple(['x']), tuple[str, ...]) assert r == [ 'x', ]
def event_mapper(): return Mapper(transcoder=Transcoder())
def test_decode_none(): t = Transcoder() assert t.decode(None, type(None)) is None
def test_decode_dict(): t = Transcoder() r = t.decode({'0': 'True'}, typing.Dict[int, bool]) assert r == {0: True}
def mapper(): return Mapper(transcoder=Transcoder())
def test_decode_single_type_sequence(): t = Transcoder() m = t.decode(tuple(['x']), tuple[str, ...]) assert m == ('x', )
def event_mapper(): mapper = Mapper(transcoder=Transcoder()) mapper.register(DomainEvent) return mapper
def mapper(): m = Mapper(transcoder=Transcoder()) m.register(ApplicationCommand) m.register(SuccessIntegrationEvent) m.register(FailureIntegrationEvent) return m