-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_tags.py
135 lines (96 loc) · 4.49 KB
/
test_tags.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
# encoding: utf-8
import math
import string
from nose.tools import assert_equal, assert_true, assert_false, assert_in
from bs4 import BeautifulSoup
from routes import url_for
import ckan.tests.helpers as helpers
from ckan.tests import factories
webtest_submit = helpers.webtest_submit
submit_and_follow = helpers.submit_and_follow
def _make_tag_list(n=26):
'''Returns a list of tag dicts, starting with 'aa, bb, ..., zz', then
'aaa, bbb, ..., zzz', etc. Tags must be at least 2 characters.'''
lc = string.lowercase
lc_len = len(lc)
return [{'name': lc[i % lc_len] * int(math.ceil(i / lc_len) + 2)}
for i in range(0, n)]
class TestTagIndex(helpers.FunctionalTestBase):
def test_tags_listed_under_50(self):
'''Tag index lists tags under 50 tags.'''
app = self._get_test_app()
expected_tags = _make_tag_list(49)
factories.Dataset(tags=expected_tags)
tag_index_url = url_for(controller='tag', action='index')
tag_response = app.get(tag_index_url)
tag_response_html = BeautifulSoup(tag_response.body)
tags = [t.string for t in tag_response_html.select('.tag')]
expected_tag_values = [t['name'] for t in expected_tags]
assert_equal(len(tags), 49)
for t in expected_tag_values:
assert_true(t in tags)
# no pagination
assert_false(tag_response_html.select('.pagination'))
def test_tags_listed_over_50(self):
'''Tag index lists tags over 50 tags.'''
app = self._get_test_app()
expected_tags = _make_tag_list(51)
factories.Dataset(tags=expected_tags)
tag_index_url = url_for(controller='tag', action='index')
tag_response = app.get(tag_index_url)
tag_response_html = BeautifulSoup(tag_response.body)
tags = [t.string for t in tag_response_html.select('.tag')]
expected_tag_values = [t['name'] for t in expected_tags]
assert_equal(len(tags), 2)
assert_true(expected_tag_values)
for t in [u'aa', u'aaa']:
assert_true(t in tags)
# has pagination
assert_true(tag_response_html.select('.pagination'))
def test_tag_search(self):
'''Tag search returns expected results'''
app = self._get_test_app()
expected_tags = _make_tag_list(50)
expected_tags.append({'name': 'find-me'})
factories.Dataset(tags=expected_tags)
tag_index_url = url_for(controller='tag', action='index')
tag_response = app.get(tag_index_url)
search_form = tag_response.forms[1]
search_form['q'] = 'find-me'
search_response = webtest_submit(search_form, status=200)
search_response_html = BeautifulSoup(search_response.body)
tags = [t.string for t in search_response_html.select('.tag')]
assert_equal(len(tags), 1)
assert_true('find-me' in tags)
# no pagination
assert_false(search_response_html.select('.pagination'))
def test_tag_search_no_results(self):
'''Searching for tags yielding no results'''
app = self._get_test_app()
expected_tags = _make_tag_list(50)
factories.Dataset(tags=expected_tags)
tag_index_url = url_for(controller='tag', action='index')
tag_response = app.get(tag_index_url)
search_form = tag_response.forms[1]
search_form['q'] = 'find-me'
search_response = webtest_submit(search_form, status=200)
search_response_html = BeautifulSoup(search_response.body)
tags = [t.string for t in search_response_html.select('.tag')]
assert_equal(len(tags), 0)
assert_true('find-me' not in tags)
# no pagination
assert_false(search_response_html.select('.pagination'))
class TestTagRead(helpers.FunctionalTestBase):
def test_tag_read_redirects_to_dataset_search(self):
app = self._get_test_app()
factories.Dataset(title='My Other Dataset', tags=[{'name': 'find-me'}])
tag_url = url_for(controller='tag', action='read', id='find-me')
tag_response = app.get(tag_url, status=302)
assert_equal(tag_response.headers['Location'],
'http://test.ckan.net/dataset?tags=find-me')
def test_tag_read_not_found(self):
'''Attempting access to non-existing tag returns a 404'''
app = self._get_test_app()
factories.Dataset(title='My Other Dataset', tags=[{'name': 'find-me'}])
tag_url = url_for(controller='tag', action='read', id='not-here')
app.get(tag_url, status=404)