summaryrefslogtreecommitdiff
path: root/json/bin
diff options
context:
space:
mode:
authorJulian Berman <Julian@GrayVines.com>2013-02-22 12:39:19 -0500
committerJulian Berman <Julian@GrayVines.com>2013-02-22 12:39:19 -0500
commit5d071d409fb51a0b8cb4e8f3310dab3345328232 (patch)
treef51d17a9f19a69f2e9c1210f303be8de861cdfc8 /json/bin
parent98ddbfa823fa481f4ac3091ddd78bf3b881b593c (diff)
downloadjsonschema-5d071d409fb51a0b8cb4e8f3310dab3345328232.tar.gz
Switching to actual subtree.
Diffstat (limited to 'json/bin')
-rwxr-xr-xjson/bin/jsonschema_suite172
1 files changed, 0 insertions, 172 deletions
diff --git a/json/bin/jsonschema_suite b/json/bin/jsonschema_suite
deleted file mode 100755
index 55a2a5e..0000000
--- a/json/bin/jsonschema_suite
+++ /dev/null
@@ -1,172 +0,0 @@
-#! /usr/bin/env python
-import argparse
-import fnmatch
-import json
-import logging
-import os
-import random
-import sys
-import unittest
-
-try:
- import jsonschema
-except ImportError:
- jsonschema = None
-
-
-logging.basicConfig(level=logging.INFO)
-
-SUITE_ROOT_DIR = os.path.join(os.path.dirname(__file__), os.pardir, "tests")
-TESTSUITE_SCHEMA = {
- "$schema": "http://json-schema.org/draft-03/schema#",
- "type": "array",
- "items": {
- "type": "object",
- "properties": {
- "description": {"type": "string", "required": True},
- "schema": {"required": True},
- "tests": {
- "type": "array",
- "items": {
- "type": "object",
- "properties": {
- "description": {"type": "string", "required": True},
- "data": {"required": True},
- "valid": {"type": "boolean", "required": True}
- },
- "additionalProperties": False
- },
- "minItems": 1
- }
- },
- "additionalProperties": False,
- "minItems": 1
- }
-}
-
-
-def files(paths):
- for path in paths:
- with open(path) as test_file:
- yield json.load(test_file)
-
-
-def groups(paths):
- for test_file in files(paths):
- for group in test_file:
- yield group
-
-
-def cases(paths):
- for test_group in groups(paths):
- for test in test_group["tests"]:
- test["schema"] = test_group["schema"]
- yield test
-
-
-def collect(root_dir):
- for root, dirs, files in os.walk(root_dir):
- for filename in fnmatch.filter(files, "*.json"):
- yield os.path.join(root, filename)
-
-
-class SanityTests(unittest.TestCase):
- @classmethod
- def setUpClass(cls):
- logging.info("Looking for tests in %s", SUITE_ROOT_DIR)
- cls.test_files = list(collect(SUITE_ROOT_DIR))
- logging.info("Found %s test files", len(cls.test_files))
- assert cls.test_files, "Didn't find the test files!"
-
- def test_all_files_are_valid_json(self):
- for path in self.test_files:
- with open(path) as test_file:
- try:
- json.load(test_file)
- except ValueError as error:
- self.fail("%s contains invalid JSON (%s)" % (path, error))
-
- def test_all_descriptions_have_reasonable_length(self):
- for case in cases(self.test_files):
- descript = case["description"]
- self.assertLess(
- len(descript),
- 60,
- "%r is too long! (keep it to less than 60 chars)" % (descript,)
- )
-
- def test_all_descriptions_are_unique(self):
- for group in groups(self.test_files):
- descriptions = {test["description"] for test in group["tests"]}
- self.assertEqual(
- len(descriptions),
- len(group["tests"]),
- "%r contains a duplicate description" % (group,)
- )
-
- @unittest.skipIf(jsonschema is None, "Validation library not present!")
- def test_all_schemas_are_valid(self):
- for schema in os.listdir(SUITE_ROOT_DIR):
- schema_validator = jsonschema.validators.get(schema)
- if schema_validator:
- test_files = collect(os.path.join(SUITE_ROOT_DIR, schema))
- for case in cases(test_files):
- try:
- schema_validator.check_schema(case["schema"])
- except jsonschema.SchemaError as error:
- self.fail("%s contains an invalid schema (%s)" %
- (case, error))
- else:
- logging.warning("No schema validator for %s" % schema)
-
- @unittest.skipIf(jsonschema is None, "Validation library not present!")
- def test_suites_are_valid(self):
- validator = jsonschema.Draft3Validator(TESTSUITE_SCHEMA)
- for tests in files(self.test_files):
- try:
- validator.validate(tests)
- except jsonschema.ValidationError as error:
- self.fail(str(error))
-
-
-def main(arguments):
- if arguments.command == "check":
- suite = unittest.TestLoader().loadTestsFromTestCase(SanityTests)
- result = unittest.TextTestRunner(verbosity=2).run(suite)
- sys.exit(not result.wasSuccessful())
- elif arguments.command == "flatten":
- selected_cases = [case for case in cases(collect(arguments.version))]
-
- if arguments.randomize:
- random.shuffle(selected_cases)
-
- json.dump(selected_cases, arguments.out, indent=4)
-
-
-parser = argparse.ArgumentParser(
- description="JSON Schema Test Suite utilities",
-)
-subparsers = parser.add_subparsers(help="utility commands", dest="command")
-
-check = subparsers.add_parser("check", help="Sanity check the test suite.")
-
-flatten = subparsers.add_parser(
- "flatten",
- help="Output a flattened file containing the test cases and any "
- "additionally specified optional test cases."
-)
-flatten.add_argument(
- "--randomize",
- action="store_true",
- help="randomize the order of the outputted cases",
-)
-flatten.add_argument(
- "version", help="the directory containing the version to output",
-)
-flatten.add_argument(
- "out", help="the output file to write to", type=argparse.FileType("w"),
-)
-
-
-if __name__ == "__main__":
- main(parser.parse_args())