Skip to content

Commit f60d19b

Browse files
committed
cosmetic improvement
1 parent 77ceacb commit f60d19b

File tree

3 files changed

+92
-59
lines changed

3 files changed

+92
-59
lines changed

packages/pytest-simcore/src/pytest_simcore/helpers/webserver_rpc_server.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@
2626
class WebserverRpcSideEffects:
2727
# pylint: disable=no-self-use
2828

29+
def __init__(
30+
self,
31+
project_job_rpc_get: ProjectJobRpcGet = ProjectJobRpcGet.model_validate(
32+
ProjectJobRpcGet.model_json_schema()["examples"][0]
33+
),
34+
):
35+
self.project_job_rpc_get = project_job_rpc_get
36+
2937
@validate_call(config={"arbitrary_types_allowed": True})
3038
async def mark_project_as_job(
3139
self,
@@ -104,7 +112,7 @@ async def get_project_marked_as_job(
104112
assert job_parent_resource_name
105113

106114
# Return a valid example from the schema
107-
example = ProjectJobRpcGet.model_json_schema()["examples"][0]
108-
example["uuid"] = str(project_uuid)
109-
example["job_parent_resource_name"] = job_parent_resource_name
110-
return ProjectJobRpcGet.model_validate(example)
115+
_data = self.project_job_rpc_get.model_dump()
116+
_data["uuid"] = str(project_uuid)
117+
_data["job_parent_resource_name"] = job_parent_resource_name
118+
return ProjectJobRpcGet.model_validate(_data)

services/api-server/tests/unit/conftest.py

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@
4242
from models_library.products import ProductName
4343
from models_library.projects import ProjectID
4444
from models_library.projects_nodes_io import BaseFileLink, SimcoreS3FileID
45+
from models_library.rpc.webserver.projects import ProjectJobRpcGet
4546
from models_library.users import UserID
4647
from moto.server import ThreadedMotoServer
4748
from packaging.version import Version
48-
from pydantic import EmailStr, HttpUrl, TypeAdapter
49+
from pydantic import EmailStr, HttpUrl, TypeAdapter, validate_call
4950
from pytest_mock import MockerFixture, MockType
5051
from pytest_simcore.helpers.catalog_rpc_server import CatalogRpcSideEffects
5152
from pytest_simcore.helpers.director_v2_rpc_server import DirectorV2SideEffects
@@ -543,9 +544,17 @@ def mocked_catalog_rest_api_base(
543544
yield respx_mock
544545

545546

547+
@pytest.fixture
548+
def project_job_rpc_get() -> ProjectJobRpcGet:
549+
example = ProjectJobRpcGet.model_json_schema()["examples"][0]
550+
return ProjectJobRpcGet.model_validate(example)
551+
552+
546553
@pytest.fixture
547554
def mocked_webserver_rpc_api(
548-
mocked_app_dependencies: None, mocker: MockerFixture
555+
mocked_app_dependencies: None,
556+
mocker: MockerFixture,
557+
project_job_rpc_get: ProjectJobRpcGet,
549558
) -> dict[str, MockType]:
550559
"""
551560
Mocks the webserver's simcore service RPC API for testing purposes.
@@ -554,7 +563,7 @@ def mocked_webserver_rpc_api(
554563
projects as projects_rpc, # keep import here
555564
)
556565

557-
side_effects = WebserverRpcSideEffects()
566+
side_effects = WebserverRpcSideEffects(project_job_rpc_get=project_job_rpc_get)
558567

559568
return {
560569
"mark_project_as_job": mocker.patch.object(
@@ -578,6 +587,52 @@ def mocked_webserver_rpc_api(
578587
}
579588

580589

590+
@pytest.fixture
591+
def mocked_get_project_marked_as_job_storage_data_missing(
592+
mocked_webserver_rpc_api: dict[str, MockType], mocker: MockerFixture
593+
) -> dict[str, MockType]:
594+
from servicelib.rabbitmq.rpc_interfaces.webserver import (
595+
projects as projects_rpc, # keep import here
596+
)
597+
598+
@validate_call(config={"arbitrary_types_allowed": True})
599+
async def _get_project_marked_as_job(
600+
rpc_client: RabbitMQRPCClient | MockType,
601+
*,
602+
product_name: ProductName,
603+
user_id: UserID,
604+
project_uuid: ProjectID,
605+
job_parent_resource_name: str,
606+
) -> ProjectJobRpcGet:
607+
assert rpc_client
608+
assert product_name
609+
assert user_id
610+
assert project_uuid
611+
assert job_parent_resource_name
612+
613+
# Return a valid example from the schema
614+
example = ProjectJobRpcGet.model_json_schema()["examples"][0]
615+
example["uuid"] = str(project_uuid)
616+
example["job_parent_resource_name"] = job_parent_resource_name
617+
example["storage_data_deleted"] = "true"
618+
project_job_rpc_get = ProjectJobRpcGet.model_validate(example)
619+
assert project_job_rpc_get.storage_data_deleted is True
620+
return project_job_rpc_get
621+
622+
# Remove the previous patch if it exists
623+
mocked_webserver_rpc_api["get_project_marked_as_job"].reset_mock()
624+
del mocked_webserver_rpc_api["get_project_marked_as_job"]
625+
626+
# Add the new patch with the custom side effect
627+
mocked_webserver_rpc_api["get_project_marked_as_job"] = mocker.patch.object(
628+
projects_rpc,
629+
"get_project_marked_as_job",
630+
autospec=True,
631+
side_effect=_get_project_marked_as_job,
632+
)
633+
return mocked_webserver_rpc_api
634+
635+
581636
@pytest.fixture
582637
def catalog_rpc_side_effects(request) -> Any:
583638
if "param" in dir(request) and request.param is not None:

services/api-server/tests/unit/test_api_solver_jobs.py

Lines changed: 22 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# pylint: disable=unused-variable
44
# pylint: disable=too-many-arguments
55

6+
from datetime import datetime
67
from decimal import Decimal
78
from pathlib import Path
89
from typing import Any, Final
@@ -15,19 +16,15 @@
1516
from fastapi.encoders import jsonable_encoder
1617
from httpx import AsyncClient
1718
from models_library.generics import Envelope
18-
from models_library.products import ProductName
19-
from models_library.projects import ProjectID
2019
from models_library.rpc.webserver.projects import ProjectJobRpcGet
21-
from models_library.users import UserID
22-
from pydantic import TypeAdapter, validate_call
23-
from pytest_mock import MockerFixture, MockType
20+
from pydantic import TypeAdapter
21+
from pytest_mock import MockType
2422
from pytest_simcore.helpers.httpx_calls_capture_models import (
2523
CreateRespxMockCallback,
2624
HttpApiCallCaptureModel,
2725
SideEffectCallback,
2826
)
2927
from respx import MockRouter
30-
from servicelib.rabbitmq import RabbitMQRPCClient
3128
from simcore_service_api_server._meta import API_VTAG
3229
from simcore_service_api_server.models.schemas.jobs import Job, JobStatus
3330
from simcore_service_api_server.models.schemas.model_adapter import (
@@ -352,56 +349,29 @@ async def test_start_solver_job_conflict(
352349
assert f"{job_status.job_id}" == _job_id
353350

354351

355-
@pytest.fixture
356-
def mocked_get_project_marked_as_job_storage_data_missing(
357-
mocked_app_dependencies: None, mocker: MockerFixture
358-
) -> dict[str, MockType]:
359-
"""
360-
Mocks the webserver's simcore service RPC API for testing purposes.
361-
"""
362-
from servicelib.rabbitmq.rpc_interfaces.webserver import (
363-
projects as projects_rpc, # keep import here
364-
)
365-
366-
@validate_call(config={"arbitrary_types_allowed": True})
367-
async def _get_project_marked_as_job(
368-
rpc_client: RabbitMQRPCClient | MockType,
369-
*,
370-
product_name: ProductName,
371-
user_id: UserID,
372-
project_uuid: ProjectID,
373-
job_parent_resource_name: str,
374-
) -> ProjectJobRpcGet:
375-
assert rpc_client
376-
assert product_name
377-
assert user_id
378-
assert project_uuid
379-
assert job_parent_resource_name
380-
381-
# Return a valid example from the schema
382-
example = ProjectJobRpcGet.model_json_schema()["examples"][0]
383-
example["uuid"] = str(project_uuid)
384-
example["job_parent_resource_name"] = job_parent_resource_name
385-
example["storage_data_deleted"] = "true"
386-
project_job_rpc_get = ProjectJobRpcGet.model_validate(example)
387-
assert project_job_rpc_get.storage_data_deleted is True
388-
return project_job_rpc_get
389-
390-
return {
391-
"get_project_marked_as_job": mocker.patch.object(
392-
projects_rpc,
393-
"get_project_marked_as_job",
394-
autospec=True,
395-
side_effect=_get_project_marked_as_job,
396-
),
397-
}
398-
399-
352+
@pytest.mark.parametrize(
353+
"project_job_rpc_get",
354+
[
355+
pytest.param(
356+
ProjectJobRpcGet(
357+
uuid=UUID("00000000-1234-5678-1234-123456789012"),
358+
name="A study job",
359+
description="A description of a study job with many node",
360+
workbench={},
361+
created_at=datetime.fromisoformat("2023-02-01T00:00:00Z"),
362+
modified_at=datetime.fromisoformat("2023-02-01T00:00:00Z"),
363+
job_parent_resource_name="studies/96642f2a-a72c-11ef-8776-02420a00087d",
364+
storage_data_deleted=True,
365+
),
366+
id="storage_data_deleted",
367+
)
368+
],
369+
)
400370
async def test_start_solver_job_storage_data_missing(
401371
client: AsyncClient,
402372
mocked_webserver_rest_api_base: MockRouter,
403373
mocked_directorv2_rest_api_base: MockRouter,
404-
mocked_get_project_marked_as_job_storage_data_missing: dict[str, MockType],
374+
mocked_webserver_rpc_api: dict[str, MockType],
405375
create_respx_mock_from_capture: CreateRespxMockCallback,
406376
auth: httpx.BasicAuth,
407377
project_tests_dir: Path,

0 commit comments

Comments
 (0)