-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_common.py
208 lines (152 loc) · 5.96 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
# encoding: utf-8
import flask
import pylons
import pytest
from six import text_type
from ckan.common import (
CKANConfig,
config as ckan_config,
request as ckan_request,
g as ckan_g,
c as ckan_c,
)
from ckan.tests import helpers
def test_del_works():
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():
my_conf = CKANConfig()
my_conf[u"test_key_1"] = u"Test value 1"
assert my_conf.get(u"test_key_1") == u"Test value 1"
def test_repr_works():
my_conf = CKANConfig()
my_conf[u"test_key_1"] = u"Test value 1"
assert repr(my_conf) == u"{u'test_key_1': u'Test value 1'}"
def test_len_works():
my_conf = CKANConfig()
my_conf[u"test_key_1"] = u"Test value 1"
my_conf[u"test_key_2"] = u"Test value 2"
assert len(my_conf) == 2
def test_keys_works():
my_conf = CKANConfig()
my_conf[u"test_key_1"] = u"Test value 1"
my_conf[u"test_key_2"] = u"Test value 2"
assert sorted(my_conf.keys()) == [u"test_key_1", u"test_key_2"]
def test_clear_works():
# 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"
assert len(my_conf.keys()) == 2
my_conf.clear()
assert len(my_conf.keys()) == 0
# Restore Pylons config
pylons.config.update(_original_pylons_config)
def test_for_in_works():
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_")
assert cnt == 2
def test_iteritems_works():
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")
assert cnt == 2
def test_not_true_if_empty():
my_conf = CKANConfig()
assert not my_conf
def test_true_if_not_empty():
my_conf = CKANConfig()
my_conf[u"test_key_1"] = u"Test value 1"
assert my_conf
@pytest.mark.ckan_config(u"ckan.site_title", u"Example title")
def test_setting_a_key_sets_it_on_pylons_config():
assert pylons.config[u"ckan.site_title"] == u"Example title"
def test_setting_a_key_sets_it_on_flask_config_if_app_context(
app, monkeypatch
):
with app.flask_app.app_context():
monkeypatch.setitem(ckan_config, u"ckan.site_title", u"Example title")
assert flask.current_app.config[u"ckan.site_title"] == u"Example title"
@pytest.mark.ckan_config(u"ckan.site_title", u"Example title")
def test_setting_a_key_does_not_set_it_on_flask_config_if_outside_app_context(
app
):
with app.flask_app.app_context():
assert flask.current_app.config[u"ckan.site_title"] != u"Example title"
@pytest.mark.ckan_config(u"ckan.site_title", u"Example title")
def test_deleting_a_key_deletes_it_on_pylons_config():
del ckan_config[u"ckan.site_title"]
assert u"ckan.site_title" not in ckan_config
# START-CONFIG-OVERRIDE
def test_deleting_a_key_delets_it_on_flask_config(
app, monkeypatch, ckan_config
):
with app.flask_app.app_context():
monkeypatch.setitem(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
# END-CONFIG-OVERRIDE
@pytest.mark.ckan_config(u"ckan.site_title", u"Example title")
def test_update_works_on_pylons_config():
ckan_config.update(
{u"ckan.site_title": u"Example title 2", u"ckan.new_key": u"test"}
)
assert pylons.config[u"ckan.site_title"] == u"Example title 2"
assert pylons.config[u"ckan.new_key"] == u"test"
def test_update_works_on_flask_config(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"}
)
assert (
flask.current_app.config[u"ckan.site_title"] == u"Example title 2"
)
assert flask.current_app.config[u"ckan.new_key"] == u"test"
def test_config_option_update_action_works_on_pylons(reset_db):
params = {u"ckan.site_title": u"Example title action"}
helpers.call_action(u"config_option_update", {}, **params)
assert pylons.config[u"ckan.site_title"] == u"Example title action"
reset_db()
def test_config_option_update_action_works_on_flask(app, reset_db):
with app.flask_app.app_context():
params = {u"ckan.site_title": u"Example title action"}
helpers.call_action(u"config_option_update", {}, **params)
assert pylons.config[u"ckan.site_title"] == u"Example title action"
reset_db()
def test_params_also_works_on_flask_request(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(app):
with app.flask_app.test_request_context(u"/?a=1"):
with pytest.raises(AttributeError):
getattr(ckan_request, u"not_here")
def test_flask_g_is_used_on_a_flask_request(app):
with app.flask_app.test_request_context():
assert u"flask.g" in text_type(ckan_g)
flask.g.user = u"example"
assert ckan_g.user == u"example"
def test_can_also_use_c_on_a_flask_request(app):
with app.flask_app.test_request_context():
flask.g.user = u"example"
assert ckan_c.user == u"example"
ckan_g.user = u"example2"
assert ckan_c.user == u"example2"
def test_accessing_missing_key_raises_error_on_flask_request(app):
with app.flask_app.test_request_context():
with pytest.raises(AttributeError):
getattr(ckan_g, u"user")