Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

actions: Add tests to test some of the examples match the schema #233

Merged
merged 2 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added tests/__init__.py
Empty file.
23 changes: 23 additions & 0 deletions tests/schema.py
Original file line number Diff line number Diff line change
@@ -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
51 changes: 51 additions & 0 deletions tests/test_examples_json.py
Original file line number Diff line number Diff line change
@@ -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)