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
2c31ad17
Commit
2c31ad17
authored
Jul 12, 2021
by
Alexander Kreft
Browse files
Options
Downloads
Patches
Plain Diff
fix + tests
parent
e0e998ea
No related branches found
No related tags found
1 merge request
!18
ENH: add is_reference to db.Property
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/caosdb/common/models.py
+20
-7
20 additions, 7 deletions
src/caosdb/common/models.py
unittests/test_property.py
+36
-0
36 additions, 0 deletions
unittests/test_property.py
with
56 additions
and
7 deletions
src/caosdb/common/models.py
+
20
−
7
View file @
2c31ad17
...
@@ -1506,20 +1506,33 @@ class Property(Entity):
...
@@ -1506,20 +1506,33 @@ class Property(Entity):
return
super
(
Property
,
self
).
to_xml
(
xml
,
add_properties
)
return
super
(
Property
,
self
).
to_xml
(
xml
,
add_properties
)
def
is_reference
(
self
):
def
is_reference
(
self
,
server_retrieval
=
False
):
"""
r
eturns whether this Property is a reference
"""
R
eturns whether this Property is a reference
If the datatype is not set, the Property is retrieved from the server.
Parameters
"""
----------
server_retrieval : bool, optional
If True and the datatype is not set, the Property is retrieved from the server, by default False
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.
"""
if
self
.
datatype
is
None
:
if
self
.
datatype
is
None
:
if
not
self
.
is_valid
():
if
not
self
.
is_valid
():
# this is a workaround to prevent side effects
# this is a workaround to prevent side effects
# since retrieve currently changes the object
# since retrieve currently changes the object
if
server_retrieval
:
tmp_prop
=
deepcopy
(
self
)
tmp_prop
=
deepcopy
(
self
)
tmp_prop
.
retrieve
()
tmp_prop
.
retrieve
()
return
tmp_prop
.
is_reference
()
return
tmp_prop
.
is_reference
()
else
:
return
None
else
:
else
:
# a valid property withoud datatype has to be an RT
# a valid property withoud datatype has to be an RT
...
...
This diff is collapsed.
Click to expand it.
unittests/test_property.py
+
36
−
0
View file @
2c31ad17
...
@@ -27,6 +27,7 @@
...
@@ -27,6 +27,7 @@
# pylint: disable=missing-docstring
# pylint: disable=missing-docstring
from
lxml
import
etree
from
lxml
import
etree
from
caosdb
import
Entity
,
Property
,
Record
from
caosdb
import
Entity
,
Property
,
Record
import
caosdb
as
db
parser
=
etree
.
XMLParser
(
remove_comments
=
True
)
parser
=
etree
.
XMLParser
(
remove_comments
=
True
)
testrecord
=
Record
.
_from_xml
(
Record
(),
testrecord
=
Record
.
_from_xml
(
Record
(),
...
@@ -89,3 +90,38 @@ def test_get_property_with_entity():
...
@@ -89,3 +90,38 @@ def test_get_property_with_entity():
def
test_selected_reference_list
():
def
test_selected_reference_list
():
assert
len
(
testrecord
.
get_property
(
"
Conductor
"
).
value
)
==
1
assert
len
(
testrecord
.
get_property
(
"
Conductor
"
).
value
)
==
1
assert
isinstance
(
testrecord
.
get_property
(
"
Conductor
"
).
value
[
0
],
Entity
)
assert
isinstance
(
testrecord
.
get_property
(
"
Conductor
"
).
value
[
0
],
Entity
)
PROPS
=
{
10
:
db
.
Property
(
id
=
10
,
datatype
=
db
.
INTEGER
),
20
:
db
.
Property
(
id
=
20
,
datatype
=
db
.
REFERENCE
)
}
def
dummy_property
(
self
):
return
PROPS
[
self
.
id
]
Entity
.
retrieve
=
dummy_property
def
test_is_reference
():
p1
=
Property
(
id
=
1
,
datatype
=
db
.
INTEGER
)
p2
=
Property
(
id
=
2
,
datatype
=
db
.
DOUBLE
)
p3
=
Property
(
id
=
3
,
datatype
=
db
.
TEXT
)
p4
=
Property
(
id
=
4
,
datatype
=
db
.
DATETIME
)
p5
=
Property
(
id
=
5
,
datatype
=
db
.
BOOLEAN
)
p6
=
Property
(
id
=
6
,
datatype
=
db
.
REFERENCE
)
assert
p1
.
is_reference
()
==
False
assert
p2
.
is_reference
()
==
False
assert
p3
.
is_reference
()
==
False
assert
p4
.
is_reference
()
==
False
assert
p5
.
is_reference
()
==
False
assert
p6
.
is_reference
()
==
True
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
p8
.
is_reference
()
==
True
p10
=
Property
(
id
=
10
)
p20
=
Property
(
id
=
20
)
assert
p10
.
is_reference
(
server_retrieval
=
True
)
==
False
assert
p20
.
is_reference
(
server_retrieval
=
True
)
==
True
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