From fb14c6865c38dc324a90017cc83c8b9c4eb2c7f8 Mon Sep 17 00:00:00 2001 From: Mads Bisgaard Date: Wed, 11 Oct 2023 11:39:10 +0200 Subject: [PATCH] unit tests for getting job wallet --- .../tests/mocks/get_job_wallet_found.json | 13 +++- .../tests/mocks/get_job_wallet_not_found.json | 34 ---------- .../tests/unit/test_api_solver_jobs.py | 68 +++++++++++++++++++ 3 files changed, 79 insertions(+), 36 deletions(-) create mode 100644 services/api-server/tests/unit/test_api_solver_jobs.py diff --git a/services/api-server/tests/mocks/get_job_wallet_found.json b/services/api-server/tests/mocks/get_job_wallet_found.json index cb59eb2eae3..acc5d6078fe 100644 --- a/services/api-server/tests/mocks/get_job_wallet_found.json +++ b/services/api-server/tests/mocks/get_job_wallet_found.json @@ -22,14 +22,23 @@ "allOf": null, "oneOf": null }, - "response_value": "87643648-3a38-44e2-9cfe-d86ab3d50629" + "response_value": "projects" } ] }, "query": null, "request_payload": null, "response_body": { - "data": null + "data": { + "walletId": 1, + "name": "my_wallet", + "description": "my awesome wallet", + "owner": 3, + "thumbnail": "string", + "status": "ACTIVE", + "created": "2023-10-10T13:58:20.381826+00:00", + "modified": "2023-10-10T13:58:20.381826+00:00" + } }, "status_code": 200 } diff --git a/services/api-server/tests/mocks/get_job_wallet_not_found.json b/services/api-server/tests/mocks/get_job_wallet_not_found.json index db4eea9c5bb..0eb04bdb6a1 100644 --- a/services/api-server/tests/mocks/get_job_wallet_not_found.json +++ b/services/api-server/tests/mocks/get_job_wallet_not_found.json @@ -1,38 +1,4 @@ [ - { - "name": "GET /projects/87643648-3a38-44e2-9cfe-d86ab3d50629/wallet", - "description": "", - "method": "GET", - "host": "webserver", - "path": { - "path": "/v0/projects/{project_id}/wallet", - "path_parameters": [ - { - "in_": "path", - "name": "project_id", - "required": true, - "schema_": { - "title": "Project Id", - "type_": "str", - "pattern": null, - "format_": "uuid", - "exclusiveMinimum": null, - "minimum": null, - "anyOf": null, - "allOf": null, - "oneOf": null - }, - "response_value": "87643648-3a38-44e2-9cfe-d86ab3d50629" - } - ] - }, - "query": null, - "request_payload": null, - "response_body": { - "data": null - }, - "status_code": 200 - }, { "name": "GET /projects/87643648-3a38-44e2-9cfe-d86ac3d50629/wallet", "description": "", diff --git a/services/api-server/tests/unit/test_api_solver_jobs.py b/services/api-server/tests/unit/test_api_solver_jobs.py new file mode 100644 index 00000000000..6002aec178d --- /dev/null +++ b/services/api-server/tests/unit/test_api_solver_jobs.py @@ -0,0 +1,68 @@ +from pathlib import Path +from typing import Any, Callable +from uuid import UUID + +import httpx +import pytest +import respx +from httpx import AsyncClient +from simcore_service_api_server._meta import API_VTAG +from simcore_service_api_server.utils.http_calls_capture import HttpApiCallCaptureModel +from unit.conftest import SideEffectCallback + + +@pytest.mark.parametrize( + "capture", ["get_job_wallet_found.json", "get_job_wallet_not_found.json"] +) +async def test_get_solver_job_wallet( + client: AsyncClient, + mocked_webserver_service_api_base, + respx_mock_from_capture: Callable[ + [respx.MockRouter, Path, list[SideEffectCallback] | None], respx.MockRouter + ], + auth: httpx.BasicAuth, + project_tests_dir: Path, + capture: str, +): + + _wallet_id: int = 1826 + + def _get_job_wallet_side_effect( + request: httpx.Request, + path_params: dict[str, Any], + capture: HttpApiCallCaptureModel, + ) -> Any: + response = capture.response_body + assert isinstance(response, dict) + if data := response.get("data"): + assert isinstance(data, dict) + assert data.get("walletId") + response["data"]["walletId"] = _wallet_id + return response + + respx_mock = respx_mock_from_capture( + mocked_webserver_service_api_base, + project_tests_dir / "mocks" / capture, + [_get_job_wallet_side_effect], + ) + + solver_key: str = "simcore/services/comp/my_super_hpc_solver" + solver_version: str = "3.14.0" + job_id: UUID = UUID("87643648-3a38-44e2-9cfe-d86ab3d50629") + response = await client.get( + f"{API_VTAG}/solvers/{solver_key}/releases/{solver_version}/jobs/{job_id}/wallet", + auth=auth, + ) + if capture == "get_job_wallet_found.json": + assert response.status_code == 200 + body = response.json() + assert isinstance(body, dict) + assert _wallet_id == body.get("walletId") + elif capture == "get_job_wallet_not_found.json": + assert response.status_code == 404 + body = response.json() + assert isinstance(body, dict) + assert body.get("data") is None + assert body.get("errors") is not None + else: + pytest.fail()