From 47a2e888f8b0b0d030be681faa3612c33de4b023 Mon Sep 17 00:00:00 2001 From: Maxime Vergez <85738261+mvergez@users.noreply.github.com> Date: Wed, 4 Jan 2023 12:09:23 +0100 Subject: [PATCH] Fix/paginate utils (#24) * fix(api): add int conversion for limit/offset * test(api): add test for get_limit_offset * fix(api): max_per_page => per_page & test * test(api): update test with changes on schema Since marshmallow schemas, the json returned by pagination has changed for items and not sites --- .../tests/test_routes/test_site.py | 7 +++++++ .../tests/test_utils/__init__.py | 0 .../tests/test_utils/test_routes.py | 14 ++++++++++++++ backend/gn_module_monitoring/utils/routes.py | 4 ++-- 4 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 backend/gn_module_monitoring/tests/test_utils/__init__.py create mode 100644 backend/gn_module_monitoring/tests/test_utils/test_routes.py diff --git a/backend/gn_module_monitoring/tests/test_routes/test_site.py b/backend/gn_module_monitoring/tests/test_routes/test_site.py index 6bddc90ca..d63591ca5 100644 --- a/backend/gn_module_monitoring/tests/test_routes/test_site.py +++ b/backend/gn_module_monitoring/tests/test_routes/test_site.py @@ -40,6 +40,13 @@ def test_get_sites(self, sites): assert r.json["count"] >= len(sites) assert any([schema.dump(site) in r.json["items"] for site in sites.values()]) + def test_get_sites_limit(self, sites): + limit = 34 + + r = self.client.get(url_for("monitorings.get_sites", limit=limit)) + + assert len(r.json["items"]) == limit + def test_get_module_sites(self): module_code = "TEST" r = self.client.get(url_for("monitorings.get_module_sites", module_code=module_code)) diff --git a/backend/gn_module_monitoring/tests/test_utils/__init__.py b/backend/gn_module_monitoring/tests/test_utils/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/backend/gn_module_monitoring/tests/test_utils/test_routes.py b/backend/gn_module_monitoring/tests/test_utils/test_routes.py new file mode 100644 index 000000000..cc6357f37 --- /dev/null +++ b/backend/gn_module_monitoring/tests/test_utils/test_routes.py @@ -0,0 +1,14 @@ +import pytest +from werkzeug.datastructures import MultiDict + +from gn_module_monitoring.utils.routes import get_limit_offset + + +@pytest.mark.parametrize("limit, offset", [("1", "2"), (1, 2), ("1", 2), (1, "2")]) +def test_get_limit_offset(limit, offset): + multi_dict = MultiDict([("limit", limit), ("offset", offset)]) + + comp_limit, comp_offset = get_limit_offset(params=multi_dict) + + assert isinstance(comp_limit, int) + assert isinstance(comp_offset, int) diff --git a/backend/gn_module_monitoring/utils/routes.py b/backend/gn_module_monitoring/utils/routes.py index 5a75c1c4b..a7889dc68 100644 --- a/backend/gn_module_monitoring/utils/routes.py +++ b/backend/gn_module_monitoring/utils/routes.py @@ -10,11 +10,11 @@ def get_limit_offset(params: MultiDict) -> Tuple[int]: - return params.pop("limit", 50), params.pop("offset", 1) + return int(params.pop("limit", 50)), int(params.pop("offset", 1)) def paginate(query: Query, schema: Schema, limit: int, page: int) -> Response: - result = query.paginate(page=page, error_out=False, max_per_page=limit) + result = query.paginate(page=page, error_out=False, per_page=limit) pagination_schema = paginate_schema(schema) data = pagination_schema().dump( dict(items=result.items, count=result.total, limit=limit, offset=page - 1)