diff --git a/src/caosdb/common/models.py b/src/caosdb/common/models.py index 6624ed765906ce6f47d36975af93e069efa9550e..57b07bd6f83cf2dd4d0523a70308ee189a96a995 100644 --- a/src/caosdb/common/models.py +++ b/src/caosdb/common/models.py @@ -28,10 +28,10 @@ """missing docstring.""" from __future__ import print_function, unicode_literals -from copy import deepcopy import re import sys from builtins import str +from copy import deepcopy from functools import cmp_to_key from hashlib import sha512 from os import listdir @@ -45,7 +45,6 @@ from caosdb.common.datatype import (BOOLEAN, DATETIME, DOUBLE, INTEGER, TEXT, is_list_datatype, is_reference) from caosdb.common.state import State from caosdb.common.utils import uuid, xml2str -from caosdb.common.datatype import is_reference from caosdb.common.versioning import Version from caosdb.configuration import get_config from caosdb.connection.connection import get_connection @@ -1506,7 +1505,7 @@ class Property(Entity): return super(Property, self).to_xml(xml, add_properties) - def is_reference(self, server_retrieval = False): + def is_reference(self, server_retrieval=False): """Returns whether this Property is a reference Parameters @@ -1517,24 +1516,26 @@ class Property(Entity): Returns ------- bool, NoneType - Returns whether this Property is a reference or None if a server call is needed to - check correctly, but server_retrieval is set to False. - + Returns whether this Property is a reference or None if a server call is needed to + check correctly, but server_retrieval is set to False. + """ + if self.datatype is None: + if not self.is_valid(): # this is a workaround to prevent side effects # since retrieve currently changes the object - + if server_retrieval: tmp_prop = deepcopy(self) tmp_prop.retrieve() return tmp_prop.is_reference() else: - return None + return None else: - # a valid property withoud datatype has to be an RT + # a valid property without datatype has to be an RT return True else: diff --git a/unittests/test_property.py b/unittests/test_property.py index 6fee32cd2304c2889e3f75992408f54139869958..6e1318bed2f9b1b945edb5e0a256b6cf7e9a722e 100644 --- a/unittests/test_property.py +++ b/unittests/test_property.py @@ -24,10 +24,10 @@ # ** end header # """Tests for the Property class.""" +import caosdb as db +from caosdb import Entity, Property, Record # pylint: disable=missing-docstring from lxml import etree -from caosdb import Entity, Property, Record -import caosdb as db parser = etree.XMLParser(remove_comments=True) testrecord = Record._from_xml(Record(), @@ -93,12 +93,19 @@ def test_selected_reference_list(): PROPS = { - 10: db.Property(id=10, datatype=db.INTEGER), - 20: db.Property(id=20, datatype=db.REFERENCE) + 10: db.INTEGER, + 20: db.REFERENCE, + 30: "SomeRT", } -def dummy_property(self): - return PROPS[self.id] -Entity.retrieve = dummy_property + + +def dummy_retrieve(self): + self.datatype = PROPS[self.id] + self.is_valid = lambda: True + + +Entity.retrieve = dummy_retrieve + def test_is_reference(): p1 = Property(id=1, datatype=db.INTEGER) @@ -117,11 +124,12 @@ def test_is_reference(): p7 = Property(id=7) p8 = Property(id=8, value=db.RecordType(id=1000)) p8.is_valid = lambda: True - assert p7.is_reference() == None #cannot be resolved without calling a server + assert p7.is_reference() == None # cannot be resolved without calling a server assert p8.is_reference() == True p10 = Property(id=10) p20 = Property(id=20) + p30 = Property(id=30) assert p10.is_reference(server_retrieval=True) == False assert p20.is_reference(server_retrieval=True) == True - + assert p30.is_reference(server_retrieval=True) == True