From fb7d0b7f007a5ac2141b0c179bf1c5e30169d9a0 Mon Sep 17 00:00:00 2001
From: NolanB <nboukachab@teklia.com>
Date: Wed, 31 Aug 2022 15:41:09 +0200
Subject: [PATCH] Add a list_corpus_entities() method to entity.py

---
 arkindex_worker/worker/entity.py            | 36 ++++++++++++++++++++-
 tests/test_elements_worker/test_entities.py | 21 +++++++++++-
 2 files changed, 55 insertions(+), 2 deletions(-)

diff --git a/arkindex_worker/worker/entity.py b/arkindex_worker/worker/entity.py
index 7b561c3e..921fedc5 100644
--- a/arkindex_worker/worker/entity.py
+++ b/arkindex_worker/worker/entity.py
@@ -10,7 +10,7 @@ from peewee import IntegrityError
 
 from arkindex_worker import logger
 from arkindex_worker.cache import CachedElement, CachedEntity, CachedTranscriptionEntity
-from arkindex_worker.models import Element, Transcription
+from arkindex_worker.models import Corpus, Element, Transcription
 
 
 class EntityType(Enum):
@@ -206,3 +206,37 @@ class EntityMixin(object):
         return self.api_client.paginate(
             "ListTranscriptionEntities", id=transcription.id, **query_params
         )
+
+    def list_corpus_entities(
+        self,
+        corpus: Corpus,
+        name: str,
+        parent: str,
+    ):
+        """
+        List all entities in a corpus
+        This method does not support cache
+
+        :param corpus Corpus: The corpus that contains the entities to list.
+        :param name str: uuid for filter entities by part of their name (case-insensitive)
+        :param parent str: uuid for restrict entities to those linked to all transcriptions of an element and all its descendants. Note that links to metadata are ignored.
+        """
+        query_params = {}
+        assert corpus and isinstance(
+            corpus, Corpus
+        ), "corpus shouldn't be null and should be a Corpus"
+
+        assert name and isinstance(
+            name, str
+        ), "name shouldn't be null and should be of type str"
+
+        assert parent and isinstance(
+            parent, str
+        ), "parent shouldn't be null and should be of type str"
+
+        query_params["name"] = name
+        query_params["parent"] = parent
+
+        return self.api_client.paginate(
+            "ListCorpusEntities", id=corpus.id, **query_params
+        )
diff --git a/tests/test_elements_worker/test_entities.py b/tests/test_elements_worker/test_entities.py
index 57ea05af..39b488ab 100644
--- a/tests/test_elements_worker/test_entities.py
+++ b/tests/test_elements_worker/test_entities.py
@@ -11,7 +11,7 @@ from arkindex_worker.cache import (
     CachedTranscription,
     CachedTranscriptionEntity,
 )
-from arkindex_worker.models import Element, Transcription
+from arkindex_worker.models import Corpus, Element, Transcription
 from arkindex_worker.worker import EntityType
 from arkindex_worker.worker.transcription import TextOrientation
 
@@ -686,3 +686,22 @@ def test_list_transcription_entities(fake_dummy_worker):
 
     assert len(fake_dummy_worker.api_client.history) == 1
     assert len(fake_dummy_worker.api_client.responses) == 0
+
+
+def test_list_corpus_entities(fake_dummy_worker):
+    corpus = Corpus({"id": "fake_corpus_id"})
+    name = "fake_name"
+    parent = "fake_parent"
+    fake_dummy_worker.api_client.add_response(
+        "ListCorpusEntities",
+        id=corpus.id,
+        name=name,
+        parent=parent,
+        response={"id": "fake_entity_id"},
+    )
+    assert fake_dummy_worker.list_corpus_entities(corpus, name, parent) == {
+        "id": "fake_entity_id"
+    }
+
+    assert len(fake_dummy_worker.api_client.history) == 1
+    assert len(fake_dummy_worker.api_client.responses) == 0
-- 
GitLab