Skip to content

Commit

Permalink
Merge pull request #104 from PDXCapstoneF/bs-validator-exceptions
Browse files Browse the repository at this point in the history
`validate()` throws exceptions
  • Loading branch information
Zonr0 authored May 3, 2018
2 parents f7d1a4e + 54f27ca commit 479d9d4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 65 deletions.
10 changes: 5 additions & 5 deletions src/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,27 @@ def validate(unvalidated):
# they need to appear in the template
for arg in run["args"]:
if arg not in t["args"]:
return None
raise Exception("Argument '{}' was not in the template {}'s arguments: {}".format(arg, run["template_type"], t["args"]))

# and if the arg isn't in the run,
# it needs to have a default
for arg in t["args"]:
if arg not in run["args"] and arg not in t["default_props"]:
return None
raise Exception("Argument '{}' did not have a default from template {}".format(arg, run["template_type"]))

# for each template,
for template in d["TemplateData"].values():
for name, template in d["TemplateData"].items():
# all of the translations need to refer to
# arguments specified by the user
if "translations" in template:
for translation in template["translations"]:
if translation not in template["args"]:
return None
raise Exception("Translation '{}' for template '{}' doesn't have an associated argument".format(translation, name))
# all of the annotations need to refer to
# arguments specified by the user
if "annotations" in template:
for annotation in template["annotations"]:
if annotation not in template["args"]:
return None
raise Exception("Annotation '{}' for template '{}' doesn't have an associated argument".format(annotation, name))

return d
124 changes: 64 additions & 60 deletions tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ def test_invalid_config_does_not_validate(self):
self.assertFalse(validate(j))

def test_TemplateData_with_extra_translations_dont_validate(self):
self.assertFalse(validate({
"TemplateData": {
"example": {
"args": [],
"translations": {
"arg1": "someValue",
with self.assertRaises(Exception):
validate({
"TemplateData": {
"example": {
"args": [],
"translations": {
"arg1": "someValue",
},
},
},
},
"RunList": [],
}))
"RunList": [],
})

def test_there_should_be_TemplateData_if_there_are_RunList(self):
with self.assertRaises(Exception):
Expand All @@ -42,64 +43,67 @@ def test_there_should_be_TemplateData_if_there_are_RunList(self):
}))

def test_RunList_with_extra_args_fail_to_validate(self):
self.assertFalse(validate({
"TemplateData": {
"example": {
"args": [],
"translations": {
"arg1": "someValue",
with self.assertRaises(Exception):
validate({
"TemplateData": {
"example": {
"args": [],
"translations": {
"arg1": "someValue",
},
},
},
},
"RunList": [
{
"template_type": "example",
"args": {
"a": "b",
"arg1": 5,
},
}
],
}))
"RunList": [
{
"template_type": "example",
"args": {
"a": "b",
"arg1": 5,
},
}
],
})

def test_RunList_with_ommitted_with_no_defaults_fail_to_validate(self):
self.assertFalse(validate({
"TemplateData": {
"example": {
"args": [
"arg1",
"noDefaults",
],
"translations": {
"arg1": "someValue",
},
"default_props": {
"arg1": "defaultvalue",
with self.assertRaises(Exception):
validate({
"TemplateData": {
"example": {
"args": [
"arg1",
"noDefaults",
],
"translations": {
"arg1": "someValue",
},
"default_props": {
"arg1": "defaultvalue",
},
},
},
},
"RunList": [
{
"template_type": "example",
"args": {
"arg1": 5,
},
}
],
}))
"RunList": [
{
"template_type": "example",
"args": {
"arg1": 5,
},
}
],
})

def test_RunList_with_extra_annotations_fail_to_validate(self):
self.assertFalse(validate({
"TemplateData": {
"example": {
"args": [
"arg1",
],
"annotations": {
"arg1": "someValue",
"extraAnnotation": "someValue",
with self.assertRaises(Exception):
validate({
"TemplateData": {
"example": {
"args": [
"arg1",
],
"annotations": {
"arg1": "someValue",
"extraAnnotation": "someValue",
},
},
},
},
"RunList": []
}))
"RunList": []
})

0 comments on commit 479d9d4

Please sign in to comment.