This repository has been archived by the owner on Dec 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
281 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,6 @@ | ||
language: python | ||
python: | ||
- "3.4" | ||
|
||
before_script: | ||
- mkdir -p bin | ||
- export PATH=$PATH:$PWD/bin | ||
- wget http:https://build.berkeleybop.org/userContent/owltools/owltools -O bin/owltools | ||
- wget http:https://build.berkeleybop.org/userContent/owltools/ontology-release-runner -O bin/ontology-release-runner | ||
- wget http:https://build.berkeleybop.org/userContent/owltools/owltools-runner-all.jar -O bin/owltools-runner-all.jar | ||
- wget http:https://build.berkeleybop.org/userContent/owltools/owltools-oort-all.jar -O bin/owltools-oort-all.jar | ||
- chmod +x bin/* | ||
|
||
# command to run tests | ||
script: make test | ||
|
||
# whitelist | ||
branches: | ||
only: | ||
- master | ||
|
||
notifications: | ||
email: | ||
- [email protected] | ||
install: "pip install -r requirements.txt" | ||
script: sh tests/unit/scriptrunner.sh | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
jsonschema | ||
python_jsonschema_objects |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import unittest | ||
import os | ||
import json | ||
import python_jsonschema_objects as jsonobjects | ||
import inspect | ||
|
||
class JsonToObjectTestCase(unittest.TestCase): | ||
""" | ||
Test JSON to Python Object Mappers such as warlock and | ||
python_jsonschema_objects, see | ||
http:https://stackoverflow.com/questions/12465588/convert-a-json-schema-to-a-python-class | ||
""" | ||
|
||
def setUp(self): | ||
schema_path = "../schema/phenopacket-schema.json" | ||
omim_path = "../examples/level-1/omim-example-l1.json" | ||
variant_path = "../examples/level-1/variant-example-l1.json" | ||
|
||
schema_fh = open(os.path.join(os.path.dirname(__file__), schema_path), 'r') | ||
self.schema = json.load(schema_fh) | ||
|
||
omim_fh = open(os.path.join(os.path.dirname(__file__), omim_path), 'r') | ||
self.omim_example = json.load(omim_fh) | ||
|
||
variant_fh = open(os.path.join(os.path.dirname(__file__), variant_path), 'r') | ||
self.variant_example = json.load(variant_fh) | ||
|
||
builder = jsonobjects.ObjectBuilder(self.schema) | ||
self.namespace = builder.build_classes() | ||
|
||
schema_fh.close() | ||
omim_fh.close() | ||
variant_fh.close() | ||
|
||
def tearDown(self): | ||
pass | ||
|
||
def test_omim_object_mapping(self): | ||
|
||
pheno_packet = self.namespace\ | ||
.UrnJsonschemaOrgMonarchinitiativePpkModelPhenopacket()\ | ||
.from_json(json.dumps(self.omim_example)) | ||
|
||
# inspect parent classes of entities | ||
# print(inspect.getmro(pheno_packet.entities[0].__class__.__bases__[0])) | ||
# (<class 'python_jsonschema_objects.classbuilder.ProtocolBase'>, | ||
# ... | ||
# <class 'collections.abc.Iterable'>, <class 'collections.abc.Container'>, | ||
# <class 'object'>) | ||
|
||
self.assertEqual(pheno_packet.entities[0].id, 'OMIM:615426') | ||
|
||
def test_variant_om(self): | ||
pheno_packet = self.namespace\ | ||
.UrnJsonschemaOrgMonarchinitiativePpkModelPhenopacket()\ | ||
.from_json(json.dumps(self.variant_example)) | ||
|
||
variant = list(filter((lambda var: var['entity'] == '_:v1'), pheno_packet.phenotype_profile)) | ||
|
||
test_json = "{\"type\": {\"label\": \"Muscular dystrophy\"," \ | ||
" \"id\": \"HP:0003560\"}, \"description\": \"blah blah\"," \ | ||
" \"onset\": {\"type\": {\"label\": \"Late onset\", \"id\": \"HP:0003584\"}}}" | ||
test_dict = json.loads(test_json) | ||
|
||
self.assertEqual(variant[0].phenotype.for_json(), test_dict) | ||
|
||
# This might be an unexpected edge case | ||
# variant[0].phenotype.type is a class | ||
# with the attribute _value that equals a dict | ||
# print(variant[0].phenotype.type.__class__) | ||
# <class 'python_jsonschema_objects.classbuilder.type'> | ||
|
||
self.assertEqual(variant[0].phenotype.type._value['id'], 'HP:0003560') | ||
self.assertEqual(variant[0].phenotype.type._value['label'], 'Muscular dystrophy') | ||
|
||
def test_rountrip_json(self): | ||
pheno_packet = self.namespace\ | ||
.UrnJsonschemaOrgMonarchinitiativePpkModelPhenopacket()\ | ||
.from_json(json.dumps(self.omim_example)) | ||
|
||
# for_json converts objects to dict | ||
self.assertEqual(self.omim_example, pheno_packet.for_json()) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import unittest | ||
import os | ||
import json | ||
from jsonschema import validate, ValidationError, SchemaError | ||
import python_jsonschema_objects as jsonobjects | ||
from python_jsonschema_objects import ValidationError as ValidationException | ||
|
||
|
||
class ValidatorTestCase(unittest.TestCase): | ||
""" | ||
Test various json validators, jsonschema seems to be a popular | ||
lib, warlock and python-jsonschema-objects are build off | ||
of jsonschema and also include json to python object mappers | ||
The goal of this is to experiment with library options but these | ||
could eventually be integrated into the phenopacket python api | ||
As a first pass, testing that the journal example is incorrect | ||
and the rest are correct. These could be expanded in the future | ||
as appropriate | ||
""" | ||
|
||
def setUp(self): | ||
schema_path = "../schema/phenopacket-schema.json" | ||
journal_path = "../examples/level-1/journal-example-l1.json" | ||
omim_path = "../examples/level-1/omim-example-l1.json" | ||
patient_path = "../examples/level-1/patient-example-l1.json" | ||
variant_path = "../examples/level-1/variant-example-l1.json" | ||
|
||
schema_fh = open(os.path.join(os.path.dirname(__file__), schema_path), 'r') | ||
self.schema = json.load(schema_fh) | ||
|
||
journal_fh = open(os.path.join(os.path.dirname(__file__), journal_path), 'r') | ||
self.journal_example = json.load(journal_fh) | ||
|
||
omim_fh = open(os.path.join(os.path.dirname(__file__), omim_path), 'r') | ||
self.omim_example = json.load(omim_fh) | ||
|
||
patient_fh = open(os.path.join(os.path.dirname(__file__), patient_path), 'r') | ||
self.patient_example = json.load(patient_fh) | ||
|
||
variant_fh = open(os.path.join(os.path.dirname(__file__), variant_path), 'r') | ||
self.variant_example = json.load(variant_fh) | ||
|
||
schema_fh.close() | ||
journal_fh.close() | ||
omim_fh.close() | ||
patient_fh.close() | ||
variant_fh.close() | ||
|
||
def tearDown(self): | ||
self.schema = None | ||
self.journal_example = None | ||
self.omim_example = None | ||
self.patient_example = None | ||
self.variant_example = None | ||
|
||
def test_incorrect_journal(self): | ||
""" | ||
Test that the journal example is invalid since it | ||
uses "human" instead of patient | ||
'human' is not one of ['disease', 'organism', 'patient', 'variant', 'genotype'] | ||
""" | ||
# with self.assertRaises(ValidationError): | ||
# validate(self.journal_example, self.schema) | ||
try: | ||
validate(self.journal_example, self.schema) | ||
self.assertFail() | ||
except ValidationError as e: | ||
self.assertEqual(e.message, | ||
"'human' is not one of ['disease', 'organism', 'patient', 'variant', 'genotype']") | ||
|
||
|
||
def test_omim_example(self): | ||
""" | ||
Test that our other example files validate as correct | ||
""" | ||
validate(self.omim_example, self.schema) | ||
|
||
def test_patient_example(self): | ||
""" | ||
Test that our other example files validate as correct | ||
""" | ||
validate(self.patient_example, self.schema) | ||
|
||
def test_variant_example(self): | ||
""" | ||
Test that our other example files validate as correct | ||
""" | ||
validate(self.variant_example, self.schema) | ||
|
||
def test_jsonobjects_as_validator(self): | ||
builder = jsonobjects.ObjectBuilder(self.schema) | ||
namespace = builder.build_classes() | ||
|
||
# check that this works | ||
pheno_packet = namespace\ | ||
.UrnJsonschemaOrgMonarchinitiativePpkModelPhenopacket()\ | ||
.from_json(json.dumps(self.omim_example)) | ||
|
||
# check that this raises an exception | ||
with self.assertRaises(ValidationException): | ||
journal = namespace\ | ||
.UrnJsonschemaOrgMonarchinitiativePpkModelPhenopacket()\ | ||
.from_json(json.dumps(self.journal_example)) | ||
|
||
def test_invalid_schema(self): | ||
schema_path = "../resources/schemas/phenopacket-level-1-schema-bad.json" | ||
schema_fh = open(os.path.join(os.path.dirname(__file__), schema_path), 'r') | ||
schema = json.load(schema_fh) | ||
schema_fh.close() | ||
|
||
# this should fail | ||
with self.assertRaises(SchemaError): | ||
validate(self.omim_example,schema) | ||
|
||
|
||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import unittest | ||
import os | ||
import json | ||
from jsonschema import validate, ValidationError, SchemaError | ||
import python_jsonschema_objects as jsonobjects | ||
import getopt,sys | ||
|
||
|
||
|
||
def main(): | ||
|
||
schema_path = "../schema/phenopacket-schema.json" | ||
|
||
try: | ||
opts,args = getopt.getopt(sys.argv[1:],"s:",["schema="]) | ||
except getopt.GetoptError as err: | ||
usage() | ||
sys.exit(2) | ||
|
||
for o,a in opts: | ||
if o in ("-s","--schema"): | ||
schema_path=a | ||
else: | ||
assert False, "unhandled option" | ||
exfile = args[0] | ||
|
||
# try: | ||
schema_fh = open(os.path.join(os.path.dirname(__file__), schema_path), 'r') | ||
schema = json.load(schema_fh) | ||
schema_fh.close() | ||
# except json.decoder.JSONDecodeError: | ||
# print("Schema file ", schema_path, " does not contain a valid JSON file") | ||
# sys.exit(1) | ||
|
||
|
||
try: | ||
file_fh = open(exfile,'r') | ||
data = json.load(file_fh) | ||
file_fh.close() | ||
except IOError: | ||
print ("Could not read phenopacket file ",exfile) | ||
sys.exit(2) | ||
except json.decoder.JSONDecodeError: | ||
print("Phenopacket file ", exfile, " does not contain a valid JSON file") | ||
sys.exit(3) | ||
|
||
|
||
# http:https://python-jsonschema.readthedocs.org/en/latest/errors/ | ||
try: | ||
print("running validator"); | ||
validate(data,schema) | ||
print("Valid phenopacket") | ||
except SchemaError as s: | ||
print ("Schema error in schema ", schema_path) | ||
print (s.message) | ||
except ValidationError as e: | ||
print("Invalid phenopacket found in ",exfile) | ||
print(e.message) | ||
|
||
|
||
|
||
def usage(): | ||
print('Usage: '+sys.argv[0]+' -s <schemafile> [option]') | ||
print(' '+sys.argv[0]+' --schema=<schemafile> [option]') | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/sh | ||
python ./tests/unit/json_object_test.py | ||
python ./tests/unit/json_validation_test.py |