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

Commit adf1f5f

Browse files
tests: updating pagination tests and pagination_tester to check exact results to make sure we get back the expected results from pagination, not just checking the number of results
1 parent fb9710e commit adf1f5f

File tree

4 files changed

+72
-12
lines changed

4 files changed

+72
-12
lines changed

tests/conftest.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def mock_product_test_wolf_cola(
7373
return Product(
7474
id="test-wolf-cola",
7575
title="Test Wolf Cola Product",
76-
description="Test product for Wolf Cola for testing GET /product pagination",
76+
description="The right cola for closure",
7777
license="CC-BY-4.0",
7878
keywords=["test", "satellite", "wolf-cola"],
7979
providers=[mock_provider],
@@ -161,7 +161,7 @@ def pagination_tester(
161161
method: str,
162162
limit: int,
163163
target: str,
164-
expected_total_returns: int,
164+
expected_returns: list,
165165
body: dict | None = None,
166166
) -> None:
167167
retrieved = []
@@ -181,6 +181,7 @@ def pagination_tester(
181181
resp_body = res.json()
182182
retrieved.extend(resp_body[target])
183183

184+
# get url w/ query params for next call if exists, and POST body if necessary
184185
if resp_body["links"]:
185186
next_url = next(
186187
(d["href"] for d in resp_body["links"] if d["rel"] == "next"), None
@@ -191,7 +192,8 @@ def pagination_tester(
191192
else:
192193
next_url = None
193194

194-
assert len(retrieved) == expected_total_returns
195+
assert len(retrieved) == len(expected_returns)
196+
assert retrieved == expected_returns
195197

196198

197199
def make_request(
@@ -203,6 +205,8 @@ def make_request(
203205
limit: int,
204206
) -> Response:
205207
"""request wrapper for pagination tests"""
208+
209+
# extract pagination token
206210
if next_token and "next=" in next_token:
207211
next_token = next_token.split("next=")[1]
208212
params = {"next": next_token, "limit": limit}

tests/test_opportunity.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ def test_search_opportunities_pagination(
104104
mock_test_pagination_opportunities: List[Opportunity],
105105
) -> None:
106106
product_backend._opportunities = mock_test_pagination_opportunities
107+
expected_returns = [
108+
x.model_dump(mode="json") for x in mock_test_pagination_opportunities
109+
]
107110

108111
now = datetime.now(UTC)
109112
start = now
@@ -133,6 +136,6 @@ def test_search_opportunities_pagination(
133136
method="POST",
134137
limit=2,
135138
target="features",
136-
expected_total_returns=3,
139+
expected_returns=expected_returns,
137140
body=request_payload,
138141
)

tests/test_order.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
from datetime import UTC, datetime, timedelta, timezone
23

34
import pytest
@@ -7,7 +8,7 @@
78
from geojson_pydantic.types import Position2D
89
from httpx import Response
910

10-
from stapi_fastapi.models.order import OrderPayload, OrderStatus, OrderStatusCode
11+
from stapi_fastapi.models.order import Order, OrderPayload, OrderStatus, OrderStatusCode
1112
from tests.conftest import pagination_tester
1213

1314
from .application import InMemoryOrderDB, MyOrderParameters
@@ -149,27 +150,43 @@ def test_order_status_after_create(
149150

150151

151152
@pytest.fixture
152-
def setup_orders_pagination(stapi_client: TestClient, create_order_payloads) -> None:
153+
def setup_orders_pagination(
154+
stapi_client: TestClient, create_order_payloads
155+
) -> list[Order]:
153156
product_id = "test-spotlight"
154-
157+
orders = []
158+
# t = {'id': 'dc2d9027-a670-475b-878c-a5a6e8ab022b', 'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [14.4, 56.5]}, 'properties': {'product_id': 'test-spotlight', 'created': '2025-01-16T19:09:45.448059Z', 'status': {'timestamp': '2025-01-16T19:09:45.447952Z', 'status_code': 'received', 'reason_code': None, 'reason_text': None, 'links': []}, 'search_parameters': {'datetime': '2024-10-09T18:55:33+00:00/2024-10-12T18:55:33+00:00', 'geometry': {'type': 'Point', 'coordinates': [14.4, 56.5]}, 'filter': None}, 'opportunity_properties': {'datetime': '2024-01-29T12:00:00Z/2024-01-30T12:00:00Z', 'off_nadir': 10}, 'order_parameters': {'s3_path': 's3://my-bucket'}}, 'links': [{'href': 'http://stapiserver/orders/dc2d9027-a670-475b-878c-a5a6e8ab022b', 'rel': 'self', 'type': 'application/geo+json'}, {'href': 'http://stapiserver/orders/dc2d9027-a670-475b-878c-a5a6e8ab022b/statuses', 'rel': 'monitor', 'type': 'application/json'}, {'href': 'http://stapiserver/orders/dc2d9027-a670-475b-878c-a5a6e8ab022b', 'rel': 'self', 'type': 'application/json'}]}
155159
for order in create_order_payloads:
156160
res = stapi_client.post(
157161
f"products/{product_id}/orders",
158162
json=order.model_dump(),
159163
)
164+
body = res.json()
165+
orders.append(body)
160166

161167
assert res.status_code == status.HTTP_201_CREATED, res.text
162168
assert res.headers["Content-Type"] == "application/geo+json"
163169

170+
return orders
171+
172+
173+
def test_order_pagination(
174+
setup_orders_pagination, create_order_payloads, stapi_client: TestClient
175+
) -> None:
176+
expected_returns = []
177+
for order in setup_orders_pagination:
178+
json_link = copy.deepcopy(order["links"][0])
179+
json_link["type"] = "application/json"
180+
order["links"].append(json_link)
181+
expected_returns.append(order)
164182

165-
def test_order_pagination(setup_orders_pagination, stapi_client: TestClient) -> None:
166183
pagination_tester(
167184
stapi_client=stapi_client,
168185
endpoint="/orders",
169186
method="GET",
170187
limit=2,
171188
target="features",
172-
expected_total_returns=3,
189+
expected_returns=expected_returns,
173190
)
174191

175192

@@ -213,14 +230,15 @@ def test_order_status_pagination(
213230
order_db._statuses = order_statuses
214231

215232
order_id = "test_order_id"
233+
expected_returns = [x.model_dump(mode="json") for x in order_statuses[order_id]]
216234

217235
pagination_tester(
218236
stapi_client=stapi_client,
219237
endpoint=f"/orders/{order_id}/statuses",
220238
method="GET",
221239
limit=2,
222240
target="statuses",
223-
expected_total_returns=3,
241+
expected_returns=expected_returns,
224242
)
225243

226244

tests/test_product.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,49 @@ def test_product_order_parameters_response(
6767
assert "s3_path" in json_schema["properties"]
6868

6969

70-
def test_product_pagination(stapi_client: TestClient):
70+
def test_product_pagination(
71+
stapi_client: TestClient, mock_product_test_spotlight, mock_product_test_wolf_cola
72+
):
73+
expected_returns = []
74+
for product in [mock_product_test_spotlight, mock_product_test_wolf_cola]:
75+
prod = product.model_dump(mode="json", by_alias=True)
76+
product_id = prod["id"]
77+
prod["links"] = [
78+
{
79+
"href": f"http://stapiserver/products/{product_id}",
80+
"rel": "self",
81+
"type": "application/json",
82+
},
83+
{
84+
"href": f"http://stapiserver/products/{product_id}/constraints",
85+
"rel": "constraints",
86+
"type": "application/json",
87+
},
88+
{
89+
"href": f"http://stapiserver/products/{product_id}/order-parameters",
90+
"rel": "order-parameters",
91+
"type": "application/json",
92+
},
93+
{
94+
"href": f"http://stapiserver/products/{product_id}/opportunities",
95+
"rel": "opportunities",
96+
"type": "application/json",
97+
},
98+
{
99+
"href": f"http://stapiserver/products/{product_id}/orders",
100+
"rel": "create-order",
101+
"type": "application/json",
102+
},
103+
]
104+
expected_returns.append(prod)
105+
71106
pagination_tester(
72107
stapi_client=stapi_client,
73108
endpoint="/products",
74109
method="GET",
75110
limit=1,
76111
target="products",
77-
expected_total_returns=2,
112+
expected_returns=expected_returns,
78113
)
79114

80115

0 commit comments

Comments
 (0)