From bdb060ab4b2ffb85037b6d5c79546a2c71162f42 Mon Sep 17 00:00:00 2001 From: Gareth Rushgrove Date: Sat, 16 Sep 2017 13:02:51 +0100 Subject: [PATCH] Adding a set of unit tests which validate the schemas This generates currently ~25,000 test cases and will automatically test newly generated files and can test against additional drafts of the JSON Schema spec as needed. --- .gitignore | 8 ++ .travis.yml | 6 ++ pytest.ini | 4 + requirements.txt | 6 ++ schemas/v4.json | 150 +++++++++++++++++++++++++++++++++++++ tests/test_valid_schema.py | 19 +++++ tox.ini | 7 ++ 7 files changed, 200 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 pytest.ini create mode 100644 requirements.txt create mode 100644 schemas/v4.json create mode 100644 tests/test_valid_schema.py create mode 100644 tox.ini diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..14f904b021 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.cache +.tox +Include +Lib +Scripts +pip-selfcheck.json +tcl +__pycache__ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000000..b5e3f8625f --- /dev/null +++ b/.travis.yml @@ -0,0 +1,6 @@ +udo: false +language: python +python: + - 3.6 +install: pip install tox-travis +script: tox diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000000..4a1e66413e --- /dev/null +++ b/pytest.ini @@ -0,0 +1,4 @@ +[pytest] +addopts = --flake8 -n4 +testpaths = tests + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000..4f8d34bc0a --- /dev/null +++ b/requirements.txt @@ -0,0 +1,6 @@ +pytest +jsonschema +pytest-flake8 +flake8 +tox +pytest-xdist diff --git a/schemas/v4.json b/schemas/v4.json new file mode 100644 index 0000000000..85eb502a68 --- /dev/null +++ b/schemas/v4.json @@ -0,0 +1,150 @@ +{ + "id": "http://json-schema.org/draft-04/schema#", + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Core schema meta-schema", + "definitions": { + "schemaArray": { + "type": "array", + "minItems": 1, + "items": { "$ref": "#" } + }, + "positiveInteger": { + "type": "integer", + "minimum": 0 + }, + "positiveIntegerDefault0": { + "allOf": [ { "$ref": "#/definitions/positiveInteger" }, { "default": 0 } ] + }, + "simpleTypes": { + "enum": [ "array", "boolean", "integer", "null", "number", "object", "string" ] + }, + "stringArray": { + "type": "array", + "items": { "type": "string" }, + "minItems": 1, + "uniqueItems": true + } + }, + "type": "object", + "properties": { + "id": { + "type": "string", + "format": "uri" + }, + "$schema": { + "type": "string", + "format": "uri" + }, + "title": { + "type": "string" + }, + "description": { + "type": "string" + }, + "default": {}, + "multipleOf": { + "type": "number", + "minimum": 0, + "exclusiveMinimum": true + }, + "maximum": { + "type": "number" + }, + "exclusiveMaximum": { + "type": "boolean", + "default": false + }, + "minimum": { + "type": "number" + }, + "exclusiveMinimum": { + "type": "boolean", + "default": false + }, + "maxLength": { "$ref": "#/definitions/positiveInteger" }, + "minLength": { "$ref": "#/definitions/positiveIntegerDefault0" }, + "pattern": { + "type": "string", + "format": "regex" + }, + "additionalItems": { + "anyOf": [ + { "type": "boolean" }, + { "$ref": "#" } + ], + "default": {} + }, + "items": { + "anyOf": [ + { "$ref": "#" }, + { "$ref": "#/definitions/schemaArray" } + ], + "default": {} + }, + "maxItems": { "$ref": "#/definitions/positiveInteger" }, + "minItems": { "$ref": "#/definitions/positiveIntegerDefault0" }, + "uniqueItems": { + "type": "boolean", + "default": false + }, + "maxProperties": { "$ref": "#/definitions/positiveInteger" }, + "minProperties": { "$ref": "#/definitions/positiveIntegerDefault0" }, + "required": { "$ref": "#/definitions/stringArray" }, + "additionalProperties": { + "anyOf": [ + { "type": "boolean" }, + { "$ref": "#" } + ], + "default": {} + }, + "definitions": { + "type": "object", + "additionalProperties": { "$ref": "#" }, + "default": {} + }, + "properties": { + "type": "object", + "additionalProperties": { "$ref": "#" }, + "default": {} + }, + "patternProperties": { + "type": "object", + "additionalProperties": { "$ref": "#" }, + "default": {} + }, + "dependencies": { + "type": "object", + "additionalProperties": { + "anyOf": [ + { "$ref": "#" }, + { "$ref": "#/definitions/stringArray" } + ] + } + }, + "enum": { + "type": "array", + "minItems": 1, + "uniqueItems": true + }, + "type": { + "anyOf": [ + { "$ref": "#/definitions/simpleTypes" }, + { + "type": "array", + "items": { "$ref": "#/definitions/simpleTypes" }, + "minItems": 1, + "uniqueItems": true + } + ] + }, + "allOf": { "$ref": "#/definitions/schemaArray" }, + "anyOf": { "$ref": "#/definitions/schemaArray" }, + "oneOf": { "$ref": "#/definitions/schemaArray" }, + "not": { "$ref": "#" } + }, + "dependencies": { + "exclusiveMaximum": [ "maximum" ], + "exclusiveMinimum": [ "minimum" ] + }, + "default": {} +} diff --git a/tests/test_valid_schema.py b/tests/test_valid_schema.py new file mode 100644 index 0000000000..129c53815c --- /dev/null +++ b/tests/test_valid_schema.py @@ -0,0 +1,19 @@ +import json +import glob + +from jsonschema import validate +import pytest + + +@pytest.fixture(scope='module', params=glob.glob('schemas/*.json')) +def metaschema(request): + with open(request.param) as schema_data: + schema = json.load(schema_data) + return schema + + +@pytest.mark.parametrize('path', glob.glob('v[0-9].*/*.json')) +def test_valid_jsonschema(metaschema, path): + with open(path) as data: + resource = json.load(data) + validate(resource, metaschema) diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000000..7395dcbaa2 --- /dev/null +++ b/tox.ini @@ -0,0 +1,7 @@ +[tox] +envlist = py36 +skipsdist = true + +[testenv] +commands = {posargs:py.test} +deps = -r{toxinidir}/requirements.txt