Skip to content

Commit

Permalink
refactor use of assert_link
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Varner committed Nov 12, 2024
1 parent 507fb90 commit 000ef59
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 27 deletions.
4 changes: 3 additions & 1 deletion src/stapi_fastapi/models/conformance.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@


class Conformance(BaseModel):
conforms_to: list[str] = Field(default_factory=list, serialization_alias="conformsTo")
conforms_to: list[str] = Field(
default_factory=list, serialization_alias="conformsTo"
)
20 changes: 19 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections.abc import Iterator
from datetime import UTC, datetime, timedelta, timezone
from typing import Callable
from typing import Any, Callable
from urllib.parse import urljoin
from uuid import uuid4

Expand All @@ -19,6 +19,7 @@
from stapi_fastapi.routers.root_router import RootRouter

from .backends import MockOrderDB, MockProductBackend, MockRootBackend
from .utils import find_link


class TestSpotlightProperties(OpportunityPropertiesBase):
Expand Down Expand Up @@ -87,6 +88,23 @@ def url_for(value: str) -> str:
yield url_for


@pytest.fixture
def assert_link(url_for) -> Callable:
def _assert_link(
req: str,
body: dict[str, Any],
rel: str,
path: str,
media_type: str = "application/json",
):
link = find_link(body["links"], rel)
assert link, f"{req} Link[rel={rel}] should exist"
assert link["type"] == media_type
assert link["href"] == url_for(path)

return _assert_link


@pytest.fixture
def products(mock_product_test_spotlight: Product) -> list[Product]:
return [mock_product_test_spotlight]
Expand Down
16 changes: 7 additions & 9 deletions tests/root_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

from stapi_fastapi.models.conformance import CORE

from .utils import assert_link


def test_root(stapi_client: TestClient, url_for) -> None:
def test_root(stapi_client: TestClient, assert_link) -> None:
res = stapi_client.get("/")

assert res.status_code == status.HTTP_200_OK
Expand All @@ -16,9 +14,9 @@ def test_root(stapi_client: TestClient, url_for) -> None:

assert body["conformsTo"] == [CORE]

assert_link("GET /", body, "self", "/", url_for)
assert_link("GET /", body, "service-description", "/openapi.json", url_for)
assert_link("GET /", body, "service-docs", "/docs", url_for, media_type="text/html")
assert_link("GET /", body, "conformance", "/conformance", url_for)
assert_link("GET /", body, "products", "/products", url_for)
assert_link("GET /", body, "orders", "/orders", url_for)
assert_link("GET /", body, "self", "/")
assert_link("GET /", body, "service-description", "/openapi.json")
assert_link("GET /", body, "service-docs", "/docs", media_type="text/html")
assert_link("GET /", body, "conformance", "/conformance")
assert_link("GET /", body, "products", "/products")
assert_link("GET /", body, "orders", "/orders")
18 changes: 2 additions & 16 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
from typing import Any, Callable
from typing import Any

link_dict = dict[str, Any]
type link_dict = dict[str, Any]


def find_link(links: list[link_dict], rel: str) -> link_dict | None:
return next((link for link in links if link["rel"] == rel), None)


def assert_link(
req: str,
body: dict[str, Any],
rel: str,
path: str,
url_for: Callable[[str], str],
media_type: str = "application/json",
) -> None:
link = find_link(body["links"], rel)
assert link, f"{req} Link[rel={rel}] should exist"
assert link["type"] == media_type
assert link["href"] == url_for(path)

0 comments on commit 000ef59

Please sign in to comment.