diff --git a/src/caosdb/common/models.py b/src/caosdb/common/models.py index df33da7cbe0e778c6531bf3685f4b96e96359e00..b55908be6c22d2eb795964e7292e0eb42c0123f3 100644 --- a/src/caosdb/common/models.py +++ b/src/caosdb/common/models.py @@ -1870,12 +1870,10 @@ class Property(Entity): class Message(object): - # @ReservedAssignment - - def __init__(self, type, code=None, description=None, body=None): # @ReservedAssignment - self.type = type - self.code = code + def __init__(self, description, type=None, code=None, body=None): # @ReservedAssignment self.description = description + self.type = type if type is not None else "Info" + self.code = int(code) if code is not None else None self.body = body def to_xml(self, xml=None): @@ -1898,11 +1896,13 @@ class Message(object): def __eq__(self, obj): if isinstance(obj, Message): - return self.type == obj.type and self.code == obj.code + return self.type == obj.type and self.code == obj.code and self.description == obj.description return False def get_code(self): + warn(("get_code is deprecated and will be removed in future. " + "Use self.code instead."), DeprecationWarning) return int(self.code) diff --git a/unittests/test_high_level_api.py b/unittests/test_high_level_api.py index a9e55c9c2a79f7ead8bbb3fb652c1b81427e69e9..51993b78b700236618daef2f07dbe754121384c4 100644 --- a/unittests/test_high_level_api.py +++ b/unittests/test_high_level_api.py @@ -154,7 +154,7 @@ def test_convert_with_references(): obj = convert_to_python_object(r) assert obj.ref.a == 42 # Parent does not automatically lead to a datatype: - assert obj.get_property_metadata("ref").datatype is "bla" + assert obj.get_property_metadata("ref").datatype == "bla" assert obj.ref.has_parent("bla") is True # Unresolved Reference: @@ -163,7 +163,7 @@ def test_convert_with_references(): obj = convert_to_python_object(r) # Parent does not automatically lead to a datatype: - assert obj.get_property_metadata("ref").datatype is "bla" + assert obj.get_property_metadata("ref").datatype == "bla" assert isinstance(obj.ref, CaosDBPythonUnresolvedReference) assert obj.ref.id == 27 diff --git a/unittests/test_message.py b/unittests/test_message.py index f72277555d0c6ba88d95a704bb537707314a33a8..0e9f6fe2dfa0d97cace9db05b53f56993e823052 100644 --- a/unittests/test_message.py +++ b/unittests/test_message.py @@ -32,9 +32,9 @@ import pytest def test_messages_dict_behavior(): from caosdb.common.models import Message - from caosdb.common.models import _Messages + from caosdb.common.models import Messages - msgs = _Messages() + msgs = Messages() # create Message msg = Message( @@ -43,12 +43,12 @@ def test_messages_dict_behavior(): description="Greeting the world", body="Hello, world!") - # append it to the _Messages + # 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 + # use Messages as list of Message objects for m in msgs: assert isinstance(m, Message) @@ -82,11 +82,11 @@ def test_messages_dict_behavior(): def test_deepcopy(): - """Test whether deepcopy of _Messages objects doesn't mess up + """Test whether deepcopy of Messages objects doesn't mess up contained Messages objects. """ - msgs = db.common.models._Messages() + msgs = db.common.models.Messages() msg = db.Message(type="bla", code=1234, description="desc", body="blabla") msgs.append(msg) msg_copy = deepcopy(msgs)[0] @@ -101,7 +101,7 @@ def test_deepcopy(): def test_deepcopy_clear_server(): - msgs = db.common.models._Messages() + msgs = db.common.models.Messages() msg = db.Message(type="bla", code=1234, description="desc", body="blabla") err_msg = db.Message(type="Error", code=1357, description="error") msgs.extend([msg, err_msg]) @@ -115,3 +115,17 @@ def test_deepcopy_clear_server(): copied_msgs.clear_server_messages() assert len(copied_msgs) == 1 assert copied_msgs[0].code == msg.code + +def test_list_behavior(): + msgs = db.common.models.Messages() + msgs.append(db.Message("test")) + assert len(msgs) == 1 + assert msgs[0] == db.Message("test") + assert msgs[0] != db.Message("test2") + + msgs.append(db.Message("test")) + assert len(msgs) == 2 + assert msgs[0] == msgs[1] + + with pytest.raises(IndexError): + msgs[3]