-
Notifications
You must be signed in to change notification settings - Fork 10
Chore/add unit tests #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
a0993e1
83e39cf
3278dd6
9db653f
73b01ec
e11d137
b5fee2e
38df5f0
44e87b2
438f673
ec97744
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. 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: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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()There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.') | ||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.