Skip to content

Commit 6681c22

Browse files
authored
Merge pull request #16 from idoshr/claude/review-pagination-bugs-011CUfCDfh8bqjSDYMjCyeov
Claude/review pagination bugs 011 c uf c dfh8bqj sdy mj cyeov
2 parents 01d101d + 12b5592 commit 6681c22

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

flask_mongoengine/pagination/basic_pagination.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ def __init__(
2323
:param first_page_index: Option for change first page index.
2424
"""
2525

26+
if per_page <= 0:
27+
raise ValueError("per_page must be a positive integer")
28+
2629
if page < first_page_index:
2730
abort(404, "Invalid page number.")
2831

flask_mongoengine/pagination/keyset_pagination.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ def __init__(
1717
:param per_page: Required number of documents per page.
1818
:param max_depth: Option for limit number of dereference documents.
1919
"""
20+
if per_page <= 0:
21+
raise ValueError("per_page must be a positive integer")
22+
2023
self.get_page(iterable, per_page, field_filter_by, last_field_value)
2124

2225
def get_page(

flask_mongoengine/pagination/list_field_pagination.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ def __init__(
2828
first_page_index is option for change first page index.
2929
3030
"""
31+
if per_page <= 0:
32+
raise ValueError("per_page must be a positive integer")
33+
3134
if page < first_page_index:
3235
abort(404, "Invalid page number.")
3336

tests/test_pagination.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,38 @@ def test_queryset_paginator_invalid(app, todo):
3737
Pagination(iterable=Todo.objects, page=-1, per_page=10, first_page_index=0)
3838

3939

40+
def test_per_page_validation(app, todo):
41+
"""Test that per_page validation works for all pagination classes"""
42+
Todo = todo
43+
44+
# Test Pagination with per_page=0
45+
with pytest.raises(ValueError, match="per_page must be a positive integer"):
46+
Pagination(iterable=Todo.objects, page=1, per_page=0)
47+
48+
# Test Pagination with negative per_page
49+
with pytest.raises(ValueError, match="per_page must be a positive integer"):
50+
Pagination(iterable=Todo.objects, page=1, per_page=-5)
51+
52+
# Test KeysetPagination with per_page=0
53+
with pytest.raises(ValueError, match="per_page must be a positive integer"):
54+
KeysetPagination(Todo.objects, per_page=0)
55+
56+
# Test KeysetPagination with negative per_page
57+
with pytest.raises(ValueError, match="per_page must be a positive integer"):
58+
KeysetPagination(Todo.objects, per_page=-5)
59+
60+
# Test ListFieldPagination with per_page=0
61+
comments = [f"comment: {i}" for i in range(10)]
62+
test_todo = Todo(title="test", comments=comments).save()
63+
64+
with pytest.raises(ValueError, match="per_page must be a positive integer"):
65+
ListFieldPagination(Todo.objects, test_todo.id, "comments", 1, per_page=0)
66+
67+
# Test ListFieldPagination with negative per_page
68+
with pytest.raises(ValueError, match="per_page must be a positive integer"):
69+
ListFieldPagination(Todo.objects, test_todo.id, "comments", 1, per_page=-5)
70+
71+
4072
def test_queryset_paginator(app, todo):
4173
Todo = todo
4274
paginator = Pagination(Todo.objects, 1, 10)

0 commit comments

Comments
 (0)