From 49978a5331f526bc4ef1fa22d0be1640024e9f4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20B=C3=A4ckmark?= Date: Thu, 21 Dec 2023 10:05:57 +0100 Subject: [PATCH] Add test for valid schema --- Makefile | 4 ++-- test_jsonschema.py | 40 ++++++++++++++++++++++++++++++++++++++++ tox.ini | 7 +++++++ 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 test_jsonschema.py diff --git a/Makefile b/Makefile index 5e333607..92baf0fc 100644 --- a/Makefile +++ b/Makefile @@ -25,8 +25,8 @@ help: ## Show this help. @egrep -h '\s##\s' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m %-30s\033[0m %s\n", $$1, $$2}' .PHONY: tests -test: ## Run all document related checks found in tox: pytest, jsonformat, validate - tox run -e pytest,jsonformat,validate +test: ## Run all document related checks found in tox: pytest, jsonformat, validate, jsonschema + tox run -e pytest,jsonformat,validate,jsonschema .PHONY: generate_docs generate_docs: ## Generate the Markdown files based on the YAML definitions diff --git a/test_jsonschema.py b/test_jsonschema.py new file mode 100644 index 00000000..20394e11 --- /dev/null +++ b/test_jsonschema.py @@ -0,0 +1,40 @@ +# Copyright 2022 Axis Communications AB. +# For a full list of individual contributors, please see the commit history. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import json +import subprocess + +import jsonschema +import pytest + + +@pytest.mark.parametrize( + "filename", + subprocess.check_output(["git", "ls-files", "--exclude-standard", "schemas/*.json"]) + .decode("utf-8") + .splitlines(), +) +def test_json_schema(filename): + with open(filename) as input_file: + data = input_file.read() + + event_schema = json.loads(data) + stricter_metaschema = dict( + jsonschema.Draft4Validator.META_SCHEMA, additionalProperties=False + ) + StrictDraft4Validator = jsonschema.validators.create( + stricter_metaschema, jsonschema.Draft4Validator.VALIDATORS, "StrictDraft4" + ) + StrictDraft4Validator.check_schema(event_schema) diff --git a/tox.ini b/tox.ini index c89a9d0d..b5c2fee6 100644 --- a/tox.ini +++ b/tox.ini @@ -20,6 +20,7 @@ envlist = black isort jsonformat + jsonschema pytest markdownlint @@ -49,6 +50,12 @@ deps = pytest==6.2.5 commands = pytest test_jsonformat.py {posargs} +[testenv:jsonschema] +deps = + pytest==6.2.5 + jsonschema==4.20.0 +commands = pytest test_jsonschema.py {posargs} + [testenv:pytest] deps = -rrequirements.txt