Skip to content

Commit

Permalink
[FIX] added tests for recursive wrapping, recursive wrapping on pagin…
Browse files Browse the repository at this point in the history
…ation, simplified logic for pagination integration router, typo in readme
  • Loading branch information
acwazz committed Aug 5, 2022
1 parent 21a2777 commit 1feb21a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class Item(BaseModel):

@app.get("/items", response_model=List[Item], description="This is a route")
def get_operation():
return [Item(id=0, name="ciao"), Item(id=1, name="hola"), Item(id=1, name="hello")]
return [Item(id=1, name="ciao"), Item(id=2, name="hola"), Item(id=3, name="hello")]
```

Te result of `GET /items`:
Expand All @@ -82,15 +82,15 @@ content-type: application/json
{
"data": [
{
"id": 0,
"id": 1,
"name": "ciao"
},
{
"id": 1,
"id": 2,
"name": "hola"
},
{
"id": 1,
"id": 3,
"name": "hello"
}
],
Expand Down
10 changes: 0 additions & 10 deletions fastapi_responseschema/integrations/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,3 @@ def get_wrapper_model(self, is_error: bool, response_model: Type[Any]) -> Type[A
return self.paged_response_schema
return self.error_response_schema if is_error else self.paged_response_schema
return super().get_wrapper_model(is_error, response_model)

def override_response_model(self, wrapper_model: Type[AbstractResponseSchema[Any]], response_model: Type[Any]) -> Type[AbstractResponseSchema[Any]]:
if issubclass(response_model, AbstractPagedResponseSchema):
return response_model
return super().override_response_model(wrapper_model, response_model)

def _wrap_endpoint_output(self, endpoint_output: Any, wrapper_model: Type[AbstractResponseSchema], response_model: Type[Any], **params) -> Any:
if issubclass(response_model, AbstractPagedResponseSchema):
response_model = response_model.__inner_types__
return super()._wrap_endpoint_output(endpoint_output, wrapper_model, response_model, **params)
34 changes: 32 additions & 2 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class Route(SchemaAPIRoute):

wrap_app_responses(app_wrapped_no_router, Route)

@app_wrapped_no_router.get("/wrapped", response_schema=bool)
@app_wrapped_no_router.get("/wrapped", response_model=bool)
def wrapped_call_first():
return True


router_unwrapped = APIRouter()
@router_unwrapped.get("/unwrapped", response_schema=dict)
@router_unwrapped.get("/unwrapped", response_model=dict)
def unwrapped_call_first():
return {"unwrapped": True}

Expand All @@ -37,3 +37,33 @@ def test_router_unwrapped_call():
resp = r.json()
assert resp.get("unwrapped")


app_not_wrapped = FastAPI()

@app_not_wrapped.get("/unwrapped", response_model=dict)
def un_wrapped_call_second():
return {"unwrapped": True}


router_wrapped = APIRouter(route_class=Route)
@router_wrapped.get("/wrapped", response_model=dict)
def wrapped_call_second():
return {"wrapped": True}

app_not_wrapped.include_router(router_wrapped)

client_app_not_wrapped = TestClient(app_not_wrapped)


def test_not_recursive_wrapping_on_router():
r = client_app_not_wrapped.get("/wrapped")
resp = r.json()
assert resp.get("data").get("wrapped")
assert not resp.get("error")


def test_app_unwrapped_call():
r = client_app_wrapped_no_router.get("/unwrapped")
resp = r.json()
assert resp.get("unwrapped")

20 changes: 18 additions & 2 deletions tests/test_pagination_integration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import TypeVar, Generic, Any, Sequence
import pytest
from fastapi import FastAPI
from fastapi import FastAPI, APIRouter
from fastapi.testclient import TestClient
from fastapi_responseschema.integrations.pagination import (
AbstractPagedResponseSchema,
Expand Down Expand Up @@ -112,14 +112,30 @@ class Route(PagedSchemaAPIRoute):
def with_response_model():
return paginate([True, False, True, False])

router = APIRouter(route_class=Route)

@router.get("/with-router", response_model=SimplePagedResponseSchema[bool])
def with_router():
return paginate([True, False, True, False])

app.include_router(router)

add_pagination(app)

client = TestClient(app)


def test_response_model_paginated_wrapping():
def test_response_model_paginated_wrapping_in_app():
raw = client.get("/with-model")
r = raw.json()
assert not r.get("data")[1]
assert r.get("pagination").get("total") == 4
assert not r.get("error")


def test_response_model_paginated_wrapping_in_router():
raw = client.get("/with-router")
r = raw.json()
assert not r.get("data")[1]
assert r.get("pagination").get("total") == 4
assert not r.get("error")

0 comments on commit 1feb21a

Please sign in to comment.