Skip to content

Commit

Permalink
further simplified tests in web
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov committed Nov 21, 2024
1 parent 48d1f9c commit 752cc1f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
41 changes: 40 additions & 1 deletion packages/pytest-simcore/src/pytest_simcore/openapi_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@
from copy import deepcopy
from functools import lru_cache
from pathlib import Path
from typing import Any, NamedTuple
from typing import Any, Callable, NamedTuple

import jsonref
import pytest
import yaml

try:
from aiohttp import web

has_aiohttp = True
except ImportError:
has_aiohttp = False


class Entrypoint(NamedTuple):
name: str
Expand Down Expand Up @@ -64,3 +71,35 @@ def openapi_specs_entrypoints(
)
)
return entrypoints


if has_aiohttp:

@pytest.fixture
def create_aiohttp_app_rest_entrypoints() -> Callable[
[web.Application], set[Entrypoint]
]:
def _(app: web.Application):
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

return _
31 changes: 5 additions & 26 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 @@ -4,6 +4,8 @@
# pylint: disable=unused-variable


from collections.abc import Callable

import pytest
from aiohttp import web
from faker import Faker
Expand Down Expand Up @@ -53,36 +55,13 @@ def app(app_environment: EnvVarsDict) -> web.Application:
return app_


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

assert app_rest_entrypoints == openapi_specs_entrypoints

Expand Down

0 comments on commit 752cc1f

Please sign in to comment.