-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_validators.py
109 lines (77 loc) · 3.63 KB
/
test_validators.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
# -*- coding: utf-8 -*-
'''Unit tests for ckan/lib/navl/validators.py.
'''
import copy
import nose.tools
def _data():
'''Return a data dict with some arbitrary data in it, suitable to be passed
to validators for testing.
This is a function that returns a dict (rather than just a dict as a
module-level variable) so that if one test method modifies the dict the
next test method gets a new clean copy.
'''
return {('other key',): 'other value'}
def _errors():
'''Return an errors dict with some arbitrary errors in it, suitable to be
passed to validators for testing.
This is a function that returns a dict (rather than just a dict as a
module-level variable) so that if one test method modifies the dict the
next test method gets a new clean copy.
'''
return {('other key',): ['other error']}
class TestValidators(object):
def test_ignore_missing_with_value_missing(self):
'''ignore_missing() should raise StopOnError if:
- data[key] is None, or
- data[key] is dictization_functions.missing, or
- key is not in data
'''
import ckan.lib.navl.dictization_functions as df
import ckan.lib.navl.validators as validators
for value in (None, df.missing, 'skip'):
# This is the key for the value that is going to be validated.
key = ('key to be validated',)
# The data to pass to the validator function for validation.
data = _data()
if value != 'skip':
data[key] = value
# The errors dict to pass to the validator function.
errors = _errors()
errors[key] = []
# Make copies of the data and errors dicts for asserting later.
original_data = copy.deepcopy(data)
original_errors = copy.deepcopy(errors)
with nose.tools.assert_raises(df.StopOnError):
validators.ignore_missing(key=key, data=data, errors=errors,
context={})
assert key not in data, ('When given a value of {value} '
'ignore_missing() should remove the item from the data '
'dict'.format(value=value))
if key in original_data:
del original_data[key]
assert data == original_data, ('When given a value of {value} '
'ignore_missing() should not modify other items in the '
'data dict'.format(value=value))
assert errors == original_errors, ('When given a value of {value} '
'ignore_missing should not modify the errors dict'.format(
value=value))
def test_ignore_missing_with_a_value(self):
'''If data[key] is neither None or missing, ignore_missing() should do
nothing.
'''
import ckan.lib.navl.validators as validators
key = ('key to be validated',)
data = _data()
data[key] = 'value to be validated'
errors = _errors()
errors[key] = []
# Make copies of the data and errors dicts for asserting later.
original_data = copy.deepcopy(data)
original_errors = copy.deepcopy(errors)
result = validators.ignore_missing(key=key, data=data, errors=errors,
context={})
assert result is None
assert data == original_data, ("ignore_missing() shouldn't modify the "
"data dict")
assert errors == original_errors, ("ignore_missing() shouldn't modify "
"the errors dict")