From 075ebabecbea228616e8bcfa7c7f0b7dd216e27f Mon Sep 17 00:00:00 2001
From: Valentin Rigal <rigal@teklia.com>
Date: Mon, 21 Jun 2021 13:16:40 +0200
Subject: [PATCH] Update tests

---
 tests/test_elements_worker/test_worker.py | 59 +++++++++++++++++------
 1 file changed, 43 insertions(+), 16 deletions(-)

diff --git a/tests/test_elements_worker/test_worker.py b/tests/test_elements_worker/test_worker.py
index 6eb28593..e8f17421 100644
--- a/tests/test_elements_worker/test_worker.py
+++ b/tests/test_elements_worker/test_worker.py
@@ -247,18 +247,21 @@ def test_run_cache(
     ]
 
 
-def test_activity_conflict(
+def test_start_activity_conflict(
     monkeypatch, responses, mocker, mock_elements_worker_with_list
 ):
     # Disable second configure call from run()
     monkeypatch.setattr(mock_elements_worker_with_list, "configure", lambda: None)
 
     # Mock a "normal" conflict during in activity update, which returns the Exception
-    mock_elements_worker_with_list.update_activity = mocker.MagicMock()
-    mock_elements_worker_with_list.update_activity.return_value = ErrorResponse(
-        title="conflict",
-        status_code=409,
-        content="Either this activity does not exists or this state is not allowed.",
+    responses.add(
+        responses.PUT,
+        "http://testserver/api/v1/workers/versions/12341234-1234-1234-1234-123412341234/activity/",
+        body=ErrorResponse(
+            title="conflict",
+            status_code=409,
+            content="Either this activity does not exists or this state is not allowed.",
+        ),
     )
     from arkindex_worker.worker import logger
 
@@ -266,25 +269,38 @@ def test_activity_conflict(
 
     mock_elements_worker_with_list.run()
 
-    assert mock_elements_worker_with_list.update_activity.call_args_list == [
-        mocker.call("1234-deadbeef", ActivityState.Started)
+    assert len(responses.calls) == len(BASE_API_CALLS) + 2
+    assert [
+        (call.request.method, call.request.url) for call in responses.calls
+    ] == BASE_API_CALLS + [
+        ("GET", "http://testserver/api/v1/element/1234-deadbeef/"),
+        (
+            "PUT",
+            "http://testserver/api/v1/workers/versions/12341234-1234-1234-1234-123412341234/activity/",
+        ),
     ]
     assert logger.warning.call_args_list == [
+        mocker.call(
+            "Failed to update activity of element 1234-deadbeef to started due to an API error: Either this activity does not exists or this state is not allowed."
+        ),
         mocker.call(
             "Cannot start processing element 1234-deadbeef due to a conflict. "
             "Another process could have processed it with the same version already."
-        )
+        ),
     ]
 
 
-def test_activity_error(monkeypatch, responses, mocker, mock_elements_worker_with_list):
+def test_start_activity_error(
+    monkeypatch, responses, mocker, mock_elements_worker_with_list
+):
     # Disable second configure call from run()
     monkeypatch.setattr(mock_elements_worker_with_list, "configure", lambda: None)
 
-    # Mock a "normal" conflict during in activity update, which returns the Exception
-    mock_elements_worker_with_list.update_activity = mocker.MagicMock()
-    mock_elements_worker_with_list.update_activity.return_value = Exception(
-        "A wild Petilil appears !"
+    # Mock a random error occurring during the activity update
+    responses.add(
+        responses.PUT,
+        "http://testserver/api/v1/workers/versions/12341234-1234-1234-1234-123412341234/activity/",
+        body=Exception("A wild Petilil appears !"),
     )
     from arkindex_worker.worker import logger
 
@@ -293,8 +309,19 @@ def test_activity_error(monkeypatch, responses, mocker, mock_elements_worker_wit
     with pytest.raises(SystemExit):
         mock_elements_worker_with_list.run()
 
-    assert mock_elements_worker_with_list.update_activity.call_args_list == [
-        mocker.call("1234-deadbeef", ActivityState.Started)
+    assert [
+        (call.request.method, call.request.url) for call in responses.calls
+    ] == BASE_API_CALLS + [
+        ("GET", "http://testserver/api/v1/element/1234-deadbeef/"),
+        (
+            "PUT",
+            "http://testserver/api/v1/workers/versions/12341234-1234-1234-1234-123412341234/activity/",
+        ),
+        # Activity is updated to the "error" state regardless of the Exception occurring during the call
+        (
+            "PUT",
+            "http://testserver/api/v1/workers/versions/12341234-1234-1234-1234-123412341234/activity/",
+        ),
     ]
     assert logger.error.call_args_list == [
         mocker.call("Ran on 1 elements: 0 completed, 1 failed")
-- 
GitLab