Skip to content

Commit

Permalink
Fix linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
george42-ctds committed Jan 9, 2025
1 parent 2fe675e commit b76cb2e
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions gdcdictionary/schema_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,24 @@

from __future__ import print_function

from jsonschema import validate, ValidationError
import argparse
import copy
import yaml
import glob
import os
import argparse
import json
import os
import unittest
from gdcdictionary import gdcdictionary
import yaml

from jsonschema import validate, ValidationError

from gdcdictionary import gdcdictionary


def load_yaml_schema(path):
"""Load yaml schema"""
with open(path, "r") as f:
return yaml.safe_load(f)


CUR_DIR = os.path.dirname(os.path.realpath(__file__))
DATA_DIR = os.path.join(CUR_DIR, "examples")
Expand All @@ -45,7 +47,8 @@ def merge_schemas(a, b, path=None):
else:
print(
"Overriding '{}':\n\t- {}\n\t+ {}".format(
".".join(path + [str(key)]), a[key], b[key])
".".join(path + [str(key)]), a[key], b[key]
)
)
a[key] = b[key]
else:
Expand Down Expand Up @@ -102,7 +105,7 @@ class SchemaTest(unittest.TestCase):
def setUp(self):
self.dictionary = gdcdictionary
self.definitions = yaml.safe_load(
open(os.path.join(CUR_DIR, "schemas","_definitions.yaml"),"r")
open(os.path.join(CUR_DIR, "schemas","_definitions.yaml"), "r")
)

def test_schemas(self):
Expand All @@ -113,10 +116,10 @@ def test_valid_files(self):
print("Validating {}".format(path))
doc = json.load(open(path, "r"))
print(doc)
if type(doc) == dict:
if isinstance(doc, dict):
self.add_system_props(doc)
validate_entity(doc, self.dictionary.schema)
elif type(doc) == list:
elif isinstance(doc, list):
for entity in doc:
self.add_system_props(entity)
validate_entity(entity, self.dictionary.schema)
Expand All @@ -127,11 +130,11 @@ def test_invalid_files(self):
for path in glob.glob(os.path.join(DATA_DIR, "invalid", "*.json")):
print("Validating {}".format(path))
doc = json.load(open(path, "r"))
if type(doc) == dict:
if isinstance(doc, dict):
self.add_system_props(doc)
with self.assertRaises(ValidationError):
validate_entity(doc, self.dictionary.schema)
elif type(doc) == list:
elif isinstance(doc, list):
for entity in doc:
self.add_system_props(entity)
with self.assertRaises(ValidationError):
Expand All @@ -152,26 +155,24 @@ def add_system_props(self, doc):


if __name__ == "__main__":

####################
# Setup
####################


parser = argparse.ArgumentParser(description="Validate JSON")
parser.add_argument(
"jsonfiles",
metavar="file",
type=argparse.FileType("r"),
nargs="*",
help="json files to test if (in)valid"
help="json files to test if (in)valid",
)

parser.add_argument(
"--invalid",
action="store_true",
default=False,
help="expect the files to be invalid instead of valid"
help="expect the files to be invalid instead of valid",
)

args = parser.parse_args()
Expand All @@ -189,23 +190,22 @@ def add_system_props(self, doc):
try:
print("CHECK if {0} is invalid:".format(f.name), end=" ")
print(type(doc))
if type(doc) == dict:
if isinstance(doc, dict):
validate_entity(doc, dictionary.schema)
elif type(doc) == list:
elif isinstance(doc, list):
for entity in doc:
validate_entity(entity, dictionary.schema)
else:
raise ValidationError("Invalid json")
except ValidationError as e:
print("Invalid as expected.")
pass
else:
raise Exception("Expected invalid, but validated.")
else:
print("CHECK if {0} is valid:".format(f.name), end=" ")
if type(doc) == dict:
if isinstance(doc, dict):
validate_entity(doc, dictionary.schema)
elif type(doc) == list:
elif isinstance(doc, list):
for entity in doc:
validate_entity(entity, dictionary.schema)
else:
Expand Down

0 comments on commit b76cb2e

Please sign in to comment.