diff --git a/src/newcrawler/identifiable_adapters.py b/src/newcrawler/identifiable_adapters.py index 4896226c5157fff6a366b24346deb8bcd9841f01..2d1268f95cab56f63ff62ce1a65f8ec8b3e93a14 100644 --- a/src/newcrawler/identifiable_adapters.py +++ b/src/newcrawler/identifiable_adapters.py @@ -256,7 +256,18 @@ class LocalStorageIdentifiableAdapter(IdentifiableAdapter): return self._records def get_file(self, identifiable: db.File): - raise NotImplementedError() + """ + Just look in records for a file with the same path. + """ + candidates = [] + for record in self._records: + if record.role == "File" and record.path == identifiable.path: + candidates.append(record) + if len(candidates) > 1: + raise RuntimeError("Identifiable was not defined unambigiously.") + if len(candidates) == 0: + return None + return candidates[0] def store_state(self, filename): with open(filename, "w") as f: diff --git a/unittests/test_file_identifiables.py b/unittests/test_file_identifiables.py index 4f85b01c1fcdb91682d1ad257ea84c541802b82f..234fae20c53e137bf049e496dbe178a30e5de833 100644 --- a/unittests/test_file_identifiables.py +++ b/unittests/test_file_identifiables.py @@ -24,8 +24,12 @@ def test_file_identifiable(): assert identifiable.path == identifiable2.path # ... and very boring: assert identifiable.path is None - - # TODO: test for corresponding retrieval "get_file" + # Test functionality of retrieving the files: + identified_file = ident.get_file(identifiable) + identified_file2 = ident.get_file(file_obj) + # The both should be None currently as there are no files in the local store yet: + assert identified_file is None + assert identified_file2 is None # Let's make it more interesting: file_obj.path = "/test/bla/bla.txt" @@ -43,5 +47,29 @@ def test_file_identifiable(): identified_file = ident.get_file(identifiable) # or directly using: identified_file2 = ident.get_file(file_obj) + # The both should be None currently as there are no files in the local store yet: + assert identified_file is None + assert identified_file2 is None + + # Try again with actual files in the store: + records = ident.get_records() + test_record_wrong_path = db.File( + path="/bla/bla/test.txt") + test_record_correct_path = db.File( + path="/test/bla/bla.txt") + test_record_alsocorrect_path = db.File( + path="/test/bla/bla.txt") + records.append(test_record_wrong_path) + identified_file = ident.get_file(file_obj) + assert identified_file is None + + records.append(test_record_correct_path) + identified_file = ident.get_file(file_obj) + assert identified_file is not None + assert identified_file.path == file_obj.path + + with raises(RuntimeError, match=".*unambigiously.*"): + records.append(test_record_alsocorrect_path) + identified_file = ident.get_file(file_obj)