-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_tag_vocab.py
224 lines (194 loc) · 9.61 KB
/
test_tag_vocab.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import json
import paste.fixture
from ckan import model
from ckan.lib.create_test_data import CreateTestData
import ckan.lib.helpers as h
from ckan.tests import WsgiAppCase
# ensure that test_tag_vocab_plugin is added as a plugin in the testing .ini file
from ckanext.test_tag_vocab_plugin import MockVocabTagsPlugin
TEST_VOCAB_NAME = 'test-vocab'
# paste.fixture.Field.Select does not handle multiple selects currently,
# so replace with our own implementations of Form and Select
class Form(paste.fixture.Form):
def __init__(self, response, text):
paste.fixture.Form.__init__(self, response, text)
def submit_fields(self, name=None, index=None):
"""
Return a list of ``[(name, value), ...]`` for the current
state of the form.
"""
submit = []
if name is not None:
field = self.get(name, index=index)
submit.append((field.name, field.value_if_submitted()))
for name, fields in self.fields.items():
if name is None:
continue
for field in fields:
value = field.value
if value is None:
continue
if isinstance(value, list):
for v in value:
submit.append((name, v))
else:
submit.append((name, value))
return submit
class Select(paste.fixture.Field):
def __init__(self, *args, **attrs):
paste.fixture.Field.__init__(self, *args, **attrs)
self.options = []
self.selectedIndex = None
def value__set(self, value):
if not value:
self.selectedIndex = None
self.options = [(option, False) for (option, checked) in self.options]
return
for v in value:
if not v in [option for (option, checked) in self.options]:
raise ValueError("Option %r not found (from %s)"
% (value, ', '.join(
[repr(o) for o, checked in self.options]))
)
new_options = [(option, True) for (option, checked) in self.options if option in value]
new_options += [(option, False) for (option, checked) in self.options if not option in value]
self.options = new_options
def value__get(self):
return [option for (option, checked) in self.options if checked]
value = property(value__get, value__set)
class TestWUI(WsgiAppCase):
@classmethod
def setup_class(cls):
MockVocabTagsPlugin().set_active(True)
CreateTestData.create(package_type='mock_vocab_tags_plugin')
cls.sysadmin_user = model.User.get('testsysadmin')
cls.dset = model.Package.get('warandpeace')
cls.tag1_name = 'vocab-tag-1'
cls.tag2_name = 'vocab-tag-2'
# use our custom select class for this test suite
cls.old_select = paste.fixture.Field.classes['select']
paste.fixture.Field.classes['select'] = Select
# create a test vocab
params = json.dumps({'name': TEST_VOCAB_NAME})
extra_environ = {'Authorization' : str(cls.sysadmin_user.apikey)}
response = cls.app.post('/api/action/vocabulary_create', params=params,
extra_environ=extra_environ)
assert json.loads(response.body)['success']
vocab_id = json.loads(response.body)['result']['id']
# add tags to the vocab
extra_environ = {'Authorization' : str(cls.sysadmin_user.apikey)}
params = json.dumps({'name': cls.tag1_name, 'vocabulary_id': vocab_id})
response = cls.app.post('/api/action/tag_create', params=params,
extra_environ=extra_environ)
assert json.loads(response.body)['success']
params = json.dumps({'name': cls.tag2_name, 'vocabulary_id': vocab_id})
response = cls.app.post('/api/action/tag_create', params=params,
extra_environ=extra_environ)
assert json.loads(response.body)['success']
@classmethod
def teardown_class(cls):
MockVocabTagsPlugin().set_active(False)
paste.fixture.Field.classes['select'] = cls.old_select
model.repo.rebuild_db()
def _get_vocab_id(self, vocab_name):
params = json.dumps({'id': vocab_name})
response = self.app.post('/api/action/vocabulary_show', params=params)
assert json.loads(response.body)['success']
return json.loads(response.body)['result']['id']
def _add_vocab_tag_to_dataset(self, dataset_id, vocab_id, tag_name):
params = json.dumps({'id': dataset_id})
response = self.app.post('/api/action/package_show', params=params)
dataset = json.loads(response.body)['result']
dataset['tags'].append({'name': tag_name, 'vocabulary_id': vocab_id})
params = json.dumps(dataset)
response = self.app.post('/api/action/package_update', params=params,
extra_environ={'Authorization': str(self.sysadmin_user.apikey)})
assert json.loads(response.body)['success']
def _remove_vocab_tags(self, dataset_id, vocab_id, tag_name):
params = json.dumps({'id': dataset_id})
response = self.app.post('/api/action/package_show', params=params)
dataset = json.loads(response.body)['result']
dataset['tags'] = [t for t in dataset['tags'] if not t['name'] == tag_name]
params = json.dumps(dataset)
response = self.app.post('/api/action/package_update', params=params,
extra_environ={'Authorization': str(self.sysadmin_user.apikey)})
assert json.loads(response.body)['success']
def test_01_dataset_view(self):
vocab_id = self._get_vocab_id(TEST_VOCAB_NAME)
self._add_vocab_tag_to_dataset(self.dset.id, vocab_id, self.tag1_name)
response = self.app.get(h.url_for(controller='package', action='read',
id=self.dset.id))
assert self.tag1_name in response.body
self._remove_vocab_tags(self.dset.id, vocab_id, self.tag1_name)
def test_02_dataset_edit_add_vocab_tag(self):
vocab_id = self._get_vocab_id(TEST_VOCAB_NAME)
url = h.url_for(controller='package', action='edit', id=self.dset.id)
response = self.app.get(url)
fv = response.forms['dataset-edit']
fv = Form(fv.response, fv.text)
fv['vocab_tags'] = [self.tag2_name]
response = fv.submit('save')
response = response.follow()
assert not self.tag1_name in response.body
assert self.tag2_name in response.body
self._remove_vocab_tags(self.dset.id, vocab_id, self.tag1_name)
self._remove_vocab_tags(self.dset.id, vocab_id, self.tag2_name)
def test_02_dataset_edit_add_free_and_vocab_tags_then_edit_again(self):
vocab_id = self._get_vocab_id(TEST_VOCAB_NAME)
url = h.url_for(controller='package', action='edit', id=self.dset.id)
response = self.app.get(url)
fv = response.forms['dataset-edit']
fv = Form(fv.response, fv.text)
# Add a free tag with a space in its name.
fv['tag_string'] = 'water quality'
# Add a vocab tag.
fv['vocab_tags'] = [self.tag2_name]
# Save the dataset and visit the page again
response = fv.submit('save')
response = response.follow()
assert not self.tag1_name in response.body
assert self.tag2_name in response.body
url = h.url_for(controller='package', action='edit', id=self.dset.id)
response = self.app.get(url)
fv = response.forms['dataset-edit']
fv = Form(fv.response, fv.text)
assert fv['vocab_tags'].value == [self.tag2_name], fv['vocab_tags'].value
self._remove_vocab_tags(self.dset.id, vocab_id, self.tag2_name)
def test_03_dataset_edit_remove_vocab_tag(self):
vocab_id = self._get_vocab_id(TEST_VOCAB_NAME)
self._add_vocab_tag_to_dataset(self.dset.id, vocab_id, self.tag1_name)
url = h.url_for(controller='package', action='edit', id=self.dset.id)
response = self.app.get(url)
fv = response.forms['dataset-edit']
fv = Form(fv.response, fv.text)
fv['vocab_tags'] = []
response = fv.submit('save')
response = response.follow()
assert not self.tag1_name in response.body
self._remove_vocab_tags(self.dset.id, vocab_id, self.tag1_name)
def test_04_dataset_edit_change_vocab_tag(self):
vocab_id = self._get_vocab_id(TEST_VOCAB_NAME)
self._add_vocab_tag_to_dataset(self.dset.id, vocab_id, self.tag1_name)
url = h.url_for(controller='package', action='edit', id=self.dset.id)
response = self.app.get(url)
fv = response.forms['dataset-edit']
fv = Form(fv.response, fv.text)
fv['vocab_tags'] = [self.tag2_name]
response = fv.submit('save')
response = response.follow()
assert not self.tag1_name in response.body
assert self.tag2_name in response.body
self._remove_vocab_tags(self.dset.id, vocab_id, self.tag2_name)
def test_05_dataset_edit_add_multiple_vocab_tags(self):
vocab_id = self._get_vocab_id(TEST_VOCAB_NAME)
url = h.url_for(controller='package', action='edit', id=self.dset.id)
response = self.app.get(url)
fv = response.forms['dataset-edit']
fv = Form(fv.response, fv.text)
fv['vocab_tags'] = [self.tag1_name, self.tag2_name]
response = fv.submit('save')
response = response.follow()
assert self.tag1_name in response.body
assert self.tag2_name in response.body
self._remove_vocab_tags(self.dset.id, vocab_id, self.tag1_name)
self._remove_vocab_tags(self.dset.id, vocab_id, self.tag2_name)