/
test_dictization_functions.py
92 lines (60 loc) · 2.47 KB
/
test_dictization_functions.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
# encoding: utf-8
import nose
from ckan.lib.navl.dictization_functions import validate, Invalid
eq_ = nose.tools.eq_
class TestValidate(object):
def test_validate_passes_a_copy_of_the_context_to_validators(self):
# We need to pass some keys on the context, otherwise validate
# will do context = context || {}, which creates a new one, defeating
# the purpose of this test
context = {'foo': 'bar'}
def my_validator(key, data, errors, context_in_validator):
assert not id(context) == id(context_in_validator)
data_dict = {
'my_field': 'test',
}
schema = {
'my_field': [my_validator],
}
data, errors = validate(data_dict, schema, context)
def test_validate_adds_schema_keys_to_context(self):
def my_validator(key, data, errors, context):
assert 'schema_keys' in context
eq_(context['schema_keys'], ['my_field'])
data_dict = {
'my_field': 'test',
}
schema = {
'my_field': [my_validator],
}
context = {}
data, errors = validate(data_dict, schema, context)
class TestDictizationError(object):
def test_str_error(self):
err_obj = Invalid('Some ascii error')
eq_(str(err_obj), "Invalid: 'Some ascii error'")
def test_unicode_error(self):
err_obj = Invalid('Some ascii error')
eq_(unicode(err_obj), u"Invalid: 'Some ascii error'")
def test_repr_error(self):
err_obj = Invalid('Some ascii error')
eq_(repr(err_obj), "<Invalid 'Some ascii error'>")
# Error msgs should be ascii, but let's just see what happens for unicode
def test_str_unicode_error(self):
err_obj = Invalid(u'Some unicode \xa3 error')
eq_(str(err_obj), "Invalid: u'Some unicode \\xa3 error'")
def test_unicode_unicode_error(self):
err_obj = Invalid(u'Some unicode \xa3 error')
eq_(unicode(err_obj), "Invalid: u'Some unicode \\xa3 error'")
def test_repr_unicode_error(self):
err_obj = Invalid(u'Some unicode \xa3 error')
eq_(repr(err_obj), "<Invalid u'Some unicode \\xa3 error'>")
def test_str_blank(self):
err_obj = Invalid('')
eq_(str(err_obj), "Invalid")
def test_unicode_blank(self):
err_obj = Invalid('')
eq_(unicode(err_obj), u"Invalid")
def test_repr_blank(self):
err_obj = Invalid('')
eq_(repr(err_obj), "<Invalid>")