-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathtest_pagination.py
131 lines (98 loc) · 3.59 KB
/
test_pagination.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import flask
import pytest
from werkzeug.exceptions import NotFound
from flask_mongoengine import ListFieldPagination, Pagination
@pytest.fixture(autouse=True)
def setup_endpoints(app, todo):
Todo = todo
for i in range(42):
Todo(title=f"post: {i}").save()
@app.route("/")
def index():
page = int(flask.request.form.get("page"))
per_page = int(flask.request.form.get("per_page"))
query_set = Todo.objects().paginate(page=page, per_page=per_page)
return {
'data': list(query_set.items),
'total': query_set.total,
'has_next': query_set.has_next,
}
def test_queryset_paginator(app, todo):
Todo = todo
with pytest.raises(NotFound):
Pagination(iterable=Todo.objects, page=0, per_page=10)
with pytest.raises(NotFound):
Pagination(iterable=Todo.objects, page=6, per_page=10)
paginator = Pagination(Todo.objects, 1, 10)
_test_paginator(paginator)
for page in range(1, 10):
for index, todo in enumerate(
Todo.objects.paginate(page=page, per_page=5).items
):
assert todo.title == f"post: {(page-1) * 5 + index}"
def test_paginate_plain_list():
with pytest.raises(NotFound):
Pagination(iterable=range(1, 42), page=0, per_page=10)
with pytest.raises(NotFound):
Pagination(iterable=range(1, 42), page=6, per_page=10)
paginator = Pagination(range(1, 42), 1, 10)
_test_paginator(paginator)
def test_list_field_pagination(app, todo):
Todo = todo
comments = [f"comment: {i}" for i in range(42)]
todo = Todo(
title="todo has comments",
comments=comments,
comment_count=len(comments),
).save()
# Check without providing a total
paginator = ListFieldPagination(Todo.objects, todo.id, "comments", 1, 10)
_test_paginator(paginator)
# Check with providing a total (saves a query)
paginator = ListFieldPagination(
Todo.objects, todo.id, "comments", 1, 10, todo.comment_count
)
_test_paginator(paginator)
paginator = todo.paginate_field("comments", 1, 10)
_test_paginator(paginator)
def _test_paginator(paginator):
assert 5 == paginator.pages
assert [1, 2, 3, 4, 5] == list(paginator.iter_pages())
for i in [1, 2, 3, 4, 5]:
if i == 1:
assert not paginator.has_prev
with pytest.raises(NotFound):
paginator.prev()
else:
assert paginator.has_prev
if i == 5:
assert not paginator.has_next
with pytest.raises(NotFound):
paginator.next()
else:
assert paginator.has_next
if i == 3:
assert [None, 2, 3, 4, None] == list(paginator.iter_pages(0, 1, 1, 0))
assert i == paginator.page
assert i - 1 == paginator.prev_num
assert i + 1 == paginator.next_num
# Paginate to the next page
if i < 5:
paginator = paginator.next()
def test_flask_pagination(app, todo):
client = app.test_client()
response = client.get("/", data={"page": 0, "per_page": 10})
print(response.status_code)
assert response.status_code == 404
response = client.get("/", data={"page": 6, "per_page": 10})
print(response.status_code)
assert response.status_code == 404
def test_flask_pagination_next(app, todo):
client = app.test_client()
has_next = True
page = 1
while has_next:
response = client.get("/", data={"page": page, "per_page": 10})
assert response.status_code == 200
has_next = response.json['has_next']
page += 1