Skip to content

Commit

Permalink
Fix/paginate utils (#24)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mvergez authored and amandine-sahl committed Jul 29, 2024
1 parent 6b39e4b commit 47a2e88
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
7 changes: 7 additions & 0 deletions backend/gn_module_monitoring/tests/test_routes/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Empty file.
14 changes: 14 additions & 0 deletions backend/gn_module_monitoring/tests/test_utils/test_routes.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 2 additions & 2 deletions backend/gn_module_monitoring/utils/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 47a2e88

Please sign in to comment.