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
6464adec
Commit
6464adec
authored
1 year ago
by
florian
Browse files
Options
Downloads
Patches
Plain Diff
TST: Add tests for removing a value from a property
parent
7d09087f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!108
ENH: F remove value or property
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
unittests/test_property.py
+76
-4
76 additions, 4 deletions
unittests/test_property.py
with
76 additions
and
4 deletions
unittests/test_property.py
+
76
−
4
View file @
6464adec
# -*- encoding: utf-8 -*-
#
# ** header v3.0
# This file is a part of the CaosDB Project.
#
# Copyright (C) 2018 Research Group Biomedical Physics,
# Max-Planck-Institute for Dynamics and Self-Organization Göttingen
# Copyright (C) 2020 IndiScale GmbH <info@indiscale.com>
# Copyright (C) 2020 - 2023 IndiScale GmbH <info@indiscale.com>
# Copyright (C) 2023 Florian Spreckelsen <f.spreckelsen@indiscale.com>
# Copyright (C) 2020 Timm Fitschen <t.fitschen@indiscale.com>
#
# This program is free software: you can redistribute it and/or modify
...
...
@@ -21,8 +21,6 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# ** end header
#
"""
Tests for the Property class.
"""
import
os
...
...
@@ -138,3 +136,77 @@ def test_is_reference():
# restore retrieve function with original
Entity
.
retrieve
=
real_retrieve
def
test_remove_value_from_property
():
rec
=
Record
()
names_values_dtypes
=
[
(
"
testListProp1
"
,
[
1
,
2
,
3
],
db
.
LIST
(
db
.
INTEGER
)),
(
"
testListProp2
"
,
[
"
a
"
,
"
b
"
,
"
a
"
],
db
.
LIST
(
db
.
TEXT
)),
(
"
testScalarProp1
"
,
"
bla
"
,
db
.
TEXT
),
(
"
testScalarProp2
"
,
False
,
db
.
BOOLEAN
),
(
"
testEmptyProp
"
,
None
,
db
.
REFERENCE
)
]
for
name
,
value
,
dtype
in
names_values_dtypes
:
rec
.
add_property
(
name
=
name
,
value
=
value
,
datatype
=
dtype
)
# property doesn't exist, so do nothing
rec
.
remove_value_from_property
(
"
nonexisting
"
,
"
some_value
"
)
for
name
,
value
,
dtype
in
names_values_dtypes
:
assert
rec
.
get_property
(
name
).
value
==
value
assert
rec
.
get_property
(
name
).
datatype
==
dtype
# value doesn't exist so nothing changes either
rec
.
remove_value_from_property
(
"
testListProp1
"
,
0
)
assert
rec
.
get_property
(
"
testListProp1
"
).
value
==
[
1
,
2
,
3
]
assert
rec
.
get_property
(
"
testListProp1
"
).
datatype
==
db
.
LIST
(
db
.
INTEGER
)
rec
.
remove_value_from_property
(
"
testScalarProp2
"
,
True
)
assert
rec
.
get_property
(
"
testScalarProp2
"
).
value
==
False
assert
rec
.
get_property
(
"
testScalarProp2
"
).
datatype
==
db
.
BOOLEAN
# Simple removals from lists without emptying them
rec
.
remove_value_from_property
(
"
testListProp1
"
,
1
)
assert
rec
.
get_property
(
"
testListProp1
"
).
value
==
[
2
,
3
]
rec
.
remove_value_from_property
(
"
testListProp1
"
,
2
)
assert
rec
.
get_property
(
"
testListProp1
"
).
value
==
[
3
]
# similarly to Python's `list.remove()`, only remove first occurrance
rec
.
remove_value_from_property
(
"
testListProp2
"
,
"
a
"
)
assert
rec
.
get_property
(
"
testListProp2
"
).
value
==
[
"
b
"
,
"
a
"
]
# default is to remove an empty property:
rec
.
remove_value_from_property
(
"
testListProp1
"
,
3
)
assert
rec
.
get_property
(
"
testListProp1
"
)
is
None
rec
.
remove_value_from_property
(
"
testScalarProp1
"
,
"
bla
"
)
assert
rec
.
get_property
(
"
testScalarProp1
"
)
is
None
# don't remove if `remove_if_empty_afterwards=False`
rec
.
remove_value_from_property
(
"
testListProp2
"
,
"
b
"
)
rec
.
remove_value_from_property
(
"
testListProp2
"
,
"
a
"
,
remove_if_empty_afterwards
=
False
)
assert
rec
.
get_property
(
"
testListProp2
"
)
is
not
None
assert
rec
.
get_property
(
"
testListProp2
"
).
value
is
None
assert
rec
.
get_property
(
"
testListProp2
"
).
datatype
==
db
.
LIST
(
db
.
TEXT
)
rec
.
remove_value_from_property
(
"
testScalarProp2
"
,
False
,
remove_if_empty_afterwards
=
False
)
assert
rec
.
get_property
(
"
testScalarProp2
"
)
is
not
None
assert
rec
.
get_property
(
"
testScalarProp2
"
).
value
is
None
assert
rec
.
get_property
(
"
testScalarProp2
"
).
datatype
==
db
.
BOOLEAN
# Special case of an already empty property: It is not empty because a value
# was removed by `remove_value_from_property` but never had a value in the
# first place. So even `remove_if_empty_afterwards=True` should not lead to
# its removal.
rec
.
remove_value_from_property
(
"
testEmptyProp
"
,
1234
,
remove_if_empty_afterwards
=
True
)
assert
rec
.
get_property
(
"
testEmptyProp
"
)
is
not
None
assert
rec
.
get_property
(
"
testEmptyProp
"
).
value
is
None
assert
rec
.
get_property
(
"
testEmptyProp
"
).
datatype
==
db
.
REFERENCE
# Corner case of corner case: remove with `value=None` explicitly and
# `remove_if_empty_afterwards=True` leads to the removal of an empty
# property.
rec
.
remove_value_from_property
(
"
testEmptyProp
"
,
None
,
remove_if_empty_afterwards
=
True
)
assert
rec
.
get_property
(
"
testEmptyProp
"
)
is
None
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