diff --git a/CHANGELOG.md b/CHANGELOG.md index e0363c4b7f755aef99f98bf353e8d103b01532af..ac82507b645fb9347fb6de57f63af510b05141b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed ### +* [#82](https://gitlab.com/caosdb/caosdb-pylib/-/issues/82) Merging an entity + with properties with missing datatype leads to Exception - The correct + exception is raised in case of a missing LIST datatype. + ### Security ### ### Documentation ### diff --git a/src/caosdb/common/models.py b/src/caosdb/common/models.py index 7000ede917995c6c01b78a822c2d39ac626fcc23..83359ac847fa62de976208f9af023a2cf2a73af6 100644 --- a/src/caosdb/common/models.py +++ b/src/caosdb/common/models.py @@ -62,7 +62,6 @@ from caosdb.exceptions import (AmbiguousEntityError, AuthorizationError, EntityDoesNotExistError, EntityError, EntityHasNoDatatypeError, HTTPURITooLongError, MismatchingEntitiesError, QueryNotUniqueError, - ServerConfigurationException, TransactionError, UniqueNamesError, UnqualifiedParentsError, UnqualifiedPropertiesError) @@ -1381,12 +1380,15 @@ def _parse_value(datatype, value): # reference via name return str(value) - except TypeError: + except TypeError as te: # deal with invalid XML: List of values without appropriate datatype if isinstance(value, list): - raise ServerConfigurationException( - "The server sent an invalid XML: List valued properties must be announced by " + raise TypeError( + "Invalid datatype: List valued properties must be announced by " "the datatype.\n" + f"Datatype: {datatype}\nvalue: {value}") + else: + # Everything else that's not related to wrong list assignments + raise te def _log_request(request, xml_body=None): diff --git a/unittests/test_apiutils.py b/unittests/test_apiutils.py index 9b87a743b201213feb49cfc660e006f2c6217387..bda381cf6427377194e272dfa14b83399b6f012f 100644 --- a/unittests/test_apiutils.py +++ b/unittests/test_apiutils.py @@ -559,3 +559,18 @@ B: something else""" assert recA.get_property("propA").unit == "cm" # unchanged assert recB.get_property("propA").unit == "cm" + + +def test_merge_missing_list_datatype_82(): + """Merging two properties, where the list-valued one has no datatype.""" + + recA = db.Record().add_property("a", 5, datatype="B") + recB_with_DT = db.Record().add_property("a", [1, 2], datatype=f"LIST<{db.DOUBLE}>") + merge_entities(recA, recB_with_DT, force=True) + assert recA.get_property("a").datatype == f"LIST<{db.DOUBLE}>" + + recA = db.Record().add_property("a", 5, datatype="B") + recB_without_DT = db.Record().add_property("a", [1, 2]) + with pytest.raises(TypeError) as te: + merge_entities(recA, recB_without_DT, force=True) + assert "Invalid datatype: List valued properties" in str(te.value) diff --git a/unittests/test_issues.py b/unittests/test_issues.py index 1e649db4f23de67e55301e0a053fba70d14680b4..2c45a6d77ba61c3f948e403f708994c0fe31481a 100644 --- a/unittests/test_issues.py +++ b/unittests/test_issues.py @@ -34,6 +34,6 @@ def test_issue_100(): # Parse from (invalid) XML file filename = os.path.join(os.path.dirname(__file__), "data", "list_in_value.xml") xml_el = lxml.etree.parse(filename).getroot() - with raises(db.ServerConfigurationException) as exc_info: + with raises(TypeError) as exc_info: db.common.models._parse_single_xml_element(xml_el) - assert "invalid XML: List valued properties" in exc_info.value.msg + assert "Invalid datatype: List valued properties" in str(exc_info.value)