Skip to content

Commit

Permalink
both tests run
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov committed Nov 21, 2024
1 parent 752cc1f commit 56512a2
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 69 deletions.
3 changes: 2 additions & 1 deletion packages/pytest-simcore/src/pytest_simcore/openapi_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
# pylint: disable=unused-variable
# pylint: disable=too-many-arguments

from collections.abc import Callable
from copy import deepcopy
from functools import lru_cache
from pathlib import Path
from typing import Any, Callable, NamedTuple
from typing import Any, NamedTuple

import jsonref
import pytest
Expand Down
1 change: 1 addition & 0 deletions services/storage/requirements/_test.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ coverage
docker
faker
fakeredis[lua]
jsonref
moto[server]
pandas
pytest
Expand Down
2 changes: 2 additions & 0 deletions services/storage/requirements/_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ jsonpath-ng==1.6.1
# via moto
jsonpointer==3.0.0
# via jsonpatch
jsonref==1.1.0
# via -r requirements/_test.in
jsonschema==4.21.1
# via
# -c requirements/_base.txt
Expand Down
1 change: 1 addition & 0 deletions services/storage/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"pytest_simcore.file_extra",
"pytest_simcore.httpbin_service",
"pytest_simcore.minio_service",
"pytest_simcore.openapi_specs",
"pytest_simcore.postgres_service",
"pytest_simcore.pytest_global_environs",
"pytest_simcore.repository_paths",
Expand Down
74 changes: 17 additions & 57 deletions services/storage/tests/unit/test__openapi_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@
# pylint: disable=unused-argument
# pylint: disable=unused-variable

from collections.abc import Callable
from pathlib import Path
from typing import NamedTuple

import openapi_core
import pytest
import simcore_service_storage.application
from aiohttp import web
from faker import Faker
from openapi_core import Spec as OpenApiSpecs
from pytest_simcore.helpers.monkeypatch_envs import setenvs_from_dict
from pytest_simcore.helpers.typing_env import EnvVarsDict
from pytest_simcore.openapi_specs import Entrypoint
from simcore_service_storage._meta import API_VTAG
from simcore_service_storage.resources import storage_resources
from simcore_service_storage.settings import Settings


class Entrypoint(NamedTuple):
name: str
method: str
path: str
@pytest.fixture
def openapi_specs_path() -> Path:
# overrides pytest_simcore.openapi_specs.app_openapi_specs_path fixture
spec_path: Path = storage_resources.get_path(f"api/{API_VTAG}/openapi.yaml")
return spec_path


