From 65e5efa5cf835196556df6d1a6aa9ebedd0f5c25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20tom=20W=C3=B6rden?= <henrik@trineo.org> Date: Tue, 13 Jul 2021 09:38:39 +0200 Subject: [PATCH] fix test --- src/caosdb/common/models.py | 19 ++++++++++--------- unittests/test_property.py | 26 +++++++++++++++++--------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/caosdb/common/models.py b/src/caosdb/common/models.py index 6624ed76..57b07bd6 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 6fee32cd..6e1318be 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 -- GitLab