Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/lims_utils/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def paginate(

if total:
if page < 0:
page = (total // limit) + page
actual_page = (total // limit) + page
page = 0 if actual_page < 0 else actual_page

new_query = query.limit(limit).offset((page) * limit)

Expand Down
13 changes: 13 additions & 0 deletions tests/database/test_paginate.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ def test_paginate_reverse(mock_session):
assert query_eq(mock_session.call_args.args[0], query.limit(20).offset(80))


@patch.object(fs, "execute")
def test_paginate_reverse_underflow(mock_session):
"""Should catch underflow if page is negative and absolute value is greater than total pages"""

mock_session.return_value.all.return_value = []
with get_session(lambda: fs):
# Page total is 5
response = db.paginate(query, 20, -8, precounted_total=100)
assert response.page == 0

assert query_eq(mock_session.call_args.args[0], query.limit(20).offset(0))


def test_total_zero():
"""Should return empty list if total is 0"""

Expand Down
Loading