Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix pagination for empty chunks #6985

Merged
Show file tree
Hide file tree
Changes from all 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": [],
},
]
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
Loading