Small refactoring and docs

This commit is contained in:
mvaradi 2018-10-14 10:31:40 +01:00
parent bf3bb72bb8
commit 7d89f1131b
2 changed files with 41 additions and 34 deletions

View file

@ -23,14 +23,14 @@ class TestValidator(unittest.TestCase):
def test_no_file_at_path(self): def test_no_file_at_path(self):
validator = Validator("resource_name") validator = Validator("resource_name")
validator.parse_json("invalid_path") validator._parse_json("invalid_path")
self.assertIn("File error", validator.error_log) self.assertIn("File error", validator.error_log)
def test_json_parser(self): def test_json_parser(self):
validator = Validator("resource_name") validator = Validator("resource_name")
data = validator.parse_json("data/test_data.json") data = validator._parse_json("data/test_data.json")
self.assertIsNotNone(data) self.assertIsNotNone(data)
validator.parse_json("data/test_data_malformed.json") validator._parse_json("data/test_data_malformed.json")
self.assertIn("JSON error", validator.error_log) self.assertIn("JSON error", validator.error_log)
def test_basic_checks(self): def test_basic_checks(self):
@ -45,22 +45,22 @@ class TestValidator(unittest.TestCase):
def test_no_resource_name(self): def test_no_resource_name(self):
validator = Validator("test") validator = Validator("test")
validator.json_data = {"pdb_id": "1abc"} validator.json_data = {"pdb_id": "1abc"}
self.assertFalse(validator.test_resource()) self.assertFalse(validator._test_resource())
def test_resource_name_mismatch(self): def test_resource_name_mismatch(self):
validator = Validator("test") validator = Validator("test")
validator.json_data = {"data_resource": "test2", "pdb_id": "1abc"} validator.json_data = {"data_resource": "test2", "pdb_id": "1abc"}
self.assertFalse(validator.test_resource()) self.assertFalse(validator._test_resource())
def test_no_pdb_id(self): def test_no_pdb_id(self):
validator = Validator("test") validator = Validator("test")
validator.json_data = {"data_resource": "test"} validator.json_data = {"data_resource": "test"}
self.assertFalse(validator.test_pdb_id()) self.assertFalse(validator._test_pdb_id())
def test_invalid_pdb_id(self): def test_invalid_pdb_id(self):
validator = Validator("test") validator = Validator("test")
validator.json_data = {"data_resource": "test", "pdb_id": "invalid"} validator.json_data = {"data_resource": "test", "pdb_id": "invalid"}
self.assertFalse(validator.test_pdb_id()) self.assertFalse(validator._test_pdb_id())
def test_json_validation(self): def test_json_validation(self):
validator = Validator("ProKinO") validator = Validator("ProKinO")

View file

@ -23,6 +23,13 @@ import re
class Validator(object): class Validator(object):
""" """
Validates FunPDBe JSON files Validates FunPDBe JSON files
Example usage:
validator = Validator()
validator.load_schema("path/to/schema")
validator.load_json("path/to/json")
if validator.basic_checks() and validator.validate_against_schema():
# the input JSON is a valid FunPDBe file
""" """
def __init__(self, resource): def __init__(self, resource):
@ -37,7 +44,7 @@ class Validator(object):
:param path_to_file: String, path to JSON file :param path_to_file: String, path to JSON file
:return: None :return: None
""" """
self.json_data = self.parse_json(path_to_file) self.json_data = self._parse_json(path_to_file)
def load_schema(self, path_to_schema): def load_schema(self, path_to_schema):
""" """
@ -45,9 +52,31 @@ class Validator(object):
:param path_to_schema: String, path to FunPDBe schema :param path_to_schema: String, path to FunPDBe schema
:return: None :return: None
""" """
self.schema = self.parse_json(path_to_schema) self.schema = self._parse_json(path_to_schema)
def parse_json(self, path): def basic_checks(self):
"""
Performs basic data checks
:return: Bool, True if valid, False if invalid
"""
if self._test_resource() and self._test_pdb_id():
return True
return False
def validate_against_schema(self):
"""
Calls jsonschema.validate() to compare the JSON with
the FunPDBe JSON schema
:return: Bool, True is valid, False if invalid
"""
try:
jsonschema.validate(self.json_data, self.schema)
return True
except jsonschema.exceptions.ValidationError as err:
self.error_log = "JSON does not comply with schema: %s" % err
return False
def _parse_json(self, path):
""" """
Parses a FunPDBe JSON file and in case of file error Parses a FunPDBe JSON file and in case of file error
or JSON error, the error message is saved to self.error_log or JSON error, the error message is saved to self.error_log
@ -65,16 +94,7 @@ class Validator(object):
self.error_log = "File error: %s" % ioerr self.error_log = "File error: %s" % ioerr
return None return None
def basic_checks(self): def _test_resource(self):
"""
Performs basic data checks
:return: Bool, True if valid, False if invalid
"""
if self.test_resource() and self.test_pdb_id():
return True
return False
def test_resource(self):
""" """
Check if data_resource field exists in the JSON, Check if data_resource field exists in the JSON,
and if it is the same as the provided resource name and if it is the same as the provided resource name
@ -88,7 +108,7 @@ class Validator(object):
return False return False
return True return True
def test_pdb_id(self): def _test_pdb_id(self):
""" """
Check if PDB id exists in the JSON, and if it follows Check if PDB id exists in the JSON, and if it follows
the PDB id pattern the PDB id pattern
@ -101,16 +121,3 @@ class Validator(object):
self.error_log = "Invalid PDB id found" self.error_log = "Invalid PDB id found"
return False return False
return True return True
def validate_against_schema(self):
"""
Calls jsonschema.validate() to compare the JSON with
the FunPDBe JSON schema
:return: Bool, True is valid, False if invalid
"""
try:
jsonschema.validate(self.json_data, self.schema)
return True
except jsonschema.exceptions.ValidationError as err:
self.error_log = "JSON does not comply with schema: %s" % err
return False