Skip to content

Commit

Permalink
adds tests for list_solvers_ports
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov committed Oct 29, 2022
1 parent 85bb6df commit b769f74
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
40 changes: 37 additions & 3 deletions services/api-server/tests/unit/api_solvers/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
# pylint: disable=unused-variable


from typing import Iterator
import json
from copy import deepcopy
from pathlib import Path
from typing import Any, Iterator

import pytest
import respx
Expand All @@ -13,18 +16,34 @@
from simcore_service_api_server.core.settings import ApplicationSettings


@pytest.fixture(scope="session")
def catalog_service_openapi_specs(osparc_simcore_services_dir: Path) -> dict[str, Any]:

openapi_path = osparc_simcore_services_dir / "catalog" / "openapi.json"
openapi_specs = json.loads(openapi_path.read_text())
return openapi_specs


@pytest.fixture
def mocked_catalog_service_api(app: FastAPI) -> Iterator[MockRouter]:
def mocked_catalog_service_api(
app: FastAPI, catalog_service_openapi_specs: dict[str, Any]
) -> Iterator[MockRouter]:
settings: ApplicationSettings = app.state.settings
assert settings.API_SERVER_CATALOG

openapi = deepcopy(catalog_service_openapi_specs)
schemas = openapi["components"]["schemas"]

# pylint: disable=not-context-manager
with respx.mock(
base_url=settings.API_SERVER_CATALOG.base_url,
base_url=settings.API_SERVER_CATALOG.api_base_url,
assert_all_called=False,
assert_all_mocked=True,
) as respx_mock:

respx_mock.get("/meta").respond(200, json=schemas["Meta"]["example"])

# ----
respx_mock.get(
"/services?user_id=1&details=false", name="list_services"
).respond(
Expand All @@ -42,4 +61,19 @@ def mocked_catalog_service_api(app: FastAPI) -> Iterator[MockRouter]:
],
)

# -----
# NOTE: we could use https://python-jsonschema.readthedocs.io/en/stable/
#

# https://regex101.com/r/drVAGr/1
respx_mock.get(
path__regex=r"/services/(?P<service_key>[\w%]+)/(?P<service_version>[\d\.]+)/ports\?user_id=(?P<user_id>\d+)",
name="list_service_ports",
).respond(
200,
json=[
schemas["ServicePortGet"]["example"],
],
)

yield respx_mock
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import httpx
import pytest
import simcore_service_api_server.api.routes.solvers
from pytest_mock import MockFixture
from respx import MockRouter
from simcore_service_api_server.models.schemas.solvers import Solver
from starlette import status
Expand All @@ -15,7 +16,7 @@
async def test_list_solvers(
client: httpx.AsyncClient,
mocked_catalog_service_api: MockRouter,
mocker,
mocker: MockFixture,
):
warn = mocker.patch.object(
simcore_service_api_server.api.routes.solvers.logger, "warning"
Expand Down Expand Up @@ -60,3 +61,29 @@ async def test_list_solvers(
assert f"GET latest {solver.id}" in resp2.json()["errors"][0]

# assert Solver(**resp2.json()) == Solver(**resp3.json())


async def test_list_solver_ports(
client: httpx.AsyncClient,
auth: httpx.BasicAuth,
mocked_catalog_service_api: MockRouter,
):
resp = await client.get(
"/v0/solvers/simcore/services/comp/itis/sleeper/releases/2.1.4/ports",
auth=auth,
)
assert resp.status_code == status.HTTP_200_OK

assert resp.json() == [
{
"name": "input_1",
"kind": "input",
"content_schema": {
"title": "Sleep interval",
"type": "integer",
"x_unit": "second",
"minimum": 0,
"maximum": 5,
},
}
]

0 comments on commit b769f74

Please sign in to comment.