From a74cb90587b982da2d5038e5fe2996207775f7e7 Mon Sep 17 00:00:00 2001 From: Eva Bardou <ebardou@teklia.com> Date: Mon, 22 Mar 2021 16:52:26 +0100 Subject: [PATCH] Add assert for query params --- arkindex_worker/worker.py | 13 +++--- tests/test_elements_worker/test_elements.py | 45 +++------------------ 2 files changed, 13 insertions(+), 45 deletions(-) diff --git a/arkindex_worker/worker.py b/arkindex_worker/worker.py index 4c56bf1e..6c667efa 100644 --- a/arkindex_worker/worker.py +++ b/arkindex_worker/worker.py @@ -889,12 +889,13 @@ class ElementsWorker(BaseWorker): ), "worker_version should be of type str" query_params["worker_version"] = worker_version - # Checking that we only have query_params handled by the cache - if self.use_cache and set(query_params.keys()) <= { - "name", - "type", - "worker_version", - }: + if self.use_cache: + # Checking that we only received query_params handled by the cache + assert set(query_params.keys()) <= { + "name", + "type", + "worker_version", + }, "When using the local cache, you can only filter by 'name', 'type' and/or 'worker_version'" parent_id_hex = convert_str_uuid_to_hex(element.id) name_condition = f" AND name LIKE '%{name}%'" if name else "" type_condition = f" AND type='{type}'" if type else "" diff --git a/tests/test_elements_worker/test_elements.py b/tests/test_elements_worker/test_elements.py index c79a106a..76e62267 100644 --- a/tests/test_elements_worker/test_elements.py +++ b/tests/test_elements_worker/test_elements.py @@ -941,51 +941,18 @@ def test_list_element_children(responses, mock_elements_worker): def test_list_element_children_with_cache_unhandled_param( - responses, mock_elements_worker_with_cache + mock_elements_worker_with_cache, ): - """ - Calls list_elements_children on a worker using the cache. - The cache doesn't contain any information about an element corpus and with_corpus query param is set to True. - The list_elements_children function will call the API (instead of the cache) to use the with_corpus param. - """ elt = Element({"id": "12341234-1234-1234-1234-123412341234"}) - expected_children = [ - { - "id": "0000", - "type": "page", - "name": "Test", - "corpus": {}, - "thumbnail_url": None, - "zone": {}, - "best_classes": None, - "has_children": None, - "worker_version_id": None, - } - ] - responses.add( - responses.GET, - "http://testserver/api/v1/elements/12341234-1234-1234-1234-123412341234/children/?with_corpus=True", - status=200, - json={ - "count": 1, - "next": None, - "results": expected_children, - }, - ) - for idx, child in enumerate( + with pytest.raises(AssertionError) as e: mock_elements_worker_with_cache.list_element_children( element=elt, with_corpus=True ) - ): - assert child == expected_children[idx] - - assert len(responses.calls) == 3 - assert [call.request.url for call in responses.calls] == [ - "http://testserver/api/v1/user/", - "http://testserver/api/v1/workers/versions/12341234-1234-1234-1234-123412341234/", - "http://testserver/api/v1/elements/12341234-1234-1234-1234-123412341234/children/?with_corpus=True", - ] + assert ( + str(e.value) + == "When using the local cache, you can only filter by 'name', 'type' and/or 'worker_version'" + ) def test_list_element_children_with_cache(responses, mock_elements_worker_with_cache): -- GitLab