Skip to content
Snippets Groups Projects
Commit 3e56a56f authored by Henrik tom Wörden's avatar Henrik tom Wörden
Browse files

Merge branch 'f-deep-copy-messages' into 'dev'

Fix deep copy of messages

See merge request caosdb/caosdb-pylib!35
parents 94c45ebc 05864aa2
Branches
Tags
No related merge requests found
...@@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ...@@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed ### ### Fixed ###
* deepcopy of `_Messages` objects
### Security ### ### Security ###
## [0.4.0] - 2020-07-17## ## [0.4.0] - 2020-07-17##
......
...@@ -2049,6 +2049,9 @@ class _Messages(dict): ...@@ -2049,6 +2049,9 @@ class _Messages(dict):
else: else:
raise TypeError( raise TypeError(
"('type', 'code'), ('type'), or 'type' expected.") "('type', 'code'), ('type'), or 'type' expected.")
if isinstance(key, _Messages._msg_key):
type = key._type # @ReservedAssignment
code = key._code
else: else:
type = key # @ReservedAssignment type = key # @ReservedAssignment
code = None code = None
...@@ -2063,6 +2066,9 @@ class _Messages(dict): ...@@ -2063,6 +2066,9 @@ class _Messages(dict):
else: else:
raise TypeError( raise TypeError(
"('description', 'body'), ('body'), or 'body' expected.") "('description', 'body'), ('body'), or 'body' expected.")
if isinstance(value, Message):
body = value.body
description = value.description
else: else:
body = value body = value
description = None description = None
......
# encoding: utf-8
#
# ** header v3.0
# This file is a part of the CaosDB Project.
#
# Copyright (C) 2020 IndiScale GmbH <info@indiscale.com>
# Copyright (C) 2020 Florian Spreckelsen <f.spreckelsen@indiscale.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# ** end header
#
import caosdb as db
from copy import deepcopy
def test_deepcopy():
"""Test whether deepcopy of _Messages objects doesn't mess up
contained Messages objects.
"""
msgs = db.common.models._Messages()
msg = db.Message(type="bla", code=1234, description="desc", body="blabla")
msgs.append(msg)
msg_copy = deepcopy(msgs)[0]
# make sure type is string-like (formerly caused problems)
assert hasattr(msg_copy.type, "lower")
assert msg_copy.type == msg.type
assert msg_copy.code == msg.code
assert msg_copy.description == msg.description
assert msg_copy.body == msg.body
def test_deepcopy_clear_server():
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])
copied_msgs = deepcopy(msgs)
assert len(copied_msgs) == 2
assert copied_msgs.get("Error", err_msg.code).code == err_msg.code
assert copied_msgs.get("bla", msg.code).code == msg.code
# Only the error should be removed
copied_msgs.clear_server_messages()
assert len(copied_msgs) == 1
assert copied_msgs[0].code == msg.code
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment