/
test_example_idatasetform.py
248 lines (204 loc) · 8.68 KB
/
test_example_idatasetform.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import nose.tools as nt
import pylons.config as config
import ckan.model as model
import ckan.plugins as plugins
import ckan.new_tests.helpers as helpers
import ckanext.example_idatasetform as idf
import ckan.lib.search
class ExampleIDatasetFormPluginBase(object):
'''Version 1, 2 and 3 of the plugin are basically the same, so this class
provides the tests that all three versions of the plugins will run'''
@classmethod
def setup_class(cls):
cls.original_config = config.copy()
def teardown(self):
model.repo.rebuild_db()
ckan.lib.search.clear()
@classmethod
def teardown_class(cls):
helpers.reset_db()
model.repo.rebuild_db()
ckan.lib.search.clear()
config.clear()
config.update(cls.original_config)
def test_package_create(self):
result = helpers.call_action('package_create', name='test_package',
custom_text='this is my custom text')
nt.assert_equals('this is my custom text', result['custom_text'])
def test_package_update(self):
helpers.call_action('package_create', name='test_package',
custom_text='this is my custom text')
result = helpers.call_action('package_update', name='test_package',
custom_text='this is my updated text')
nt.assert_equals('this is my updated text', result['custom_text'])
def test_package_show(self):
helpers.call_action('package_create', name='test_package',
custom_text='this is my custom text')
result = helpers.call_action('package_show', name_or_id='test_package')
nt.assert_equals('this is my custom text', result['custom_text'])
class TestVersion1(ExampleIDatasetFormPluginBase):
@classmethod
def setup_class(cls):
super(TestVersion1, cls).setup_class()
plugins.load('example_idatasetform_v1')
@classmethod
def teardown_class(cls):
plugins.unload('example_idatasetform_v1')
super(TestVersion1, cls).teardown_class()
class TestVersion2(ExampleIDatasetFormPluginBase):
@classmethod
def setup_class(cls):
super(TestVersion2, cls).setup_class()
plugins.load('example_idatasetform_v2')
@classmethod
def teardown_class(cls):
plugins.unload('example_idatasetform_v2')
super(TestVersion2, cls).teardown_class()
class TestVersion3(ExampleIDatasetFormPluginBase):
@classmethod
def setup_class(cls):
super(TestVersion3, cls).setup_class()
plugins.load('example_idatasetform_v3')
@classmethod
def teardown_class(cls):
plugins.unload('example_idatasetform_v3')
super(TestVersion3, cls).teardown_class()
class TestIDatasetFormPluginVersion4(object):
@classmethod
def setup_class(cls):
cls.original_config = config.copy()
plugins.load('example_idatasetform_v4')
def teardown(self):
model.repo.rebuild_db()
@classmethod
def teardown_class(cls):
plugins.unload('example_idatasetform_v4')
helpers.reset_db()
ckan.lib.search.clear()
config.clear()
config.update(cls.original_config)
def test_package_create(self):
idf.plugin_v4.create_country_codes()
result = helpers.call_action('package_create', name='test_package',
custom_text='this is my custom text',
country_code='uk')
nt.assert_equals('this is my custom text', result['custom_text'])
nt.assert_equals([u'uk'], result['country_code'])
def test_package_create_wrong_country_code(self):
idf.plugin_v4.create_country_codes()
nt.assert_raises(plugins.toolkit.ValidationError,
helpers.call_action,
'package_create',
name='test_package',
custom_text='this is my custom text',
country_code='notcode')
def test_package_update(self):
idf.plugin_v4.create_country_codes()
helpers.call_action('package_create', name='test_package',
custom_text='this is my custom text',
country_code='uk')
result = helpers.call_action('package_update', name='test_package',
custom_text='this is my updated text',
country_code='ie')
nt.assert_equals('this is my updated text', result['custom_text'])
nt.assert_equals([u'ie'], result['country_code'])
class TestIDatasetFormPlugin(object):
@classmethod
def setup_class(cls):
cls.original_config = config.copy()
plugins.load('example_idatasetform')
def teardown(self):
model.repo.rebuild_db()
ckan.lib.search.clear()
@classmethod
def teardown_class(cls):
plugins.unload('example_idatasetform')
helpers.reset_db()
ckan.lib.search.clear()
config.clear()
config.update(cls.original_config)
def test_package_create(self):
idf.plugin.create_country_codes()
result = helpers.call_action(
'package_create', name='test_package',
custom_text='this is my custom text', country_code='uk',
resources=[{
'url': 'http://test.com/',
'custom_resource_text': 'my custom resource',
}])
nt.assert_equals('my custom resource',
result['resources'][0]['custom_resource_text'])
def test_package_update(self):
idf.plugin.create_country_codes()
helpers.call_action(
'package_create', name='test_package',
custom_text='this is my custom text', country_code='uk',
resources=[{
'url': 'http://test.com/',
'custom_resource_text': 'my custom resource',
}])
result = helpers.call_action(
'package_update',
name='test_package',
custom_text='this is my updated text',
country_code='ie',
resources=[{
'url': 'http://test.com/',
'custom_resource_text': 'updated custom resource',
}]
)
nt.assert_equals('this is my updated text', result['custom_text'])
nt.assert_equals([u'ie'], result['country_code'])
nt.assert_equals('updated custom resource',
result['resources'][0]['custom_resource_text'])
def test_package_show(self):
idf.plugin.create_country_codes()
helpers.call_action(
'package_create', name='test_package',
custom_text='this is my custom text', country_code='uk',
resources=[{
'url': 'http://test.com/',
'custom_resource_text': 'my custom resource',
}]
)
result = helpers.call_action('package_show', name_or_id='test_package')
nt.assert_equals('my custom resource',
result['resources'][0]['custom_resource_text'])
nt.assert_equals('my custom resource',
result['resources'][0]['custom_resource_text'])
class TestCustomSearch(object):
@classmethod
def setup_class(cls):
cls.original_config = config.copy()
cls.app = helpers._get_test_app()
plugins.load('example_idatasetform')
def teardown(self):
model.repo.rebuild_db()
ckan.lib.search.clear()
@classmethod
def teardown_class(cls):
plugins.unload('example_idatasetform')
helpers.reset_db()
ckan.lib.search.clear()
config.clear()
config.update(cls.original_config)
def test_custom_search(self):
helpers.call_action('package_create', name='test_package_a',
custom_text='z')
helpers.call_action('package_create', name='test_package_b',
custom_text='y')
response = self.app.get('/dataset')
# change the sort by form to our custom_text ascending
response.forms[1].fields['sort'][0].value = 'custom_text asc'
response = response.forms[1].submit()
# check that package_b appears before package a (y < z)
a = response.body.index('test_package_a')
b = response.body.index('test_package_b')
nt.assert_true(b < a)
response.forms[1].fields['sort'][0].value = 'custom_text desc'
# check that package_a appears before package b (z is first in
# descending order)
response = response.forms[1].submit()
a = response.body.index('test_package_a')
b = response.body.index('test_package_b')
nt.assert_true(a < b)