From 1b0d1a5ebec35cc9d5ccb17557ec5cbef4f5fc21 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20tom=20W=C3=B6rden?= <h.tomwoerden@indiscale.com>
Date: Wed, 10 May 2023 13:16:21 +0200
Subject: [PATCH] MAINT: reuse code

---
 unittests/test_crawler.py            |  9 ++++++++-
 unittests/test_file_identifiables.py | 12 ++----------
 2 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/unittests/test_crawler.py b/unittests/test_crawler.py
index 1722ec0f..743a3c64 100644
--- a/unittests/test_crawler.py
+++ b/unittests/test_crawler.py
@@ -84,7 +84,7 @@ NEW_ELEMENT = (db.Record()
                .add_property(name="result", value="homogeneous"))
 
 
-def mock_get_entity_by(eid=None, name=None):
+def mock_get_entity_by(eid=None, name=None, path=None):
     if eid is not None:
         candidates = [el for el in EXAMPLE_SERVER_STATE if el.id == eid]
         if len(candidates) > 0:
@@ -98,6 +98,13 @@ def mock_get_entity_by(eid=None, name=None):
             return candidates[0]
         else:
             raise EmptyUniqueQueryError("")
+    if path is not None:
+        candidates = [el for el in EXAMPLE_SERVER_STATE
+                      if (el.path is not None and el.path == path)]
+        if len(candidates) > 0:
+            return candidates[0]
+        else:
+            raise EmptyUniqueQueryError("")
 
 
 @pytest.fixture(autouse=True)
diff --git a/unittests/test_file_identifiables.py b/unittests/test_file_identifiables.py
index 8c633189..2852b40f 100644
--- a/unittests/test_file_identifiables.py
+++ b/unittests/test_file_identifiables.py
@@ -12,22 +12,14 @@ from caosdb.cached import cache_clear
 from caosdb.exceptions import EmptyUniqueQueryError
 from pytest import raises
 
+from test_crawler import mock_get_entity_by
+
 
 @pytest.fixture(autouse=True)
 def clear_cache():
     cache_clear()
 
 
-def mock_get_entity_by(eid=None, name=None, path=None):
-    if eid is not None:
-        candidates = [el for el in EXAMPLE_SERVER_STATE if el.id == eid]
-        if len(candidates) > 0:
-            return candidates[0]
-        else:
-            raise EmptyUniqueQueryError("")
-    raise EmptyUniqueQueryError("")
-
-
 @patch("caoscrawler.identifiable_adapters.cached_get_entity_by",
        new=Mock(side_effect=mock_get_entity_by))
 def test_file_identifiable():
-- 
GitLab