Skip to content

Commit 48d1f9c

Browse files
committed
mv to pytest_simcore:
1 parent 39d351b commit 48d1f9c

File tree

4 files changed

+75
-49
lines changed

4 files changed

+75
-49
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# pylint: disable=redefined-outer-name
2+
# pylint: disable=unused-argument
3+
# pylint: disable=unused-variable
4+
# pylint: disable=too-many-arguments
5+
6+
from copy import deepcopy
7+
from functools import lru_cache
8+
from pathlib import Path
9+
from typing import Any, NamedTuple
10+
11+
import jsonref
12+
import pytest
13+
import yaml
14+
15+
16+
class Entrypoint(NamedTuple):
17+
name: str
18+
method: str
19+
path: str
20+
21+
22+
@lru_cache # ANE: required to boost tests speed, gains 3.5s per test
23+
def _load(file: Path, base_uri: str = "") -> dict:
24+
# SEE https://jsonref.readthedocs.io/en/latest/#lazy-load-and-load-on-repr
25+
data: dict = jsonref.replace_refs( # type: ignore
26+
yaml.safe_load(file.read_text()),
27+
base_uri=base_uri,
28+
lazy_load=True, # this data will be iterated
29+
merge_props=False,
30+
)
31+
return data
32+
33+
34+
@pytest.fixture
35+
def openapi_specs_path() -> Path:
36+
pytest.fail(reason="Must be overriden in caller tests package")
37+
38+
39+
@pytest.fixture
40+
def openapi_specs(openapi_specs_path: Path) -> dict[str, Any]:
41+
assert openapi_specs_path.is_file()
42+
# TODO: If openapi_specs_path is a url, download in tmp_dir and get path
43+
44+
openapi: dict[str, Any] = _load(
45+
openapi_specs_path, base_uri=openapi_specs_path.as_uri()
46+
)
47+
return deepcopy(openapi)
48+
49+
50+
@pytest.fixture
51+
def openapi_specs_entrypoints(
52+
openapi_specs: dict,
53+
) -> set[Entrypoint]:
54+
entrypoints: set[Entrypoint] = set()
55+
56+
# openapi-specifications, i.e. "contract"
57+
for path, path_obj in openapi_specs["paths"].items():
58+
for operation, operation_obj in path_obj.items():
59+
entrypoints.add(
60+
Entrypoint(
61+
method=operation.upper(),
62+
path=path,
63+
name=operation_obj["operationId"],
64+
)
65+
)
66+
return entrypoints

services/web/server/tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"pytest_simcore.simcore_service_library_fixtures",
7676
"pytest_simcore.simcore_services",
7777
"pytest_simcore.socketio_client",
78+
"pytest_simcore.openapi_specs",
7879
]
7980

8081

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

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88
import json
99
import sys
1010
from collections.abc import Callable, Iterable
11-
from functools import lru_cache
1211
from pathlib import Path
1312
from typing import Any
1413
from unittest.mock import MagicMock
1514

16-
import jsonref
1715
import pytest
1816
import yaml
1917
from pytest_simcore.helpers.dict_tools import ConfigDict
@@ -81,23 +79,7 @@ def disable_gc_manual_guest_users(mocker):
8179
)
8280

8381

84-
@lru_cache # ANE: required to boost tests speed, gains 3.5s per test
85-
def _load_openapi_specs(spec_path: Path | None = None) -> dict:
86-
if spec_path is None:
87-
spec_path = get_openapi_specs_path()
88-
89-
with spec_path.open() as fh:
90-
spec_dict = yaml.safe_load(fh)
91-
92-
# SEE https://jsonref.readthedocs.io/en/latest/#lazy-load-and-load-on-repr
93-
openapi: dict = jsonref.replace_refs(
94-
spec_dict, base_uri=spec_path.as_uri(), lazy_load=True, merge_props=False
95-
)
96-
return openapi
97-
98-
9982
@pytest.fixture
100-
def openapi_specs(api_version_prefix) -> dict:
101-
102-
spec_path = get_openapi_specs_path(api_version_prefix)
103-
return _load_openapi_specs(spec_path)
83+
def openapi_specs_path(api_version_prefix: str) -> Path:
84+
# overrides pytest_simcore.openapi_specs.app_openapi_specs_path fixture
85+
return get_openapi_specs_path(api_version_prefix)

services/web/server/tests/unit/with_dbs/03/test__openapi_specs.py

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,17 @@
33
# pylint: disable=unused-argument
44
# pylint: disable=unused-variable
55

6-
from typing import NamedTuple
76

87
import pytest
98
from aiohttp import web
109
from faker import Faker
1110
from pytest_simcore.helpers.monkeypatch_envs import setenvs_from_dict
1211
from pytest_simcore.helpers.typing_env import EnvVarsDict
12+
from pytest_simcore.openapi_specs import Entrypoint
1313
from simcore_service_webserver.application import create_application
1414
from simcore_service_webserver.application_settings import get_application_settings
1515

1616

17-
class Entrypoint(NamedTuple):
18-
name: str
19-
method: str
20-
path: str
21-
22-
2317
@pytest.fixture
2418
def app_environment(
2519
mock_env_devel_environment: EnvVarsDict,
@@ -60,24 +54,7 @@ def app(app_environment: EnvVarsDict) -> web.Application:
6054

6155

6256
@pytest.fixture
63-
def expected_openapi_entrypoints(openapi_specs: dict) -> set[Entrypoint]:
64-
entrypoints: set[Entrypoint] = set()
65-
66-
# openapi-specifications, i.e. "contract"
67-
for path, path_obj in openapi_specs["paths"].items():
68-
for operation, operation_obj in path_obj.items():
69-
entrypoints.add(
70-
Entrypoint(
71-
method=operation.upper(),
72-
path=path,
73-
name=operation_obj["operationId"],
74-
)
75-
)
76-
return entrypoints
77-
78-
79-
@pytest.fixture
80-
def app_entrypoints(app: web.Application) -> set[Entrypoint]:
57+
def app_rest_entrypoints(app: web.Application) -> set[Entrypoint]:
8158
entrypoints: set[Entrypoint] = set()
8259

8360
# app routes, i.e. "exposed"
@@ -102,12 +79,12 @@ def app_entrypoints(app: web.Application) -> set[Entrypoint]:
10279

10380

10481
def test_app_named_resources_against_openapi_specs(
105-
expected_openapi_entrypoints: set[Entrypoint],
106-
app_entrypoints: set[Entrypoint],
82+
openapi_specs_entrypoints: set[Entrypoint],
83+
app_rest_entrypoints: set[Entrypoint],
10784
):
10885
# check whether exposed routes implements openapi.json contract
10986

110-
assert app_entrypoints == expected_openapi_entrypoints
87+
assert app_rest_entrypoints == openapi_specs_entrypoints
11188

11289
# NOTE: missing here is:
11390
# - input schemas (path, query and body)

0 commit comments

Comments
 (0)