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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
caosdb
Software
caosdb-pylib
Commits
e71431c4
Commit
e71431c4
authored
Mar 2, 2022
by
Alexander Schlemmer
Browse files
Options
Downloads
Patches
Plain Diff
ENH: completed implementation of resolve references
parent
c2e8e4e9
No related branches found
No related tags found
2 merge requests
!57
RELEASE 0.7.3
,
!52
F refactor high level api
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/caosdb/high_level_api.py
+4
-1
4 additions, 1 deletion
src/caosdb/high_level_api.py
unittests/test_high_level_api.py
+75
-1
75 additions, 1 deletion
unittests/test_high_level_api.py
with
79 additions
and
2 deletions
src/caosdb/high_level_api.py
+
4
−
1
View file @
e71431c4
...
...
@@ -478,7 +478,10 @@ class CaosDBPythonEntity(object):
for
prop
in
self
.
get_properties
():
propval
=
self
.
__getattribute__
(
prop
)
if
isinstance
(
propval
,
CaosDBPythonUnresolvedReference
):
# Resolve all previously unresolved attributes that are entities:
if
deep
and
isinstance
(
propval
,
CaosDBPythonEntity
):
propval
.
resolve_references
(
deep
,
references
)
elif
isinstance
(
propval
,
CaosDBPythonUnresolvedReference
):
# This does not make sense for unset ids:
if
propval
.
id
is
None
:
raise
RuntimeError
(
"
Unresolved property reference without an ID.
"
)
...
...
...
...
This diff is collapsed.
Click to expand it.
unittests/test_high_level_api.py
+
75
−
1
View file @
e71431c4
...
...
@@ -27,7 +27,8 @@
import
caosdb
as
db
from
caosdb.high_level_api
import
(
convert_to_entity
,
convert_to_python_object
)
from
caosdb.high_level_api
import
(
CaosDBPythonUnresolvedParent
,
CaosDBPythonUnresolvedReference
)
CaosDBPythonUnresolvedReference
,
CaosDBPythonRecord
)
import
pytest
from
lxml
import
etree
import
os
...
...
@@ -152,3 +153,76 @@ def test_convert_with_references():
assert
obj
.
get_property_metadata
(
"
ref
"
).
datatype
is
"
bla
"
assert
isinstance
(
obj
.
ref
,
CaosDBPythonUnresolvedReference
)
assert
obj
.
ref
.
id
==
27
def
test_resolve_references
():
r
=
db
.
Record
()
r
.
add_property
(
name
=
"
ref
"
,
value
=
27
,
datatype
=
"
bla
"
)
r
.
add_property
(
name
=
"
ref_false
"
,
value
=
27
)
# this should be interpreted as integer property
obj
=
convert_to_python_object
(
r
)
ref
=
db
.
Record
(
id
=
27
)
ref
.
add_property
(
name
=
"
a
"
,
value
=
57
)
unused_ref1
=
db
.
Record
(
id
=
28
)
unused_ref2
=
db
.
Record
(
id
=
29
)
unused_ref3
=
db
.
Record
(
name
=
"
bla
"
)
references
=
db
.
Container
().
extend
([
unused_ref1
,
ref
,
unused_ref2
,
unused_ref3
])
# Nothing is going to be resolved:
obj
.
resolve_references
(
False
,
db
.
Container
())
assert
isinstance
(
obj
.
ref
,
CaosDBPythonUnresolvedReference
)
assert
obj
.
ref
.
id
==
27
assert
obj
.
ref_false
==
27
# deep == True does not help:
obj
.
resolve_references
(
True
,
db
.
Container
())
assert
isinstance
(
obj
.
ref
,
CaosDBPythonUnresolvedReference
)
assert
obj
.
ref
.
id
==
27
# But adding the reference container will do:
obj
.
resolve_references
(
False
,
references
)
assert
not
isinstance
(
obj
.
ref
,
CaosDBPythonUnresolvedReference
)
assert
isinstance
(
obj
.
ref
,
CaosDBPythonRecord
)
assert
obj
.
ref
.
id
==
27
assert
obj
.
ref
.
a
==
57
# Datatypes will not automatically be set:
assert
obj
.
ref
.
get_property_metadata
(
"
a
"
).
datatype
is
None
# Test deep resolve:
ref2
=
db
.
Record
(
id
=
225
)
ref2
.
add_property
(
name
=
"
c
"
,
value
=
"
test
"
)
ref
.
add_property
(
name
=
"
ref
"
,
value
=
225
,
datatype
=
"
bla
"
)
obj
=
convert_to_python_object
(
r
)
assert
isinstance
(
obj
.
ref
,
CaosDBPythonUnresolvedReference
)
obj
.
resolve_references
(
False
,
references
)
assert
not
isinstance
(
obj
.
ref
,
CaosDBPythonUnresolvedReference
)
assert
isinstance
(
obj
.
ref
.
ref
,
CaosDBPythonUnresolvedReference
)
assert
obj
.
ref
.
ref
.
id
==
225
# Will not help, because ref2 is missing in container:
obj
.
resolve_references
(
True
,
references
)
assert
not
isinstance
(
obj
.
ref
,
CaosDBPythonUnresolvedReference
)
assert
isinstance
(
obj
.
ref
.
ref
,
CaosDBPythonUnresolvedReference
)
assert
obj
.
ref
.
ref
.
id
==
225
references
.
append
(
ref2
)
obj
.
resolve_references
(
False
,
references
)
assert
not
isinstance
(
obj
.
ref
,
CaosDBPythonUnresolvedReference
)
assert
isinstance
(
obj
.
ref
.
ref
,
CaosDBPythonUnresolvedReference
)
assert
obj
.
ref
.
ref
.
id
==
225
obj
.
resolve_references
(
True
,
references
)
assert
not
isinstance
(
obj
.
ref
,
CaosDBPythonUnresolvedReference
)
assert
not
isinstance
(
obj
.
ref
.
ref
,
CaosDBPythonUnresolvedReference
)
assert
obj
.
ref
.
ref
.
c
==
"
test
"
# Test circular dependencies:
ref2
.
add_property
(
name
=
"
ref
"
,
value
=
27
,
datatype
=
"
bla
"
)
obj
=
convert_to_python_object
(
r
)
obj
.
resolve_references
(
True
,
references
)
assert
obj
.
ref
.
ref
.
ref
==
obj
.
ref
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