Skip to content
Snippets Groups Projects
Verified Commit 5cba79f3 authored by Alexander Kreft's avatar Alexander Kreft
Browse files

ENH: Better handling for entity lists

parent d5b3e46b
No related branches found
No related tags found
1 merge request!22Release 0.3
Pipeline #14733 passed with warnings
...@@ -47,6 +47,7 @@ from abc import ABCMeta, abstractmethod ...@@ -47,6 +47,7 @@ from abc import ABCMeta, abstractmethod
from datetime import datetime from datetime import datetime
import caosdb as db import caosdb as db
from caosdb.common.models import Entity
from caosdb.exceptions import (BadQueryError, EmptyUniqueQueryError, from caosdb.exceptions import (BadQueryError, EmptyUniqueQueryError,
QueryNotUniqueError, TransactionError) QueryNotUniqueError, TransactionError)
...@@ -661,12 +662,18 @@ def assure_has_property(entity, name, value, to_be_updated=None, ...@@ -661,12 +662,18 @@ def assure_has_property(entity, name, value, to_be_updated=None,
if isinstance(value, db.Entity): if isinstance(value, db.Entity):
value = value.id value = value.id
if isinstance(value, list):
value = [i.id if isinstance(i, db.Entity) else i for i in value]
for el in possible_properties: for el in possible_properties:
tmp_value = el.value tmp_value = el.value
if isinstance(tmp_value, db.Entity): if isinstance(tmp_value, db.Entity):
tmp_value = el.value.id tmp_value = el.value.id
if isinstance(tmp_value, list):
tmp_value = [i.id if isinstance(i, db.Entity) else i for i in tmp_value]
if tmp_value == value: if tmp_value == value:
contained = True contained = True
......
...@@ -190,6 +190,28 @@ class InsertionTest(unittest.TestCase): ...@@ -190,6 +190,28 @@ class InsertionTest(unittest.TestCase):
value=new_int, to_be_updated=to_be_updated) value=new_int, to_be_updated=to_be_updated)
assert to_be_updated[0] is entity assert to_be_updated[0] is entity
"""Test properties with lists"""
rec1 = db.Record(id=12323)
rec1.add_property("Exp", value=[123333], datatype=db.LIST("Exp"))
rec2 = db.Record(id=123333)
update = []
assure_has_property(rec1, "Exp", [rec2], to_be_updated=update)
assert len(update) == 0
update = []
assure_has_property(rec1, "Exp", [123333], to_be_updated=update)
assert len(update) == 0
update = []
assure_has_property(rec1, "Exp2", [123333, 456, 789], to_be_updated=update)
assert len(update) == 1
rec = db.Record(id=456456)
rec3 = db.Record(id=789789)
rec.add_property("Exp", value=[123123, rec3], datatype=db.LIST("Exp"))
rec2 = db.Record(id=123123)
update = []
assure_has_property(rec, "Exp", [rec2, 789789], to_be_updated=update)
assert len(update) == 0
def test_property_is(self): def test_property_is(self):
"""Test properties with string, int, float, and Boolean values""" """Test properties with string, int, float, and Boolean values"""
entity = db.Record() entity = db.Record()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment