Skip to content

Commit d22c24e

Browse files
committed
Merge pull request #17 from gazpachoking/validate_suite_content
Add sanity checks for actual format of test suites
2 parents e8bcd5a + c0dbbb5 commit d22c24e

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

bin/suite_sanity_check

+52-3
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,49 @@ def cases(paths):
3131
yield test
3232

3333

34+
def errortree_messages(et, path=""):
35+
for e in et.errors.values():
36+
yield "%s: %s" % (path, e)
37+
for key in et:
38+
for msg in errortree_messages(et[key], "%s/%s" % (path, key)):
39+
yield msg
40+
41+
42+
TESTSUITE_SCHEMA = {
43+
"$schema": "http://json-schema.org/draft-03/schema#",
44+
"type": "array",
45+
"items": {
46+
"type": "object",
47+
"properties": {
48+
"description": {"type": "string", "required": True},
49+
"schema": {"required": True},
50+
"tests": {
51+
"type": "array",
52+
"items": {
53+
"type": "object",
54+
"properties": {
55+
"description": {"type": "string", "required": True},
56+
"data": {"required": True},
57+
"valid": {"type": "boolean", "required": True}
58+
},
59+
"additionalProperties": False
60+
},
61+
"minItems": 1
62+
}
63+
},
64+
"additionalProperties": False,
65+
"minItems": 1
66+
}
67+
}
68+
69+
3470
class SanityTests(TestCase):
3571
@classmethod
3672
def setUpClass(cls):
37-
test_glob = os.path.join(SUITE_ROOT_DIR, "*/*.json")
38-
logging.info("Looking for tests in %s", test_glob)
39-
cls.test_files = glob(test_glob)
73+
logging.info("Looking for tests in %s", SUITE_ROOT_DIR)
74+
cls.test_files = []
75+
for root, dirs, files in os.walk(SUITE_ROOT_DIR):
76+
cls.test_files.extend(glob(os.path.join(root, "*.json")))
4077
logging.info("Found %s test files", len(cls.test_files))
4178
assert cls.test_files, "Didn't find the test files!"
4279

@@ -56,6 +93,18 @@ class SanityTests(TestCase):
5693
descriptions = {test["description"] for test in group["tests"]}
5794
self.assertEqual(len(descriptions), len(group["tests"]))
5895

96+
def test_suites_are_valid(self):
97+
validator = jsonschema.Draft3Validator(TESTSUITE_SCHEMA)
98+
for root, dirs, files in os.walk(SUITE_ROOT_DIR):
99+
for f in glob(os.path.join(root, "*.json")):
100+
with open(f) as test_file:
101+
test_data = json.load(test_file)
102+
errortree = jsonschema.ErrorTree(validator.iter_errors(test_data))
103+
msg = "Test suite '%s' does not follow the test format:\n" % f
104+
msg += "\n".join("\t" + e for e in errortree_messages(errortree))
105+
assert not len(errortree), msg
106+
107+
59108

60109
if __name__ == "__main__":
61110
from unittest import main

0 commit comments

Comments
 (0)