-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_pagination.py
141 lines (113 loc) · 5.79 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
132
133
134
135
136
137
138
139
140
141
# encoding: utf-8
import re
from nose.tools import assert_equal
from ckan.lib.create_test_data import CreateTestData
import ckan.model as model
from ckan.tests.legacy import TestController, url_for, setup_test_search_index
def scrape_search_results(response, object_type):
assert object_type in ('dataset', 'group_dataset', 'group', 'user')
if object_type != 'group_dataset':
results = re.findall('a href="/%s/%s_(\d\d)' % (object_type, object_type),
str(response))
else:
object_type = 'dataset'
results = re.findall('href="/%s/%s_(\d\d)' % (object_type, object_type),
str(response))
return results
def test_scrape_user():
html = '''
<li class="username">
<img src="//gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e?s=16&d=http://test.ckan.net/images/icons/user.png" /> <a href="/user/user_00">user_00</a>
</li>
...
<li class="username">
<img src="//gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e?s=16&d=http://test.ckan.net/images/icons/user.png" /> <a href="/user/user_01">user_01</a>
</li>
'''
res = scrape_search_results(html, 'user')
assert_equal(res, ['00', '01'])
class TestPaginationPackage(TestController):
@classmethod
def setup_class(cls):
setup_test_search_index()
model.repo.init_db()
# no. entities per page is hardcoded into the controllers, so
# create enough of each here so that we can test pagination
cls.num_packages_in_large_group = 51
packages = []
for i in range(cls.num_packages_in_large_group):
packages.append({
# CS: nasty_string ignore
'name': u'dataset_%s' % str(i).zfill(2),
'groups': u'group_00'
})
CreateTestData.create_arbitrary(packages)
@classmethod
def teardown_class(self):
model.repo.rebuild_db()
def test_package_search_p1(self):
res = self.app.get(url_for('dataset.search', q='groups:group_00'))
assert 'href="/dataset/?q=groups%3Agroup_00&page=2"' in res
pkg_numbers = scrape_search_results(res, 'dataset')
assert_equal(['50', '49', '48', '47', '46', '45', '44', '43', '42', '41', '40', '39', '38', '37', '36', '35', '34', '33', '32', '31'], pkg_numbers)
def test_package_search_p2(self):
res = self.app.get(url_for('dataset.search', q='groups:group_00', page=2))
assert 'href="/dataset/?q=groups%3Agroup_00&page=1"' in res
pkg_numbers = scrape_search_results(res, 'dataset')
assert_equal(['30', '29', '28', '27', '26', '25', '24', '23', '22', '21', '20', '19', '18', '17', '16', '15', '14', '13', '12', '11'], pkg_numbers)
def test_group_datasets_read_p1(self):
res = self.app.get(url_for(controller='group', action='read', id='group_00'))
assert 'href="/group/group_00?page=2' in res, res
pkg_numbers = scrape_search_results(res, 'group_dataset')
assert_equal(['50', '49', '48', '47', '46', '45', '44', '43', '42', '41', '40', '39', '38', '37', '36', '35', '34', '33', '32', '31'], pkg_numbers)
def test_group_datasets_read_p2(self):
res = self.app.get(url_for(controller='group', action='read', id='group_00', page=2))
assert 'href="/group/group_00?page=1' in res, res
pkg_numbers = scrape_search_results(res, 'group_dataset')
assert_equal(['30', '29', '28', '27', '26', '25', '24', '23', '22', '21', '20', '19', '18', '17', '16', '15', '14', '13', '12', '11'], pkg_numbers)
class TestPaginationGroup(TestController):
@classmethod
def setup_class(cls):
# no. entities per page is hardcoded into the controllers, so
# create enough of each here so that we can test pagination
cls.num_groups = 22
# CS: nasty_string ignore
groups = [u'group_%s' % str(i).zfill(2) for i in range(0, cls.num_groups)]
CreateTestData.create_arbitrary(
[], extra_group_names=groups
)
@classmethod
def teardown_class(self):
model.repo.rebuild_db()
def test_group_index(self):
res = self.app.get(url_for('group.index'))
assert 'href="/group/?q=&sort=&page=2"' in res, res
grp_numbers = scrape_search_results(res, 'group')
assert_equal(['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19'], grp_numbers)
res = self.app.get(url_for('group.index', page=2))
assert 'href="/group/?q=&sort=&page=1"' in res
grp_numbers = scrape_search_results(res, 'group')
assert_equal(['20', '21'], grp_numbers)
class TestPaginationUsers(TestController):
@classmethod
def setup_class(cls):
# no. entities per page is hardcoded into the controllers, so
# create enough of each here so that we can test pagination
cls.num_users = 21
# CS: nasty_string ignore
users = [u'user_%s' % str(i).zfill(2) for i in range(cls.num_users)]
CreateTestData.create_arbitrary(
[], extra_user_names = users,
)
@classmethod
def teardown_class(self):
model.repo.rebuild_db()
def test_users_index(self):
res = self.app.get(url_for('user.index'))
assert 'href="/user/?q=&order_by=name&page=2"' in res
user_numbers = scrape_search_results(res, 'user')
assert_equal(['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19'], user_numbers)
res = self.app.get(url_for('user.index', page=2))
assert 'href="/user/?q=&order_by=name&page=1"' in res
user_numbers = scrape_search_results(res, 'user')
assert_equal(['20'], user_numbers)