Exemplo n.º 1
0
    def test_to_document_and_to_aggregate() -> None:
        class _Event(Event):
            @dataclass
            class Attributes:
                foo: str

            attributes: Attributes

        class _EventMapper(EventMapper):
            event_type = _Event
            service_name = '_service_name'
            event_name = '_name'

            def decode(self, data: Dict[str, Any]) -> Event:
                return _Event(_Event.Attributes(foo=data['foo']))

            @staticmethod
            def map_attributes(msg: _Event.Attributes) -> Dict[str, Any]:
                return {'foo': msg.foo}

        evt = _Event(_Event.Attributes(foo=Id.generate().value()))

        sut = MongoDBEventPublisherMapper([_EventMapper()])

        doc = sut.to_document(evt)

        assert Id.validate(doc['id'])
        assert doc['name'] == '_service_name._name'
        assert doc['type'] == 'event'
        assert doc['payload'] == {'foo': evt.attributes.foo}
        assert (datetime.utcnow() - doc['occurred_on']).total_seconds() < 1

        agg: _Event = sut.to_aggregate(doc)

        assert agg.attributes.foo == evt.attributes.foo
Exemplo n.º 2
0
def test_event_and_event_mapper() -> None:
    class _EventTest(Event):
        @dataclass
        class Attributes:
            __slots__ = ('foo', )
            foo: str

        attributes: Attributes

    event = _EventTest(attributes=_EventTest.Attributes(foo='test'))

    assert Id.validate(event.meta.id)
    assert event.meta.type == 'event'
    assert event.attributes.foo == 'test'

    class _EventTestEventMapper(EventMapper):
        event_type = _EventTest
        service_name = 'test_service_name'
        event_name = 'test_name'

    event_mapper = _EventTestEventMapper()

    assert event_mapper.belongs_to(event)
    assert event_mapper.service_name == 'test_service_name'
    assert event_mapper.event_name == 'test_name'

    event_encoded = event_mapper.encode(event)

    assert event_encoded['attributes'] == asdict(event.attributes)
    event_decoded = event_mapper.decode(event_encoded)

    assert event_decoded.meta == event.meta
    assert event_decoded.attributes == event.attributes
Exemplo n.º 3
0
def test_id_is_not_valid() -> None:
    assert not Id.validate('0')
Exemplo n.º 4
0
def test_id_generated_is_valid() -> None:
    id_ = Id.generate()
    assert Id.validate(id_.value())