Skip to content
This repository has been archived by the owner on Dec 13, 2019. It is now read-only.

Commit

Permalink
Merge pull request #64 from harryhoch/master
Browse files Browse the repository at this point in the history
basic validation test
  • Loading branch information
cmungall committed Apr 14, 2016
2 parents 7219fdc + 95ea513 commit 9f6b2c0
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__pycache__
.gitignore
*~
*~
*#
23 changes: 2 additions & 21 deletions .travis.yml
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 test/scriptrunner.sh

2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
jsonschema
python_jsonschema_objects
36 changes: 36 additions & 0 deletions test/json_validation_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import unittest
import os
import json
from jsonschema import validate, ValidationError, SchemaError,Draft4Validator
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 test_valid_schema(self):

schema_path = "../schema/phenopacket-schema.json"

schema_fh = open(os.path.join(os.path.dirname(__file__), schema_path), 'r')
schema = json.load(schema_fh)
schema_fh.close()

## call validator
Draft4Validator.check_schema(schema)

if __name__ == '__main__':
unittest.main()
67 changes: 67 additions & 0 deletions test/phenopacket_validator.py
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()
2 changes: 2 additions & 0 deletions test/scriptrunner.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
python ./test/json_validation_test.py

0 comments on commit 9f6b2c0

Please sign in to comment.