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
|
from .compat import unittest
import ucl
import json
import os.path
import glob
import re
TESTS_SCHEMA_FOLDER = '../tests/schema/*.json'
comment_re = re.compile('\/\*((?!\*\/).)*?\*\/', re.DOTALL | re.MULTILINE)
def json_remove_comments(content):
return comment_re.sub('', content)
class ValidationTest(unittest.TestCase):
def validate(self, jsonfile):
def perform_test(schema, data, valid, description):
msg = '%s (valid=%r)' % (description, valid)
if valid:
self.assertTrue(ucl.validate(schema, data), msg)
else:
with self.assertRaises(ucl.SchemaError):
ucl.validate(schema, data)
self.fail(msg) # fail() will be called only if SchemaError is not raised
with open(jsonfile) as f:
try:
# data = json.load(f)
data = json.loads(json_remove_comments(f.read()))
except ValueError as e:
raise self.skipTest('Failed to load JSON: %s' % str(e))
for testgroup in data:
for test in testgroup['tests']:
perform_test(testgroup['schema'], test['data'],
test['valid'], test['description'])
@classmethod
def setupValidationTests(cls):
"""Creates each test dynamically from a folder"""
def test_gen(filename):
def run_test(self):
self.validate(filename)
return run_test
for jsonfile in glob.glob(TESTS_SCHEMA_FOLDER):
testname = os.path.splitext(os.path.basename(jsonfile))[0]
setattr(cls, 'test_%s' % testname, test_gen(jsonfile))
ValidationTest.setupValidationTests()
|