diff --git a/src/caosdb/common/models.py b/src/caosdb/common/models.py index 4523b1f89d27e26dc32bb7f02485d9bd4acdff10..a5adf6af68b72663c0fe8cd6ab58c39cdcea5d27 100644 --- a/src/caosdb/common/models.py +++ b/src/caosdb/common/models.py @@ -2049,7 +2049,7 @@ class _Messages(dict): else: raise TypeError( "('type', 'code'), ('type'), or 'type' expected.") - if isinstance(key, _Messages._msg_key): + elif isinstance(key, _Messages._msg_key): type = key._type # @ReservedAssignment code = key._code else: @@ -2073,7 +2073,7 @@ class _Messages(dict): body = value description = None m = Message(type=type, code=code, description=description, body=body) - self.append(m) + dict.__setitem__(self, _Messages._msg_key(type, code), m) def __getitem__(self, key): if isinstance(key, tuple): diff --git a/unittests/test_message.py b/unittests/test_message.py index b209b0f5b851b929d42f0624cd2a9357755534f1..5e1003056c1b606a004b63bb7618e5e0474952bc 100644 --- a/unittests/test_message.py +++ b/unittests/test_message.py @@ -3,6 +3,8 @@ # ** header v3.0 # This file is a part of the CaosDB Project. # +# Copyright (C) 2018 Research Group Biomedical Physics, +# Max-Planck-Institute for Dynamics and Self-Organization Göttingen # Copyright (C) 2020 IndiScale GmbH <info@indiscale.com> # Copyright (C) 2020 Florian Spreckelsen <f.spreckelsen@indiscale.com> # @@ -25,6 +27,61 @@ import caosdb as db from copy import deepcopy +def test_messages_dict_behavior(): + from caosdb.common.models import Message + from caosdb.common.models import _Messages + + msgs = _Messages() + + # create Message + msg = Message( + type="HelloWorld", + code=1, + description="Greeting the world", + body="Hello, world!") + + # append it to the _Messages + assert repr(msg) == '<HelloWorld code="1" description="Greeting the world">Hello, world!</HelloWorld>\n' + msgs.append(msg) + assert len(msgs) == 1 + + # use _Messages as list of Message objects + for m in msgs: + assert isinstance(m, Message) + + # remove it + msgs.remove(msg) + assert len(msgs) == 0 + + # ok append it again ... + msgs.append(msg) + assert len(msgs) == 1 + # get it back via get(...) and the key tuple (type, code) + assert id(msgs.get("HelloWorld", 1)) == id(msg) + + # delete Message via remove and the (type,code) tuple + msgs.remove("HelloWorld", 1) + assert msgs.get("HelloWorld", 1) is None + assert len(msgs) == 0 + + # short version of adding/setting/resetting a new Message + msgs["HelloWorld", 2] = "Greeting the world in German", "Hallo, Welt!" + assert len(msgs) == 1 + assert msgs["HelloWorld", 2] == ( + "Greeting the world in German", "Hallo, Welt!") + + msgs["HelloWorld", 2] = "Greeting the world in German", "Huhu, Welt!" + assert len(msgs) == 1 + assert msgs["HelloWorld", 2] == ( + "Greeting the world in German", "Huhu, Welt!") + del msgs["HelloWorld", 2] + assert msgs.get("HelloWorld", 2) is None + + # this Message has no code and no description (make easy things easy...) + msgs["HelloWorld"] = "Hello!" + assert msgs["HelloWorld"] == "Hello!" + + def test_deepcopy(): """Test whether deepcopy of _Messages objects doesn't mess up contained Messages objects.