Skip to content

Commit 47fab99

Browse files
support for empty chunks
1 parent d75b0a3 commit 47fab99

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

packages/models-library/src/models_library/rpc_pagination.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,22 @@
3030
class PageRefsParams(PageRefs[PageQueryParameters]):
3131
@classmethod
3232
def create(cls, total: int, limit: int, offset: int) -> "PageRefsParams":
33-
last_page = ceil(total / limit) - 1
33+
last_page = ceil(total / limit) - 1 if total > 0 else 0
3434
return cls.model_validate(
3535
{
3636
"self": {"offset": offset, "limit": limit},
3737
"first": {"offset": 0, "limit": limit},
3838
"prev": (
3939
{"offset": max(offset - limit, 0), "limit": limit}
40-
if offset > 0
40+
if offset > 0 and total > 0
4141
else None
4242
),
4343
"next": (
4444
{
4545
"offset": min(offset + limit, last_page * limit),
4646
"limit": limit,
4747
}
48-
if offset < (last_page * limit)
48+
if offset < (last_page * limit) and total > 0
4949
else None
5050
),
5151
"last": {"offset": last_page * limit, "limit": limit},

packages/models-library/tests/test_rest_pagination.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44
from models_library.rest_pagination import Page, PageMetaInfoLimitOffset
5-
from pydantic.main import BaseModel
5+
from pydantic import BaseModel, ValidationError
66
from pytest_simcore.examples.models_library import PAGE_EXAMPLES
77

88

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

2727

2828
def test_invalid_offset():
29-
with pytest.raises(ValueError):
29+
with pytest.raises(ValidationError):
3030
PageMetaInfoLimitOffset(limit=6, total=5, offset=5, count=2)
3131

3232

@@ -39,14 +39,14 @@ def test_invalid_offset():
3939
],
4040
)
4141
def test_invalid_count(count: int, offset: int):
42-
with pytest.raises(ValueError):
42+
with pytest.raises(ValidationError):
4343
PageMetaInfoLimitOffset(limit=6, total=5, offset=offset, count=count)
4444

4545

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

5252

packages/pytest-simcore/src/pytest_simcore/examples/models_library.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@
2525
},
2626
"data": ["data 5", "data 6", "data 7"],
2727
},
28+
# empty page
29+
{
30+
"_meta": {"total": 0, "count": 0, "limit": 4, "offset": 0},
31+
"_links": {
32+
"self": "https://osparc.io/v2/listing?offset=0&limit=4",
33+
"first": "https://osparc.io/v2/listing?offset=0&limit=4",
34+
"prev": None,
35+
"next": None,
36+
"last": "https://osparc.io/v2/listing?offset=0&limit=4",
37+
},
38+
"data": [],
39+
},
2840
]
2941

3042
RPC_PAGE_EXAMPLES: Final[list[dict]] = [
@@ -52,4 +64,16 @@
5264
},
5365
"data": ["data 5", "data 6", "data 7"],
5466
},
67+
# empty page
68+
{
69+
"_meta": {"total": 0, "count": 0, "limit": 4, "offset": 0},
70+
"_links": {
71+
"self": {"offset": 0, "limit": 4},
72+
"first": {"offset": 0, "limit": 4},
73+
"prev": None,
74+
"next": None,
75+
"last": {"offset": 0, "limit": 4},
76+
},
77+
"data": [],
78+
},
5579
]

0 commit comments

Comments
 (0)