|
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(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
|
|
|
def test_10_validate(self): |
|
31
|
|
|
(ret, msg) = TT.validate(self.obj, self.schema) |
|
32
|
|
|
self.assertFalse(msg) |
|
33
|
|
|
self.assertTrue(ret) |
|
34
|
|
|
|
|
35
|
|
|
def test_12_validate__ng(self): |
|
36
|
|
|
(ret, msg) = TT.validate({'a': "aaa"}, self.schema) |
|
37
|
|
|
self.assertTrue(msg) |
|
38
|
|
|
self.assertFalse(ret) |
|
39
|
|
|
|
|
40
|
|
|
def test_20_array_to_schema_node(self): |
|
41
|
|
|
scm = TT.array_to_schema_node([1]) |
|
42
|
|
|
ref_scm = {'type': 'integer'} |
|
43
|
|
|
self.assertTrue(dicts_equal(scm, ref_scm), scm) |
|
44
|
|
|
|
|
45
|
|
|
def test_22_array_to_schema_node__empty_array(self): |
|
46
|
|
|
scm = TT.array_to_schema_node([]) |
|
47
|
|
|
ref_scm = {'type': 'string'} |
|
48
|
|
|
self.assertTrue(dicts_equal(scm, ref_scm), scm) |
|
49
|
|
|
|
|
50
|
|
|
def test_30_object_to_schema_nodes_iter(self): |
|
51
|
|
|
nscm = list(TT.object_to_schema_nodes_iter({'a': 1}))[0] |
|
52
|
|
|
ref_nscm = ('a', {'type': 'integer'}) |
|
53
|
|
|
self.assertTrue(nscm, ref_nscm) |
|
54
|
|
|
|
|
55
|
|
|
def test_40_gen_schema__primitive_types(self): |
|
56
|
|
|
self.assertEqual(TT.gen_schema(None), {'type': 'null'}) |
|
57
|
|
|
self.assertEqual(TT.gen_schema(0), {'type': 'integer'}) |
|
58
|
|
|
self.assertEqual(TT.gen_schema("aaa"), {'type': 'string'}) |
|
59
|
|
|
|
|
60
|
|
|
scm = TT.gen_schema([1]) |
|
61
|
|
|
ref_scm = {'items': {'type': 'integer'}, 'type': 'array'} |
|
62
|
|
|
self.assertTrue(dicts_equal(scm, ref_scm)) |
|
63
|
|
|
|
|
64
|
|
|
scm = TT.gen_schema({'a': 1}) |
|
65
|
|
|
ref_scm = {'properties': {'a': {'type': 'integer'}}, 'type': 'object'} |
|
66
|
|
|
self.assertTrue(dicts_equal(scm, ref_scm)) |
|
67
|
|
|
|
|
68
|
|
|
def test_42_gen_schema_and_validate(self): |
|
69
|
|
|
scm = TT.gen_schema(self.obj) |
|
70
|
|
|
self.assertTrue(TT.validate(self.obj, scm)) |
|
71
|
|
|
|
|
72
|
|
|
def test_44_gen_schema__complex_types(self): |
|
73
|
|
|
scm = TT.gen_schema(self.obj2) |
|
74
|
|
|
self.assertTrue(dicts_equal(scm, self.ref_scm)) |
|
75
|
|
|
|
|
76
|
|
|
def test_46_gen_schema_and_validate__complex_types(self): |
|
77
|
|
|
scm = TT.gen_schema(self.obj2) |
|
78
|
|
|
self.assertTrue(TT.validate(self.obj2, scm)) |
|
79
|
|
|
|
|
80
|
|
|
# vim:sw=4:ts=4:et: |
|
81
|
|
|
|