-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathbase.py
59 lines (42 loc) · 1.73 KB
/
base.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
import sys
import unittest
import jsonschema
from genson import SchemaNode, SchemaBuilder
PYTHON_VERSION = sys.version[:sys.version.find(' ')]
class BaseTestCase(unittest.TestCase):
def setUp(self):
self.builder = self.CLASS()
self._objects = []
self._schemas = []
def set_schema_options(self, **options):
self.builder = SchemaNode(**options)
def add_object(self, obj):
self.builder.add_object(obj)
self._objects.append(obj)
def add_schema(self, schema):
self.builder.add_schema(schema)
self._schemas.append(schema)
def assertObjectValidates(self, obj):
jsonschema.Draft7Validator(self.builder.to_schema()).validate(obj)
def assertObjectDoesNotValidate(self, obj):
with self.assertRaises(jsonschema.exceptions.ValidationError):
jsonschema.Draft7Validator(self.builder.to_schema()).validate(obj)
def assertResult(self, expected, enforceUserContract=True):
self.assertEqual(
expected, self.builder.to_schema(),
'Generated schema (below) does not match expected (above)')
if enforceUserContract:
self.assertUserContract()
def assertUserContract(self):
self._assertSchemaIsValid()
self._assertComponentObjectsValidate()
def _assertSchemaIsValid(self):
jsonschema.Draft7Validator.check_schema(self.builder.to_schema())
def _assertComponentObjectsValidate(self):
compiled_schema = self.builder.to_schema()
for obj in self._objects:
jsonschema.Draft7Validator(compiled_schema).validate(obj)
class SchemaNodeTestCase(BaseTestCase):
CLASS = SchemaNode
class SchemaBuilderTestCase(BaseTestCase):
CLASS = SchemaBuilder