Skip to content

Commit

Permalink
Fix/pagination (#28)
Browse files Browse the repository at this point in the history
* fix(api): page - 1 that returned the wrong page

* test(api): add test for paginate
  • Loading branch information
mvergez authored and andriacap committed Sep 7, 2023
1 parent f100beb commit 2dc929a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion backend/gn_module_monitoring/tests/test_utils/test_routes.py
Original file line number Diff line number Diff line change
@@ -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")])
Expand All @@ -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
2 changes: 1 addition & 1 deletion backend/gn_module_monitoring/utils/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 2dc929a

Please sign in to comment.