Completed
Push — master ( c73223...1ab817 )
by Satoru
01:13
created

Test_00_Functions.test_20_array_to_schema()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 4
rs 10
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
33
class Test_00_Functions(Test_00_Base):
34
35
    def test_20_array_to_schema(self):
36
        scm = TT.array_to_schema([1])
37
        ref = dict(items=dict(type="integer"), type="array")
38
        self.assertTrue(dicts_equal(scm, ref), scm)
39
40
    def test_22_array_to_schema__empty_array(self):
41
        scm = TT.array_to_schema([])
42
        ref = dict(items=dict(type="string"), type="array")
43
        self.assertTrue(dicts_equal(scm, ref), scm)
44
45
    def test_30_object_to_schema_nodes_iter(self):
46
        scm = TT.object_to_schema({'a': 1})
47
        ref = dict(type="object", properties=dict(a=dict(type="integer")))
48
        self.assertTrue(dicts_equal(scm, ref), scm)
49
50
51
class Test_10_Validation(Test_00_Base):
52
53
    def test_10_validate(self):
54
        (ret, msg) = TT.validate(self.obj, self.schema)
55
        self.assertFalse(msg)
56
        self.assertTrue(ret)
57
58
    def test_12_validate__ng(self):
59
        (ret, msg) = TT.validate({'a': "aaa"}, self.schema)
60
        self.assertTrue(msg)
61
        self.assertFalse(ret)
62
63
64
class Test_20_GenSchema(Test_00_Base):
65
66
    def test_40_gen_schema__primitive_types(self):
67
        self.assertEqual(TT.gen_schema(None), {'type': 'null'})
68
        self.assertEqual(TT.gen_schema(0), {'type': 'integer'})
69
        self.assertEqual(TT.gen_schema("aaa"), {'type': 'string'})
70
71
        scm = TT.gen_schema([1])
72
        ref_scm = {'items': {'type': 'integer'}, 'type': 'array'}
73
        self.assertTrue(dicts_equal(scm, ref_scm))
74
75
        scm = TT.gen_schema({'a': 1})
76
        ref_scm = {'properties': {'a': {'type': 'integer'}}, 'type': 'object'}
77
        self.assertTrue(dicts_equal(scm, ref_scm))
78
79
    def test_42_gen_schema_and_validate(self):
80
        scm = TT.gen_schema(self.obj)
81
        self.assertTrue(TT.validate(self.obj, scm))
82
83
    def test_44_gen_schema__complex_types(self):
84
        scm = TT.gen_schema(self.obj2)
85
        self.assertTrue(dicts_equal(scm, self.ref_scm))
86
87
    def test_46_gen_schema_and_validate__complex_types(self):
88
        scm = TT.gen_schema(self.obj2)
89
        self.assertTrue(TT.validate(self.obj2, scm))
90
91
92
def _gen_scm(val):
93
    return TT.gen_schema(val, ac_schema_strict=True)
94
95
96
class Test_30_GenStrictSchema(Test_00_Base):
97
98
    schema = {"type": "object",
99
              "properties": {"a": {"type": "integer"}},
100
              "required": ["a"]}
101
102
    ref_scm = {'properties': {'a': {'type': 'integer'},
103
                              'b': {'items': {'type': 'integer'},
104
                                    'type': 'array',
105
                                    'minItems': 2, 'uniqueItems': True},
106
                              'c': {'properties': {'d': {'type': 'string'},
107
                                                   'e': {'type':
108
                                                         'number'}},
109
                                    'type': 'object',
110
                                    'required': ['d', 'e']}},
111
               'type': 'object',
112
               'required': ['a', 'b', 'c']}
113
114
    def test_40_gen_schema__primitive_types(self):
115
        self.assertEqual(_gen_scm(None), {'type': 'null'})
116
        self.assertEqual(_gen_scm(0), {'type': 'integer'})
117
        self.assertEqual(_gen_scm("aaa"), {'type': 'string'})
118
119
        scm = _gen_scm([1])
120
        ref_scm = {'items': {'type': 'integer'}, 'type': 'array',
121
                   'minItems': 1, 'uniqueItems': True}
122
        self.assertTrue(dicts_equal(scm, ref_scm))
123
124
        scm = _gen_scm(["aaa", "bbb", "aaa"])
125
        ref_scm = {'items': {'type': 'string'}, 'type': 'array',
126
                   'minItems': 3, 'uniqueItems': False}
127
        self.assertTrue(dicts_equal(scm, ref_scm))
128
129
        scm = _gen_scm({'a': 1})
130
        ref_scm = {'properties': {'a': {'type': 'integer'}},
131
                   'type': 'object', 'required': ['a']}
132
        self.assertTrue(dicts_equal(scm, ref_scm))
133
134
    def test_42_gen_schema_and_validate(self):
135
        scm = _gen_scm(self.obj)
136
        self.assertTrue(TT.validate(self.obj, scm))
137
138
    def test_44_gen_schema__complex_types(self):
139
        scm = _gen_scm(self.obj2)
140
        self.assertTrue(dicts_equal(scm, self.ref_scm))
141
142
    def test_46_gen_schema_and_validate__complex_types(self):
143
        scm = _gen_scm(self.obj2)
144
        self.assertTrue(TT.validate(self.obj2, scm))
145
146
# vim:sw=4:ts=4:et:
147