-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_common.py
273 lines (159 loc) · 7.05 KB
/
test_common.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# encoding: utf-8
import flask
import pylons
from nose.tools import eq_, assert_not_equal as neq_, assert_raises
from six import text_type
from ckan.tests import helpers
from ckan.common import (CKANConfig, config as ckan_config,
request as ckan_request, g as ckan_g, c as ckan_c)
class TestConfigObject(object):
def test_del_works(self):
my_conf = CKANConfig()
my_conf[u'test_key_1'] = u'Test value 1'
del my_conf[u'test_key_1']
assert u'test_key_1' not in my_conf
def test_get_item_works(self):
my_conf = CKANConfig()
my_conf[u'test_key_1'] = u'Test value 1'
eq_(my_conf.get(u'test_key_1'), u'Test value 1')
def test_repr_works(self):
my_conf = CKANConfig()
my_conf[u'test_key_1'] = u'Test value 1'
eq_(repr(my_conf), u"{u'test_key_1': u'Test value 1'}")
def test_len_works(self):
my_conf = CKANConfig()
my_conf[u'test_key_1'] = u'Test value 1'
my_conf[u'test_key_2'] = u'Test value 2'
eq_(len(my_conf), 2)
def test_keys_works(self):
my_conf = CKANConfig()
my_conf[u'test_key_1'] = u'Test value 1'
my_conf[u'test_key_2'] = u'Test value 2'
eq_(sorted(my_conf.keys()), [u'test_key_1', u'test_key_2'])
def test_clear_works(self):
# Keep a copy of the original Pylons config
_original_pylons_config = pylons.config.copy()
my_conf = CKANConfig()
my_conf[u'test_key_1'] = u'Test value 1'
my_conf[u'test_key_2'] = u'Test value 2'
eq_(len(my_conf.keys()), 2)
my_conf.clear()
eq_(len(my_conf.keys()), 0)
# Restore Pylons config
pylons.config.update(_original_pylons_config)
def test_for_in_works(self):
my_conf = CKANConfig()
my_conf[u'test_key_1'] = u'Test value 1'
my_conf[u'test_key_2'] = u'Test value 2'
cnt = 0
for key in my_conf:
cnt += 1
assert key.startswith(u'test_key_')
eq_(cnt, 2)
def test_iteritems_works(self):
my_conf = CKANConfig()
my_conf[u'test_key_1'] = u'Test value 1'
my_conf[u'test_key_2'] = u'Test value 2'
cnt = 0
for key, value in my_conf.iteritems():
cnt += 1
assert key.startswith(u'test_key_')
assert value.startswith(u'Test value')
eq_(cnt, 2)
def test_not_true_if_empty(self):
my_conf = CKANConfig()
assert not my_conf
def test_true_if_not_empty(self):
my_conf = CKANConfig()
my_conf[u'test_key_1'] = u'Test value 1'
assert my_conf
class TestCommonConfig(object):
def setup(self):
self._original_config = ckan_config.copy()
def teardown(self):
ckan_config.clear()
ckan_config.update(self._original_config)
@classmethod
def teardown_class(cls):
helpers.reset_db()
def test_setting_a_key_sets_it_on_pylons_config(self):
ckan_config[u'ckan.site_title'] = u'Example title'
eq_(pylons.config[u'ckan.site_title'], u'Example title')
def test_setting_a_key_sets_it_on_flask_config_if_app_context(self):
app = helpers._get_test_app()
with app.flask_app.app_context():
ckan_config[u'ckan.site_title'] = u'Example title'
eq_(flask.current_app.config[u'ckan.site_title'], u'Example title')
def test_setting_a_key_does_not_set_it_on_flask_config_if_outside_app_context(self):
ckan_config[u'ckan.site_title'] = u'Example title'
app = helpers._get_test_app()
with app.flask_app.app_context():
neq_(flask.current_app.config[u'ckan.site_title'], u'Example title')
def test_deleting_a_key_deletes_it_on_pylons_config(self):
ckan_config[u'ckan.site_title'] = u'Example title'
del ckan_config[u'ckan.site_title']
assert u'ckan.site_title' not in ckan_config
def test_deleting_a_key_delets_it_on_flask_config(self):
app = helpers._get_test_app()
with app.flask_app.app_context():
ckan_config[u'ckan.site_title'] = u'Example title'
del ckan_config[u'ckan.site_title']
assert u'ckan.site_title' not in flask.current_app.config
def test_update_works_on_pylons_config(self):
ckan_config[u'ckan.site_title'] = u'Example title'
ckan_config.update({
u'ckan.site_title': u'Example title 2',
u'ckan.new_key': u'test'})
eq_(pylons.config[u'ckan.site_title'], u'Example title 2')
eq_(pylons.config[u'ckan.new_key'], u'test')
def test_update_works_on_flask_config(self):
app = helpers._get_test_app()
with app.flask_app.app_context():
ckan_config[u'ckan.site_title'] = u'Example title'
ckan_config.update({
u'ckan.site_title': u'Example title 2',
u'ckan.new_key': u'test'})
eq_(flask.current_app.config[u'ckan.site_title'], u'Example title 2')
eq_(flask.current_app.config[u'ckan.new_key'], u'test')
def test_config_option_update_action_works_on_pylons(self):
params = {
u'ckan.site_title': u'Example title action',
}
helpers.call_action(u'config_option_update', {}, **params)
eq_(pylons.config[u'ckan.site_title'], u'Example title action')
def test_config_option_update_action_works_on_flask(self):
app = helpers._get_test_app()
with app.flask_app.app_context():
params = {
u'ckan.site_title': u'Example title action',
}
helpers.call_action(u'config_option_update', {}, **params)
eq_(pylons.config[u'ckan.site_title'], u'Example title action')
class TestCommonRequest(object):
def test_params_also_works_on_flask_request(self):
app = helpers._get_test_app()
with app.flask_app.test_request_context(u'/?a=1'):
assert u'a' in ckan_request.args
assert u'a' in ckan_request.params
def test_other_missing_attributes_raise_attributeerror_exceptions(self):
app = helpers._get_test_app()
with app.flask_app.test_request_context(u'/?a=1'):
assert_raises(AttributeError, getattr, ckan_request, u'not_here')
class TestCommonG(object):
def test_flask_g_is_used_on_a_flask_request(self):
app = helpers._get_test_app()
with app.flask_app.test_request_context():
assert u'flask.g' in text_type(ckan_g)
flask.g.user = u'example'
eq_(ckan_g.user, u'example')
def test_can_also_use_c_on_a_flask_request(self):
app = helpers._get_test_app()
with app.flask_app.test_request_context():
flask.g.user = u'example'
eq_(ckan_c.user, u'example')
ckan_g.user = u'example2'
eq_(ckan_c.user, u'example2')
def test_accessing_missing_key_raises_error_on_flask_request(self):
app = helpers._get_test_app()
with app.flask_app.test_request_context():
assert_raises(AttributeError, getattr, ckan_g, u'user')