@pytest.fixture
Expand All @@ -42,6 +42,7 @@ def app_environment(

@pytest.fixture
def app(app_environment: EnvVarsDict) -> web.Application:
assert app_environment
# Expects that:
# - routings happen during setup!
# - all plugins are setup but app is NOT started (i.e events are not triggered)
Expand All @@ -50,61 +51,20 @@ def app(app_environment: EnvVarsDict) -> web.Application:
return simcore_service_storage.application.create(settings)


@pytest.fixture(scope="module")
def openapi_specs() -> openapi_core.Spec:
spec_path: Path = storage_resources.get_path(f"api/{API_VTAG}/openapi.yaml")
return openapi_core.Spec.from_path(spec_path)


@pytest.fixture
def expected_openapi_entrypoints(openapi_specs: OpenApiSpecs) -> set[Entrypoint]:
entrypoints: set[Entrypoint] = set()

# openapi-specifications, i.e. "contract"
for path, path_obj in openapi_specs["paths"].items():
for operation, operation_obj in path_obj.items():
entrypoints.add(
Entrypoint(
method=operation.upper(),
path=path,
name=operation_obj["operationId"],
)
)
return entrypoints


@pytest.fixture
def app_entrypoints(app: web.Application) -> set[Entrypoint]:
entrypoints: set[Entrypoint] = set()

# app routes, i.e. "exposed"
for resource_name, resource in app.router.named_resources().items():
resource_path = resource.canonical
for route in resource:
assert route.name == resource_name
assert route.resource
assert route.name is not None

if route.method == "HEAD":
continue

entrypoints.add(
Entrypoint(
method=route.method,
path=resource_path,
name=route.name,
)
)
return entrypoints
def app_rest_entrypoints(
app: web.Application,
create_aiohttp_app_rest_entrypoints: Callable[[web.Application], set[Entrypoint]],
) -> set[Entrypoint]:
# check whether exposed routes implements openapi.json contract
return create_aiohttp_app_rest_entrypoints(app)


def test_app_named_resources_against_openapi_specs(
expected_openapi_entrypoints: set[Entrypoint],
app_entrypoints: set[Entrypoint],
openapi_specs_entrypoints: set[Entrypoint],
app_rest_entrypoints: set[Entrypoint],
):
# check whether exposed routes implements openapi.json contract

assert app_entrypoints == expected_openapi_entrypoints
assert app_rest_entrypoints == openapi_specs_entrypoints

# NOTE: missing here is:
# - input schemas (path, query and body)
Expand Down
7 changes: 0 additions & 7 deletions services/web/server/tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import yaml
from pytest_simcore.helpers.dict_tools import ConfigDict
from pytest_simcore.helpers.webserver_projects import empty_project_data
from simcore_service_webserver.rest._utils import get_openapi_specs_path

CURRENT_DIR = Path(sys.argv[0] if __name__ == "__main__" else __file__).resolve().parent

Expand Down Expand Up @@ -77,9 +76,3 @@ def disable_gc_manual_guest_users(mocker):
"simcore_service_webserver.garbage_collector._core.remove_users_manually_marked_as_guests",
return_value=None,
)


@pytest.fixture
def openapi_specs_path(api_version_prefix: str) -> Path:
# overrides pytest_simcore.openapi_specs.app_openapi_specs_path fixture
return get_openapi_specs_path(api_version_prefix)
22 changes: 18 additions & 4 deletions services/web/server/tests/unit/with_dbs/03/test__openapi_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


from collections.abc import Callable
from pathlib import Path

import pytest
from aiohttp import web
Expand All @@ -14,6 +15,13 @@
from pytest_simcore.openapi_specs import Entrypoint
from simcore_service_webserver.application import create_application
from simcore_service_webserver.application_settings import get_application_settings
from simcore_service_webserver.rest._utils import get_openapi_specs_path


@pytest.fixture
def openapi_specs_path(api_version_prefix: str) -> Path:
# overrides pytest_simcore.openapi_specs.app_openapi_specs_path fixture
return get_openapi_specs_path(api_version_prefix)


@pytest.fixture
Expand Down Expand Up @@ -46,6 +54,7 @@ def app_environment(

@pytest.fixture
def app(app_environment: EnvVarsDict) -> web.Application:
assert app_environment
# Expects that:
# - routings happen during setup!
# - all plugins are setup but app is NOT started (i.e events are not triggered)
Expand All @@ -55,14 +64,19 @@ def app(app_environment: EnvVarsDict) -> web.Application:
return app_


def test_app_named_resources_against_openapi_specs(
openapi_specs_entrypoints: set[Entrypoint],
@pytest.fixture
def app_rest_entrypoints(
app: web.Application,
create_aiohttp_app_rest_entrypoints: Callable[[web.Application], set[Entrypoint]],
):
) -> set[Entrypoint]:
# check whether exposed routes implements openapi.json contract
app_rest_entrypoints: set[Entrypoint] = create_aiohttp_app_rest_entrypoints(app)
return create_aiohttp_app_rest_entrypoints(app)


def test_app_named_resources_against_openapi_specs(
openapi_specs_entrypoints: set[Entrypoint],
app_rest_entrypoints: set[Entrypoint],
):
assert app_rest_entrypoints == openapi_specs_entrypoints

# NOTE: missing here is:
Expand Down

0 comments on commit 56512a2

Please sign in to comment.