-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_dictization_schema.py
215 lines (162 loc) · 8.17 KB
/
test_dictization_schema.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
from nose.tools import assert_equal
from pprint import pprint, pformat
from difflib import unified_diff
from ckan.lib.create_test_data import CreateTestData
from ckan import model
from ckan.lib.dictization import (table_dictize,
table_dict_save)
from ckan.lib.dictization.model_dictize import (package_dictize,
group_dictize
)
from ckan.lib.dictization.model_save import package_dict_save
from ckan.logic.schema import default_package_schema, default_group_schema, \
default_tags_schema
from ckan.lib.navl.dictization_functions import validate
class TestBasicDictize:
def setup(self):
self.context = {'model': model,
'session': model.Session}
@classmethod
def setup_class(cls):
CreateTestData.create()
@classmethod
def teardown_class(cls):
model.repo.rebuild_db()
model.Session.remove()
def remove_changable_columns(self, dict):
for key, value in dict.items():
if key.endswith('id') and key <> 'license_id':
dict.pop(key)
if isinstance(value, list):
for new_dict in value:
self.remove_changable_columns(new_dict)
return dict
def remove_revision_id(self, dict):
for key, value in dict.items():
if key == 'revision_id':
dict.pop(key)
if isinstance(value, list):
for new_dict in value:
self.remove_revision_id(new_dict)
return dict
def test_1_package_schema(self):
pkg = model.Session.query(model.Package).filter_by(name='annakarenina').first()
package_id = pkg.id
result = package_dictize(pkg, self.context)
self.remove_changable_columns(result)
pprint(result)
result['name'] = 'anna2'
converted_data, errors = validate(result, default_package_schema(), self.context)
pprint(errors)
assert converted_data == {'extras': [{'key': u'genre', 'value': u'"romantic novel"'},
{'key': u'original media', 'value': u'"book"'}],
'groups': [{'name': u'david'}, {'name': u'roger'}],
'license_id': u'other-open',
'name': u'anna2',
'notes': u'Some test notes\n\n### A 3rd level heading\n\n**Some bolded text.**\n\n*Some italicized text.*\n\nForeign characters:\nu with umlaut \xfc\n66-style quote \u201c\nforeign word: th\xfcmb\n \nNeeds escaping:\nleft arrow <\n\n<http://ckan.net/>\n\n',
'resources': [{'alt_url': u'alt123',
'description': u'Full text. Needs escaping: " Umlaut: \xfc',
'format': u'plain text',
'hash': u'abc123',
'size_extra': u'123',
'url': u'http://www.annakarenina.com/download/x=1&y=2'},
{'alt_url': u'alt345',
'description': u'Index of the novel',
'format': u'json',
'hash': u'def456',
'size_extra': u'345',
'url': u'http://www.annakarenina.com/index.json'}],
'tags': [{'name': u'Flexible \u0489!'},
{'name': u'russian'},
{'name': u'tolstoy'}],
'title': u'A Novel By Tolstoy',
'url': u'http://www.annakarenina.com',
'version': u'0.7a'}, pformat(converted_data)
assert not errors, errors
data = converted_data
data['name'] = u'annakarenina'
data.pop("title")
data["resources"][0]["url"] = 'fsdfafasfsaf'
data["resources"][1].pop("url")
converted_data, errors = validate(data, default_package_schema(), self.context)
assert errors == {
'name': [u'That URL is already in use.'],
#'resources': [{}
# {'name': [u'That URL is already in use.']}]
}, pformat(errors)
data["id"] = package_id
converted_data, errors = validate(data, default_package_schema(), self.context)
assert errors == {
#'resources': [{}, {'url': [u'Missing value']}]
}, pformat(errors)
data['name'] = '????jfaiofjioafjij'
converted_data, errors = validate(data, default_package_schema(), self.context)
assert errors == {
'name': [u'Url must be purely lowercase alphanumeric (ascii) characters and these symbols: -_'],
#'resources': [{}, {'url': [u'Missing value']}]
},pformat(errors)
def test_2_group_schema(self):
group = model.Session.query(model.Group).first()
data = group_dictize(group, self.context)
converted_data, errors = validate(data, default_group_schema(), self.context)
group_pack = sorted(group.packages, key=lambda x:x.id)
converted_data["packages"] = sorted(converted_data["packages"], key=lambda x:x["id"])
expected = {'description': u'These are books that David likes.',
'id': group.id,
'name': u'david',
'packages': sorted([{'id': group_pack[0].id},
{'id': group_pack[1].id,
}], key=lambda x:x["id"]),
'title': u"Dave's books"}
assert not errors
assert converted_data == expected, pformat(converted_data) + '\n\n' + pformat(expected)
data["packages"].sort(key=lambda x:x["id"])
data["packages"][0]["id"] = 'fjdlksajfalsf'
data["packages"][1].pop("id")
data["packages"][1].pop("name")
converted_data, errors = validate(data, default_group_schema(), self.context)
assert errors == {'packages': [{'id': [u'Dataset was not found.']}, {'id': [u'Missing value']}]} , pformat(errors)
def test_3_tag_schema_allows_spaces(self):
"""Asserts that a tag name with space is valid"""
ignored = ""
data = {
'name': u'with space',
'revision_timestamp': ignored,
'state': ignored
}
_, errors = validate(data, default_tags_schema(), self.context)
assert not errors, str(errors)
def test_4_tag_schema_allows_punctuation(self):
"""Asserts that a tag name with punctuation (except commas) is valid"""
ignored = ""
data = {
'name': u'.?!<>\\/%^&*()-_+=~#\'@`',
'revision_timestamp': ignored,
'state': ignored
}
_, errors = validate(data, default_tags_schema(), self.context)
assert not errors, str(errors)
def test_5_tag_schema_allows_capital_letters(self):
"""Asserts that tag names can have capital letters"""
ignored = ""
data = {
'name': u'CAPITALS',
'revision_timestamp': ignored,
'state': ignored
}
_, errors = validate(data, default_tags_schema(), self.context)
assert not errors, str(errors)
def test_6_tag_schema_does_not_allow_commas(self):
"""Asserts that a tag name cannot contain commas"""
ignored = ""
data = {
'name': u'eats, shoots, and leaves',
'revision_timestamp': ignored,
'state': ignored
}
_, errors = validate(data, default_tags_schema(), self.context)
assert errors, pprint(errors)
assert 'name' in errors
error_message = errors['name'][0]
assert data['name'] in error_message
assert "must not contain commas" in error_message