From f2055b9d27aae71baa6fc04c17a5c4a2b04942b7 Mon Sep 17 00:00:00 2001 From: manonBlanco <blanco@teklia.com> Date: Wed, 16 Aug 2023 11:13:47 +0200 Subject: [PATCH] Update CachedElement --- arkindex_worker/worker/element.py | 21 ++++++++--- tests/test_elements_worker/test_elements.py | 42 ++++++++++++++------- 2 files changed, 44 insertions(+), 19 deletions(-) diff --git a/arkindex_worker/worker/element.py b/arkindex_worker/worker/element.py index ddac5662..2d54563f 100644 --- a/arkindex_worker/worker/element.py +++ b/arkindex_worker/worker/element.py @@ -277,7 +277,7 @@ class ElementMixin(object): def update_element( self, - element: Element, + element: Union[Element, CachedElement], type: Optional[str] = None, name: Optional[str] = None, polygon: Optional[List[List[Union[int, float]]]] = None, @@ -286,7 +286,7 @@ class ElementMixin(object): """ Partially update an element through the API. - :param Element element: The element to update. + :param element: The element to update. :param type: Optional new slug type of the element. :param name: Optional new name of the element. :param polygon: Optional new polygon of the element. @@ -294,8 +294,8 @@ class ElementMixin(object): :returns: A dict from the ``PartialUpdateElement`` API endpoint, """ assert element and isinstance( - element, Element - ), "element shouldn't be null and should be of type Element" + element, (Element, CachedElement) + ), "element shouldn't be null and should be an Element or CachedElement" assert type is None or isinstance(type, str), "type should be None or a str" assert name is None or isinstance(name, str), "name should be None or a str" assert polygon is None or isinstance( @@ -317,7 +317,7 @@ class ElementMixin(object): logger.warning("Cannot update element as this worker is in read-only mode") return - return self.request( + updated_element = self.request( "PartialUpdateElement", id=element.id, body={ @@ -328,6 +328,17 @@ class ElementMixin(object): }, ) + if self.use_cache: + CachedElement.update( + { + CachedElement.type: type, + CachedElement.polygon: str(polygon), + CachedElement.confidence: confidence, + } + ).where(CachedElement.id == element.id).execute() + + return updated_element + def list_element_children( self, element: Union[Element, CachedElement], diff --git a/tests/test_elements_worker/test_elements.py b/tests/test_elements_worker/test_elements.py index fe128526..5d734d6f 100644 --- a/tests/test_elements_worker/test_elements.py +++ b/tests/test_elements_worker/test_elements.py @@ -1215,13 +1215,13 @@ def test_update_element_wrong_element(mock_elements_worker): mock_elements_worker.update_element( element=None, ) - assert str(e.value) == "element shouldn't be null and should be of type Element" + assert str(e.value) == "element shouldn't be null and should be an Element or CachedElement" with pytest.raises(AssertionError) as e: mock_elements_worker.update_element( element="not element type", ) - assert str(e.value) == "element shouldn't be null and should be of type Element" + assert str(e.value) == "element shouldn't be null and should be an Element or CachedElement" def test_update_element_wrong_type(mock_elements_worker): @@ -1320,12 +1320,14 @@ def test_update_element_api_error(responses, mock_elements_worker): ] -def test_update_element(responses, mock_elements_worker): - elt = Element({"id": "12341234-1234-1234-1234-123412341234"}) +def test_update_element( + responses, mock_elements_worker_with_cache, mock_cached_elements +): + elt = CachedElement.select().first() elt_response = { - "type": "something", - "name": "0", - "polygon": [[1, 1], [2, 2], [2, 1], [1, 2]], + "type": "new type", + "name": "new name", + "polygon": [[10, 10], [20, 20], [20, 10], [10, 20]], "confidence": None, } responses.add( @@ -1335,7 +1337,7 @@ def test_update_element(responses, mock_elements_worker): json=elt_response, ) - element_update_response = mock_elements_worker.update_element( + element_update_response = mock_elements_worker_with_cache.update_element( element=elt, **elt_response, ) @@ -1352,13 +1354,20 @@ def test_update_element(responses, mock_elements_worker): assert json.loads(responses.calls[-1].request.body) == elt_response assert element_update_response == elt_response + cached_element = CachedElement.get(CachedElement.id == elt.id) + assert cached_element.type == elt_response["type"] + assert cached_element.polygon == str(elt_response["polygon"]) + assert cached_element.confidence == elt_response["confidence"] -def test_update_element_confidence(responses, mock_elements_worker): - elt = Element({"id": "12341234-1234-1234-1234-123412341234"}) + +def test_update_element_confidence( + responses, mock_elements_worker_with_cache, mock_cached_elements +): + elt = CachedElement.select().first() elt_response = { - "type": "something", - "name": "0", - "polygon": [[1, 1], [2, 2], [2, 1], [1, 2]], + "type": "new type", + "name": "new name", + "polygon": [[10, 10], [20, 20], [20, 10], [10, 20]], "confidence": 0.42, } responses.add( @@ -1368,7 +1377,7 @@ def test_update_element_confidence(responses, mock_elements_worker): json=elt_response, ) - element_update_response = mock_elements_worker.update_element( + element_update_response = mock_elements_worker_with_cache.update_element( element=elt, **elt_response, ) @@ -1385,6 +1394,11 @@ def test_update_element_confidence(responses, mock_elements_worker): assert json.loads(responses.calls[-1].request.body) == elt_response assert element_update_response == elt_response + cached_element = CachedElement.get(CachedElement.id == elt.id) + assert cached_element.type == elt_response["type"] + assert cached_element.polygon == str(elt_response["polygon"]) + assert cached_element.confidence == elt_response["confidence"] + def test_list_element_children_wrong_element(mock_elements_worker): with pytest.raises(AssertionError) as e: -- GitLab