Skip to content
Snippets Groups Projects
Commit 523ff91a authored by Henrik tom Wörden's avatar Henrik tom Wörden
Browse files

TST: add more tests

parent daeb11a3
No related branches found
No related tags found
2 merge requests!53Release 0.1,!32ENH: add security levels that may prevent updates or inserts
Pipeline #26556 failed
...@@ -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
candidates = [el for el in full_data.values() if el.name.lower() == name.lower()] crawler = 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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment