Skip to content

Commit f655ca2

Browse files
authored
refactor: opinionated tweaks to pystapi-client tests (#92)
## What I'm changing - Tweak some import styles to @gadomski's preference (classes imported directly) - Rename `mocked_api` to `api` since it's unlikely we'll ever have a real API in the test suite - Use `http://stapi.test` instead of `https://stapi.example.com` (@gadomski prefers [.test](https://en.wikipedia.org/wiki/.test) to make it clear that urls are for a test case ... `example.com` actually resolves to something, wheras `.test` never will) ## Checklist - [x] Tests pass: `uv run pytest` - [x] Checks pass: `uv run pre-commit --all-files`
1 parent 9648659 commit f655ca2

File tree

4 files changed

+25
-30
lines changed

4 files changed

+25
-30
lines changed

pystapi-client/tests/conftest.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,35 @@
11
import json
2-
from collections.abc import Generator
2+
from collections.abc import Iterator
33
from copy import deepcopy
44
from pathlib import Path
5-
from typing import Any
5+
from typing import Any, cast
66

7-
import httpx
87
import pytest
98
import respx
10-
from httpx import Response
9+
from httpx import Request, Response
10+
from respx import MockRouter
1111

1212
WORKING_DIR = Path(__file__).parent
1313

1414

1515
def load_fixture(name: str) -> dict[str, Any]:
1616
with open(WORKING_DIR / "fixtures" / f"{name}.json") as f:
17-
return json.load(f) # type: ignore[no-any-return]
17+
return cast(dict, json.load(f))
1818

1919

2020
@pytest.fixture
21-
def mocked_api() -> Generator[respx.MockRouter, None, None]:
21+
def api() -> Iterator[MockRouter]:
2222
landing_page_data = load_fixture("landing_page")
2323
products = load_fixture("products")
2424

25-
with respx.mock(base_url="https://stapi.example.com", assert_all_called=False) as respx_mock:
25+
with respx.mock(base_url="http://stapi.test", assert_all_called=False) as respx_mock:
2626
landing_page = respx_mock.get("/")
2727
landing_page.return_value = Response(200, json=landing_page_data)
2828

2929
conformance_route = respx_mock.get("/conformance")
3030
conformance_route.return_value = Response(200, json={"conformsTo": landing_page_data["conformsTo"]})
3131

32-
# products_route.return_value = Response(200, json=products)
33-
34-
def mock_products_response(request: httpx.Request) -> httpx.Response:
32+
def mock_products_response(request: Request) -> Response:
3533
products_limited = deepcopy(products)
3634
limit = request.url.params.get("limit")
3735
page = int(request.url.params.get("page", 1))
@@ -43,7 +41,7 @@ def mock_products_response(request: httpx.Request) -> httpx.Response:
4341
if has_next_page:
4442
products_limited["links"].append(
4543
{
46-
"href": "https://stapi.example.com/products?limit=1&page=2",
44+
"href": "http://stapi.test/products?limit=1&page=2",
4745
"method": "GET",
4846
"rel": "next",
4947
}
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
21
{
32
"id": "example-stapi",
43
"title": "A simple STAPI Example",
54
"description": "This API demonstrated the landing page for a SpatioTemporal Asset Tasking API",
6-
"conformsTo" : [
5+
"conformsTo": [
76
"https://stapi.example.com/v0.1.0/core",
87
"https://geojson.org/schema/Point.json",
98
"https://geojson.org/schema/Polygon.json"
@@ -12,34 +11,32 @@
1211
{
1312
"rel": "conformance",
1413
"type": "application/json",
15-
"href": "https://stapi.example.com/conformance",
14+
"href": "http://stapi.test/conformance",
1615
"title": "Conformance classes implemented by this API"
1716
},
1817
{
1918
"rel": "orders",
2019
"type": "application/json",
21-
"href": "https://stapi.example.com/orders",
20+
"href": "http://stapi.test/orders",
2221
"title": "List of existing orders"
2322
},
2423
{
2524
"rel": "products",
2625
"type": "application/json",
27-
"href": "https://stapi.example.com/products",
26+
"href": "http://stapi.test/products",
2827
"title": "List of available products"
2928
},
3029
{
3130
"rel": "service-desc",
3231
"type": "application/vnd.oai.openapi+json;version=3.0",
33-
"href": "https://stapi.example.com/api",
32+
"href": "http://stapi.test/api",
3433
"title": "The machine-readable API definition"
35-
3634
},
3735
{
3836
"rel": "service-doc",
3937
"type": "text/html",
40-
"href": "https://stapi.example.com/api.html",
38+
"href": "http://stapi.test/api.html",
4139
"title": "The human-readable API documentation"
42-
4340
}
4441
]
4542
}

pystapi-client/tests/fixtures/products.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
],
2323
"links": [
2424
{
25-
"href": "https://stapi.example.com/",
25+
"href": "http://stapi.test/",
2626
"rel": "latest-version",
2727
"type": "media type",
2828
"title": "title"
@@ -123,7 +123,7 @@
123123
],
124124
"links": [
125125
{
126-
"href": "https://stapi.example.com/",
126+
"href": "http://stapi.test/",
127127
"rel": "latest-version",
128128
"type": "media type",
129129
"title": "title"
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
import respx
21
from pystapi_client.client import Client
2+
from respx import MockRouter
33
from stapi_pydantic import Link
44

55

6-
def test_get_products(mocked_api: respx.MockRouter) -> None:
7-
client = Client.open(url="https://stapi.example.com")
6+
def test_get_products(api: MockRouter) -> None:
7+
client = Client.open(url="http://stapi.test")
88

99
products = list(client.get_products())
1010
assert len(products) == 2
1111

1212

13-
def test_get_products_paginated(mocked_api: respx.MockRouter) -> None:
14-
client = Client.open(url="https://stapi.example.com")
13+
def test_get_products_paginated(api: MockRouter) -> None:
14+
client = Client.open(url="http://stapi.test")
1515

1616
products = list(client.get_products(limit=1))
1717
assert len(products) == 2
1818

1919

20-
def test_pagination(mocked_api: respx.MockRouter) -> None:
21-
client = Client.open(url="https://stapi.example.com")
20+
def test_pagination(api: MockRouter) -> None:
21+
client = Client.open(url="http://stapi.test")
2222

23-
products_link = Link(href="https://stapi.example.com/products", method="GET", body={"limit": 1}, rel="")
23+
products_link = Link(href="http://stapi.test/products", method="GET", body={"limit": 1}, rel="")
2424
for products_collection in client.stapi_io.get_pages(products_link, "products"):
2525
assert len(products_collection["products"]) == 1

0 commit comments

Comments
 (0)