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
132
|
# Copyright (C) 2013-2014 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
# GNU Mailman is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
"""paginate helper tests."""
from __future__ import absolute_import, print_function, unicode_literals
__metaclass__ = type
__all__ = [
'TestPaginateHelper',
]
import unittest
from falcon import InvalidParam, Request
from mailman.app.lifecycle import create_list
from mailman.database.transaction import transaction
from mailman.rest.helpers import paginate
from mailman.testing.layers import RESTLayer
class _FakeRequest(Request):
def __init__(self, count=None, page=None):
self._params = {}
if count is not None:
self._params['count'] = count
if page is not None:
self._params['page'] = page
class TestPaginateHelper(unittest.TestCase):
"""Test the @paginate decorator."""
layer = RESTLayer
def setUp(self):
with transaction():
self._mlist = create_list('test@example.com')
def test_no_pagination(self):
# When there is no pagination params in the request, all 5 items in
# the collection are returned.
@paginate
def get_collection(self, request):
return ['one', 'two', 'three', 'four', 'five']
# Expect 5 items
page = get_collection(None, _FakeRequest())
self.assertEqual(page, ['one', 'two', 'three', 'four', 'five'])
def test_valid_pagination_request_page_one(self):
# ?count=2&page=1 returns the first page, with two items in it.
@paginate
def get_collection(self, request):
return ['one', 'two', 'three', 'four', 'five']
page = get_collection(None, _FakeRequest(2, 1))
self.assertEqual(page, ['one', 'two'])
def test_valid_pagination_request_page_two(self):
# ?count=2&page=2 returns the second page, where a page has two items
# in it.
@paginate
def get_collection(self, request):
return ['one', 'two', 'three', 'four', 'five']
page = get_collection(None, _FakeRequest(2, 2))
self.assertEqual(page, ['three', 'four'])
def test_2nd_index_larger_than_total(self):
# ?count=2&page=3 returns the third page with page size 2, but the
# last page only has one item in it.
@paginate
def get_collection(self, request):
return ['one', 'two', 'three', 'four', 'five']
page = get_collection(None, _FakeRequest(2, 3))
self.assertEqual(page, ['five'])
def test_out_of_range_returns_empty_list(self):
# ?count=2&page=4 returns the fourth page, which doesn't exist, so an
# empty collection is returned.
@paginate
def get_collection(self, request):
return ['one', 'two', 'three', 'four', 'five']
page = get_collection(None, _FakeRequest(2, 4))
self.assertEqual(page, [])
def test_count_as_string_returns_bad_request(self):
# ?count=two&page=2 are not valid values, so a bad request occurs.
@paginate
def get_collection(self, request):
return []
self.assertRaises(InvalidParam, get_collection,
None, _FakeRequest('two', 1))
def test_negative_count(self):
# ?count=-1&page=1
@paginate
def get_collection(self, request):
return ['one', 'two', 'three', 'four', 'five']
self.assertRaises(InvalidParam, get_collection,
None, _FakeRequest(-1, 1))
def test_negative_page(self):
# ?count=1&page=-1
@paginate
def get_collection(self, request):
return ['one', 'two', 'three', 'four', 'five']
self.assertRaises(InvalidParam, get_collection,
None, _FakeRequest(1, -1))
def test_negative_page_and_count(self):
# ?count=1&page=-1
@paginate
def get_collection(self, request):
return ['one', 'two', 'three', 'four', 'five']
self.assertRaises(InvalidParam, get_collection,
None, _FakeRequest(-1, -1))
|