forked from python-jsonschema/jsonschema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_types.py
189 lines (137 loc) · 5.74 KB
/
test_types.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
"""
Tests on the new type interface. The actual correctness of the type checking
is handled in test_jsonschema_test_suite; these tests check that TypeChecker
functions correctly and can facilitate extensions to type checking
"""
from collections import namedtuple
from unittest import TestCase
from jsonschema import _types, ValidationError, _validators
from jsonschema.exceptions import UndefinedTypeCheck
from jsonschema.validators import Draft6Validator, extend
def is_int_or_string_int(instance):
if Draft6Validator.TYPE_CHECKER.is_type(instance, "integer"):
return True
if Draft6Validator.TYPE_CHECKER.is_type(instance, "string"):
try:
int(instance)
return True
except ValueError:
pass
return False
def is_namedtuple(instance):
if isinstance(instance, tuple) and getattr(instance, '_fields',
None):
return True
return False
def is_object_or_named_tuple(instance):
if Draft6Validator.TYPE_CHECKER.is_type(instance, "object"):
return True
if is_namedtuple(instance):
return True
return False
def coerce_named_tuple(fn):
def coerced(validator, required, instance, schema):
if is_namedtuple(instance):
instance = instance._asdict()
return fn(validator, required, instance, schema)
return coerced
required = coerce_named_tuple(_validators.required)
properties = coerce_named_tuple(_validators.properties)
class TestTypeChecker(TestCase):
def test_initialised_empty(self):
tc = _types.TypeChecker()
self.assertEqual(len(tc.type_checkers), 0)
def test_checks_can_be_added(self):
tc = _types.TypeChecker()
tc = tc.redefine("integer", _types.is_integer)
self.assertEqual(len(tc.type_checkers), 1)
def test_added_checks_are_accessible(self):
tc = _types.TypeChecker()
tc = tc.redefine("integer", _types.is_integer)
self.assertTrue(tc.is_type(4, "integer"))
self.assertFalse(tc.is_type(4.4, "integer"))
def test_checks_can_be_redefined(self):
tc = _types.TypeChecker()
tc = tc.redefine("integer", _types.is_integer)
self.assertEqual(tc.type_checkers["integer"], _types.is_integer)
tc = tc.redefine("integer", _types.is_string)
self.assertEqual(tc.type_checkers["integer"], _types.is_string)
def test_checks_can_be_removed(self):
tc = _types.TypeChecker()
tc = tc.redefine("integer", _types.is_integer)
tc = tc.remove("integer")
with self.assertRaises(UndefinedTypeCheck):
tc.is_type(4, "integer")
def test_changes_do_not_affect_original(self):
tc = _types.TypeChecker()
tc2 = tc.redefine("integer", _types.is_integer)
self.assertEqual(len(tc.type_checkers), 0)
tc3 = tc2.remove("integer")
self.assertEqual(len(tc2.type_checkers), 1)
def test_many_checks_can_be_added(self):
tc = _types.TypeChecker()
tc = tc.redefine_many({
"integer": _types.is_integer,
"string": _types.is_string
})
self.assertEqual(len(tc.type_checkers), 2)
def test_many_checks_can_be_removed(self):
tc = _types.TypeChecker()
tc = tc.redefine_many({
"integer": _types.is_integer,
"string": _types.is_string
})
tc = tc.remove_many(("integer", "string"))
self.assertEqual(len(tc.type_checkers), 0)
class TestCustomTypes(TestCase):
def test_simple_type_can_be_extended(self):
schema = {'type': 'integer'}
type_checker = Draft6Validator.TYPE_CHECKER.redefine(
"integer", is_int_or_string_int
)
CustomValidator = extend(Draft6Validator, type_checker=type_checker)
v = CustomValidator(schema)
v.validate(4)
v.validate('4')
with self.assertRaises(ValidationError):
v.validate(4.4)
def test_object_can_be_extended(self):
schema = {'type': 'object'}
Point = namedtuple('Point', ['x', 'y'])
type_checker = Draft6Validator.TYPE_CHECKER.redefine(
u"object", is_object_or_named_tuple
)
CustomValidator = extend(Draft6Validator, type_checker=type_checker)
v = CustomValidator(schema)
v.validate(Point(x=4, y=5))
def test_object_extensions_require_custom_validators(self):
schema = {'type': 'object', 'required': ['x']}
type_checker = Draft6Validator.TYPE_CHECKER.redefine(
u"object", is_object_or_named_tuple
)
CustomValidator = extend(Draft6Validator, type_checker=type_checker)
v = CustomValidator(schema)
Point = namedtuple('Point', ['x', 'y'])
# Cannot handle required
with self.assertRaises(ValidationError):
v.validate(Point(x=4, y=5))
def test_object_extensions_can_handle_custom_validators(self):
schema = {'type': 'object',
'required': ['x'],
'properties': {'x':
{'type': 'integer'}
}
}
type_checker = Draft6Validator.TYPE_CHECKER.redefine(
u"object", is_object_or_named_tuple
)
CustomValidator = extend(Draft6Validator,
type_checker=type_checker,
validators={"required": required,
'properties': properties})
v = CustomValidator(schema)
Point = namedtuple('Point', ['x', 'y'])
# Can now process required and properties
v.validate(Point(x=4, y=5))
with self.assertRaises(ValidationError):
v.validate(Point(x="not an integer", y=5))