Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
CaosDB Crawler
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
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 Crawler
Commits
523ff91a
Commit
523ff91a
authored
2 years ago
by
Henrik tom Wörden
Browse files
Options
Downloads
Patches
Plain Diff
TST: add more tests
parent
daeb11a3
No related branches found
No related tags found
2 merge requests
!53
Release 0.1
,
!32
ENH: add security levels that may prevent updates or inserts
Pipeline
#26556
failed
2 years ago
Stage: info
Stage: setup
Stage: cert
Stage: style
Stage: test
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
unittests/test_tool.py
+111
-54
111 additions, 54 deletions
unittests/test_tool.py
with
111 additions
and
54 deletions
unittests/test_tool.py
+
111
−
54
View file @
523ff91a
...
@@ -541,75 +541,132 @@ def test_replace_entities_with_ids(crawler):
...
@@ -541,75 +541,132 @@ def test_replace_entities_with_ids(crawler):
assert
a
.
get_property
(
"
C
"
).
value
==
[
12345
,
233324
]
assert
a
.
get_property
(
"
C
"
).
value
==
[
12345
,
233324
]
@patch
(
"
caoscrawler.crawl.db.Container.insert
"
)
@patch
(
"
caoscrawler.crawl.db.Container.update
"
)
@patch
(
"
caoscrawler.crawl.UpdateCache.insert
"
)
def
test_security_mode_trivial
(
updateCacheMock
,
insmock
,
upmock
,
ident
):
crawler
=
Crawler
(
debug
=
True
,
securityMode
=
SecurityMode
.
RETRIEVE
)
crawler
.
crawl_directory
(
rfp
(
"
test_directories
"
,
"
examples_article
"
),
rfp
(
"
scifolder_cfood.yml
"
))
crawler
.
identifiableAdapter
=
ident
def
mock_get_entity_by_name
(
name
):
def
mock_get_entity_by_name
(
name
):
candidates
=
[
el
for
el
in
full_data
.
values
()
if
el
.
name
.
lower
()
==
name
.
lower
()]
candidates
=
[
el
for
el
in
full_data
.
values
()
if
el
.
name
.
lower
()
==
name
.
lower
()]
if
len
(
candidates
)
>
0
:
if
len
(
candidates
)
>
0
:
return
candidates
[
0
]
return
candidates
[
0
]
else
:
else
:
return
None
return
None
Crawler
.
_get_entity_by_name
=
mock_get_entity_by_name
#Crawler._get_entity_by_name = lambda x: db.RecordType(id=1111, name="A")
insl
,
updl
=
crawler
.
synchronize
(
commit_changes
=
True
)
assert
crawler
.
run_id
is
not
None
insmock
.
assert_not_called
()
upmock
.
update
.
assert_not_called
()
updateCacheMock
.
assert_not_called
()
@patch
(
"
caoscrawler.crawl.db.Container.insert
"
)
def
prepare_crawler_with_sec_mode
(
mode
,
ident
):
@patch
(
"
caoscrawler.crawl.db.Container.update
"
)
crawler
=
Crawler
(
debug
=
True
,
securityMode
=
mode
)
@patch
(
"
caoscrawler.crawl.UpdateCache.insert
"
)
def
test_security_mode_forbidden
(
updateCacheMock
,
insmock
,
upmock
,
ident
):
crawler
=
Crawler
(
debug
=
True
,
securityMode
=
SecurityMode
.
RETRIEVE
)
crawler
.
crawl_directory
(
rfp
(
"
test_directories
"
,
"
examples_article
"
),
crawler
.
crawl_directory
(
rfp
(
"
test_directories
"
,
"
examples_article
"
),
rfp
(
"
scifolder_cfood.yml
"
))
rfp
(
"
scifolder_cfood.yml
"
))
crawler
.
identifiableAdapter
=
ident
crawler
.
identifiableAdapter
=
ident
def
mock_get_entity_by_name
(
name
):
return
crawler
candidates
=
[
el
for
el
in
full_data
.
values
()
if
el
.
name
.
lower
()
==
name
.
lower
()]
if
len
(
candidates
)
>
0
:
return
candidates
[
0
]
else
:
return
None
Crawler
.
_get_entity_by_name
=
mock_get_entity_by_name
del
ident
.
_records
[
-
1
]
insl
,
updl
=
crawler
.
synchronize
(
commit_changes
=
True
)
assert
crawler
.
run_id
is
not
None
insmock
.
assert_not_called
()
upmock
.
update
.
assert_not_called
()
assert
updateCacheMock
.
call_count
==
1
@patch
(
"
caoscrawler.crawl.db.Container.update
"
)
def
reset_mocks
(
mocks
):
for
mock
in
mocks
:
mock
.
reset_mock
()
def
change_identifiable_prop
(
ident
):
# the checks in here are only to make sure we change the record as we intend to
meas
=
ident
.
_records
[
-
2
]
assert
meas
.
parents
[
0
].
name
==
"
Measurement
"
resps
=
meas
.
properties
[
0
]
assert
resps
.
name
==
"
date
"
# change one element; This changes the date which is part of the identifiable
resps
.
value
=
"
2022-01-04
"
def
change_non_identifiable_prop
(
ident
):
# the checks in here are only to make sure we change the record as we intend to
meas
=
ident
.
_records
[
-
1
]
assert
meas
.
parents
[
0
].
name
==
"
Measurement
"
resps
=
meas
.
properties
[
-
1
]
assert
resps
.
name
==
"
responsible
"
assert
len
(
resps
.
value
)
==
2
# change one element; This removes a responsible which is not part of the identifiable
del
resps
.
value
[
-
1
]
@patch
(
"
caoscrawler.crawl.Crawler._get_entity_by_name
"
,
new
=
Mock
(
side_effect
=
mock_get_entity_by_name
))
@patch
(
"
caoscrawler.crawl.db.Container.insert
"
)
@patch
(
"
caoscrawler.crawl.db.Container.insert
"
)
@patch
(
"
caoscrawler.crawl.db.Container.update
"
)
@patch
(
"
caoscrawler.crawl.UpdateCache.insert
"
)
@patch
(
"
caoscrawler.crawl.UpdateCache.insert
"
)
def
test_security_mode_forbidden_up
(
updateCacheMock
,
insmock
,
upmock
,
ident
):
def
test_security_mode
(
updateCacheMock
,
upmock
,
insmock
,
ident
):
crawler
=
Crawler
(
debug
=
True
,
securityMode
=
SecurityMode
.
INSERT
)
records_backup
=
deepcopy
(
ident
.
_records
)
crawler
.
crawl_directory
(
rfp
(
"
test_directories
"
,
"
examples_article
"
),
rfp
(
"
scifolder_cfood.yml
"
))
crawler
.
identifiableAdapter
=
ident
state
=
full_data
def
mock_get_entity_by_name
(
name
):
# trivial case: nothing to do
c
andidates
=
[
el
for
el
in
full_data
.
values
()
if
el
.
name
.
lower
()
==
name
.
lower
()]
c
rawler
=
prepare_crawler_with_sec_mode
(
SecurityMode
.
RETRIEVE
,
ident
)
if
len
(
candidates
)
>
0
:
insl
,
updl
=
crawler
.
synchronize
(
commit_changes
=
True
)
return
candidates
[
0
]
assert
crawler
.
run_id
is
not
None
else
:
insmock
.
assert_not_called
()
return
None
upmock
.
assert_not_called
()
Crawler
.
_get_entity_by_name
=
mock_get_entity_by_name
updateCacheMock
.
assert_not_called
()
del
ident
.
_registered_identifiables
[
"
Person
"
]
# RETRIEVE: insert only
crawler
=
prepare_crawler_with_sec_mode
(
SecurityMode
.
RETRIEVE
,
ident
)
# remove one element
del
ident
.
_records
[
-
1
]
insl
,
updl
=
crawler
.
synchronize
(
commit_changes
=
True
)
insl
,
updl
=
crawler
.
synchronize
(
commit_changes
=
True
)
assert
crawler
.
run_id
is
not
None
assert
crawler
.
run_id
is
not
None
insmock
.
assert_called_once
()
insmock
.
assert_not_called
()
upmock
.
update
.
assert_not_called
()
upmock
.
assert_not_called
()
assert
updateCacheMock
.
call_count
==
1
# reset counts
reset_mocks
([
updateCacheMock
,
insmock
,
upmock
])
# restore original ident
ident
.
_records
=
deepcopy
(
records_backup
)
# RETRIEVE: update only
crawler
=
prepare_crawler_with_sec_mode
(
SecurityMode
.
RETRIEVE
,
ident
)
# change one element
change_non_identifiable_prop
(
ident
)
insl
,
updl
=
crawler
.
synchronize
(
commit_changes
=
True
)
assert
crawler
.
run_id
is
not
None
insmock
.
assert_not_called
()
upmock
.
assert_not_called
()
assert
updateCacheMock
.
call_count
==
1
assert
updateCacheMock
.
call_count
==
1
# reset counts
reset_mocks
([
updateCacheMock
,
insmock
,
upmock
])
# restore original ident
ident
.
_records
=
deepcopy
(
records_backup
)
# INSERT: insert only
crawler
=
prepare_crawler_with_sec_mode
(
SecurityMode
.
INSERT
,
ident
)
# remove one element
del
ident
.
_records
[
-
1
]
insl
,
updl
=
crawler
.
synchronize
(
commit_changes
=
True
)
assert
crawler
.
run_id
is
not
None
insmock
.
assert_called_once
()
upmock
.
assert_not_called
()
updateCacheMock
.
assert_not_called
()
# reset counts
reset_mocks
([
updateCacheMock
,
insmock
,
upmock
])
# restore original ident
ident
.
_records
=
deepcopy
(
records_backup
)
# INSERT: update only
crawler
=
prepare_crawler_with_sec_mode
(
SecurityMode
.
INSERT
,
ident
)
# change one element
change_non_identifiable_prop
(
ident
)
insl
,
updl
=
crawler
.
synchronize
(
commit_changes
=
True
)
assert
crawler
.
run_id
is
not
None
insmock
.
assert_not_called
()
upmock
.
assert_not_called
()
updateCacheMock
.
assert_called_once
()
# reset counts
reset_mocks
([
updateCacheMock
,
insmock
,
upmock
])
# restore original ident
ident
.
_records
=
deepcopy
(
records_backup
)
# INSERT: insert and update
crawler
=
prepare_crawler_with_sec_mode
(
SecurityMode
.
INSERT
,
ident
)
# change two elements
change_non_identifiable_prop
(
ident
)
change_identifiable_prop
(
ident
)
insl
,
updl
=
crawler
.
synchronize
(
commit_changes
=
True
)
assert
crawler
.
run_id
is
not
None
insmock
.
asser_called_once
()
upmock
.
assert_not_called
()
updateCacheMock
.
assert_called_once
()
# reset counts
reset_mocks
([
updateCacheMock
,
insmock
,
upmock
])
# restore original ident
ident
.
_records
=
deepcopy
(
records_backup
)
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