Exemplo n.º 1
0
def get_todo_by_id(request, id):
    loop = asyncio.new_event_loop()
    response = loop.run_until_complete(pydiator.send(
        GetTodoByIdRequest(id=id)))
    loop.close()

    return Response(response.to_json())
    def test_handle_return_empty_list(self, mock_pydiator):
        # Given
        mock_pydiator.send.side_effect = [self.async_return([])]
        request = GetTodoAllRequest(id=1)
        expected_response = []

        # When
        response = self.async_loop(pydiator.send(request))

        # Then
        assert response == expected_response
Exemplo n.º 3
0
    def test_handle_return_empty_list(self, mock_fake_todo_db):
        # Given
        mock_fake_todo_db.__iter__.return_value = []
        request = GetTodoAllDataRequest()
        expected_response = []

        # When
        response = self.async_loop(pydiator.send(request))

        # Then
        assert response == expected_response
    def test_handle_return_success_false_when_todo_is_not_exist(
            self, mock_fake_todo_db):
        # Given
        mock_fake_todo_db.__iter__.return_value = []
        request = DeleteTodoByIdDataRequest(id=1)
        expected_response = DeleteTodoByIdDataResponse(success=False)

        # When
        response = self.async_loop(pydiator.send(request))

        # Then
        assert response == expected_response
Exemplo n.º 5
0
    def test_handle_return_none_when_todo_is_not_exist(self,
                                                       mock_fake_todo_db):
        # Given
        mock_fake_todo_db.__iter__.return_value = []
        request = GetTodoByIdDataRequest(id=1)
        expected_response = None

        # When
        response = self.async_loop(pydiator.send(request))

        # Then
        assert response == expected_response
Exemplo n.º 6
0
    def test_handle_return_false_when_data_response_is_not_successful(self, mock_pydiator):
        # Given
        mock_pydiator.send.side_effect = [self.async_return(DeleteTodoByIdDataResponse(success=False))]
        request = DeleteTodoByIdRequest(id=1)
        expected_response = DeleteTodoByIdResponse(success=False)

        # When
        response = self.async_loop(pydiator.send(request))

        # Then
        assert response == expected_response
        assert mock_pydiator.send.called
        assert mock_pydiator.publish.called is False
Exemplo n.º 7
0
    def test_handle_return_exception_when_todo_not_found(
            self, mock_fake_todo_db):
        # Given
        mock_fake_todo_db.__iter__.return_value = []
        title_val = "title 1 updated"
        request = UpdateTodoDataRequest(title=title_val)

        # When
        with raises(DataException) as exc:
            self.async_loop(pydiator.send(request))

        # Then
        assert exc.value.error_info == ErrorInfoContainer.todo_not_found_error
Exemplo n.º 8
0
    def test_handle_return_success(self, mock_fake_todo_db):
        # Given
        self.register_request(AddTodoDataRequest(), AddTodoDataUseCase())
        mock_fake_todo_db.__iter__.return_value = []

        title_val = "title"
        request = AddTodoDataRequest(title=title_val)
        expected_response = AddTodoDataResponse(success=True, id=1)

        # When
        response = self.async_loop(pydiator.send(request))

        # Then
        assert response == expected_response
        assert mock_fake_todo_db.append.call_count == 1
        assert mock_fake_todo_db.append.called
Exemplo n.º 9
0
    def test_handle_return_success(self, mock_pydiator):
        # Given
        mock_pydiator.send.side_effect = [self.async_return(DeleteTodoByIdDataResponse(success=True))]
        mock_pydiator.publish.side_effect = [self.async_return(True)]

        id_val = 1
        request = DeleteTodoByIdRequest(id=id_val)
        expected_response = DeleteTodoByIdResponse(success=True)

        # When
        response = self.async_loop(pydiator.send(request))

        # Then
        assert response == expected_response
        assert mock_pydiator.send.called
        assert mock_pydiator.publish.called
Exemplo n.º 10
0
    def test_handle_return_todo(self, mock_fake_todo_db):
        # Given
        id_val = 1
        title_val = "title 1"
        mock_fake_todo_db.__iter__.return_value = [{
            "id": id_val,
            "title": title_val
        }]

        request = GetTodoByIdDataRequest(id=id_val)
        expected_response = GetTodoByIdDataResponse(id=id_val, title=title_val)

        # When
        response = self.async_loop(pydiator.send(request))

        # Then
        assert response == expected_response
    def test_handle_return_success(self, mock_fake_todo_db):
        # Given
        id_val = 1
        mock_fake_todo_db.__iter__.return_value = [{
            "id": id_val,
            "title": "title 1"
        }]
        request = DeleteTodoByIdDataRequest(id=id_val)
        expected_response = DeleteTodoByIdResponse(success=True)

        # When
        response = self.async_loop(pydiator.send(request))

        # Then
        assert response == expected_response
        assert mock_fake_todo_db.remove.called
        assert mock_fake_todo_db.remove.call_count == 1
