From 23aaa21a03255d9a3e6c09a2a20748bfef325565 Mon Sep 17 00:00:00 2001 From: James Date: Thu, 15 Dec 2022 11:47:24 +0000 Subject: [PATCH] actions: Add tests to test some of the examples match the schema https://github.com/Open-Telecoms-Data/open-fibre-data-standard/issues/57 --- tests/__init__.py | 0 tests/schema.py | 23 +++++++++++++++++ tests/test_examples_json.py | 51 +++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/schema.py create mode 100644 tests/test_examples_json.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/schema.py b/tests/schema.py new file mode 100644 index 0000000..80a70ff --- /dev/null +++ b/tests/schema.py @@ -0,0 +1,23 @@ +import json +import os +import jsonref + +from libcoveofds.schema import OFDSSchema + + +class CurrentVersionOFDSSchema(OFDSSchema): + + def __init__(self): + filename_package = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "schema", "network-package-schema.json") + filename_network = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "schema", "network-schema.json") + with open(filename_package) as fp: + self.schema = json.load(fp) + with open(filename_network) as fp: + self.data_schema = jsonref.load(fp) + self.schema['properties']['networks']['items'] = self.data_schema + + def get_package_schema_dereferenced(self): + return self.schema + + def get_package_schema(self): + return self.schema diff --git a/tests/test_examples_json.py b/tests/test_examples_json.py new file mode 100644 index 0000000..8fc9830 --- /dev/null +++ b/tests/test_examples_json.py @@ -0,0 +1,51 @@ +import os +import json + +from .schema import CurrentVersionOFDSSchema +from libcoveofds.additionalfields import AdditionalFields +from libcoveofds.python_validate import PythonValidate +from libcoveofds.jsonschemavalidate import JSONSchemaValidator + + +class BaseTest: + + def setup_class(self): + self.schema = CurrentVersionOFDSSchema() + + def get_data(self): + return {} + + def test_additional_fields(self): + worker = AdditionalFields(self.schema) + results = worker.process(self.get_data()) + assert results == {} + + def test_jsonschema_validation(self): + worker = JSONSchemaValidator(self.schema) + results = worker.validate(self.get_data()) + # We call .json() so that if there any any problems, the output error messages are usefull. + results = [r.json() for r in results] + assert results == [] + + def test_python_validation(self): + worker = PythonValidate(self.schema) + results = worker.validate(self.get_data()) + assert results == [] + + +class TestNetworkPackage(BaseTest): + + def get_data(self): + filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "examples", "json", + "network-package.json") + with open(filename) as fp: + return json.load(fp) + + +class TestMultipleNetworks(BaseTest): + + def get_data(self): + filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "examples", "json", + "multiple-networks.json") + with open(filename) as fp: + return json.load(fp)