Skip to content

Commit

Permalink
Fix issue with links.LimiOffsetPage revalidation (#799)
Browse files Browse the repository at this point in the history
  • Loading branch information
uriyyo authored Aug 24, 2023
1 parent bdcf526 commit 37fb440
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
5 changes: 4 additions & 1 deletion fastapi_pagination/links/limit_offset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
__all__ = ["LimitOffsetPage"]

from math import floor
from typing import Any, Generic, TypeVar
from typing import Any, Generic, MutableMapping, TypeVar

from ..limit_offset import LimitOffsetPage as BasePage
from .bases import Links, create_links, validation_decorator
Expand All @@ -16,6 +16,9 @@ class LimitOffsetPage(BasePage[T], Generic[T]):

@validation_decorator
def __root_validator__(cls, value: Any) -> Any:
if not isinstance(value, MutableMapping):
return value

if "links" not in value:
offset, limit, total = [value[k] for k in ("offset", "limit", "total")]

Expand Down
49 changes: 40 additions & 9 deletions tests/test_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,23 @@ async def route_2():
return paginate([])


class MySchema(BaseModel):
class MySchemaPage(BaseModel):
page: Page[Any]


class MySchemaLimitOffset(BaseModel):
page: LimitOffsetPage[Any]


@app.get(
"/revalidate",
dependencies=[Depends(pagination_ctx(Page))],
response_model=MySchema,
"/revalidate/default",
dependencies=[Depends(pagination_ctx(Page[int]))],
response_model=MySchemaPage,
)
@app.get(
"/revalidate/limit-offset",
dependencies=[Depends(pagination_ctx(LimitOffsetPage[int]))],
response_model=MySchemaLimitOffset,
)
async def route_3():
page = paginate([*range(10)])
Expand Down Expand Up @@ -124,8 +133,9 @@ def test_links(self, prev, next, first, last):
}


def test_revalidation():
response = client.get("/revalidate")
def test_revalidation_default():
response = client.get("/revalidate/default")
print(response.json())

assert response.status_code == status.HTTP_200_OK
assert response.json() == {
Expand All @@ -136,9 +146,30 @@ def test_revalidation():
"size": 50,
"pages": 1,
"links": {
"first": "/revalidate?page=1",
"last": "/revalidate?page=1",
"self": "/revalidate",
"first": "/revalidate/default?page=1",
"last": "/revalidate/default?page=1",
"self": "/revalidate/default",
"next": None,
"prev": None,
},
},
}


def test_revalidation_limit_offset():
response = client.get("/revalidate/limit-offset")

assert response.status_code == status.HTTP_200_OK
assert response.json() == {
"page": {
"items": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"total": 10,
"limit": 50,
"offset": 0,
"links": {
"first": "/revalidate/limit-offset?offset=0",
"last": "/revalidate/limit-offset?offset=0",
"self": "/revalidate/limit-offset",
"next": None,
"prev": None,
},
Expand Down

0 comments on commit 37fb440

Please sign in to comment.