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

TST: refactor test

parent f5d985a7
No related branches found
No related tags found
2 merge requests!53Release 0.1,!32ENH: add security levels that may prevent updates or inserts
......@@ -762,14 +762,14 @@ class Crawler(object):
pass
@staticmethod
def _get_property_entity(name):
def _get_entity_by_name(name):
return db.Entity(name=name).retrieve()
@staticmethod
def execute_inserts_in_list(to_be_inserted, securityMode, run_id: int = None):
for record in to_be_inserted:
for prop in record.properties:
entity = Crawler._get_property_entity(prop.name)
entity = Crawler._get_entity_by_name(prop.name)
_resolve_datatype(prop, entity)
print("INSERT")
print(to_be_inserted)
......@@ -785,10 +785,10 @@ class Crawler(object):
for record in rec_list:
for parent in record.parents:
if parent.id is None:
parent.id = db.Entity(name=parent.name).retrieve().id
parent.id = Crawler._get_entity_by_name(parent.name).id
for prop in record.properties:
if prop.id is None:
entity = db.Entity(name=prop.name).retrieve()
entity = Crawler._get_entity_by_name(prop.name)
prop.id = entity.id
_resolve_datatype(prop, entity)
......
......@@ -542,55 +542,100 @@ def test_replace_entities_with_ids(crawler):
@patch("caoscrawler.crawl.db.Container.insert")
@patch("caoscrawler.crawl.db.Container.update")
@patch("caoscrawler.crawl.Crawler.set_ids_and_datatype_of_parents_and_properties")
@patch("caoscrawler.crawl.Crawler._get_property_entity")
@patch("caoscrawler.crawl.UpdateCache.insert")
def test_security_mode_trivial(UpdateCacheMock, get_prop_mock, set_id_mock, update_mock,
insert_mock, ident):
get_prop_mock.side_effect = lambda x: db.Property(name="A")
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 find_record(name):
print(name)
return [el for el in ident.get_records() if el.name == name][0]
Crawler._get_entity_by_name = find_record
#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
insert_mock.assert_not_called()
update_mock.assert_not_called()
assert UpdateCacheMock.call_count == 0
insmock.assert_not_called()
upmock.update.assert_not_called()
updateCacheMock.assert_not_called()
@patch("caoscrawler.crawl.db.Container.insert")
@patch("caoscrawler.crawl.db.Container.update")
@patch("caoscrawler.crawl.Crawler.set_ids_and_datatype_of_parents_and_properties")
@patch("caoscrawler.crawl.Crawler._get_property_entity")
@patch("caoscrawler.crawl.UpdateCache.insert")
def test_security_mode_trivial(UpdateCacheMock, get_prop_mock, set_id_mock, update_mock,
insert_mock, ident):
get_prop_mock.side_effect = lambda x: db.Property(name="A")
def test_security_mode_forbidden(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 find_record(name):
name = name.lower()
state = {"person": (db.RecordType(id=10001)
.add_parent(name="Person")
.add_property(name="first_name")
.add_property(name="last_name")),
"measurement": (db.RecordType(id=10002)
.add_parent(name="Measurement")
.add_property(name="identifier")
.add_property(name="date")
.add_property(name="project")),
"project": (db.RecordType(id=10003)
.add_parent(name="Project")
.add_property(name="date")
.add_property(name="identifier")),
"first_name": db.Property(name="first_name", datatype=db.TEXT, id=10004),
"responsible": db.Property(name="responsible", datatype="Person", id=10005),
"last_name": db.Property(name="last_name", datatype=db.TEXT, id=10006),
"identifier": db.Property(name="identifier", datatype=db.TEXT, id=10007),
"date": db.Property(name="date", datatype=db.DATETIME, id=10008),
}
return state[name]
Crawler._get_entity_by_name = find_record
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")
@patch("caoscrawler.crawl.db.Container.insert")
@patch("caoscrawler.crawl.UpdateCache.insert")
def test_security_mode_forbidden_up(updateCacheMock, insmock, upmock, ident):
crawler = Crawler(debug=True, securityMode=SecurityMode.INSERT)
crawler.crawl_directory(rfp("test_directories", "examples_article"),
rfp("scifolder_cfood.yml"))
crawler.identifiableAdapter = ident
def find_record(name):
name = name.lower()
state = {"person": (db.RecordType(id=10001)
.add_parent(name="Person")
.add_property(name="first_name")
.add_property(name="last_name")),
"measurement": (db.RecordType(id=10002)
.add_parent(name="Measurement")
.add_property(name="identifier")
.add_property(name="date")
.add_property(name="project")),
"project": (db.RecordType(id=10003)
.add_parent(name="Project")
.add_property(name="date")
.add_property(name="identifier")),
"first_name": db.Property(name="first_name", datatype=db.TEXT, id=10004),
"responsible": db.Property(name="responsible", datatype="Person", id=10005),
"last_name": db.Property(name="last_name", datatype=db.TEXT, id=10006),
"identifier": db.Property(name="identifier", datatype=db.TEXT, id=10007),
"date": db.Property(name="date", datatype=db.DATETIME, id=10008),
}
return state[name]
Crawler._get_entity_by_name = find_record
del ident._registered_identifiables["Person"]
insl, updl = crawler.synchronize(commit_changes=True)
assert crawler.run_id is not None
insert_mock.assert_not_called()
update_mock.assert_not_called()
assert UpdateCacheMock.call_count == 2
#
# @patch("caoscrawler.crawl.db.Container.insert")
# @patch("caoscrawler.crawl.db.Container.update")
# @patch("caoscrawler.crawl.Crawler.set_ids_and_datatype_of_parents_and_properties")
# @patch("caoscrawler.crawl.UpdateCache.insert")
# def test_security_mode_insert_allowed(UpdateCacheMock, set_id_mock, update_mock, insert_mock,
# ident):
# crawler = Crawler(debug=True, securityMode=SecurityMode.INSERT)
# crawler.crawl_directory(rfp("test_directories", "examples_article"),
# rfp("scifolder_cfood.yml"))
# crawler.identifiableAdapter = ident
# del ident._registered_identifiables["Person"]
# insl, updl = crawler.synchronize(commit_changes=True)
# insert_mock.assert_called()
# update_mock.assert_not_called()
# assert UpdateCacheMock.call_count == 1
insmock.assert_called_once()
upmock.update.assert_not_called()
assert updateCacheMock.call_count == 1
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment