From 8a2f327e6b58b83d3f25704d34a2dd940c2619ce Mon Sep 17 00:00:00 2001 From: Maxime Vergez <85738261+mvergez@users.noreply.github.com> Date: Fri, 13 Jan 2023 10:08:37 +0100 Subject: [PATCH] Fix/pagination (#28) * fix(api): page - 1 that returned the wrong page * test(api): add test for paginate --- .../tests/test_utils/test_routes.py | 15 ++++++++++++++- backend/gn_module_monitoring/utils/routes.py | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/backend/gn_module_monitoring/tests/test_utils/test_routes.py b/backend/gn_module_monitoring/tests/test_utils/test_routes.py index cc6357f37..39434141d 100644 --- a/backend/gn_module_monitoring/tests/test_utils/test_routes.py +++ b/backend/gn_module_monitoring/tests/test_utils/test_routes.py @@ -1,7 +1,9 @@ import pytest from werkzeug.datastructures import MultiDict -from gn_module_monitoring.utils.routes import get_limit_offset +from gn_module_monitoring.monitoring.models import TMonitoringSites +from gn_module_monitoring.monitoring.schemas import MonitoringSitesSchema +from gn_module_monitoring.utils.routes import get_limit_offset, paginate @pytest.mark.parametrize("limit, offset", [("1", "2"), (1, 2), ("1", 2), (1, "2")]) @@ -12,3 +14,14 @@ def test_get_limit_offset(limit, offset): assert isinstance(comp_limit, int) assert isinstance(comp_offset, int) + + +def test_paginate(sites): + limit = 1 + page = 2 + + res = paginate( + query=TMonitoringSites.query, schema=MonitoringSitesSchema, limit=limit, page=page + ) + + assert res.json["offset"] == page diff --git a/backend/gn_module_monitoring/utils/routes.py b/backend/gn_module_monitoring/utils/routes.py index 0102f323c..e5adabab0 100644 --- a/backend/gn_module_monitoring/utils/routes.py +++ b/backend/gn_module_monitoring/utils/routes.py @@ -22,7 +22,7 @@ def paginate(query: Query, schema: Schema, limit: int, page: int) -> Response: 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) + dict(items=result.items, count=result.total, limit=limit, offset=page) ) return jsonify(data)