Skip to content
Snippets Groups Projects
Commit 227669d6 authored by Eva Bardou's avatar Eva Bardou
Browse files

Add assert for query params

parent b1a1b21c
No related branches found
No related tags found
1 merge request!68Retrieve children elements from SQLite cache in list_element_children
This commit is part of merge request !68. Comments created here will be created in the context of that merge request.
...@@ -845,12 +845,13 @@ class ElementsWorker(BaseWorker): ...@@ -845,12 +845,13 @@ class ElementsWorker(BaseWorker):
), "worker_version should be of type str" ), "worker_version should be of type str"
query_params["worker_version"] = worker_version query_params["worker_version"] = worker_version
# Checking that we only have query_params handled by the cache if self.use_cache:
if self.use_cache and set(query_params.keys()) <= { # Checking that we only received query_params handled by the cache
"name", assert set(query_params.keys()) <= {
"type", "name",
"worker_version", "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) parent_id_hex = convert_str_uuid_to_hex(element.id)
name_condition = f" AND name LIKE '%{name}%'" if name else "" name_condition = f" AND name LIKE '%{name}%'" if name else ""
type_condition = f" AND type='{type}'" if type else "" type_condition = f" AND type='{type}'" if type else ""
......
...@@ -940,51 +940,18 @@ def test_list_element_children(responses, mock_elements_worker): ...@@ -940,51 +940,18 @@ def test_list_element_children(responses, mock_elements_worker):
def test_list_element_children_with_cache_unhandled_param( 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"}) 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( mock_elements_worker_with_cache.list_element_children(
element=elt, with_corpus=True element=elt, with_corpus=True
) )
): assert (
assert child == expected_children[idx] str(e.value)
== "When using the local cache, you can only filter by 'name', 'type' and/or 'worker_version'"
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",
]
def test_list_element_children_with_cache(responses, mock_elements_worker_with_cache): def test_list_element_children_with_cache(responses, mock_elements_worker_with_cache):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment