Skip to content

Commit 082e844

Browse files
committed
Cleanup
* Make errors in the tests be failures * Removed use of ErrorTree * Added another helper fn * Use fnmatch instead of glob
1 parent e39d537 commit 082e844

File tree

1 file changed

+54
-49
lines changed

1 file changed

+54
-49
lines changed

bin/suite_sanity_check

+54-49
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#! /usr/bin/env python
2-
from glob import glob
32
from unittest import TestCase, skipIf
3+
import fnmatch
44
import json
55
import logging
66
import os
@@ -11,34 +11,9 @@ except ImportError:
1111
jsonschema = None
1212

1313

14-
SUITE_ROOT_DIR = os.path.join(os.path.dirname(__file__), os.pardir, "tests")
15-
1614
logging.basicConfig(level=logging.INFO)
1715

18-
19-
def groups(paths):
20-
for path in paths:
21-
with open(path) as test_file:
22-
groups = json.load(test_file)
23-
for group in groups:
24-
yield group
25-
26-
27-
def cases(paths):
28-
for test_group in groups(paths):
29-
for test in test_group["tests"]:
30-
test["schema"] = test_group["schema"]
31-
yield test
32-
33-
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-
16+
SUITE_ROOT_DIR = os.path.join(os.path.dirname(__file__), os.pardir, "tests")
4217
TESTSUITE_SCHEMA = {
4318
"$schema": "http://json-schema.org/draft-03/schema#",
4419
"type": "array",
@@ -67,43 +42,73 @@ TESTSUITE_SCHEMA = {
6742
}
6843

6944

45+
def files(paths):
46+
for path in paths:
47+
with open(path) as test_file:
48+
yield json.load(test_file)
49+
50+
51+
def groups(paths):
52+
for test_file in files(paths):
53+
for group in test_file:
54+
yield group
55+
56+
57+
def cases(paths):
58+
for test_group in groups(paths):
59+
for test in test_group["tests"]:
60+
test["schema"] = test_group["schema"]
61+
yield test
62+
63+
64+
def collect(root_dir):
65+
for root, dirs, files in os.walk(root_dir):
66+
for filename in fnmatch.filter(files, "*.json"):
67+
yield os.path.join(root, filename)
68+
69+
7070
class SanityTests(TestCase):
7171
@classmethod
7272
def setUpClass(cls):
7373
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")))
74+
cls.test_files = list(collect(SUITE_ROOT_DIR))
7775
logging.info("Found %s test files", len(cls.test_files))
7876
assert cls.test_files, "Didn't find the test files!"
7977

8078
def test_all_files_are_valid_json(self):
81-
for test_path in self.test_files:
82-
with open(test_path) as test_file:
83-
json.load(test_file)
84-
85-
@skipIf(jsonschema is None, "Validation library not present! Skipping.")
86-
def test_all_schemas_are_valid(self):
87-
for test_case in cases(self.test_files):
88-
# XXX: Add the other versions for the other drafts
89-
jsonschema.Draft3Validator.check_schema(test_case["schema"])
79+
for path in self.test_files:
80+
with open(path) as test_file:
81+
try:
82+
json.load(test_file)
83+
except ValueError as error:
84+
self.fail("%s contains invalid JSON (%s)" % (path, error))
9085

9186
def test_all_descriptions_are_unique(self):
9287
for group in groups(self.test_files):
9388
descriptions = {test["description"] for test in group["tests"]}
94-
self.assertEqual(len(descriptions), len(group["tests"]))
89+
self.assertEqual(
90+
len(descriptions),
91+
len(group["tests"]),
92+
"%r contains a duplicate description" % (group,)
93+
)
9594

95+
@skipIf(jsonschema is None, "Validation library not present! Skipping.")
96+
def test_all_schemas_are_valid(self):
97+
for case in cases(self.test_files):
98+
# XXX: Add the other versions for the other drafts
99+
try:
100+
jsonschema.Draft3Validator.check_schema(case["schema"])
101+
except jsonschema.SchemaError as error:
102+
self.fail("%s contains an invalid schema (%s)" % (case, error))
103+
104+
@skipIf(jsonschema is None, "Validation library not present! Skipping.")
96105
def test_suites_are_valid(self):
97106
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+
for tests in files(self.test_files):
108+
try:
109+
validator.validate(tests)
110+
except jsonschema.ValidationError as error:
111+
self.fail(str(error))
107112

108113

109114
if __name__ == "__main__":

0 commit comments

Comments
 (0)