From 47fab9974a22ab7b6e1b0288da2cab43eb7fb989 Mon Sep 17 00:00:00 2001 From: Giancarlo Romeo Date: Fri, 20 Dec 2024 10:35:29 +0100 Subject: [PATCH 1/2] support for empty chunks --- .../src/models_library/rpc_pagination.py | 6 ++--- .../tests/test_rest_pagination.py | 8 +++---- .../pytest_simcore/examples/models_library.py | 24 +++++++++++++++++++ 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/packages/models-library/src/models_library/rpc_pagination.py b/packages/models-library/src/models_library/rpc_pagination.py index 96d6308f66c..f1aecabab81 100644 --- a/packages/models-library/src/models_library/rpc_pagination.py +++ b/packages/models-library/src/models_library/rpc_pagination.py @@ -30,14 +30,14 @@ 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": ( @@ -45,7 +45,7 @@ def create(cls, total: int, limit: int, offset: int) -> "PageRefsParams": "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}, diff --git a/packages/models-library/tests/test_rest_pagination.py b/packages/models-library/tests/test_rest_pagination.py index a32bec673bb..85669e4a6de 100644 --- a/packages/models-library/tests/test_rest_pagination.py +++ b/packages/models-library/tests/test_rest_pagination.py @@ -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 @@ -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) @@ -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) diff --git a/packages/pytest-simcore/src/pytest_simcore/examples/models_library.py b/packages/pytest-simcore/src/pytest_simcore/examples/models_library.py index 8af09913f71..cbd0f18d8ff 100644 --- a/packages/pytest-simcore/src/pytest_simcore/examples/models_library.py +++ b/packages/pytest-simcore/src/pytest_simcore/examples/models_library.py @@ -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]] = [ @@ -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": [], + }, ] From bf19cdd19513822ae250feb07923890413d2b822 Mon Sep 17 00:00:00 2001 From: Giancarlo Romeo Date: Fri, 20 Dec 2024 13:53:03 +0100 Subject: [PATCH 2/2] add catalog test --- .../tests/unit/with_dbs/test_api_rpc.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/services/catalog/tests/unit/with_dbs/test_api_rpc.py b/services/catalog/tests/unit/with_dbs/test_api_rpc.py index 16fb6adb4cb..3192eabbfe6 100644 --- a/services/catalog/tests/unit/with_dbs/test_api_rpc.py +++ b/services/catalog/tests/unit/with_dbs/test_api_rpc.py @@ -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,