-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_schema.py
369 lines (296 loc) · 11.2 KB
/
test_schema.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import unittest
from os import path
import gcl
from gcl import schema
from gcl import exceptions
from gcl import util
class TestSchemaObjects(unittest.TestCase):
"""Tests to ensure that the schema objects do the right thing."""
def testScalarString(self):
schema.from_spec('string').validate('a')
def testScalarInt(self):
schema.from_spec('int').validate(3)
def testScalarIntFails(self):
self.assertRaises(lambda: schema.from_spec('int').validate('a'))
def testScalarFloat(self):
schema.from_spec('float').validate(3.0)
def testScalarBool(self):
schema.from_spec('bool').validate(False)
def testListWithSpec(self):
int_schema = schema.ScalarSchema('int')
schema.from_spec([int_schema]).validate([3])
schema.from_spec([int_schema]).validate([])
schema.from_spec([int_schema]).validate([1, 2])
schema.from_spec([]).validate([1, 2])
def testListSchema(self):
with self.assertRaises(exceptions.SchemaError):
schema.from_spec([]).validate('whoops')
def testListWithSpecFails(self):
string_schema = schema.ScalarSchema('string')
with self.assertRaises(exceptions.SchemaError):
schema.from_spec([string_schema]).validate([1, 2])
def testTuple(self):
schema.from_spec({}).validate({})
def testTupleWithSubSchemas(self):
s = schema.from_spec({'fields': {'a': schema.from_spec('string')}})
s.validate({'a': 'b'})
s.validate({})
# This doesn't fail! TupleSchema only checks required fields!
s.validate({'a': 3})
def testTupleSchemaCorrectSubSchemaType(self):
"""To make sure we can't use tuple schemas incorrectly."""
with self.assertRaises(ValueError):
schema.from_spec({'fields': {'a': 'string'}}).validate({})
def testTupleSchemaSubSchema(self):
s = schema.from_spec({'fields': {'a': schema.from_spec('string')}})
# Subschema for a requires a string
with self.assertRaises(exceptions.SchemaError):
s.get_subschema('a').validate(3)
# No schema, so don't care
s.get_subschema('b').validate(3)
def testTupleWithDeepSchemas(self):
s = schema.from_spec({'fields': {'a': schema.from_spec({'fields': { 'b': schema.from_spec('int') }})}})
s.validate({'a': {}})
# This doesn't fail! No deep verification!
s.validate({'a': {'b': 'c'}})
def testTupleWithRequiredField(self):
s = schema.from_spec({'required': ['a']})
s.validate({'a': 3})
with self.assertRaises(exceptions.SchemaError):
s.validate({})
def testValidateShouldNotAttach(self):
s = schema.from_spec({'required': ['a']})
x = gcl.loads('a = 3')
s.validate(x)
self.assertEquals([], x.tuple_schema.required_fields)
class TestSchemaEquality(unittest.TestCase):
def setUp(self):
self.int1 = schema.ScalarSchema('int')
self.int2 = schema.ScalarSchema('int')
self.string = schema.ScalarSchema('string')
def testScalarEquality(self):
self.assertTrue(self.int1 == self.int2)
self.assertFalse(self.int1 != self.int2)
self.assertFalse(self.int1 == self.string)
self.assertTrue(self.int1 != self.string)
class TestCollapseAndSchema(unittest.TestCase):
def collapsesTo(self, one, two, expected):
s = schema.AndSchema.make(one, two)
self.assertEquals(expected, s)
def testCollapseAniesLeft(self):
self.collapsesTo(schema.AnySchema(), schema.ScalarSchema('int'),
schema.ScalarSchema('int'))
def testCollapseAniesRight(self):
self.collapsesTo(schema.ScalarSchema('int'), schema.AnySchema(),
schema.ScalarSchema('int'))
def testCollapseTwoAnies(self):
self.collapsesTo(schema.AnySchema(), schema.AnySchema(),
schema.AnySchema())
def testCollapseEqualSchemas(self):
self.collapsesTo(schema.ScalarSchema('int'), schema.ScalarSchema('int'),
schema.ScalarSchema('int'))
class TestSchemaFromGCL(unittest.TestCase):
"""Tests to ensure that schema objects parsed directly from JSON look good."""
def testSubSchema(self):
x = gcl.loads("""
a : int;
""")
self.assertEquals(schema.ScalarSchema('int'), x.tuple_schema.get_subschema('a'))
def testTupleRequiredFields(self):
x = gcl.loads("""
a : required int = 3;
b : string;
c : required = 1;
""")
self.assertEquals(['a', 'c'], sorted(x.tuple_schema.required_fields))
def testSchemaRefersToVariable(self):
x = gcl.loads("""
Xint = { x : int; };
y : Xint = { x = 'bla' };
""")
self.assertEquals(schema.ScalarSchema('int'), x['y'].tuple_schema.get_subschema('x'))
def testSchemasCompose(self):
x = gcl.loads("""
Xint = { x : int; };
y = Xint { x = 'bla'; y : string };
""")
self.assertEquals(schema.ScalarSchema('string'), x['y'].tuple_schema.get_subschema('y'))
self.assertEquals(schema.ScalarSchema('int'), x['y'].tuple_schema.get_subschema('x'))
def testSchemasCompose3Times(self):
x = gcl.loads("""
y = { x : int } { y : string } { z : bool };
""")
self.assertEquals(schema.ScalarSchema('string'), x['y'].tuple_schema.get_subschema('y'))
self.assertEquals(schema.ScalarSchema('int'), x['y'].tuple_schema.get_subschema('x'))
self.assertEquals(schema.ScalarSchema('bool'), x['y'].tuple_schema.get_subschema('z'))
def testInheritSchemaWithOverriddenValue(self):
x = gcl.loads("""
y = { x : int } { x = 3 };
""")
self.assertEquals(schema.ScalarSchema('int'), x['y'].tuple_schema.get_subschema('x'))
def testListSchema(self):
x = gcl.loads("""
y = { x : [int] }
""")
self.assertEquals(schema.ListSchema(schema.ScalarSchema('int')), x['y'].tuple_schema.get_subschema('x'))
def testAnyListSchema(self):
x = gcl.loads("""
y = { x : [] }
""")
self.assertEquals(schema.ListSchema(schema.AnySchema()), x['y'].tuple_schema.get_subschema('x'))
def testInheritSchemaWithOverriddenValueWhichIsAList(self):
x = gcl.loads("""
y = { x : [int] } { x = [3] };
""")
self.assertEquals(schema.ListSchema(schema.ScalarSchema('int')), x['y'].tuple_schema.get_subschema('x'))
class TestSchemaInGCL(unittest.TestCase):
"""Tests to ensure that using schemas directly inside GCL do the right thing."""
def testFailingToParseTupleWithSchema(self):
with self.assertRaises(exceptions.SchemaError):
gcl.loads("""
a : required;
""")
def testSchemaValidationIsLazy(self):
x = gcl.loads("""
a : int = 'foo';
""")
with self.assertRaises(exceptions.SchemaError):
print(x['a'])
def testListSchema(self):
x = gcl.loads("""
a : [int] = ['boo'];
""")
with self.assertRaises(exceptions.SchemaError):
print(x['a'])
def testSchemaSurvivesComposition(self):
x = gcl.loads("""
A = { foo : int };
B = A { foo = 'hello' };
""")
with self.assertRaises(exceptions.SchemaError):
print(x['B']['foo'])
def testRequiredFieldsInComposition(self):
x = gcl.loads("""
SuperClass = { x : required };
ok_instance = SuperClass { x = 1 };
failing_instance = SuperClass { y = 1 };
""")
print(x['ok_instance'])
with self.assertRaises(exceptions.SchemaError):
print(x['failing_instance'])
def testClassesInTupleComposition(self):
x = gcl.loads("""
Atom = {
name : required;
};
Molecule = {
atoms : [Atom];
};
objects = {
correct = Molecule {
atoms = [{ name = 'O' }, { name = 'C' }];
};
broken = Molecule {
atoms = [{ valence = 3 }];
};
}
""")
zzz = x['objects']['correct']['atoms']
with self.assertRaises(exceptions.SchemaError):
zzz = x['objects']['broken']['atoms']
def testSchemasFromScopes(self):
"""Found a case in the wild where using schema objects from a scope seems to make a difference."""
model = gcl.loads("""
scope = {
X = { id: required };
Y = { xs: X };
};
y = scope.Y { xs = scope.X { id = 'hoi' }};
""")
self.assertEquals('hoi', model['y']['xs']['id'])
def testGetUnrelatedAttributeOfNonValidatingObject(self):
"""Getting a value from a nonvalidating object should fail."""
model = gcl.loads("""
obj = {
id: required;
ego = 3;
};
copy = obj.ego;
""")
with self.assertRaises(exceptions.EvaluationError):
zzz = model['copy']
def testValidateOuterObjectWhileApplyingInnerObject(self):
"""We disable validations while getting the LHS of an application, but the inner deref should
be validated again."""
model = gcl.loads("""
obj = {
id: required;
Class = { X: required; }
};
# This is not allowed because 'obj' should fail validation, no matter if
# 'obj' appears in the LHS of an application.
copy = obj.Class { X = 3 };
""")
with self.assertRaises(exceptions.EvaluationError):
self.assertEquals(3, model['copy']['X'])
def testSchemaTypoExceptionIsDescriptive(self):
"""Check that if you make a typo in a type name the error is helpful."""
with self.assertRaises(exceptions.EvaluationError):
x = gcl.loads("""
a : strong;
""")
def testSpecifySchemaDeep(self):
obj = gcl.loads("x : { a : required } = { b = 'foo' };")
with self.assertRaises(exceptions.SchemaError):
print(obj['x'])
def testSpecifySchemaDeepInList(self):
obj = gcl.loads("x : [{ a : int }] = [{ a = 'foo' }];")
with self.assertRaises(exceptions.SchemaError):
print(obj['x'][0]['a'])
def testSchemaCanBeSetFromAbove(self):
obj = gcl.loads("x : { x : { a : int }} = { x = { a = 'hoi' }}")
with self.assertRaises(exceptions.SchemaError):
print(obj['x']['x']['a'])
def testSchemaCombinesFromAbove(self):
obj = gcl.loads("x : { x : { a : int }} = { x = { a = 3; b : required }}")
with self.assertRaises(exceptions.SchemaError):
print(obj['x']['x']['a'])
def testDontNeedToWriteSpace(self):
"""It's annoying me to have to write a space for schema definitions."""
obj = gcl.loads('x: int = 3;')
print(obj['x'])
def testRecursiveSchemaReference(self):
obj = gcl.loads('''
LinkedList = {
value : required int;
next : LinkedList;
};
one_two_three = LinkedList {
value = 1;
next = LinkedList {
value = 2;
next = LinkedList {
value = 3;
};
};
};
''')
self.assertEquals(3, obj['one_two_three']['next']['next']['value'])
class TestExportVisibilityThroughSchemas(unittest.TestCase):
"""Test the annotation of schemas with non-exportable fields for JSON exports."""
def testNoExportFieldFromTuple(self):
obj = gcl.loads("x : private = 3; y = 5")
x = util.to_python(obj)
self.assertEquals({'y': 5}, x)
def testNoExportFieldFromCompositeTuple(self):
obj = gcl.loads("x = { x : private = 3 } { y = 5 }")
x = util.to_python(obj['x'])
self.assertEquals({'y': 5}, x)
def testNoExportFieldFromCompositeTupleWithPrivateOverride(self):
obj = gcl.loads("x = { x = 3; y = 5 } { x : private }")
x = util.to_python(obj['x'])
self.assertEquals({'y': 5}, x)
def testNoExportFieldFromTupleComposedWithDict(self):
obj = gcl.loads("x = { x : private = 3 } Y", env={'Y': {'y': 5}})
x = util.to_python(obj['x'])
self.assertEquals({'y': 5}, x)