From 1ce56524dd3e1ad3d1240292781d81f1b7f210db Mon Sep 17 00:00:00 2001 From: Timm Fitschen <t.fitschen@indiscale.com> Date: Mon, 31 Aug 2020 15:38:35 +0200 Subject: [PATCH] TST: moved tests from pyinttest here --- src/caosdb/common/models.py | 4 +-- unittests/test_message.py | 57 +++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/caosdb/common/models.py b/src/caosdb/common/models.py index 4523b1f8..a5adf6af 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 b209b0f5..5e100305 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. -- GitLab