Exemplo n.º 12
0
    def test_handle_return_success(self, mock_fake_todo_db):
        # Given
        id_val = 1
        mock_fake_todo_db.__iter__.return_value = [{
            "id": id_val,
            "title": "title 1"
        }]
        title_val = "title 1 updated"
        request = UpdateTodoDataRequest(title=title_val)
        request.id = id_val
        expected_response = UpdateTodoDataResponse(success=True)

        # When
        response = self.async_loop(pydiator.send(request))

        # Then
        assert response == expected_response
Exemplo n.º 13
0
    def test_handle_return_success_false_when_data_response_is_not_successful(
            self, mock_pydiator):
        # Given
        mock_pydiator.send.side_effect = [
            self.async_return(AddTodoDataResponse(success=False, id=0))
        ]

        title_val = "title"
        request = AddTodoRequest(title=title_val)
        expected_response = AddTodoResponse(success=False)

        # When
        response = self.async_loop(pydiator.send(request))

        # Then
        assert response == expected_response
        assert mock_pydiator.send.called
        assert mock_pydiator.publish.called is False
Exemplo n.º 14
0
    def test_handle_return_list(self, mock_pydiator):
        # Given
        id_val = 1
        title_val = "title 1"
        mock_pydiator.send.side_effect = [
            self.async_return(
                [GetTodoAllDataResponse(id=id_val, title=title_val)])
        ]

        request = GetTodoAllRequest(id=id_val)
        expected_response = [GetTodoAllResponse(id=id_val, title=title_val)]

        # When
        response = self.async_loop(pydiator.send(request))

        # Then
        assert response is not None
        assert expected_response == response
Exemplo n.º 15
0
    def test_handle_return_success(self, mock_pydiator):
        # Given
        mock_pydiator.send.side_effect = [
            self.async_return(AddTodoDataResponse(success=True, id=1))
        ]
        mock_pydiator.publish.side_effect = [self.async_return(True)]

        title_val = "title"
        request = AddTodoRequest(title=title_val)
        expected_response = AddTodoResponse(success=True)

        # When
        response = self.async_loop(pydiator.send(request))

        # Then
        assert response == expected_response
        assert mock_pydiator.send.called
        assert mock_pydiator.publish.called
Exemplo n.º 16
0
    def test_handle_return_success(self, mock_pydiator):
        # Given
        mock_pydiator.send.side_effect = [
            self.async_return(UpdateTodoDataResponse(success=True))
        ]
        mock_pydiator.publish.side_effect = [self.async_return(True)]

        id_val = 1
        title_val = "title 1 updated"
        request = UpdateTodoRequest(title=title_val)
        request.CustomFields.id = id_val
        expected_response = UpdateTodoResponse(success=True)

        # When
        response = self.async_loop(pydiator.send(request))

        # Then
        assert response == expected_response
        assert mock_pydiator.send.called
        assert mock_pydiator.publish.called
Exemplo n.º 17
0
    def test_handle_return_false_when_data_response_is_not_successful(
            self, mock_pydiator):
        # Given
        self.register_request(UpdateTodoRequest(), UpdateTodoUseCase())
        mock_pydiator.send.side_effect = [
            self.async_return(UpdateTodoDataResponse(success=False))
        ]

        id_val = 1
        title_val = "title 1 updated"
        request = UpdateTodoRequest(title=title_val)
        request.CustomFields.id = id_val
        expected_response = UpdateTodoResponse(success=False)

        # When
        response = self.async_loop(pydiator.send(request))

        # Then
        assert response == expected_response
        assert mock_pydiator.send.called
        assert mock_pydiator.publish.called is False
Exemplo n.º 18
0
class GetSampleByIdResponse(BaseResponse):
    def __init__(self, id: int, title: str):
        self.id = id
        self.title = title


class GetSampleByIdUseCase(BaseHandler):
    async def handle(self, req: GetSampleByIdRequest):
        # related codes are here such as business
        return GetSampleByIdResponse(id=req.id, title="hello pydiator")


from pydiator_core.mediatr import pydiator
from pydiator_core.mediatr_container import MediatrContainer


def set_up_pydiator():
    container = MediatrContainer()
    container.register_request(GetSampleByIdRequest, GetSampleByIdUseCase())
    container.register_pipeline(LogPipeline())
    pydiator.ready(container=container)


if __name__ == "__main__":
    set_up_pydiator()
    loop = asyncio.new_event_loop()
    response: GetSampleByIdResponse = loop.run_until_complete(
        pydiator.send(GetSampleByIdRequest(id=1)))
    loop.close()
    print(response.to_json())
Exemplo n.º 19
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))