Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ install:
- poetry install -vv

script:
- poetry run pytest -vv
- poetry run pytest -vv tests
Copy link

@AlbertSnows AlbertSnows Oct 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: this looks for a tests directory right? If so, I don't see a tests directory.


before_deploy:
- poetry config pypi-token.pypi $PYPI_API_TOKEN
Expand Down
70 changes: 70 additions & 0 deletions example_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""
This is an example of validating json dictionaries (e.g., gdcdictionary/examples)
against the schemas in the local yaml files.
Example usage:
python example_validation.py gdcdictionary/examples/valid/*json
"""

from jsonschema import ValidationError
import argparse
import json

from gdcdictionary import gdcdictionary
from tests.utils import validate_entity

if __name__ == '__main__':

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


parser = argparse.ArgumentParser(description='Validate JSON')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: since parser is only used for args, you can move this to a function call to simplify the main flow

parser.add_argument('jsonfiles', metavar='file',
type=argparse.FileType('r'), nargs='+',
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')

args = parser.parse_args()

####################
# Example validation
####################

# Load schemata
dictionary = gdcdictionary

for f in args.jsonfiles:
doc = json.load(f)
if args.invalid:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: this should be moved outside the function since args is/seems to be unmodified. Even further, you can move the contents to each block to a function and assign that function to a variable.
e.g.

def check_is_invalid(f, doc):
  # ...
def check_is_valid(f, doc):
  # ...

check_validation = check_is_invalid if args.invalid else check_is_valid
for json_file in args.jsonfiles:
  doc = json.load(f)
  check_validation(f, doc)

try:
print("CHECK if {0} is invalid:".format(f.name), end=" ")
print(type(doc))
if type(doc) == dict:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: this if-elif-else section matches the same section except for the else case and can thus be abstracted provided you pass in a custom message
e.g.

def handle_validation(doc, dictionary, unknown_type_handler):
            if type(doc) == dict:
                validate_entity(doc, dictionary.schema)
            elif type(doc) == list:
                for entity in doc:
                    validate_entity(entity, dictionary.schema)
            else:
                unknown_type_handler()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are better ways to do it than what I've written here if I stared at the code longer, but in terms of simplicity I thought of this first.

validate_entity(doc, dictionary.schema)
elif type(doc) == list:
for entity in doc:
validate_entity(entity, dictionary.schema)
else:
raise ValidationError("Invalid json")
except ValidationError as e:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: I don't think this exception has much use can can be removed to simplify behavior since you're just printing

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:
validate_entity(doc, dictionary.schema)
elif type(doc) == list:
for entity in doc:
validate_entity(entity, dictionary.schema)
else:
print("Invalid json")

print("Valid as expected")

print('ok.')
201 changes: 0 additions & 201 deletions gdcdictionary/schema_test.py

This file was deleted.

Loading