Skip to content
Snippets Groups Projects
Commit 176cf6fb authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[protocoltemplate] Use error_on_duplicate_key_hook when loading data

parent 771f3fa0
Branches
Tags
1 merge request!51Handle duplicate key
Pipeline #30907 passed
......@@ -42,7 +42,7 @@ protocoltemplates
Validation of database protocol templates
"""
import simplejson
import simplejson as json
from .dataformat import DataFormat
......@@ -142,7 +142,15 @@ class ProtocolTemplate(object):
return
with open(json_path, "rt") as f:
self.data = simplejson.loads(f.read())
try:
self.data = json.loads(
f.read(), object_pairs_hook=utils.error_on_duplicate_key_hook
)
except RuntimeError as error:
self.errors.append(
"Protocol template declaration file invalid: %s" % error
)
return
for set_ in self.data["sets"]:
......@@ -238,7 +246,7 @@ class ProtocolTemplate(object):
"""
return simplejson.dumps(self.data, indent=indent, cls=utils.NumpyJSONEncoder)
return json.dumps(self.data, indent=indent, cls=utils.NumpyJSONEncoder)
def __str__(self):
return self.json_dumps()
......
{
"schema_version": 1,
"sets": [
{
"name": "labelled",
"name": "labelled1",
"outputs": {
"value": "user/single_integer/1",
"label": "user/single_string/1"
}
}
]
}
......@@ -111,3 +111,14 @@ def test_load_protocol_with_two_sets():
nose.tools.assert_is_not_none(set_["outputs"]["b"])
nose.tools.assert_is_not_none(set_["outputs"]["c"])
nose.tools.assert_is_not_none(set_["outputs"]["sum"])
# ----------------------------------------------------------
def test_duplicate_key_error():
protocoltemplate = ProtocolTemplate(prefix, "duplicate_key_error/1")
nose.tools.assert_false(protocoltemplate.valid)
nose.tools.assert_true(
"Protocol template declaration file invalid" in protocoltemplate.errors[0]
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment