diff --git a/src/newcrawler/identifiable_adapters.py b/src/newcrawler/identifiable_adapters.py index 6c10448467b09c3c59521fbc5f57ba41d0e00c54..9802e066d4f2b48366f2dac85801b7d0342fbd80 100644 --- a/src/newcrawler/identifiable_adapters.py +++ b/src/newcrawler/identifiable_adapters.py @@ -196,8 +196,9 @@ class LocalStorageIdentifiableAdapter(IdentifiableAdapter): return False for prop in registered_identifiable.properties: - if record.get_property(name=prop.name) is None: + if record.get_property(prop.name) is None: return False + return True def get_registered_identifiable(self, record: db.Record): identifiable_candidates = [] @@ -210,6 +211,29 @@ class LocalStorageIdentifiableAdapter(IdentifiableAdapter): return None return identifiable_candidates[0] + @staticmethod + def check_record(record: db.Record, identifiable: db.Record): + if len(identifiable.parents) != 1: + raise RuntimeError("Multiple parents for identifiables not supported.") + if not record.has_parent(identifiable.parents[0]): + return False + for prop in identifiable.properties: + prop_record = record.get_property(prop.name) + if prop_record is None: + return False + if prop.value != prop_record.value: + return False + return True + def retrieve_identified_record(self, identifiable: db.Record): - pass + candidates = [] + for record in self._records: + if self.check_record(record, identifiable): + candidates.append(record) + if len(candidates) > 1: + raise RuntimeError("Identifiable was not defined unambigiously.") + if len(candidates) == 0: + return None + return candidates[0] + diff --git a/tests/test_tool.py b/tests/test_tool.py index b3458155a28bfe4dcc0c69906e3025899d4623ad..cd1ff2226cbdac3ebe8ac930ff24bea94919f3b4 100755 --- a/tests/test_tool.py +++ b/tests/test_tool.py @@ -11,6 +11,8 @@ from os.path import join, dirname, basename import yaml import caosdb as db +from pytest import raises + # Some notes: # Track provenance information in two ways: # - DONE: provenance in structure elements and converters for properties of records @@ -162,11 +164,76 @@ def test_crawler_update_list(): # assert len(crawler.updateList) == 8 ident = LocalStorageIdentifiableAdapter() - ident.get_records().extend(crawler.updateList) - ident.store_state(rfp("records.xml")) - # ident.restore_state(rfp("records.xml")) + # ident.get_records().extend(crawler.updateList) + # ident.store_state(rfp("records.xml")) + ident.restore_state(rfp("records.xml")) assert len(ident.get_records()) == len(crawler.updateList) + ident.register_identifiable( + "Person", db.RecordType() + .add_parent(name="Person") + .add_property(name="first_name") + .add_property(name="last_name")) + ident.register_identifiable( + "Measurement", db.RecordType() + .add_parent(name="Measurement") + .add_property(name="identifier") + .add_property(name="date") + .add_property(name="project")) + ident.register_identifiable( + "Project", db.RecordType() + .add_parent(name="Project") + .add_property(name="date") + .add_property(name="identifier")) + + curind = 0 + r = ident.get_records() + id_r0 = ident.get_identifiable(r[0]) + assert r[curind].parents[0].name == id_r0.parents[0].name + assert r[curind].get_property("first_name").value == id_r0.get_property("first_name").value + assert r[curind].get_property("last_name").value == id_r0.get_property("last_name").value + assert len(r[curind].parents) == 1 + assert len(id_r0.parents) == 1 + assert len(r[curind].properties) == 2 + assert len(id_r0.properties) == 2 + + with raises(RuntimeError, match=".*unambigiously.*"): + ident.retrieve_identified_record(id_r0) + + # clean record list: + recordlist = ident.get_records() + for i in range(len(recordlist)-1, 1, -1): + if recordlist[i].parents[0].name == "Person": + del recordlist[i] + + idr_r0_test = ident.retrieve_identified_record(id_r0) + idr_r0 = ident.retrieve_identifiable(r[curind]) + assert idr_r0 == idr_r0_test + + curind += 1 + r = ident.get_records() + id_r1 = ident.get_identifiable(r[curind]) + print(r[curind]) + print(id_r1) + assert r[curind].parents[0].name == id_r1.parents[0].name + assert r[curind].get_property("identifier").value == id_r1.get_property("identifier").value + assert r[curind].get_property("date").value == id_r1.get_property("date").value + assert r[curind].get_property("project").value == id_r1.get_property("project").value + assert len(r[curind].parents) == 1 + assert len(id_r1.parents) == 1 + assert len(r[curind].properties) == 5 + assert len(id_r1.properties) == 3 + + idr_r1_test = ident.retrieve_identified_record(id_r1) + idr_r1 = ident.retrieve_identifiable(r[curind]) + assert idr_r1 == idr_r1_test + assert idr_r1 != idr_r0 + assert idr_r1_test != idr_r0_test + + assert len(idr_r1.properties) == 5 + assert r[curind].get_property("responsible").value == idr_r1.get_property("responsible").value + assert r[curind].get_property("description").value == idr_r1.get_property("description").value + def test_provenance_debug_data(): crawler = Crawler(debug=True)