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

Commit cc8fd62

Browse files
feat: ensure that paginated endpoints can handle limit=0 and return empty set tests: update tests to ensure limit=0 is properly handled
1 parent adf1f5f commit cc8fd62

File tree

5 files changed

+66
-53
lines changed

5 files changed

+66
-53
lines changed

src/stapi_fastapi/routers/root_router.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def get_products(
160160
type=TYPE_JSON,
161161
),
162162
]
163-
if end < len(product_ids):
163+
if end < len(product_ids) and end != 0:
164164
links.append(
165165
Link(
166166
href=str(

tests/application.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ async def get_orders(
6060
ids = order_ids[start:end]
6161
orders = [self._orders_db._orders[order_id] for order_id in ids]
6262

63-
if end < len(order_ids):
63+
if end < len(order_ids) and end != 0:
6464
return Success((orders, self._orders_db._orders[order_ids[end]].id))
6565
return Success((orders, ""))
6666
except Exception as e:
@@ -87,7 +87,7 @@ async def get_order_statuses(
8787
end = min(start + limit, len(statuses))
8888
stati = statuses[start:end]
8989

90-
if end < len(statuses):
90+
if end < len(statuses) and end != 0:
9191
return Success((stati, str(end)))
9292
return Success((stati, ""))
9393
except Exception as e:
@@ -119,7 +119,7 @@ async def search_opportunities(
119119
o.model_copy(update=search.model_dump())
120120
for o in self._opportunities[start:end]
121121
]
122-
if end < len(self._opportunities):
122+
if end < len(self._opportunities) and end != 0:
123123
return Success((opportunities, str(end)))
124124
return Success((opportunities, ""))
125125
except Exception as e:

tests/test_opportunity.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,20 @@ def test_search_opportunities_response(
9696
assert_link(f"POST {url}", body, "create-order", f"/products/{product_id}/orders")
9797

9898

99-
@pytest.mark.parametrize("product_id", ["test-spotlight"])
99+
@pytest.mark.parametrize("limit", [0, 2, 4])
100100
def test_search_opportunities_pagination(
101-
product_id: str,
101+
limit: int,
102102
stapi_client: TestClient,
103103
product_backend: MockProductBackend,
104104
mock_test_pagination_opportunities: List[Opportunity],
105105
) -> None:
106+
product_id = "test-spotlight"
106107
product_backend._opportunities = mock_test_pagination_opportunities
107-
expected_returns = [
108-
x.model_dump(mode="json") for x in mock_test_pagination_opportunities
109-
]
108+
expected_returns = []
109+
if limit != 0:
110+
expected_returns = [
111+
x.model_dump(mode="json") for x in mock_test_pagination_opportunities
112+
]
110113

111114
now = datetime.now(UTC)
112115
start = now
@@ -134,7 +137,7 @@ def test_search_opportunities_pagination(
134137
stapi_client=stapi_client,
135138
endpoint=f"/products/{product_id}/opportunities",
136139
method="POST",
137-
limit=2,
140+
limit=limit,
138141
target="features",
139142
expected_returns=expected_returns,
140143
body=request_payload,

tests/test_order.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,21 +170,23 @@ def setup_orders_pagination(
170170
return orders
171171

172172

173+
@pytest.mark.parametrize("limit", [0, 2, 4])
173174
def test_order_pagination(
174-
setup_orders_pagination, create_order_payloads, stapi_client: TestClient
175+
limit, setup_orders_pagination, create_order_payloads, stapi_client: TestClient
175176
) -> None:
176177
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)
178+
if limit != 0:
179+
for order in setup_orders_pagination:
180+
json_link = copy.deepcopy(order["links"][0])
181+
json_link["type"] = "application/json"
182+
order["links"].append(json_link)
183+
expected_returns.append(order)
182184

183185
pagination_tester(
184186
stapi_client=stapi_client,
185187
endpoint="/orders",
186188
method="GET",
187-
limit=2,
189+
limit=limit,
188190
target="features",
189191
expected_returns=expected_returns,
190192
)
@@ -221,22 +223,25 @@ def order_statuses() -> dict[str, list[OrderStatus]]:
221223
return statuses
222224

223225

226+
@pytest.mark.parametrize("limit", [0, 2, 4])
224227
def test_order_status_pagination(
228+
limit: int,
225229
stapi_client: TestClient,
226230
order_db: InMemoryOrderDB,
227231
order_statuses: dict[str, list[OrderStatus]],
228-
limit: int = 2,
229232
) -> None:
230233
order_db._statuses = order_statuses
231234

232235
order_id = "test_order_id"
233-
expected_returns = [x.model_dump(mode="json") for x in order_statuses[order_id]]
236+
expected_returns = []
237+
if limit != 0:
238+
expected_returns = [x.model_dump(mode="json") for x in order_statuses[order_id]]
234239

235240
pagination_tester(
236241
stapi_client=stapi_client,
237242
endpoint=f"/orders/{order_id}/statuses",
238243
method="GET",
239-
limit=2,
244+
limit=limit,
240245
target="statuses",
241246
expected_returns=expected_returns,
242247
)

tests/test_product.py

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -67,47 +67,52 @@ def test_product_order_parameters_response(
6767
assert "s3_path" in json_schema["properties"]
6868

6969

70+
@pytest.mark.parametrize("limit", [0, 1, 2, 4])
7071
def test_product_pagination(
71-
stapi_client: TestClient, mock_product_test_spotlight, mock_product_test_wolf_cola
72+
limit: int,
73+
stapi_client: TestClient,
74+
mock_product_test_spotlight,
75+
mock_product_test_wolf_cola,
7276
):
7377
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)
78+
if limit != 0:
79+
for product in [mock_product_test_spotlight, mock_product_test_wolf_cola]:
80+
prod = product.model_dump(mode="json", by_alias=True)
81+
product_id = prod["id"]
82+
prod["links"] = [
83+
{
84+
"href": f"http://stapiserver/products/{product_id}",
85+
"rel": "self",
86+
"type": "application/json",
87+
},
88+
{
89+
"href": f"http://stapiserver/products/{product_id}/constraints",
90+
"rel": "constraints",
91+
"type": "application/json",
92+
},
93+
{
94+
"href": f"http://stapiserver/products/{product_id}/order-parameters",
95+
"rel": "order-parameters",
96+
"type": "application/json",
97+
},
98+
{
99+
"href": f"http://stapiserver/products/{product_id}/opportunities",
100+
"rel": "opportunities",
101+
"type": "application/json",
102+
},
103+
{
104+
"href": f"http://stapiserver/products/{product_id}/orders",
105+
"rel": "create-order",
106+
"type": "application/json",
107+
},
108+
]
109+
expected_returns.append(prod)
105110

106111
pagination_tester(
107112
stapi_client=stapi_client,
108113
endpoint="/products",
109114
method="GET",
110-
limit=1,
115+
limit=limit,
111116
target="products",
112117
expected_returns=expected_returns,
113118
)

0 commit comments

Comments
 (0)