|
1
|
|
|
# |
|
2
|
|
|
# Copyright (C) 2015 Satoru SATOH <ssato at redhat.com> |
|
3
|
|
|
# License: MIT |
|
4
|
|
|
# |
|
5
|
|
|
# pylint: disable=missing-docstring, invalid-name, protected-access |
|
6
|
|
|
# pylint: disable=bare-except |
|
7
|
|
|
from __future__ import absolute_import, print_function |
|
8
|
|
|
|
|
9
|
|
|
import unittest |
|
10
|
|
|
import anyconfig.schema as TT |
|
11
|
|
|
|
|
12
|
|
|
from tests.common import dicts_equal |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class Test_00_Base(unittest.TestCase): |
|
16
|
|
|
|
|
17
|
|
|
obj = {'a': 1} |
|
18
|
|
|
schema = {"type": "object", |
|
19
|
|
|
"properties": {"a": {"type": "integer"}}} |
|
20
|
|
|
|
|
21
|
|
|
obj2 = dict(a=1, b=[1, 2], c=dict(d="aaa", e=0.1)) |
|
22
|
|
|
ref_scm = {'properties': {'a': {'type': 'integer'}, |
|
23
|
|
|
'b': {'items': {'type': 'integer'}, |
|
24
|
|
|
'type': 'array'}, |
|
25
|
|
|
'c': {'properties': {'d': {'type': 'string'}, |
|
26
|
|
|
'e': {'type': |
|
27
|
|
|
'number'}}, |
|
28
|
|
|
'type': 'object'}}, |
|
29
|
|
|
'type': 'object'} |
|
30
|
|
|
|
|
31
|
|
|
opts = dict(ac_schema_typemap=TT._SIMPLETYPE_MAP) |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
class Test_00_Functions(Test_00_Base): |
|
35
|
|
|
|
|
36
|
|
|
def test_20_array_to_schema(self): |
|
37
|
|
|
scm = TT.array_to_schema([1]) |
|
38
|
|
|
ref = dict(items=dict(type="integer"), type="array") |
|
39
|
|
|
self.assertTrue(dicts_equal(scm, ref), scm) |
|
40
|
|
|
|
|
41
|
|
|
def test_22_array_to_schema__empty_array(self): |
|
42
|
|
|
scm = TT.array_to_schema([]) |
|
43
|
|
|
ref = dict(items=dict(type="string"), type="array") |
|
44
|
|
|
self.assertTrue(dicts_equal(scm, ref), scm) |
|
45
|
|
|
|
|
46
|
|
|
def test_30_object_to_schema_nodes_iter(self): |
|
47
|
|
|
scm = TT.object_to_schema({'a': 1}) |
|
48
|
|
|
ref = dict(type="object", properties=dict(a=dict(type="integer"))) |
|
49
|
|
|
self.assertTrue(dicts_equal(scm, ref), scm) |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
class Test_10_Validation(Test_00_Base): |
|
53
|
|
|
|
|
54
|
|
|
def test_10_validate(self): |
|
55
|
|
|
(ret, msg) = TT.validate(self.obj, self.schema) |
|
56
|
|
|
self.assertFalse(msg) |
|
57
|
|
|
self.assertTrue(ret) |
|
58
|
|
|
|
|
59
|
|
|
def test_12_validate__ng(self): |
|
60
|
|
|
(ret, msg) = TT.validate({'a': "aaa"}, self.schema) |
|
61
|
|
|
self.assertTrue(msg) |
|
62
|
|
|
self.assertFalse(ret) |
|
63
|
|
|
|
|
64
|
|
|
def test_14_validate__ng_no_safe(self): |
|
65
|
|
|
raised = False |
|
66
|
|
|
try: |
|
67
|
|
|
_rc = TT.validate({'a': "aaa"}, self.schema, ac_schema_safe=False) |
|
68
|
|
|
print(_rc) # Should not be reached here... |
|
69
|
|
|
except Exception: |
|
70
|
|
|
raised = True |
|
71
|
|
|
|
|
72
|
|
|
self.assertTrue(raised) |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
class Test_12_Validation_Errors(Test_00_Base): |
|
76
|
|
|
|
|
77
|
|
|
obj = dict(a=1, b=2.0) |
|
78
|
|
|
scm = {"type": "object", "properties": {"a": {"type": "integer"}, |
|
79
|
|
|
"b": {"type": "string"}}} |
|
80
|
|
|
|
|
81
|
|
|
def test_12_validate__ng(self): |
|
82
|
|
|
(ret, msg) = TT.validate(self.obj, self.scm, ac_schema_errors=True) |
|
83
|
|
|
self.assertTrue(msg) # ["'a' is not of type ...", "'b' is not ..."] |
|
84
|
|
|
self.assertFalse(ret) |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
class Test_20_GenSchema(Test_00_Base): |
|
88
|
|
|
|
|
89
|
|
|
def test_40_gen_schema__primitive_types(self): |
|
90
|
|
|
self.assertEqual(TT.gen_schema(None), {'type': 'null'}) |
|
91
|
|
|
self.assertEqual(TT.gen_schema(0), {'type': 'integer'}) |
|
92
|
|
|
self.assertEqual(TT.gen_schema("aaa"), {'type': 'string'}) |
|
93
|
|
|
|
|
94
|
|
|
scm = TT.gen_schema([1]) |
|
95
|
|
|
ref_scm = {'items': {'type': 'integer'}, 'type': 'array'} |
|
96
|
|
|
self.assertTrue(dicts_equal(scm, ref_scm)) |
|
97
|
|
|
|
|
98
|
|
|
scm = TT.gen_schema({'a': 1}) |
|
99
|
|
|
ref_scm = {'properties': {'a': {'type': 'integer'}}, 'type': 'object'} |
|
100
|
|
|
self.assertTrue(dicts_equal(scm, ref_scm)) |
|
101
|
|
|
|
|
102
|
|
|
def test_42_gen_schema_and_validate(self): |
|
103
|
|
|
scm = TT.gen_schema(self.obj) |
|
104
|
|
|
self.assertTrue(TT.validate(self.obj, scm)) |
|
105
|
|
|
|
|
106
|
|
|
def test_44_gen_schema__complex_types(self): |
|
107
|
|
|
scm = TT.gen_schema(self.obj2) |
|
108
|
|
|
self.assertTrue(dicts_equal(scm, self.ref_scm)) |
|
109
|
|
|
|
|
110
|
|
|
def test_46_gen_schema_and_validate__complex_types(self): |
|
111
|
|
|
scm = TT.gen_schema(self.obj2) |
|
112
|
|
|
self.assertTrue(TT.validate(self.obj2, scm)) |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
def _gen_scm(val): |
|
116
|
|
|
return TT.gen_schema(val, ac_schema_strict=True) |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
class Test_30_GenStrictSchema(Test_00_Base): |
|
120
|
|
|
|
|
121
|
|
|
schema = {"type": "object", |
|
122
|
|
|
"properties": {"a": {"type": "integer"}}, |
|
123
|
|
|
"required": ["a"]} |
|
124
|
|
|
|
|
125
|
|
|
ref_scm = {'properties': {'a': {'type': 'integer'}, |
|
126
|
|
|
'b': {'items': {'type': 'integer'}, |
|
127
|
|
|
'type': 'array', |
|
128
|
|
|
'minItems': 2, 'uniqueItems': True}, |
|
129
|
|
|
'c': {'properties': {'d': {'type': 'string'}, |
|
130
|
|
|
'e': {'type': |
|
131
|
|
|
'number'}}, |
|
132
|
|
|
'type': 'object', |
|
133
|
|
|
'required': ['d', 'e']}}, |
|
134
|
|
|
'type': 'object', |
|
135
|
|
|
'required': ['a', 'b', 'c']} |
|
136
|
|
|
|
|
137
|
|
|
def test_40_gen_schema__primitive_types(self): |
|
138
|
|
|
self.assertEqual(_gen_scm(None), {'type': 'null'}) |
|
139
|
|
|
self.assertEqual(_gen_scm(0), {'type': 'integer'}) |
|
140
|
|
|
self.assertEqual(_gen_scm("aaa"), {'type': 'string'}) |
|
141
|
|
|
|
|
142
|
|
|
scm = _gen_scm([1]) |
|
143
|
|
|
ref_scm = {'items': {'type': 'integer'}, 'type': 'array', |
|
144
|
|
|
'minItems': 1, 'uniqueItems': True} |
|
145
|
|
|
self.assertTrue(dicts_equal(scm, ref_scm)) |
|
146
|
|
|
|
|
147
|
|
|
scm = _gen_scm(["aaa", "bbb", "aaa"]) |
|
148
|
|
|
ref_scm = {'items': {'type': 'string'}, 'type': 'array', |
|
149
|
|
|
'minItems': 3, 'uniqueItems': False} |
|
150
|
|
|
self.assertTrue(dicts_equal(scm, ref_scm)) |
|
151
|
|
|
|
|
152
|
|
|
scm = _gen_scm({'a': 1}) |
|
153
|
|
|
ref_scm = {'properties': {'a': {'type': 'integer'}}, |
|
154
|
|
|
'type': 'object', 'required': ['a']} |
|
155
|
|
|
self.assertTrue(dicts_equal(scm, ref_scm)) |
|
156
|
|
|
|
|
157
|
|
|
def test_42_gen_schema_and_validate(self): |
|
158
|
|
|
scm = _gen_scm(self.obj) |
|
159
|
|
|
self.assertTrue(TT.validate(self.obj, scm)) |
|
160
|
|
|
|
|
161
|
|
|
def test_44_gen_schema__complex_types(self): |
|
162
|
|
|
scm = _gen_scm(self.obj2) |
|
163
|
|
|
self.assertTrue(dicts_equal(scm, self.ref_scm)) |
|
164
|
|
|
|
|
165
|
|
|
def test_46_gen_schema_and_validate__complex_types(self): |
|
166
|
|
|
scm = _gen_scm(self.obj2) |
|
167
|
|
|
self.assertTrue(TT.validate(self.obj2, scm)) |
|
168
|
|
|
|
|
169
|
|
|
# vim:sw=4:ts=4:et: |
|
170
|
|
|
|