Skip to content

Commit 6ac1607

Browse files
move tasks tests
1 parent f6676de commit 6ac1607

File tree

3 files changed

+247
-167
lines changed

3 files changed

+247
-167
lines changed

services/web/server/tests/unit/with_dbs/01/conftest.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
# pylint: disable=unused-argument
33
# pylint: disable=unused-variable
44

5+
from collections.abc import Callable
6+
from typing import Any
7+
58
import pytest
9+
from pytest_mock import MockerFixture
610

711

812
@pytest.fixture
@@ -12,3 +16,20 @@ def app_environment(
1216
# NOTE: overrides app_environment
1317
monkeypatch.setenv("WEBSERVER_GARBAGE_COLLECTOR", "null")
1418
return app_environment | {"WEBSERVER_GARBAGE_COLLECTOR": "null"}
19+
20+
21+
@pytest.fixture
22+
def create_backend_mock(
23+
mocker: MockerFixture,
24+
) -> Callable[[str, str, Any], None]:
25+
def _(module: str, method: str, result_or_exception: Any):
26+
def side_effect(*args, **kwargs):
27+
if isinstance(result_or_exception, Exception):
28+
raise result_or_exception
29+
30+
return result_or_exception
31+
32+
for fct in (f"{module}.{method}",):
33+
mocker.patch(fct, side_effect=side_effect)
34+
35+
return _

services/web/server/tests/unit/with_dbs/01/storage/test_storage.py

Lines changed: 0 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from urllib.parse import quote
1010

1111
import pytest
12-
import simcore_service_webserver.tasks._controller._rest
1312
from aiohttp.test_utils import TestClient
1413
from faker import Faker
1514
from models_library.api_schemas_long_running_tasks.tasks import (
@@ -25,10 +24,7 @@
2524
AsyncJobStatus,
2625
)
2726
from models_library.api_schemas_rpc_async_jobs.exceptions import (
28-
JobAbortedError,
29-
JobError,
3027
JobMissingError,
31-
JobNotDoneError,
3228
JobSchedulerError,
3329
)
3430
from models_library.api_schemas_storage.export_data_async_jobs import (
@@ -425,23 +421,6 @@ async def test_upload_file(
425421
assert not data
426422

427423

428-
@pytest.fixture
429-
def create_backend_mock(
430-
mocker: MockerFixture,
431-
) -> Callable[[str, str, Any], None]:
432-
def _(module: str, method: str, result_or_exception: Any):
433-
def side_effect(*args, **kwargs):
434-
if isinstance(result_or_exception, Exception):
435-
raise result_or_exception
436-
437-
return result_or_exception
438-
439-
for fct in (f"{module}.{method}",):
440-
mocker.patch(fct, side_effect=side_effect)
441-
442-
return _
443-
444-
445424
@pytest.mark.parametrize("user_role", _user_roles)
446425
@pytest.mark.parametrize(
447426
"backend_result_or_exception, expected_status",
@@ -572,76 +551,6 @@ async def test_cancel_async_jobs(
572551
assert response.status == expected_status
573552

574553

575-
@pytest.mark.parametrize("user_role", _user_roles)
576-
@pytest.mark.parametrize(
577-
"backend_result_or_exception, expected_status",
578-
[
579-
(JobNotDoneError(job_id=_faker.uuid4()), status.HTTP_404_NOT_FOUND),
580-
(AsyncJobResult(result=None), status.HTTP_200_OK),
581-
(JobError(job_id=_faker.uuid4()), status.HTTP_500_INTERNAL_SERVER_ERROR),
582-
(JobAbortedError(job_id=_faker.uuid4()), status.HTTP_410_GONE),
583-
(JobSchedulerError(exc=_faker.text()), status.HTTP_500_INTERNAL_SERVER_ERROR),
584-
(JobMissingError(job_id=_faker.uuid4()), status.HTTP_404_NOT_FOUND),
585-
],
586-
ids=lambda x: type(x).__name__,
587-
)
588-
async def test_get_async_job_result(
589-
user_role: UserRole,
590-
logged_user: UserInfoDict,
591-
client: TestClient,
592-
create_backend_mock: Callable[[str, str, Any], None],
593-
faker: Faker,
594-
backend_result_or_exception: Any,
595-
expected_status: int,
596-
):
597-
_job_id = AsyncJobId(faker.uuid4())
598-
create_backend_mock(
599-
tasks_rest.__name__,
600-
f"_tasks_service.{_tasks_service.get_task_result.__name__}",
601-
backend_result_or_exception,
602-
)
603-
604-
response = await client.get(f"/{API_VERSION}/tasks/{_job_id}/result")
605-
assert response.status == expected_status
606-
607-
608-
@pytest.mark.parametrize("user_role", _user_roles)
609-
@pytest.mark.parametrize(
610-
"backend_result_or_exception, expected_status",
611-
[
612-
(
613-
[
614-
AsyncJobGet(
615-
job_id=AsyncJobId(_faker.uuid4()),
616-
job_name="task_name",
617-
)
618-
],
619-
status.HTTP_200_OK,
620-
),
621-
(JobSchedulerError(exc=_faker.text()), status.HTTP_500_INTERNAL_SERVER_ERROR),
622-
],
623-
ids=lambda x: type(x).__name__,
624-
)
625-
async def test_get_user_async_jobs(
626-
user_role: UserRole,
627-
logged_user: UserInfoDict,
628-
client: TestClient,
629-
create_backend_mock: Callable[[str, str, Any], None],
630-
backend_result_or_exception: Any,
631-
expected_status: int,
632-
):
633-
create_backend_mock(
634-
tasks_rest.__name__,
635-
f"_tasks_service.{_tasks_service.list_tasks.__name__}",
636-
backend_result_or_exception,
637-
)
638-
639-
response = await client.get(f"/{API_VERSION}/tasks")
640-
assert response.status == expected_status
641-
if response.status == status.HTTP_200_OK:
642-
Envelope[list[TaskGet]].model_validate(await response.json())
643-
644-
645554
@pytest.mark.parametrize("user_role", _user_roles)
646555
@pytest.mark.parametrize(
647556
"http_method, href, backend_method, backend_object, return_status, return_schema",
@@ -723,79 +632,3 @@ async def test_get_async_job_links(
723632
assert response.status == return_status
724633
if return_schema:
725634
Envelope[return_schema].model_validate(await response.json())
726-
727-
728-
@pytest.fixture
729-
def create_consume_events_mock(
730-
mocker: MockerFixture,
731-
) -> Callable[[Any], None]:
732-
def _(result_or_exception: Any):
733-
async def mock_consume_events(*args, **kwargs):
734-
if isinstance(result_or_exception, Exception):
735-
raise result_or_exception
736-
# Yield the mock events
737-
for event_id, event_data in result_or_exception:
738-
yield event_id, event_data
739-
740-
mock_task_manager = mocker.MagicMock()
741-
mock_task_manager.consume_task_events = mock_consume_events
742-
mocker.patch.object(
743-
simcore_service_webserver.tasks._controller._rest,
744-
"get_task_manager",
745-
return_value=mock_task_manager,
746-
)
747-
748-
return _
749-
750-
751-
class MockEvent:
752-
def __init__(self, event_type: str, event_data: dict[str, Any]):
753-
self.type = event_type
754-
self.data = event_data
755-
756-
757-
@pytest.mark.parametrize("user_role", _user_roles)
758-
@pytest.mark.parametrize(
759-
"backend_result_or_exception, expected_status",
760-
[
761-
(
762-
[
763-
("event-1", MockEvent("status", {"status": "running"})),
764-
("event-2", MockEvent("progress", {"percent": 50})),
765-
("event-3", MockEvent("status", {"status": "completed"})),
766-
],
767-
status.HTTP_200_OK,
768-
),
769-
([], status.HTTP_200_OK), # No events
770-
],
771-
ids=lambda x: (
772-
"with_events" if x and isinstance(x, list) and len(x) > 0 else "no_events"
773-
),
774-
)
775-
async def test_get_async_job_stream(
776-
user_role: UserRole,
777-
logged_user: UserInfoDict,
778-
client: TestClient,
779-
create_consume_events_mock: Callable[[Any], None],
780-
faker: Faker,
781-
backend_result_or_exception: Any,
782-
expected_status: int,
783-
):
784-
create_consume_events_mock(backend_result_or_exception)
785-
786-
_task_id = AsyncJobId(faker.uuid4())
787-
788-
response = await client.get(
789-
f"/{API_VERSION}/tasks/{_task_id}/stream",
790-
headers={"Accept": "text/event-stream"},
791-
)
792-
assert response.status == expected_status
793-
794-
if response.status == status.HTTP_200_OK:
795-
assert response.headers.get("Content-Type") == "text/event-stream"
796-
797-
content = await response.text()
798-
if backend_result_or_exception:
799-
assert "data:" in content or len(backend_result_or_exception) == 0
800-
else:
801-
assert content == ""

0 commit comments

Comments
 (0)