1
|
|
|
# |
2
|
|
|
# Copyright (C) 2015 Satoru SATOH <ssato at redhat.com> |
3
|
|
|
# License: MIT |
4
|
|
|
# |
5
|
|
|
# pylint: disable=missing-docstring, invalid-name |
6
|
|
|
from __future__ import absolute_import |
7
|
|
|
|
8
|
|
|
import unittest |
9
|
|
|
import anyconfig.schema as TT |
10
|
|
|
|
11
|
|
|
from anyconfig.tests.common import dicts_equal |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class Test_00_Base(unittest.TestCase): |
15
|
|
|
|
16
|
|
|
obj = {'a': 1} |
17
|
|
|
schema = {"type": "object", |
18
|
|
|
"properties": {"a": {"type": "integer"}}} |
19
|
|
|
|
20
|
|
|
obj2 = dict(a=1, b=[1, 2], c=dict(d="aaa", e=0.1)) |
21
|
|
|
ref_scm = {'properties': {'a': {'type': 'integer'}, |
22
|
|
|
'b': {'items': {'type': 'integer'}, |
23
|
|
|
'type': 'array'}, |
24
|
|
|
'c': {'properties': {'d': {'type': 'string'}, |
25
|
|
|
'e': {'type': |
26
|
|
|
'number'}}, |
27
|
|
|
'type': 'object'}}, |
28
|
|
|
'type': 'object'} |
29
|
|
|
|
30
|
|
|
opts = dict(ac_schema_typemap=TT._SIMPLETYPE_MAP) |
31
|
|
|
|
32
|
|
|
def test_20_array_to_schema_node(self): |
33
|
|
|
scm = TT.array_to_schema_node([1]) |
34
|
|
|
ref_scm = {'type': 'integer'} |
35
|
|
|
self.assertTrue(dicts_equal(scm, ref_scm), scm) |
36
|
|
|
|
37
|
|
|
def test_22_array_to_schema_node__empty_array(self): |
38
|
|
|
scm = TT.array_to_schema_node([]) |
39
|
|
|
ref_scm = {'type': 'string'} |
40
|
|
|
self.assertTrue(dicts_equal(scm, ref_scm), scm) |
41
|
|
|
|
42
|
|
|
def test_30_object_to_schema_nodes_iter(self): |
43
|
|
|
nscm = list(TT.object_to_schema_nodes_iter({'a': 1}))[0] |
44
|
|
|
ref_nscm = ('a', {'type': 'integer'}) |
45
|
|
|
self.assertTrue(nscm, ref_nscm) |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
class Test_10_Validation(Test_00_Base): |
49
|
|
|
|
50
|
|
|
def test_10_validate(self): |
51
|
|
|
(ret, msg) = TT.validate(self.obj, self.schema) |
52
|
|
|
self.assertFalse(msg) |
53
|
|
|
self.assertTrue(ret) |
54
|
|
|
|
55
|
|
|
def test_12_validate__ng(self): |
56
|
|
|
(ret, msg) = TT.validate({'a': "aaa"}, self.schema) |
57
|
|
|
self.assertTrue(msg) |
58
|
|
|
self.assertFalse(ret) |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
class Test_20_GenSchema(Test_00_Base): |
62
|
|
|
|
63
|
|
|
def test_40_gen_schema__primitive_types(self): |
64
|
|
|
self.assertEqual(TT.gen_schema(None), {'type': 'null'}) |
65
|
|
|
self.assertEqual(TT.gen_schema(0), {'type': 'integer'}) |
66
|
|
|
self.assertEqual(TT.gen_schema("aaa"), {'type': 'string'}) |
67
|
|
|
|
68
|
|
|
scm = TT.gen_schema([1]) |
69
|
|
|
ref_scm = {'items': {'type': 'integer'}, 'type': 'array'} |
70
|
|
|
self.assertTrue(dicts_equal(scm, ref_scm)) |
71
|
|
|
|
72
|
|
|
scm = TT.gen_schema({'a': 1}) |
73
|
|
|
ref_scm = {'properties': {'a': {'type': 'integer'}}, 'type': 'object'} |
74
|
|
|
self.assertTrue(dicts_equal(scm, ref_scm)) |
75
|
|
|
|
76
|
|
|
def test_42_gen_schema_and_validate(self): |
77
|
|
|
scm = TT.gen_schema(self.obj) |
78
|
|
|
self.assertTrue(TT.validate(self.obj, scm)) |
79
|
|
|
|
80
|
|
|
def test_44_gen_schema__complex_types(self): |
81
|
|
|
scm = TT.gen_schema(self.obj2) |
82
|
|
|
self.assertTrue(dicts_equal(scm, self.ref_scm)) |
83
|
|
|
|
84
|
|
|
def test_46_gen_schema_and_validate__complex_types(self): |
85
|
|
|
scm = TT.gen_schema(self.obj2) |
86
|
|
|
self.assertTrue(TT.validate(self.obj2, scm)) |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
def _gen_scm(val): |
90
|
|
|
return TT.gen_schema(val, ac_schema_type="strict") |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
class Test_30_GenStrictSchema(Test_00_Base): |
94
|
|
|
|
95
|
|
|
schema = {"type": "object", |
96
|
|
|
"properties": {"a": {"type": "integer"}}, |
97
|
|
|
"required": ["a"]} |
98
|
|
|
|
99
|
|
|
ref_scm = {'properties': {'a': {'type': 'integer'}, |
100
|
|
|
'b': {'items': {'type': 'integer'}, |
101
|
|
|
'type': 'array', |
102
|
|
|
'minItems': 2, 'uniqueItems': True}, |
103
|
|
|
'c': {'properties': {'d': {'type': 'string'}, |
104
|
|
|
'e': {'type': |
105
|
|
|
'number'}}, |
106
|
|
|
'type': 'object', |
107
|
|
|
'required': ['d', 'e']}}, |
108
|
|
|
'type': 'object', |
109
|
|
|
'required': ['a', 'b', 'c']} |
110
|
|
|
|
111
|
|
|
def test_40_gen_schema__primitive_types(self): |
112
|
|
|
self.assertEqual(_gen_scm(None), {'type': 'null'}) |
113
|
|
|
self.assertEqual(_gen_scm(0), {'type': 'integer'}) |
114
|
|
|
self.assertEqual(_gen_scm("aaa"), {'type': 'string'}) |
115
|
|
|
|
116
|
|
|
scm = _gen_scm([1]) |
117
|
|
|
ref_scm = {'items': {'type': 'integer'}, 'type': 'array', |
118
|
|
|
'minItems': 1, 'uniqueItems': True} |
119
|
|
|
self.assertTrue(dicts_equal(scm, ref_scm)) |
120
|
|
|
|
121
|
|
|
scm = _gen_scm(["aaa", "bbb", "aaa"]) |
122
|
|
|
ref_scm = {'items': {'type': 'string'}, 'type': 'array', |
123
|
|
|
'minItems': 3, 'uniqueItems': False} |
124
|
|
|
self.assertTrue(dicts_equal(scm, ref_scm)) |
125
|
|
|
|
126
|
|
|
scm = _gen_scm({'a': 1}) |
127
|
|
|
ref_scm = {'properties': {'a': {'type': 'integer'}}, |
128
|
|
|
'type': 'object', 'required': ['a']} |
129
|
|
|
self.assertTrue(dicts_equal(scm, ref_scm)) |
130
|
|
|
|
131
|
|
|
def test_42_gen_schema_and_validate(self): |
132
|
|
|
scm = _gen_scm(self.obj) |
133
|
|
|
self.assertTrue(TT.validate(self.obj, scm)) |
134
|
|
|
|
135
|
|
|
def test_44_gen_schema__complex_types(self): |
136
|
|
|
scm = _gen_scm(self.obj2) |
137
|
|
|
self.assertTrue(dicts_equal(scm, self.ref_scm)) |
138
|
|
|
|
139
|
|
|
def test_46_gen_schema_and_validate__complex_types(self): |
140
|
|
|
scm = _gen_scm(self.obj2) |
141
|
|
|
self.assertTrue(TT.validate(self.obj2, scm)) |
142
|
|
|
|
143
|
|
|
# vim:sw=4:ts=4:et: |
144
|
|
|
|