Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@
class PageRefsParams(PageRefs[PageQueryParameters]):
@classmethod
def create(cls, total: int, limit: int, offset: int) -> "PageRefsParams":
last_page = ceil(total / limit) - 1
last_page = ceil(total / limit) - 1 if total > 0 else 0
return cls.model_validate(
{
"self": {"offset": offset, "limit": limit},
"first": {"offset": 0, "limit": limit},
"prev": (
{"offset": max(offset - limit, 0), "limit": limit}
if offset > 0
if offset > 0 and total > 0
else None
),
"next": (
{
"offset": min(offset + limit, last_page * limit),
"limit": limit,
}
if offset < (last_page * limit)
if offset < (last_page * limit) and total > 0
else None
),
"last": {"offset": last_page * limit, "limit": limit},
Expand Down
8 changes: 4 additions & 4 deletions packages/models-library/tests/test_rest_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest
from models_library.rest_pagination import Page, PageMetaInfoLimitOffset
from pydantic.main import BaseModel
from pydantic import BaseModel, ValidationError
from pytest_simcore.examples.models_library import PAGE_EXAMPLES


Expand All @@ -26,7 +26,7 @@ def test_page_response_limit_offset_models(cls_model: BaseModel, examples: list[


def test_invalid_offset():
with pytest.raises(ValueError):
with pytest.raises(ValidationError):
PageMetaInfoLimitOffset(limit=6, total=5, offset=5, count=2)


Expand All @@ -39,14 +39,14 @@ def test_invalid_offset():
],
)
def test_invalid_count(count: int, offset: int):
with pytest.raises(ValueError):
with pytest.raises(ValidationError):
PageMetaInfoLimitOffset(limit=6, total=5, offset=offset, count=count)


def test_data_size_does_not_fit_count():
example = deepcopy(PAGE_EXAMPLES[0])
example["_meta"]["count"] = len(example["data"]) - 1
with pytest.raises(ValueError):
with pytest.raises(ValidationError):
Page[str](**example)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@
},
"data": ["data 5", "data 6", "data 7"],
},
# empty page
{
"_meta": {"total": 0, "count": 0, "limit": 4, "offset": 0},
"_links": {
"self": "https://osparc.io/v2/listing?offset=0&limit=4",
"first": "https://osparc.io/v2/listing?offset=0&limit=4",
"prev": None,
"next": None,
"last": "https://osparc.io/v2/listing?offset=0&limit=4",
},
"data": [],
},
]

RPC_PAGE_EXAMPLES: Final[list[dict]] = [
Expand Down Expand Up @@ -52,4 +64,16 @@
},
"data": ["data 5", "data 6", "data 7"],
},
# empty page
{
"_meta": {"total": 0, "count": 0, "limit": 4, "offset": 0},
"_links": {
"self": {"offset": 0, "limit": 4},
"first": {"offset": 0, "limit": 4},
"prev": None,
"next": None,
"last": {"offset": 0, "limit": 4},
},
"data": [],
},
]
Loading