From 176cf6fb54cbbcdd02b1f4fe51a5668c9a1f1224 Mon Sep 17 00:00:00 2001 From: Samuel Gaist <samuel.gaist@idiap.ch> Date: Fri, 14 Jun 2019 09:58:19 +0200 Subject: [PATCH] [protocoltemplate] Use error_on_duplicate_key_hook when loading data --- beat/backend/python/protocoltemplate.py | 14 +++++++++++--- .../protocoltemplates/duplicate_key_error/1.json | 13 +++++++++++++ .../protocoltemplates/duplicate_key_error/1.rst | 0 beat/backend/python/test/test_protocoltemplate.py | 11 +++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 beat/backend/python/test/prefix/protocoltemplates/duplicate_key_error/1.json create mode 100644 beat/backend/python/test/prefix/protocoltemplates/duplicate_key_error/1.rst diff --git a/beat/backend/python/protocoltemplate.py b/beat/backend/python/protocoltemplate.py index 7c0e12d..b6bf9dc 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 0000000..17b86c1 --- /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 0000000..e69de29 diff --git a/beat/backend/python/test/test_protocoltemplate.py b/beat/backend/python/test/test_protocoltemplate.py index 6f1a931..b6162a0 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] + ) -- GitLab