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

Commit 5db53c1

Browse files
tests: reworked pagination test to iterate using next tokens to keep getting records until no more are available ftests: added separate test to get 404 back with no orders and using a token
1 parent 7b3453e commit 5db53c1

File tree

2 files changed

+31
-80
lines changed

2 files changed

+31
-80
lines changed

tests/application.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ async def get_orders(
5555
limit = 100
5656

5757
order_ids = [*self._orders_db._orders.keys()]
58-
if not order_ids: # no data in db
58+
if not order_ids and not next: # no data in db
5959
return Success(
6060
(
6161
OrderCollection(

tests/test_order.py

Lines changed: 30 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -197,102 +197,53 @@ def create_order_payloads() -> list[OrderPayload]:
197197
@pytest.fixture
198198
def prepare_order_pagination(
199199
stapi_client: TestClient, create_order_payloads: list[OrderPayload]
200-
) -> tuple[str, str, str]:
201-
# product_backend._allowed_payloads = create_order_payloads
200+
) -> None:
202201
product_id = "test-spotlight"
203202

204-
# # check empty
205-
# res = stapi_client.get("/orders")
206-
# default_orders = {"type": "FeatureCollection", "features": [], "links": []}
207-
# assert res.status_code == status.HTTP_200_OK
208-
# assert res.headers["Content-Type"] == "application/geo+json"
209-
# assert res.json() == default_orders
203+
# check empty
204+
res = stapi_client.get("/orders")
205+
default_orders = {"type": "FeatureCollection", "features": [], "links": []}
206+
assert res.status_code == status.HTTP_200_OK
207+
assert res.headers["Content-Type"] == "application/geo+json"
208+
assert res.json() == default_orders
210209

211210
# get uuids created to use as pagination tokens
212-
order_ids = []
213211
for payload in create_order_payloads:
214212
res = stapi_client.post(
215213
f"products/{product_id}/orders",
216214
json=payload.model_dump(),
217215
)
218216
assert res.status_code == status.HTTP_201_CREATED, res.text
219217
assert res.headers["Content-Type"] == "application/geo+json"
220-
order_ids.append(res.json()["id"])
221-
222-
# res = stapi_client.get("/orders")
223-
# checker = res.json()
224-
# assert len(checker['features']) == 3
225218

226-
return tuple(order_ids)
219+
res = stapi_client.get("/orders")
220+
checker = res.json()
221+
assert len(checker["features"]) == 3
227222

228223

229-
@pytest.mark.parametrize(
230-
"product_id,expected_status,limit,id_retrieval,token_back",
231-
[
232-
pytest.param(
233-
"test-spotlight",
234-
status.HTTP_200_OK,
235-
1,
236-
0,
237-
True,
238-
id="input frst order_id token get new token back",
239-
),
240-
pytest.param(
241-
"test-spotlight",
242-
status.HTTP_200_OK,
243-
1,
244-
2,
245-
False,
246-
id="input last order_id token get NO token back",
247-
),
248-
pytest.param(
249-
"test-spotlight",
250-
status.HTTP_404_NOT_FOUND,
251-
1,
252-
"BAD_TOKEN",
253-
False,
254-
id="input bad token get 404 back",
255-
),
256-
pytest.param(
257-
"test-spotlight",
258-
status.HTTP_200_OK,
259-
1,
260-
1000000,
261-
False,
262-
id="high limit handled and returns valid records",
263-
),
264-
],
265-
)
266224
def test_order_pagination(
267225
prepare_order_pagination,
268226
stapi_client: TestClient,
269-
product_id: str,
270-
expected_status: int,
271-
limit: int,
272-
id_retrieval: int | str,
273-
token_back: bool,
274227
) -> None:
275-
order_ids = prepare_order_pagination
276-
277-
res = stapi_client.get(
278-
"/orders", params={"next": order_ids[id_retrieval], "limit": limit}
279-
)
280-
assert res.status_code == expected_status
228+
# prepare_order_pagination
281229

230+
res = stapi_client.get("/orders", params={"next": None, "limit": 1})
231+
assert res.status_code == status.HTTP_200_OK
282232
body = res.json()
283-
for link in body["features"][0]["links"]:
284-
assert link["rel"] != "next"
285-
assert body["links"] != []
286-
287-
# check to make sure new token in link
288-
if token_back:
289-
assert order_ids[id_retrieval] not in body["links"][0]["href"]
290-
291-
assert len(body["features"]) == limit
292-
293-
294-
# test cases to check
295-
# 1. Input token and get last record. Should not return a token if we are returning the last record - 'last' record being what is sorted
296-
# 2. Input a crzy high limit - how to handle? Default to max or all records if less than max
297-
# 3. Input token and get some intermediate records - return a token for next records
298-
# 4. handle requesting an orderid/token that does't exist and returns 400/404. Bad token --> bad request.
233+
next = body["links"][0]["href"]
234+
235+
while next:
236+
res = stapi_client.get(next)
237+
assert res.status_code == status.HTTP_200_OK
238+
body = res.json()
239+
if body["links"]:
240+
next = body["links"][0]["href"]
241+
else:
242+
break
243+
244+
245+
# separate test here to check for bad token getting back 404
246+
def test_token_not_found(stapi_client: TestClient):
247+
res = stapi_client.get("/orders", params={"next": "a_token"})
248+
# should return 404 as a result of bad token
249+
assert res.status_code == status.HTTP_404_NOT_FOUND

0 commit comments

Comments
 (0)