Skip to content

Commit 9031e8d

Browse files
author
Andrei Neagu
committed
refactored tests
1 parent 1e7aa1c commit 9031e8d

File tree

3 files changed

+69
-57
lines changed

3 files changed

+69
-57
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import pytest
2+
from celery import Celery, Task
3+
from fastapi import FastAPI
4+
from models_library.progress_bar import ProgressReport
5+
from pytest_mock import MockerFixture
6+
from simcore_service_storage.modules.celery.utils import (
7+
set_celery_worker_client,
8+
set_fastapi_app,
9+
)
10+
from simcore_service_storage.modules.celery.worker import CeleryWorkerClient
11+
12+
13+
@pytest.fixture
14+
def fake_celery_task(celery_app: Celery, initialized_app: FastAPI) -> Task:
15+
celery_task = Task()
16+
celery_task.app = celery_app
17+
set_fastapi_app(celery_app, initialized_app)
18+
set_celery_worker_client(celery_app, CeleryWorkerClient(celery_app))
19+
return celery_task
20+
21+
22+
@pytest.fixture
23+
def mock_task_progress(mocker: MockerFixture) -> list[ProgressReport]:
24+
progress_updates = []
25+
26+
async def _progress(*args, **_) -> None:
27+
progress_updates.append(args[1])
28+
29+
mocker.patch(
30+
"simcore_service_storage.modules.celery.worker.CeleryWorkerClient.set_task_progress",
31+
side_effect=_progress,
32+
)
33+
return progress_updates
Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,52 @@
11
import re
2-
from collections.abc import Callable
32

43
import pytest
5-
from celery import Celery
4+
from celery import Task
5+
from models_library.progress_bar import ProgressReport, ProgressStructuredMessage
66
from models_library.users import UserID
77
from simcore_service_storage.api._worker_tasks._data_export import data_export
8-
from simcore_service_storage.modules.celery._task import define_task
9-
from simcore_service_storage.modules.celery.client import CeleryTaskQueueClient
10-
from simcore_service_storage.modules.celery.models import (
11-
TaskContext,
12-
TaskState,
13-
)
14-
from tenacity import Retrying, retry_if_exception_type, stop_after_delay, wait_fixed
158

169
pytest_simcore_core_services_selection = [
1710
"postgres",
1811
"rabbit",
1912
]
2013

2114

22-
@pytest.fixture
23-
def register_celery_tasks() -> Callable[[Celery], None]:
24-
def _(celery_app: Celery) -> None:
25-
define_task(celery_app, data_export)
26-
27-
return _
28-
29-
3015
@pytest.mark.usefixtures("celery_worker")
31-
async def test_data_export(celery_client: CeleryTaskQueueClient, user_id: UserID):
32-
task_context = TaskContext()
33-
34-
task_uuid = await celery_client.send_task(
35-
data_export.__name__,
36-
task_context=task_context,
37-
user_id=user_id,
38-
paths_to_export=[],
39-
)
40-
41-
for attempt in Retrying(
42-
retry=retry_if_exception_type(AssertionError),
43-
wait=wait_fixed(1),
44-
stop=stop_after_delay(30),
45-
):
46-
with attempt:
47-
status = await celery_client.get_task_status(task_context, task_uuid)
48-
assert status.task_state == TaskState.SUCCESS
49-
50-
assert (
51-
await celery_client.get_task_status(task_context, task_uuid)
52-
).task_state == TaskState.SUCCESS
53-
54-
result = await celery_client.get_task_result(task_context, task_uuid)
16+
async def test_data_export(
17+
mock_task_progress: list[ProgressReport], fake_celery_task: Task, user_id: UserID
18+
):
19+
result = await data_export(fake_celery_task, user_id=user_id, paths_to_export=[])
5520
assert re.fullmatch(
5621
rf"^exports/{user_id}/[0-9a-fA-F]{{8}}-[0-9a-fA-F]{{4}}-[0-9a-fA-F]{{4}}-[0-9a-fA-F]{{4}}-[0-9a-fA-F]{{12}}\.zip$",
5722
result,
5823
)
24+
25+
assert mock_task_progress == [
26+
ProgressReport(
27+
actual_value=0.0,
28+
total=1.0,
29+
attempt=0,
30+
unit=None,
31+
message=ProgressStructuredMessage(
32+
description="create and upload export",
33+
current=0.0,
34+
total=1,
35+
unit=None,
36+
sub=None,
37+
),
38+
),
39+
ProgressReport(
40+
actual_value=1.0,
41+
total=1.0,
42+
attempt=0,
43+
unit=None,
44+
message=ProgressStructuredMessage(
45+
description="create and upload export",
46+
current=1.0,
47+
total=1,
48+
unit=None,
49+
sub=None,
50+
),
51+
),
52+
]

services/storage/tests/unit/api/_worker_tasks/test__paths.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,14 @@
1212

1313
import httpx
1414
import pytest
15-
from celery import Celery, Task
15+
from celery import Task
1616
from faker import Faker
1717
from fastapi import FastAPI
1818
from models_library.projects_nodes_io import LocationID, NodeID, SimcoreS3FileID
1919
from models_library.users import UserID
2020
from pydantic import ByteSize, TypeAdapter
2121
from pytest_simcore.helpers.storage_utils import FileIDDict, ProjectWithFilesParams
2222
from simcore_service_storage.api._worker_tasks._paths import compute_path_size
23-
from simcore_service_storage.modules.celery.utils import (
24-
set_celery_worker_client,
25-
set_fastapi_app,
26-
)
27-
from simcore_service_storage.modules.celery.worker import CeleryWorkerClient
2823
from simcore_service_storage.simcore_s3_dsm import SimcoreS3DataManager
2924

3025
pytest_simcore_core_services_selection = [
@@ -69,16 +64,6 @@ async def _assert_compute_path_size(
6964
return response
7065

7166

72-
@pytest.fixture
73-
def fake_celery_task(celery_app: Celery, initialized_app: FastAPI) -> Task:
74-
celery_task = Task()
75-
celery_task.name = "fake_name"
76-
celery_task.app = celery_app
77-
set_fastapi_app(celery_app, initialized_app)
78-
set_celery_worker_client(celery_app, CeleryWorkerClient(celery_app))
79-
return celery_task
80-
81-
8267
@pytest.mark.parametrize(
8368
"location_id",
8469
[SimcoreS3DataManager.get_location_id()],

0 commit comments

Comments
 (0)