Skip to content

Commit

Permalink
🐛 Fix pagination for empty chunks (#6985)
Browse files Browse the repository at this point in the history
  • Loading branch information
giancarloromeo authored Dec 20, 2024
1 parent 68f0b24 commit 493488c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
6 changes: 3 additions & 3 deletions packages/models-library/src/models_library/rpc_pagination.py
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": [],
},
]
19 changes: 19 additions & 0 deletions services/catalog/tests/unit/with_dbs/test_api_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,25 @@ async def background_sync_task_mocked(
await services_db_tables_injector(fake_data_for_services)


async def test_rpc_catalog_with_no_services_returns_empty_page(
background_sync_task_mocked: None,
mocked_director_service_api: MockRouter,
rpc_client: RabbitMQRPCClient,
user_id: UserID,
app: FastAPI,
):
assert app

page = await list_services_paginated(
rpc_client, product_name="not_existing_returns_no_services", user_id=user_id
)
assert page.data == []
assert page.links.next is None
assert page.links.prev is None
assert page.meta.count == 0
assert page.meta.total == 0


async def test_rpc_catalog_client(
background_sync_task_mocked: None,
mocked_director_service_api: MockRouter,
Expand Down

0 comments on commit 493488c

Please sign in to comment.