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
df29c846
Commit
df29c846
authored
3 years ago
by
Florian Spreckelsen
Browse files
Options
Downloads
Plain Diff
Merge branch 'f-is-reference' into 'dev'
ENH: add is_reference to db.Property See merge request
!18
parents
9038284f
72aa0fb1
Branches
Branches containing commit
Tags
Tags containing commit
2 merge requests
!33
MAINT: change arguments of create_user
,
!18
ENH: add is_reference to db.Property
Pipeline
#10265
passed
3 years ago
Stage: code_style
Stage: linting
Stage: test
Stage: deploy
Changes
3
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
CHANGELOG.md
+2
-0
2 additions, 0 deletions
CHANGELOG.md
src/caosdb/common/models.py
+37
-0
37 additions, 0 deletions
src/caosdb/common/models.py
unittests/test_property.py
+46
-1
46 additions, 1 deletion
unittests/test_property.py
with
85 additions
and
1 deletion
CHANGELOG.md
+
2
−
0
View file @
df29c846
...
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
...
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ###
### Added ###
*
is_reference function for Properties
### Changed ###
### Changed ###
### Deprecated ###
### Deprecated ###
...
...
This diff is collapsed.
Click to expand it.
src/caosdb/common/models.py
+
37
−
0
View file @
df29c846
...
@@ -31,6 +31,7 @@ from __future__ import print_function, unicode_literals
...
@@ -31,6 +31,7 @@ from __future__ import print_function, unicode_literals
import
re
import
re
import
sys
import
sys
from
builtins
import
str
from
builtins
import
str
from
copy
import
deepcopy
from
functools
import
cmp_to_key
from
functools
import
cmp_to_key
from
hashlib
import
sha512
from
hashlib
import
sha512
from
os
import
listdir
from
os
import
listdir
...
@@ -1504,6 +1505,42 @@ class Property(Entity):
...
@@ -1504,6 +1505,42 @@ 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
,
server_retrieval
=
False
):
"""
Returns whether this Property is a reference
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
not
self
.
is_valid
():
# this is a workaround to prevent side effects
# since retrieve currently changes the object
if
server_retrieval
:
tmp_prop
=
deepcopy
(
self
)
tmp_prop
.
retrieve
()
return
tmp_prop
.
is_reference
()
else
:
return
None
else
:
# a valid property without datatype has to be an RT
return
True
else
:
return
is_reference
(
self
.
datatype
)
class
Message
(
object
):
class
Message
(
object
):
...
...
This diff is collapsed.
Click to expand it.
unittests/test_property.py
+
46
−
1
View file @
df29c846
...
@@ -24,9 +24,10 @@
...
@@ -24,9 +24,10 @@
# ** end header
# ** end header
#
#
"""
Tests for the Property class.
"""
"""
Tests for the Property class.
"""
import
caosdb
as
db
from
caosdb
import
Entity
,
Property
,
Record
# pylint: disable=missing-docstring
# pylint: disable=missing-docstring
from
lxml
import
etree
from
lxml
import
etree
from
caosdb
import
Entity
,
Property
,
Record
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,47 @@ def test_get_property_with_entity():
...
@@ -89,3 +90,47 @@ 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
)
def
test_is_reference
():
PROPS
=
{
10
:
db
.
INTEGER
,
20
:
db
.
REFERENCE
,
30
:
"
SomeRT
"
,
}
def
dummy_retrieve
(
self
):
self
.
datatype
=
PROPS
[
self
.
id
]
self
.
is_valid
=
lambda
:
True
# replace retrieve function by dummy
real_retrieve
=
Entity
.
retrieve
Entity
.
retrieve
=
dummy_retrieve
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
()
is
False
assert
p2
.
is_reference
()
is
False
assert
p3
.
is_reference
()
is
False
assert
p4
.
is_reference
()
is
False
assert
p5
.
is_reference
()
is
False
assert
p6
.
is_reference
()
is
True
p7
=
Property
(
id
=
7
)
p8
=
Property
(
id
=
8
,
value
=
db
.
RecordType
(
id
=
1000
))
p8
.
is_valid
=
lambda
:
True
assert
p7
.
is_reference
()
is
None
# cannot be resolved without calling a server
assert
p8
.
is_reference
()
is
True
p10
=
Property
(
id
=
10
)
p20
=
Property
(
id
=
20
)
p30
=
Property
(
id
=
30
)
assert
p10
.
is_reference
(
server_retrieval
=
True
)
is
False
assert
p20
.
is_reference
(
server_retrieval
=
True
)
is
True
assert
p30
.
is_reference
(
server_retrieval
=
True
)
is
True
# restore retrieve function with original
Entity
.
retrieve
=
real_retrieve
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