Beispiel #1
0
def test_handling_fallback_fails(
    handler,
    on_stopped,
    on_finished,
    on_publishing_failed,
    on_published,
    on_serialization_failed,
    on_serialized,
    on_handling_failed,
    on_handled,
    on_deserialization_failed,
    on_deserialized,
    on_received,
):
    executor = Executor(handler=TestHandler())
    executor.run({"orig": 'msg'})

    def assert_callbacks():
        on_received.assert_called_with({"orig": 'msg'})
        on_deserialized.assert_called_with(
            original_message={"orig": 'msg'},
            deserialized_message={"orig": 'msg'})
        on_deserialization_failed.assert_not_called()
        handler.assert_called_with({'orig': 'msg'})
        on_handled.assert_not_called()
        on_handling_failed.assert_called_with(
            original_message={'orig': 'msg'},
            deserialized_message={'orig': 'msg'},
            error=_ERR,
        )
        on_serialized.assert_not_called()
        on_serialization_failed.assert_not_called()
        on_published.assert_not_called()
        on_publishing_failed.assert_not_called()
        on_finished.assert_called_with(original_message={"orig": 'msg'},
                                       error=_ERR)
        on_stopped.assert_not_called()

        for callback in [
                handler,
                on_stopped,
                on_finished,
                on_publishing_failed,
                on_published,
                on_serialization_failed,
                on_serialized,
                on_handling_failed,
                on_handled,
                on_deserialization_failed,
                on_deserialized,
                on_received,
        ]:
            callback.reset_mock()

    assert_callbacks()
    with pytest.raises(FetchedNoResult):
        executor.run_for_result({"orig": 'msg'})
    assert_callbacks()
Beispiel #2
0
def test_stop_on_deserialized(
    handler,
    on_stopped,
    on_finished,
    on_publishing_failed,
    on_published,
    on_serialization_failed,
    on_serialized,
    on_handling_failed,
    on_handled,
    on_deserialization_failed,
    on_deserialized,
    on_received,
):
    class TestDeser(Deserializer):
        def deserialize(self, message: Any) -> Mapping[str, Any]:
            return {'spam': 'eggs'}

    class TestSer(Serializer):
        def serialize(self, message_attributes: Mapping[str, Any]) -> Any:
            return {'i am': 'serialized'}

    des = TestDeser()
    ser = TestSer()
    executor = Executor(handler=TestHandler(),
                        deserializer=des,
                        publisher=None,
                        serializer=ser)
    assert executor.deserializer == des
    assert executor.serializer == ser
    assert executor.publisher is None
    executor.run("original message")

    def assert_callbacks():
        on_received.assert_called_with("original message")
        on_deserialized.assert_called_with(
            original_message='original message',
            deserialized_message={'spam': 'eggs'})
        on_deserialization_failed.assert_not_called()
        handler.assert_not_called()
        on_handled.assert_not_called()
        on_handling_failed.assert_not_called()
        on_serialized.assert_not_called()
        on_serialization_failed.assert_not_called()
        on_published.assert_not_called()
        on_publishing_failed.assert_not_called()
        on_finished.assert_not_called()
        on_stopped.assert_called_with(original_message="original message",
                                      reason='reason')

        for callback in [
                handler,
                on_stopped,
                on_finished,
                on_publishing_failed,
                on_published,
                on_serialization_failed,
                on_serialized,
                on_handling_failed,
                on_handled,
                on_deserialization_failed,
                on_deserialized,
                on_received,
        ]:
            callback.reset_mock()

    assert_callbacks()
    with pytest.raises(FetchedNoResult):
        executor.run_for_result("original message")
    assert_callbacks()
Beispiel #3
0
def test_handling_status_err(
    handler,
    on_stopped,
    on_finished,
    on_publishing_failed,
    on_published,
    on_serialization_failed,
    on_serialized,
    on_handling_failed,
    on_handled,
    on_deserialization_failed,
    on_deserialized,
    on_received,
):
    executor = Executor(handler=TestHandler())
    executor.run({"orig": 'msg'})

    def assert_callbacks():
        on_received.assert_called_with({"orig": 'msg'})
        on_deserialized.assert_called_with(
            original_message={"orig": 'msg'},
            deserialized_message={"orig": 'msg'})
        on_deserialization_failed.assert_not_called()
        handler.assert_called_with({'orig': 'msg'})
        on_handled.assert_called_with(
            original_message={'orig': 'msg'},
            deserialized_message={'orig': 'msg'},
            result={'error': 42},
        )
        on_handling_failed.assert_not_called()
        on_serialized.assert_called_with(
            original_message={'orig': 'msg'},
            deserialized_message={'orig': 'msg'},
            result={'error': 42},
            serialized_message={'error': 42},
        )
        on_serialization_failed.assert_not_called()
        on_published.assert_not_called()
        on_publishing_failed.assert_not_called()
        on_finished.assert_called_with(original_message={"orig": 'msg'},
                                       error=None)
        on_stopped.assert_not_called()

        for callback in [
                handler,
                on_stopped,
                on_finished,
                on_publishing_failed,
                on_published,
                on_serialization_failed,
                on_serialized,
                on_handling_failed,
                on_handled,
                on_deserialization_failed,
                on_deserialized,
                on_received,
        ]:
            callback.reset_mock()

    assert_callbacks()
    result = executor.run_for_result({"orig": 'msg'})
    assert_callbacks()
    assert result == {'error': 42}
