Skip to content
This repository was archived by the owner on Apr 2, 2025. It is now read-only.

Commit 000ef59

Browse files
author
Phil Varner
committed
refactor use of assert_link
1 parent 507fb90 commit 000ef59

File tree

4 files changed

+31
-27
lines changed

4 files changed

+31
-27
lines changed

src/stapi_fastapi/models/conformance.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55

66

77
class Conformance(BaseModel):
8-
conforms_to: list[str] = Field(default_factory=list, serialization_alias="conformsTo")
8+
conforms_to: list[str] = Field(
9+
default_factory=list, serialization_alias="conformsTo"
10+
)

tests/conftest.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from collections.abc import Iterator
22
from datetime import UTC, datetime, timedelta, timezone
3-
from typing import Callable
3+
from typing import Any, Callable
44
from urllib.parse import urljoin
55
from uuid import uuid4
66

@@ -19,6 +19,7 @@
1919
from stapi_fastapi.routers.root_router import RootRouter
2020

2121
from .backends import MockOrderDB, MockProductBackend, MockRootBackend
22+
from .utils import find_link
2223

2324

2425
class TestSpotlightProperties(OpportunityPropertiesBase):
@@ -87,6 +88,23 @@ def url_for(value: str) -> str:
8788
yield url_for
8889

8990

91+
@pytest.fixture
92+
def assert_link(url_for) -> Callable:
93+
def _assert_link(
94+
req: str,
95+
body: dict[str, Any],
96+
rel: str,
97+
path: str,
98+
media_type: str = "application/json",
99+
):
100+
link = find_link(body["links"], rel)
101+
assert link, f"{req} Link[rel={rel}] should exist"
102+
assert link["type"] == media_type
103+
assert link["href"] == url_for(path)
104+
105+
return _assert_link
106+
107+
90108
@pytest.fixture
91109
def products(mock_product_test_spotlight: Product) -> list[Product]:
92110
return [mock_product_test_spotlight]

tests/root_test.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33

44
from stapi_fastapi.models.conformance import CORE
55

6-
from .utils import assert_link
76

8-
9-
def test_root(stapi_client: TestClient, url_for) -> None:
7+
def test_root(stapi_client: TestClient, assert_link) -> None:
108
res = stapi_client.get("/")
119

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

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

19-
assert_link("GET /", body, "self", "/", url_for)
20-
assert_link("GET /", body, "service-description", "/openapi.json", url_for)
21-
assert_link("GET /", body, "service-docs", "/docs", url_for, media_type="text/html")
22-
assert_link("GET /", body, "conformance", "/conformance", url_for)
23-
assert_link("GET /", body, "products", "/products", url_for)
24-
assert_link("GET /", body, "orders", "/orders", url_for)
17+
assert_link("GET /", body, "self", "/")
18+
assert_link("GET /", body, "service-description", "/openapi.json")
19+
assert_link("GET /", body, "service-docs", "/docs", media_type="text/html")
20+
assert_link("GET /", body, "conformance", "/conformance")
21+
assert_link("GET /", body, "products", "/products")
22+
assert_link("GET /", body, "orders", "/orders")

tests/utils.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
1-
from typing import Any, Callable
1+
from typing import Any
22

3-
link_dict = dict[str, Any]
3+
type link_dict = dict[str, Any]
44

55

66
def find_link(links: list[link_dict], rel: str) -> link_dict | None:
77
return next((link for link in links if link["rel"] == rel), None)
8-
9-
10-
def assert_link(
11-
req: str,
12-
body: dict[str, Any],
13-
rel: str,
14-
path: str,
15-
url_for: Callable[[str], str],
16-
media_type: str = "application/json",
17-
) -> None:
18-
link = find_link(body["links"], rel)
19-
assert link, f"{req} Link[rel={rel}] should exist"
20-
assert link["type"] == media_type
21-
assert link["href"] == url_for(path)

0 commit comments

Comments
 (0)