diff --git a/beat/backend/python/protocoltemplate.py b/beat/backend/python/protocoltemplate.py index 7c0e12d309f3cd2dfc25a32572293ab97e0deff3..b6bf9dc1ab0b72f564e53f797048a89979b2e3b3 100644 --- a/beat/backend/python/protocoltemplate.py +++ b/beat/backend/python/protocoltemplate.py @@ -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() diff --git a/beat/backend/python/test/prefix/protocoltemplates/duplicate_key_error/1.json b/beat/backend/python/test/prefix/protocoltemplates/duplicate_key_error/1.json new file mode 100644 index 0000000000000000000000000000000000000000..17b86c1ad2f3d3ec580f62fc4de10bbe1aec5d41 --- /dev/null +++ b/beat/backend/python/test/prefix/protocoltemplates/duplicate_key_error/1.json @@ -0,0 +1,13 @@ +{ + "schema_version": 1, + "sets": [ + { + "name": "labelled", + "name": "labelled1", + "outputs": { + "value": "user/single_integer/1", + "label": "user/single_string/1" + } + } + ] +} diff --git a/beat/backend/python/test/prefix/protocoltemplates/duplicate_key_error/1.rst b/beat/backend/python/test/prefix/protocoltemplates/duplicate_key_error/1.rst new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/beat/backend/python/test/test_protocoltemplate.py b/beat/backend/python/test/test_protocoltemplate.py index 6f1a931cd3967e272fdd040cb9767e84d5a98361..b6162a0b6cd451ff7703f9acb338fdeae0b1dc58 100644 --- a/beat/backend/python/test/test_protocoltemplate.py +++ b/beat/backend/python/test/test_protocoltemplate.py @@ -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] + )