From 47fd98b8dff1c8151676cd1a955a19a04511b5bd Mon Sep 17 00:00:00 2001
From: NolanB <nboukachab@teklia.com>
Date: Wed, 31 Aug 2022 18:19:10 +0200
Subject: [PATCH] Modif code with the review

---
 arkindex_worker/worker/entity.py            | 31 ++++++++++++---------
 tests/test_elements_worker/test_entities.py | 25 +++++++++++++----
 2 files changed, 38 insertions(+), 18 deletions(-)

diff --git a/arkindex_worker/worker/entity.py b/arkindex_worker/worker/entity.py
index 55a53a9c..78c15115 100644
--- a/arkindex_worker/worker/entity.py
+++ b/arkindex_worker/worker/entity.py
@@ -200,32 +200,37 @@ class EntityMixin(object):
     def list_corpus_entities(
         self,
         corpus: Corpus,
-        name: str,
-        parent: str,
+        name: str = None,
+        parent: str or Element = None,
     ):
         """
         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 name str: 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
+        if name is not None:
+            assert name and isinstance(name, str), "name should be of type str"
+            query_params["name"] = name
+
+        if parent is not None:
+            assert (
+                parent
+                and isinstance(parent, str)
+                or parent
+                and isinstance(parent, Element)
+            ), "parent should be of type str or Element"
+            query_params["parent"] = parent
+
+            if type(parent) == Element:
+                query_params["parent"] = parent.id
 
         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 94e22806..1ec7853f 100644
--- a/tests/test_elements_worker/test_entities.py
+++ b/tests/test_elements_worker/test_entities.py
@@ -657,6 +657,9 @@ def test_list_transcription_entities(fake_dummy_worker):
 
 
 <<<<<<< HEAD
+<<<<<<< HEAD
+=======
+>>>>>>> a8b7730... Modif code with the review
 @pytest.mark.parametrize(
     "name, parent",
     [
@@ -668,6 +671,7 @@ def test_list_transcription_entities(fake_dummy_worker):
     ],
 )
 def test_list_corpus_entities(fake_dummy_worker, name, parent):
+<<<<<<< HEAD
     corpus = Corpus({"id": "fake_corpus_id"})
     query_params = {}
     if name is not None:
@@ -689,20 +693,31 @@ def test_list_corpus_entities(fake_dummy_worker, name, parent):
     }
 =======
 def test_list_corpus_entities(fake_dummy_worker):
+=======
+>>>>>>> a8b7730... Modif code with the review
     corpus = Corpus({"id": "fake_corpus_id"})
-    name = "fake_name"
-    parent = "fake_parent"
+    query_params = {}
+    if name is not None:
+        query_params["name"] = name
+    if parent is not None:
+        query_params["parent"] = parent
+        if type(parent) == Element:
+            query_params["parent"] = parent.id
+
     fake_dummy_worker.api_client.add_response(
         "ListCorpusEntities",
         id=corpus.id,
-        name=name,
-        parent=parent,
         response={"id": "fake_entity_id"},
+        **query_params,
     )
-    assert fake_dummy_worker.list_corpus_entities(corpus, name, parent) == {
+
+    assert fake_dummy_worker.list_corpus_entities(corpus, **query_params) == {
         "id": "fake_entity_id"
     }
+<<<<<<< HEAD
 
 >>>>>>> fb7d0b7... Add a list_corpus_entities() method to entity.py
+=======
+>>>>>>> a8b7730... Modif code with the review
     assert len(fake_dummy_worker.api_client.history) == 1
     assert len(fake_dummy_worker.api_client.responses) == 0
-- 
GitLab