Ejemplo n.º 1
0
    def test_ready_when_container_and_serializer_set(self):
        # Given
        container = FakeMediatrContainer()
        mediatr = Mediatr()

        # When
        mediatr.ready(container=container, serializer={})

        # Then
        assert mediatr.is_ready is True
        assert not isinstance(SerializerFactory.get_serializer(),
                              BaseSerializer)
        assert SerializerFactory.get_serializer() == {}
        assert len(container.get_pipelines()) == 1
Ejemplo n.º 2
0
    def test_handle_when_req_result_is_not_in_cache(self):
        # Given
        next_response = TestResponse(success=True)

        async def next_handle(req):
            return next_response

        mock_test_pipeline = MagicMock()
        mock_test_pipeline.handle = next_handle

        mock_cache_provider = MagicMock()
        mock_cache_provider.get.return_value = None

        cache_pipeline = CachePipeline(mock_cache_provider)
        cache_pipeline.set_next(mock_test_pipeline)

        test_request = TestRequestWithCacheable("cache_key", 1, CacheType.DISTRIBUTED)

        # When
        response = self.async_loop(cache_pipeline.handle(test_request))

        # Then
        assert response is not None
        assert isinstance(response, TestResponse)
        assert response == next_response
        mock_cache_provider.get.assert_called_once_with(test_request.get_cache_key())
        mock_cache_provider.add.assert_called_once_with(test_request.get_cache_key(),
                                                        SerializerFactory.get_serializer().dumps(next_response),
                                                        test_request.get_cache_duration())
    def test_serializer_when_does_not_call_set(self):
        # Given

        # When
        current_serializer = SerializerFactory.get_serializer()

        # Then
        assert isinstance(current_serializer, Serializer)
Ejemplo n.º 4
0
    def test_ready_when_serializer_is_none(self):
        # Given
        mediatr_container = FakeMediatrContainer()
        mediatr = Mediatr()

        # When
        mediatr.ready(container=mediatr_container)

        # Then
        assert mediatr.is_ready is True
        assert isinstance(SerializerFactory.get_serializer(), BaseSerializer)
Ejemplo n.º 5
0
    def test_ready_when_serializer_is_not_none(self):
        # Given
        container = FakeMediatrContainer()
        mediatr = Mediatr()

        # When
        mediatr.ready(container=container, serializer={})

        # Then
        assert mediatr.is_ready is True
        assert SerializerFactory.get_serializer() == {}
Ejemplo n.º 6
0
    def ready(self,
              container: BaseMediatrContainer,
              serializer: BaseSerializer = None,
              logger: BaseLogger = None):
        if self.is_ready:
            return

        if container is None:
            raise Exception("mediatr_container_is_none")

        self.__container = container
        self.__container.prepare_pipes(DefaultPipeline(self.__container))

        if serializer is not None:
            SerializerFactory.set_serializer(serializer)

        if logger is not None:
            LoggerFactory.set_logger(logger)

        self.is_ready = True
    def test_set_serializer(self):
        # Given
        class TestSerializerObj(BaseSerializer):
            def dumps(self, obj):
                return ""

            def loads(self, obj):
                return "test"

            def deserialize(self, obj):
                return self.loads(self.dumps(obj))

        test_serializer = TestSerializerObj()
        SerializerFactory.set_serializer(serializer=test_serializer)

        test_response = TestResponse(True)

        # When
        response = test_response.to_json()

        # Then
        assert response == "test"
        assert test_serializer == SerializerFactory.get_serializer()
Ejemplo n.º 8
0
    def test_handle_when_req_result_is_in_cache(self):
        # Given
        next_response = TestResponse(success=True)

        mock_cache_provider = MagicMock()
        mock_cache_provider.get.return_value = SerializerFactory.get_serializer().dumps(next_response)

        cache_pipeline = CachePipeline(mock_cache_provider)
        cache_pipeline.set_next(TestPipeline(True))

        # When
        response = self.async_loop(
            cache_pipeline.handle(TestRequestWithCacheable("cache_key", 1, CacheType.DISTRIBUTED)))

        # Then
        assert response is not None
        assert mock_cache_provider.get.call_count == 1
Ejemplo n.º 9
0
def get_todo_all(request):
    loop = asyncio.new_event_loop()
    response = loop.run_until_complete(pydiator.send(GetTodoAllRequest()))
    loop.close()

    return Response(SerializerFactory.get_serializer().deserialize(response))
Ejemplo n.º 10
0
 def setUp(self):
     SerializerFactory.set_serializer(None)
     LoggerFactory.set_logger(None)
Ejemplo n.º 11
0
 def __get_serializer():
     return SerializerFactory.get_serializer()
Ejemplo n.º 12
0
 def __init__(self):
     self.serializer = SerializerFactory.get_serializer()
     self.logger = LoggerFactory.get_logger()