Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
caosdb-pylib
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
caosdb
Software
caosdb-pylib
Commits
7962c030
Commit
7962c030
authored
3 years ago
by
Alexander Schlemmer
Browse files
Options
Downloads
Patches
Plain Diff
ENH: Added a function for copying an entity including tests
parent
8f983393
No related branches found
Branches containing commit
No related tags found
Tags containing commit
3 merge requests
!57
RELEASE 0.7.3
,
!50
F merge entities
,
!45
F copy entity
Pipeline
#18774
canceled
3 years ago
Stage: code_style
Stage: linting
Stage: test
Stage: deploy
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/caosdb/apiutils.py
+40
-1
40 additions, 1 deletion
src/caosdb/apiutils.py
unittests/test_apiutils.py
+21
-1
21 additions, 1 deletion
unittests/test_apiutils.py
with
61 additions
and
2 deletions
src/caosdb/apiutils.py
+
40
−
1
View file @
7962c030
...
...
@@ -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
=
""
...
...
This diff is collapsed.
Click to expand it.
unittests/test_apiutils.py
+
21
−
1
View file @
7962c030
...
...
@@ -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
])
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment