示例#1
0
def test_unknown_codec_raises():
    class UnknownType:
        pass

    t = Transcoder()
    with pytest.raises(MissingCodecError):
        t.encode(None, UnknownType)
示例#2
0
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'
示例#3
0
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'
示例#4
0
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'
示例#5
0
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'
示例#6
0
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
示例#7
0
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')
示例#8
0
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)
示例#9
0
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'}
示例#10
0
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)
示例#11
0
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)
示例#12
0
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'
示例#13
0
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
示例#14
0
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
示例#15
0
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 == {}
示例#16
0
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
示例#17
0
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)
示例#18
0
def test_encode_single_type_sequence_builtin():
    t = Transcoder()
    r = t.encode(tuple(['x']), tuple[str, ...])
    assert r == [
        'x',
    ]
示例#19
0
def event_mapper():
    return Mapper(transcoder=Transcoder())
示例#20
0
def test_decode_none():
    t = Transcoder()
    assert t.decode(None, type(None)) is None
示例#21
0
def test_decode_dict():
    t = Transcoder()

    r = t.decode({'0': 'True'}, typing.Dict[int, bool])
    assert r == {0: True}
示例#22
0
def mapper():
    return Mapper(transcoder=Transcoder())
示例#23
0
def test_decode_single_type_sequence():
    t = Transcoder()
    m = t.decode(tuple(['x']), tuple[str, ...])
    assert m == ('x', )
示例#24
0
def event_mapper():
    mapper = Mapper(transcoder=Transcoder())
    mapper.register(DomainEvent)
    return mapper
示例#25
0
def mapper():
    m = Mapper(transcoder=Transcoder())
    m.register(ApplicationCommand)
    m.register(SuccessIntegrationEvent)
    m.register(FailureIntegrationEvent)
    return m