Skip to content
Snippets Groups Projects
Commit b20d19a5 authored by Eva Bardou's avatar Eva Bardou :frog: Committed by Yoann Schneider
Browse files

Remove duplicated tests introduced by !599

parent f9ed766a
No related branches found
No related tags found
1 merge request!600Remove duplicated tests introduced by !599
Pipeline #198997 passed
import json
import sys
import pytest
from arkindex.exceptions import ErrorResponse
from arkindex_worker.cache import CachedElement
from arkindex_worker.worker import ActivityState, ElementsWorker
from . import BASE_API_CALLS
TEST_VERSION_ID = "test_123"
TEST_SLUG = "some_slug"
......@@ -67,248 +58,3 @@ def test_get_worker_version_slug_none(fake_dummy_worker):
pytest.raises(ValueError, match="No worker version ID"),
):
fake_dummy_worker.get_worker_version_slug(None)
def test_readonly(responses, mock_elements_worker):
"""Test readonly worker does not trigger any API calls"""
# Setup the worker as read-only
mock_elements_worker.worker_run_id = None
assert mock_elements_worker.is_read_only is True
out = mock_elements_worker.update_activity("1234-deadbeef", ActivityState.Processed)
# update_activity returns False in very specific cases
assert out is True
assert len(responses.calls) == len(BASE_API_CALLS)
assert [
(call.request.method, call.request.url) for call in responses.calls
] == BASE_API_CALLS
@pytest.mark.usefixtures("_mock_worker_run_api")
def test_activities_disabled(responses, monkeypatch):
"""Test worker process elements without updating activities when they are disabled for the process"""
monkeypatch.setattr(sys, "argv", ["worker"])
worker = ElementsWorker()
worker.configure()
assert not worker.is_read_only
assert len(responses.calls) == len(BASE_API_CALLS)
assert [
(call.request.method, call.request.url) for call in responses.calls
] == BASE_API_CALLS
def test_activities_dev_mode(mocker):
"""
Worker activities are not stored in dev mode
"""
worker = ElementsWorker()
mocker.patch.object(sys, "argv", ["worker", "--dev"])
worker.configure()
assert worker.args.dev is True
assert worker.process_information is None
assert worker.is_read_only is True
assert worker.store_activity is False
@pytest.mark.usefixtures("_mock_worker_run_api")
def test_update_call(responses, mock_elements_worker):
"""Test an update call with feature enabled triggers an API call"""
responses.add(
responses.PUT,
"http://testserver/api/v1/workers/versions/56785678-5678-5678-5678-567856785678/activity/",
status=200,
json={
"element_id": "1234-deadbeef",
"process_id": "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeffff",
"state": "processed",
},
)
out = mock_elements_worker.update_activity("1234-deadbeef", ActivityState.Processed)
# Check the response received by worker
assert out is True
assert len(responses.calls) == len(BASE_API_CALLS) + 1
assert [
(call.request.method, call.request.url) for call in responses.calls
] == BASE_API_CALLS + [
(
"PUT",
"http://testserver/api/v1/workers/versions/56785678-5678-5678-5678-567856785678/activity/",
),
]
# Check the request sent by worker
assert json.loads(responses.calls[-1].request.body) == {
"element_id": "1234-deadbeef",
"process_id": "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeffff",
"state": "processed",
}
@pytest.mark.usefixtures("_mock_activity_calls")
@pytest.mark.parametrize(
("process_exception", "final_state"),
[
# Successful process_element
(None, "processed"),
# Failures in process_element
(
ErrorResponse(title="bad gateway", status_code=502, content="Bad gateway"),
"error",
),
(ValueError("Something bad"), "error"),
(Exception("Any error"), "error"),
],
)
def test_run(
monkeypatch,
mock_elements_worker_with_list,
responses,
process_exception,
final_state,
):
"""Check the normal runtime sends 2 API calls to update activity"""
# Disable second configure call from run()
monkeypatch.setattr(mock_elements_worker_with_list, "configure", lambda: None)
assert mock_elements_worker_with_list.is_read_only is False
# Mock exception in process_element
if process_exception:
def _err():
raise process_exception
monkeypatch.setattr(mock_elements_worker_with_list, "process_element", _err)
# The worker stops because all elements failed !
with pytest.raises(SystemExit):
mock_elements_worker_with_list.run()
else:
# Simply run the process
mock_elements_worker_with_list.run()
assert len(responses.calls) == len(BASE_API_CALLS) + 3
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/56785678-5678-5678-5678-567856785678/activity/",
),
(
"PUT",
"http://testserver/api/v1/workers/versions/56785678-5678-5678-5678-567856785678/activity/",
),
]
# Check the requests sent by worker
assert json.loads(responses.calls[-2].request.body) == {
"element_id": "1234-deadbeef",
"process_id": "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeffff",
"state": "started",
}
assert json.loads(responses.calls[-1].request.body) == {
"element_id": "1234-deadbeef",
"process_id": "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeffff",
"state": final_state,
}
@pytest.mark.usefixtures("_mock_cached_elements", "_mock_activity_calls")
def test_run_cache(monkeypatch, mocker, mock_elements_worker_with_cache):
# Disable second configure call from run()
monkeypatch.setattr(mock_elements_worker_with_cache, "configure", lambda: None)
# Make all the cached elements from the fixture initial elements
CachedElement.update(initial=True).execute()
mock_elements_worker_with_cache.process_element = mocker.MagicMock()
mock_elements_worker_with_cache.run()
assert mock_elements_worker_with_cache.process_element.call_args_list == [
# Called once for each cached element
mocker.call(elt)
for elt in CachedElement.select()
]
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
responses.add(
responses.PUT,
"http://testserver/api/v1/workers/versions/56785678-5678-5678-5678-567856785678/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
logger.info = mocker.MagicMock()
mock_elements_worker_with_list.run()
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/56785678-5678-5678-5678-567856785678/activity/",
),
]
assert logger.info.call_args_list[:2] == [
mocker.call("Processing page Test Page n°1 (1234-deadbeef) (1/1)"),
mocker.call("Skipping element 1234-deadbeef as it was already processed"),
]
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 random error occurring during the activity update
responses.add(
responses.PUT,
"http://testserver/api/v1/workers/versions/56785678-5678-5678-5678-567856785678/activity/",
body=Exception("A wild Petilil appears !"),
)
from arkindex_worker.worker import logger
logger.error = mocker.MagicMock()
with pytest.raises(SystemExit):
mock_elements_worker_with_list.run()
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/56785678-5678-5678-5678-567856785678/activity/",
),
# Activity is updated to the "error" state regardless of the Exception occurring during the call
(
"PUT",
"http://testserver/api/v1/workers/versions/56785678-5678-5678-5678-567856785678/activity/",
),
]
assert logger.error.call_args_list == [
mocker.call("Ran on 1 element: 0 completed, 1 failed")
]
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