Commit a795cb20 authored by Vilem Ded's avatar Vilem Ded
Browse files

fix - testing JSON schemas

parent 29d485d9
from unittest import TestCase
from json import loads
from jsonschema import validate
from jsonschema.exceptions import SchemaError
import os
# Fixtures
dirname = os.path.dirname(__file__)
testdata_dir = os.path.abspath(os.path.join(dirname, "data/"))
schema_dir = os.path.abspath(os.path.join(dirname,"../../metadata_tools/resources/"))
schema_testdata_filename_map = [
('elu-dataset.json', 'datasets.json'),
('elu-project.json', 'projects.json'),
('elu-institution.json', 'partners.json')
]
class TestParser(TestCase):
class TestJSONSchemas(TestCase):
def test_validation(self):
print(os.getcwd())
schemaFile = open('../../metadata_tools/resources/elu-dataset.json', encoding='utf-8')
dataFile = open('data/datasets.json', encoding='utf-8')
schema = loads(schemaFile.read())
data = loads(dataFile.read())
try:
validate(data, schema['schema'])
self.assert_(True)
except:
self.fail()
finally:
dataFile.close()
schemaFile.close()
for schema_filename, dataset_filename in schema_testdata_filename_map:
schema_filepath = os.path.abspath(os.path.join(schema_dir, schema_filename))
schemaFile = open(os.path.join(schema_dir, schema_filename), encoding='utf-8')
data_filepath = os.path.abspath(os.path.join(testdata_dir, dataset_filename))
dataFile = open(data_filepath, encoding='utf-8')
schema = loads(schemaFile.read())
data = loads(dataFile.read())['items']
for item in data:
try:
validate(item, schema)
self.assert_(True)
except SchemaError:
self.fail(f"JSONSchema {schema_filepath} is not valid - SchemaError.")
except:
self.fail(f"JSONSchema {schema_filepath} is not valid - failed on test data {data_filepath}")
finally:
dataFile.close()
schemaFile.close()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment