Skip to content
Snippets Groups Projects
Commit 2c31ad17 authored by Alexander Kreft's avatar Alexander Kreft
Browse files

fix + tests

parent e0e998ea
No related branches found
No related tags found
1 merge request!18ENH: add is_reference to db.Property
...@@ -1506,20 +1506,33 @@ class Property(Entity): ...@@ -1506,20 +1506,33 @@ class Property(Entity):
return super(Property, self).to_xml(xml, add_properties) return super(Property, self).to_xml(xml, add_properties)
def is_reference(self): def is_reference(self, server_retrieval = False):
""" returns whether this Property is a reference """Returns whether this Property is a reference
If the datatype is not set, the Property is retrieved from the server. Parameters
""" ----------
server_retrieval : bool, optional
If True and the datatype is not set, the Property is retrieved from the server, by default False
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.
"""
if self.datatype is None: if self.datatype is None:
if not self.is_valid(): if not self.is_valid():
# this is a workaround to prevent side effects # this is a workaround to prevent side effects
# since retrieve currently changes the object # since retrieve currently changes the object
if server_retrieval:
tmp_prop = deepcopy(self) tmp_prop = deepcopy(self)
tmp_prop.retrieve() tmp_prop.retrieve()
return tmp_prop.is_reference() return tmp_prop.is_reference()
else:
return None
else: else:
# a valid property withoud datatype has to be an RT # a valid property withoud datatype has to be an RT
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
# pylint: disable=missing-docstring # pylint: disable=missing-docstring
from lxml import etree from lxml import etree
from caosdb import Entity, Property, Record from caosdb import Entity, Property, Record
import caosdb as db
parser = etree.XMLParser(remove_comments=True) parser = etree.XMLParser(remove_comments=True)
testrecord = Record._from_xml(Record(), testrecord = Record._from_xml(Record(),
...@@ -89,3 +90,38 @@ def test_get_property_with_entity(): ...@@ -89,3 +90,38 @@ def test_get_property_with_entity():
def test_selected_reference_list(): def test_selected_reference_list():
assert len(testrecord.get_property("Conductor").value) == 1 assert len(testrecord.get_property("Conductor").value) == 1
assert isinstance(testrecord.get_property("Conductor").value[0], Entity) assert isinstance(testrecord.get_property("Conductor").value[0], Entity)
PROPS = {
10: db.Property(id=10, datatype=db.INTEGER),
20: db.Property(id=20, datatype=db.REFERENCE)
}
def dummy_property(self):
return PROPS[self.id]
Entity.retrieve = dummy_property
def test_is_reference():
p1 = Property(id=1, datatype=db.INTEGER)
p2 = Property(id=2, datatype=db.DOUBLE)
p3 = Property(id=3, datatype=db.TEXT)
p4 = Property(id=4, datatype=db.DATETIME)
p5 = Property(id=5, datatype=db.BOOLEAN)
p6 = Property(id=6, datatype=db.REFERENCE)
assert p1.is_reference() == False
assert p2.is_reference() == False
assert p3.is_reference() == False
assert p4.is_reference() == False
assert p5.is_reference() == False
assert p6.is_reference() == True
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 p8.is_reference() == True
p10 = Property(id=10)
p20 = Property(id=20)
assert p10.is_reference(server_retrieval=True) == False
assert p20.is_reference(server_retrieval=True) == True
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment