From b3ae9bc1f0caffd0262cb4eb262ddc2e3656613a Mon Sep 17 00:00:00 2001
From: Alexander Schlemmer <alexander@mail-schlemmer.de>
Date: Tue, 1 Feb 2022 11:26:48 +0100
Subject: [PATCH] TST: extended test for getting file identifiables

---
 src/newcrawler/identifiable_adapters.py | 13 +++++++++-
 unittests/test_file_identifiables.py    | 32 +++++++++++++++++++++++--
 2 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/src/newcrawler/identifiable_adapters.py b/src/newcrawler/identifiable_adapters.py
index 4896226c..2d1268f9 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 4f85b01c..234fae20 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)
     
     
-- 
GitLab