Beispiel #4
0
def test_serialization_fallback_failure(
    handler,
    on_stopped,
    on_finished,
    on_publishing_failed,
    on_published,
    on_serialization_failed,
    on_serialized,
    on_handling_failed,
    on_handled,
    on_deserialization_failed,
    on_deserialized,
    on_received,
):
    error = KeyError('123')

    class TestDeser(Deserializer):
        def deserialize(self, message: Any) -> Mapping[str, Any]:
            raise error

    des = TestDeser()
    executor = Executor(handler=TestHandler(),
                        deserializer=des,
                        publisher=None)
    executor.run("orig")

    def assert_callbacks():
        on_received.assert_called_with("orig")
        on_deserialized.assert_not_called()
        on_deserialization_failed.assert_called_with(original_message="orig",
                                                     error=error)
        handler.assert_not_called()
        on_handled.assert_not_called()
        on_handling_failed.assert_not_called()
        on_serialized.assert_not_called()
        on_serialization_failed.assert_not_called()
        on_published.assert_not_called()
        on_publishing_failed.assert_not_called()
        on_finished.assert_called_with(original_message="orig", error=error)
        on_stopped.assert_not_called()

        for callback in [
                handler,
                on_stopped,
                on_finished,
                on_publishing_failed,
                on_published,
                on_serialization_failed,
                on_serialized,
                on_handling_failed,
                on_handled,
                on_deserialization_failed,
                on_deserialized,
                on_received,
        ]:
            callback.reset_mock()

    assert_callbacks()
    with pytest.raises(FetchedNoResult):
        executor.run_for_result("orig")
    assert_callbacks()
Beispiel #5
0
def test_executor_no_input(
    handler,
    on_stopped,
    on_finished,
    on_publishing_failed,
    on_published,
    on_serialization_failed,
    on_serialized,
    on_handling_failed,
    on_handled,
    on_deserialization_failed,
    on_deserialized,
    on_received,
):
    executor = Executor(handler=TestHandler())
    assert executor.deserializer is DUMMY_SERDE
    assert executor.serializer is DUMMY_SERDE
    assert executor.publisher is None
    executor.run()

    def assert_callbacks():
        on_received.assert_called_with(None)
        on_deserialized.assert_called_with(original_message=None,
                                           deserialized_message={})
        on_deserialization_failed.assert_not_called()
        handler.assert_called_with({})
        on_handled.assert_called_with(original_message=None,
                                      deserialized_message={},
                                      result={'result': 42})
        on_handling_failed.assert_not_called()
        on_serialized.assert_called_with(
            original_message=None,
            deserialized_message={},
            result={'result': 42},
            serialized_message={'result': 42},
        )
        on_serialization_failed.assert_not_called()
        on_published.assert_not_called()
        on_publishing_failed.assert_not_called()
        on_finished.assert_called_with(original_message=None, error=None)
        on_stopped.assert_not_called()

        for callback in [
                handler,
                on_stopped,
                on_finished,
                on_publishing_failed,
                on_published,
                on_serialization_failed,
                on_serialized,
                on_handling_failed,
                on_handled,
                on_deserialization_failed,
                on_deserialized,
                on_received,
        ]:
            callback.reset_mock()

    assert_callbacks()
    result = executor.run_for_result()
    assert_callbacks()
    assert result == {'result': 42}
Beispiel #6
0
def test_serialization_failure(
    handler,
    on_stopped,
    on_finished,
    on_publishing_failed,
    on_published,
    on_serialization_failed,
    on_serialized,
    on_handling_failed,
    on_handled,
    on_deserialization_failed,
    on_deserialized,
    on_received,
):
    error = KeyError('123')

    class TestDeser(Deserializer):
        def deserialize(self, message: Any) -> Mapping[str, Any]:
            raise error

        def build_error_result(self, message: Any,
                               error: Exception) -> Mapping[str, Any]:
            return {"SPAM": "EGGS"}

    des = TestDeser()
    executor = Executor(handler=TestHandler(),
                        deserializer=des,
                        publisher=None)
    executor.run("orig")

    def assert_callbacks():
        on_received.assert_called_with("orig")
        on_deserialized.assert_not_called()
        on_deserialization_failed.assert_called_with(original_message="orig",
                                                     error=error)
        handler.assert_not_called()
        on_handled.assert_not_called()
        on_handling_failed.assert_not_called()
        on_serialized.assert_called_with(
            original_message="orig",
            deserialized_message=None,
            result={'SPAM': 'EGGS'},
            serialized_message={'SPAM': 'EGGS'},
        )
        on_serialization_failed.assert_not_called()
        on_published.assert_not_called()
        on_publishing_failed.assert_not_called()
        on_finished.assert_called_with(original_message="orig", error=None)
        on_stopped.assert_not_called()

        for callback in [
                handler,
                on_stopped,
                on_finished,
                on_publishing_failed,
                on_published,
                on_serialization_failed,
                on_serialized,
                on_handling_failed,
                on_handled,
                on_deserialization_failed,
                on_deserialized,
                on_received,
        ]:
            callback.reset_mock()

    assert_callbacks()
    result = executor.run_for_result("orig")
    assert_callbacks()
    assert result == {'SPAM': 'EGGS'}