-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathtest_gen_single.py
206 lines (170 loc) · 5.98 KB
/
test_gen_single.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
from . import base
class TestBasicTypes(base.SchemaNodeTestCase):
def test_no_object(self):
self.assertResult({})
def test_string(self):
self.add_object("string")
self.assertResult({"type": "string"})
def test_integer(self):
self.add_object(1)
self.assertResult({"type": "integer"})
def test_number(self):
self.add_object(1.1)
self.assertResult({"type": "number"})
def test_boolean(self):
self.add_object(True)
self.assertResult({"type": "boolean"})
def test_null(self):
self.add_object(None)
self.assertResult({"type": "null"})
class TestArrayList(base.SchemaNodeTestCase):
def setUp(self):
base.SchemaNodeTestCase.setUp(self)
def test_empty(self):
self.add_object([])
self.assertResult({"type": "array"})
def test_monotype(self):
self.add_object(["spam", "spam", "spam", "eggs", "spam"])
self.assertResult({"type": "array", "items": {"type": "string"}})
def test_multitype(self):
self.add_object([1, "2", None, False])
self.assertResult({
"type": "array",
"items": {
"type": ["boolean", "integer", "null", "string"]}
})
self.assertObjectValidates([False, None, "2", 1])
def test_nested(self):
self.add_object([
["surprise"],
["fear", "surprise"],
["fear", "surprise", "ruthless efficiency"],
["fear", "surprise", "ruthless efficiency",
"an almost fanatical devotion to the Pope"]
])
self.assertResult({
"type": "array",
"items": {
"type": "array",
"items": {"type": "string"}}
})
class TestArrayTuple(base.SchemaNodeTestCase):
def setUp(self):
base.SchemaNodeTestCase.setUp(self)
def test_empty(self):
self.add_schema({"type": "array", "items": []})
self.add_object([])
self.assertResult({"type": "array", "items": [{}]})
def test_empty_schema(self):
self.add_schema({"type": "array", "items": [{}]})
self.add_object([])
self.assertResult({"type": "array", "items": [{}]})
def test_multitype(self):
self.add_schema({"type": "array", "items": []})
self.add_object([1, "2", "3", None, False])
self.assertResult({
"type": "array",
"items": [
{"type": "integer"},
{"type": "string"},
{"type": "string"},
{"type": "null"},
{"type": "boolean"}]
})
self.assertObjectDoesNotValidate([1, 2, "3", None, False])
def test_nested(self):
self.add_schema(
{"type": "array", "items": {"type": "array", "items": []}})
self.add_object([
["surprise"],
["fear", "surprise"],
["fear", "surprise", "ruthless efficiency"],
["fear", "surprise", "ruthless efficiency",
"an almost fanatical devotion to the Pope"]
])
self.assertResult({
"type": "array",
"items": {
"type": "array",
"items": [
{"type": "string"},
{"type": "string"},
{"type": "string"},
{"type": "string"}
]
}
})
class TestObject(base.SchemaNodeTestCase):
def test_empty_object(self):
self.add_object({})
self.assertResult({"type": "object"})
def test_basic_object(self):
self.add_object({
"Red Windsor": "Normally, but today the van broke down.",
"Stilton": "Sorry.",
"Gruyere": False})
self.assertResult({
"required": ["Gruyere", "Red Windsor", "Stilton"],
"type": "object",
"properties": {
"Red Windsor": {"type": "string"},
"Gruyere": {"type": "boolean"},
"Stilton": {"type": "string"}
}
})
class TestComplex(base.SchemaNodeTestCase):
def test_array_in_object(self):
self.add_object({"a": "b", "c": [1, 2, 3]})
self.assertResult({
"required": ["a", "c"],
"type": "object",
"properties": {
"a": {"type": "string"},
"c": {
"type": "array",
"items": {"type": "integer"}
}
}
})
def test_object_in_array(self):
self.add_object([
{"name": "Sir Lancelot of Camelot",
"quest": "to seek the Holy Grail",
"favorite colour": "blue"},
{"name": "Sir Robin of Camelot",
"quest": "to seek the Holy Grail",
"capitol of Assyria": None}])
self.assertResult({
"type": "array",
"items": {
"type": "object",
"required": ["name", "quest"],
"properties": {
"quest": {"type": "string"},
"name": {"type": "string"},
"favorite colour": {"type": "string"},
"capitol of Assyria": {"type": "null"}
}
}
})
def test_three_deep(self):
self.add_object({"matryoshka": {"design": {"principle": "FTW!"}}})
self.assertResult({
"type": "object",
"required": ["matryoshka"],
"properties": {
"matryoshka": {
"type": "object",
"required": ["design"],
"properties": {
"design": {
"type": "object",
"required": ["principle"],
"properties": {
"principle": {"type": "string"}
}
}
}
}
}
})