Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
LOFAR
Manage
Activity
Members
Labels
Plan
Issues
Wiki
Jira issues
Open Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
RadioObservatory
LOFAR
Commits
05fcf7fc
Commit
05fcf7fc
authored
5 years ago
by
Jorrit Schaap
Browse files
Options
Downloads
Patches
Plain Diff
TMSS-142
: check for valid json
parent
d042c9c9
No related branches found
Branches containing commit
No related tags found
Tags containing commit
2 merge requests
!98
Resolve TMSS-142 and TMSS-141
,
!97
Resolve TMSS-138
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
SAS/TMSS/src/tmss/tmssapp/models/scheduling.py
+8
-1
8 additions, 1 deletion
SAS/TMSS/src/tmss/tmssapp/models/scheduling.py
SAS/TMSS/test/t_subtask_validation.py
+19
-0
19 additions, 0 deletions
SAS/TMSS/test/t_subtask_validation.py
with
27 additions
and
1 deletion
SAS/TMSS/src/tmss/tmssapp/models/scheduling.py
+
8
−
1
View file @
05fcf7fc
...
...
@@ -153,12 +153,19 @@ class Subtask(BasicCommon):
# resource_claim = ForeignKey("ResourceClaim", null=False, on_delete=PROTECT) # todo <-- how is this external reference supposed to work?
def
validate_specification_against_schema
(
self
):
if
self
.
specifications_doc
is
None
or
self
.
specifications_template_id
is
None
:
return
try
:
# ensure the specification and schema are both valid json in the first place
spec
=
json
.
loads
(
self
.
specifications_doc
)
if
type
(
self
.
specifications_doc
)
==
str
else
self
.
specifications_doc
schema
=
json
.
loads
(
self
.
specifications_template
.
schema
)
if
type
(
self
.
specifications_template
.
schema
)
==
str
else
self
.
specifications_template
.
schema
except
json
.
decoder
.
JSONDecodeError
as
e
:
raise
SpecificationException
(
"
Invalid JSON: %s
"
%
str
(
e
))
try
:
jsonschema
.
validate
(
spec
,
schema
)
except
Exception
as
e
:
except
jsonschema
.
ValidationError
as
e
:
raise
SpecificationException
(
str
(
e
))
def
save
(
self
,
force_insert
=
False
,
force_update
=
False
,
using
=
None
,
update_fields
=
None
):
...
...
This diff is collapsed.
Click to expand it.
SAS/TMSS/test/t_subtask_validation.py
+
19
−
0
View file @
05fcf7fc
...
...
@@ -86,6 +86,25 @@ class SubtaskValidationTest(unittest.TestCase):
subtask
=
models
.
Subtask
.
objects
.
create
(
**
subtask_data
)
self
.
assertIsNotNone
(
subtask
)
def
test_validate_flawed_json_schema
(
self
):
subtask_template
=
self
.
create_subtask_template
(
'
{ this is not a json object }
'
)
specifications_doc
=
'"
a random string
"'
subtask_data
=
Subtask_test_data
(
subtask_template
,
specifications_doc
)
with
self
.
assertRaises
(
SpecificationException
)
as
context
:
models
.
Subtask
.
objects
.
create
(
**
subtask_data
)
self
.
assertTrue
(
'
invalid json
'
in
str
(
context
.
exception
).
lower
())
def
test_validate_flawed_json_specification
(
self
):
subtask_template
=
self
.
create_subtask_template
(
'
{
"
type
"
:
"
string
"
}
'
)
specifications_doc
=
'
{ this is not a json object }
'
subtask_data
=
Subtask_test_data
(
subtask_template
,
specifications_doc
)
with
self
.
assertRaises
(
SpecificationException
)
as
context
:
models
.
Subtask
.
objects
.
create
(
**
subtask_data
)
self
.
assertTrue
(
'
invalid json
'
in
str
(
context
.
exception
).
lower
())
if
__name__
==
"
__main__
"
:
os
.
environ
[
'
TZ
'
]
=
'
UTC
'
unittest
.
main
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment