From 8e74ee269e670bd3f7f1b54820a8cd9206dd006a Mon Sep 17 00:00:00 2001 From: ozgurkara Date: Wed, 11 Jan 2023 01:43:08 +0300 Subject: [PATCH] pydiatr-core updated & nested object result support added --- app/resources/todo/usecases/get_todo_all.py | 17 +++++++++++------ app/utils/config.py | 2 +- requirements.txt | 2 +- .../resources/todo/test_todo_resource.py | 2 +- .../todo/usecases/test_get_todo_all.py | 8 ++++---- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/app/resources/todo/usecases/get_todo_all.py b/app/resources/todo/usecases/get_todo_all.py index 3f21ea7..6b790a8 100644 --- a/app/resources/todo/usecases/get_todo_all.py +++ b/app/resources/todo/usecases/get_todo_all.py @@ -2,6 +2,7 @@ from pydiator_core.interfaces import BaseRequest, BaseResponse, BaseHandler, BaseCacheable, CacheType from typing import List from pydiator_core.mediatr import pydiator + from app.data.todo.usecases.get_todo_all_data import GetTodoAllDataRequest @@ -16,17 +17,21 @@ def get_cache_type(self) -> CacheType: return CacheType.DISTRIBUTED -class GetTodoAllResponse(BaseModel, BaseResponse): +class Todo(BaseModel): id: int = Field(...) title: str = Field(...) +class GetTodoAllResponse(BaseModel, BaseResponse): + items: List[Todo] = [] + + class GetTodoAllUseCase(BaseHandler): - async def handle(self, req: GetTodoAllRequest) -> List[GetTodoAllResponse]: - response = [] - data_response = await pydiator.send(GetTodoAllDataRequest()) - for d in data_response: - response.append(GetTodoAllResponse(id=d.id, title=d.title)) + async def handle(self, req: GetTodoAllRequest) -> GetTodoAllResponse: + response = GetTodoAllResponse() + todo_data = await pydiator.send(GetTodoAllDataRequest()) + for item in todo_data: + response.items.append(Todo(id=item.id, title=item.title)) return response diff --git a/app/utils/config.py b/app/utils/config.py index d0e54ff..80fecbe 100644 --- a/app/utils/config.py +++ b/app/utils/config.py @@ -11,7 +11,7 @@ REDIS_KEY_PREFIX = config('REDIS_KEY_PREFIX', str, 'fastapi_pydiator:') DISTRIBUTED_CACHE_IS_ENABLED = config("DISTRIBUTED_CACHE_IS_ENABLED", bool, True) -CACHE_PIPELINE_IS_ENABLED = config("CACHE_PIPELINE_IS_ENABLED", bool, True) +CACHE_PIPELINE_IS_ENABLED = config("CACHE_PIPELINE_IS_ENABLED", bool, False) LOG_PIPELINE_IS_ENABLED = config("LOG_PIPELINE_IS_ENABLED", bool, True) TRACER_PIPELINE_IS_ENABLED = config("TRACER_PIPELINE_IS_ENABLED", bool, True) TRACER_IS_ENABLED = config('TRACER_IS_ENABLED', bool, True) diff --git a/requirements.txt b/requirements.txt index 890f4a9..4e20142 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ uvicorn==0.13.3 fastapi==0.65.2 fastapi-contrib==0.2.9 -pydiator-core==1.0.9 +pydiator-core==1.0.11 pydantic==1.7.4 redis==3.5.3 diff --git a/tests/integration/resources/todo/test_todo_resource.py b/tests/integration/resources/todo/test_todo_resource.py index c31b951..1806837 100644 --- a/tests/integration/resources/todo/test_todo_resource.py +++ b/tests/integration/resources/todo/test_todo_resource.py @@ -5,7 +5,7 @@ class TestTodo: def test_get_todo_all(self, test_app): response = test_app.get("/v1/todos") - items = response.json() + items = response.json()["items"] assert response.status_code == HTTP_200_OK assert len(items) == 2 diff --git a/tests/unit/resources/todo/usecases/test_get_todo_all.py b/tests/unit/resources/todo/usecases/test_get_todo_all.py index 9ba1315..ca5059d 100644 --- a/tests/unit/resources/todo/usecases/test_get_todo_all.py +++ b/tests/unit/resources/todo/usecases/test_get_todo_all.py @@ -2,7 +2,7 @@ from app.data.todo.usecases.get_todo_all_data import GetTodoAllDataResponse from pydiator_core.mediatr import pydiator -from app.resources.todo.usecases.get_todo_all import GetTodoAllRequest, GetTodoAllUseCase, GetTodoAllResponse +from app.resources.todo.usecases.get_todo_all import GetTodoAllRequest, GetTodoAllUseCase, GetTodoAllResponse, Todo from tests.unit.base_test_case import BaseTestCase @@ -21,7 +21,7 @@ def test_handle_return_list(self, mock_pydiator): 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)] + expected_response = GetTodoAllResponse(items=[Todo(id=id_val, title=title_val)]) # When response = self.async_loop(pydiator.send(request)) @@ -34,8 +34,8 @@ def test_handle_return_list(self, mock_pydiator): def test_handle_return_empty_list(self, mock_pydiator): # Given mock_pydiator.send.side_effect = [self.async_return([])] - request = GetTodoAllRequest(id=1) - expected_response = [] + request = GetTodoAllRequest() + expected_response = GetTodoAllResponse() # When response = self.async_loop(pydiator.send(request))