Skip to content

Commit d073037

Browse files
committed
Migrate test_pagination to pytest_engine
1 parent 0e5d7f9 commit d073037

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,7 @@ class Todo(db.Document):
4545
text = mongoengine.StringField()
4646
done = mongoengine.BooleanField(default=False)
4747
pub_date = mongoengine.DateTimeField(default=datetime.utcnow)
48+
comments = mongoengine.ListField(mongoengine.StringField())
49+
comment_count = mongoengine.IntField()
4850

4951
return Todo

tests/test_pagination.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import pytest
2+
from werkzeug.exceptions import NotFound
3+
4+
from flask_mongoengine import ListFieldPagination, Pagination
5+
6+
7+
def test_queryset_paginator(app, todo):
8+
Todo = todo
9+
for i in range(42):
10+
Todo(title="post: %s" % i).save()
11+
12+
with pytest.raises(NotFound):
13+
Pagination(iterable=Todo.objects, page=0, per_page=10)
14+
15+
with pytest.raises(NotFound):
16+
Pagination(iterable=Todo.objects, page=6, per_page=10)
17+
18+
paginator = Pagination(Todo.objects, 1, 10)
19+
_test_paginator(paginator)
20+
21+
22+
def test_paginate_plain_list():
23+
with pytest.raises(NotFound):
24+
Pagination(iterable=range(1, 42), page=0, per_page=10)
25+
26+
with pytest.raises(NotFound):
27+
Pagination(iterable=range(1, 42), page=6, per_page=10)
28+
29+
paginator = Pagination(range(1, 42), 1, 10)
30+
_test_paginator(paginator)
31+
32+
33+
def test_list_field_pagination(app, todo):
34+
Todo = todo
35+
36+
comments = ["comment: %s" % i for i in range(42)]
37+
todo = Todo(
38+
title="todo has comments", comments=comments, comment_count=len(comments),
39+
).save()
40+
41+
# Check without providing a total
42+
paginator = ListFieldPagination(Todo.objects, todo.id, "comments", 1, 10)
43+
_test_paginator(paginator)
44+
45+
# Check with providing a total (saves a query)
46+
paginator = ListFieldPagination(
47+
Todo.objects, todo.id, "comments", 1, 10, todo.comment_count
48+
)
49+
_test_paginator(paginator)
50+
51+
paginator = todo.paginate_field("comments", 1, 10)
52+
_test_paginator(paginator)
53+
54+
55+
def _test_paginator(paginator):
56+
assert 5 == paginator.pages
57+
assert [1, 2, 3, 4, 5] == list(paginator.iter_pages())
58+
59+
for i in [1, 2, 3, 4, 5]:
60+
61+
if i == 1:
62+
assert not paginator.has_prev
63+
with pytest.raises(NotFound):
64+
paginator.prev()
65+
else:
66+
assert paginator.has_prev
67+
68+
if i == 5:
69+
assert not paginator.has_next
70+
with pytest.raises(NotFound):
71+
paginator.next()
72+
else:
73+
assert paginator.has_next
74+
75+
if i == 3:
76+
assert [None, 2, 3, 4, None] == list(paginator.iter_pages(0, 1, 1, 0))
77+
78+
assert i == paginator.page
79+
assert i - 1 == paginator.prev_num
80+
assert i + 1 == paginator.next_num
81+
82+
# Paginate to the next page
83+
if i < 5:
84+
paginator = paginator.next()

0 commit comments

Comments
 (0)