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

Commit fb9710e

Browse files
tests: extend pagination tester to inlcude rebuilding and passing POST body tests: add pagination tester to search opportunties test
1 parent 0d5d837 commit fb9710e

File tree

4 files changed

+24
-31
lines changed

4 files changed

+24
-31
lines changed

src/stapi_fastapi/routers/product_router.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ async def search_opportunities(
179179
):
180180
case Success((features, pagination_token)):
181181
links = [
182-
Link( # current bug is missing method set and setting body for
182+
Link(
183183
href=str(
184184
request.url_for(
185185
f"{self.root_router.name}:{self.product.id}:create-order",

tests/conftest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ def pagination_tester(
185185
next_url = next(
186186
(d["href"] for d in resp_body["links"] if d["rel"] == "next"), None
187187
)
188+
body = next(
189+
(d.et("body") for d in resp_body["links"] if d.get("body")), None
190+
)
188191
else:
189192
next_url = None
190193

tests/test_opportunity.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
from uuid import uuid4
44

55
import pytest
6-
from fastapi import status
76
from fastapi.testclient import TestClient
87
from geojson_pydantic import Point
98
from geojson_pydantic.types import Position2D
109

1110
from stapi_fastapi.models.opportunity import Opportunity, OpportunityCollection
1211
from tests.application import MyOpportunityProperties
12+
from tests.conftest import pagination_tester
1313

1414
from .backends import MockProductBackend
1515
from .test_datetime_interval import rfc3339_strftime
@@ -127,12 +127,12 @@ def test_search_opportunities_pagination(
127127
},
128128
}
129129

130-
res = stapi_client.post(
131-
f"/products/{product_id}/opportunities",
132-
json=request_payload,
133-
params={"next": None, "limit": 2},
130+
pagination_tester(
131+
stapi_client=stapi_client,
132+
endpoint=f"/products/{product_id}/opportunities",
133+
method="POST",
134+
limit=2,
135+
target="features",
136+
expected_total_returns=3,
137+
body=request_payload,
134138
)
135-
body = res.json() # noqa: F841
136-
137-
assert res.status_code == status.HTTP_200_OK
138-
assert 1 == 2

tests/test_product.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from fastapi import status
33
from fastapi.testclient import TestClient
44

5+
from tests.conftest import pagination_tester
6+
57

68
def test_products_response(stapi_client: TestClient):
79
res = stapi_client.get("/products")
@@ -65,27 +67,15 @@ def test_product_order_parameters_response(
6567
assert "s3_path" in json_schema["properties"]
6668

6769

68-
@pytest.mark.parametrize("limit", [1])
69-
def test_product_pagination(stapi_client: TestClient, limit: int):
70-
res = stapi_client.get("/products", params={"next": None, "limit": limit})
71-
assert res.status_code == status.HTTP_200_OK
72-
body = res.json()
73-
assert len(body["products"]) == limit
74-
links = body["links"]
75-
for d in body["links"]:
76-
if ("rel", "next") in d.items():
77-
next = d["href"]
78-
79-
while len(links) > 1:
80-
res = stapi_client.get(next)
81-
assert res.status_code == status.HTTP_200_OK
82-
body = res.json()
83-
assert body["products"] != []
84-
links = body["links"]
85-
for d in body["links"]:
86-
if ("rel", "next") in d.items():
87-
assert len(body["products"]) == limit
88-
next = body["links"][0]["href"]
70+
def test_product_pagination(stapi_client: TestClient):
71+
pagination_tester(
72+
stapi_client=stapi_client,
73+
endpoint="/products",
74+
method="GET",
75+
limit=1,
76+
target="products",
77+
expected_total_returns=2,
78+
)
8979

9080

9181
def test_token_not_found(stapi_client: TestClient) -> None:

0 commit comments

Comments
 (0)