Skip to content
Snippets Groups Projects
Commit 7962c030 authored by Alexander Schlemmer's avatar Alexander Schlemmer
Browse files

ENH: Added a function for copying an entity including tests

parent 8f983393
No related branches found
No related tags found
3 merge requests!57RELEASE 0.7.3,!50F merge entities,!45F copy entity
Pipeline #18774 canceled
......@@ -557,7 +557,7 @@ def getCommitIn(folder):
return t.readline().strip()
COMPARED = ["name", "role", "datatype", "description", "importance",
COMPARED = ["name", "role", "datatype", "description",
"id", "path", "checksum", "size"]
......@@ -673,6 +673,45 @@ def compare_entities(old_entity: Entity, new_entity: Entity):
return (olddiff, newdiff)
def copy_entity(entity: Entity):
"""
Return a copy of entity.
If deep == True return a deep copy, recursively copying all sub entities.
"""
print(entity)
if entity.role == "File":
new = File()
elif entity.role == "Property":
new = Property()
elif entity.role == "RecordType":
new = RecordType()
elif entity.role == "Record":
new = Record()
elif entity.role == "Entity":
new = Entity()
else:
raise RuntimeError("Unkonwn role.")
# Copy special attributes:
# TODO: this might rise an exception when copying
# special file attributes like checksum and size.
for attribute in COMPARED + ["value"]:
val = getattr(entity, attribute)
if val is not None:
setattr(new, attribute, val)
# Copy parents:
for p in entity.parents:
new.add_parent(p)
# Copy properties:
for p in entity.properties:
new.add_property(p, importance=entity.get_importance(p))
return new
def describe_diff(olddiff, newdiff, name=None, as_update=True):
description = ""
......
......@@ -31,7 +31,7 @@ import tempfile
import caosdb as db
import caosdb.apiutils
from caosdb.apiutils import (apply_to_ids, compare_entities, create_id_query,
resolve_reference)
resolve_reference, copy_entity, merge_entities)
from .test_property import testrecord
......@@ -230,3 +230,23 @@ def test_compare_special_properties():
assert diff_r2[key] == 2
assert len(diff_r1["properties"]) == 0
assert len(diff_r2["properties"]) == 0
def test_copy_entities():
r = db.Record(name="A")
r.add_parent(name="B")
r.add_property(name="C", value=4, importance="OBLIGATORY")
r.add_property(name="D", value=[3, 4, 7], importance="OBLIGATORY")
r.description = "A fancy test record"
c = copy_entity(r)
assert c != r
assert c.name == "A"
assert c.parents[0].name == "B"
# Currently parents and properties are always individual to a copy:
assert c.parents[0] != r.parents[0]
for i in [0, 1]:
assert c.properties[i] != r.properties[i]
assert c.properties[i].value == r.properties[i].value
assert c.get_importance(c.properties[i]) == r.get_importance(r.properties[i])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment