Skip to content

Commit

Permalink
Merge pull request #7 from ozgurkara/nested-object-support-in-response
Browse files Browse the repository at this point in the history
pydiatr-core updated & nested object result support added
  • Loading branch information
ozgurkara authored Jan 10, 2023
2 parents 5fea398 + 8e74ee2 commit 6af5d9b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
17 changes: 11 additions & 6 deletions app/resources/todo/usecases/get_todo_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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
2 changes: 1 addition & 1 deletion app/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/resources/todo/test_todo_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/resources/todo/usecases/test_get_todo_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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))
Expand All @@ -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))
Expand Down

0 comments on commit 6af5d9b

Please sign in to comment.