Skip to content

Commit 56a3196

Browse files
committed
Add tests for /orders endpoint
Add tests/tests_orders.py with pytest tests using FastAPI TestClient to exercise the /orders endpoint. Tests validate response structure (meta, pagination, data, search), ensure data is a list and contains expected item keys when present, assert meta fields (severity and title), and verify handling of the s search parameter. This helps catch regressions in paginated order reads and search behavior.
1 parent bb97bd5 commit 56a3196

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

tests/test_orders.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import pytest
2+
from fastapi.testclient import TestClient
3+
from app.main import app
4+
5+
client = TestClient(app)
6+
7+
8+
def test_get_orders_root():
9+
response = client.get("/orders")
10+
assert response.status_code == 200
11+
data = response.json()
12+
assert "meta" in data
13+
assert "pagination" in data
14+
assert "data" in data
15+
assert "search" in data
16+
assert isinstance(data["data"], list)
17+
# Check that the expected keys are present in the data list (if not empty)
18+
if data["data"]:
19+
first_item = data["data"][0]
20+
# Accept any unique identifier, e.g., 'sku' or 'name' or 'order_id'
21+
assert (
22+
"sku" in first_item or
23+
"name" in first_item or
24+
"order_id" in first_item
25+
)
26+
assert "name" in first_item or "description" in first_item or "categories" in first_item
27+
# Meta checks
28+
meta = data["meta"]
29+
assert meta["severity"] == "success"
30+
assert meta["title"] == "Read paginated orders"
31+
32+
def test_orders_search_param():
33+
search_term = "test"
34+
response = client.get(f"/orders?s={search_term}")
35+
assert response.status_code == 200
36+
data = response.json()
37+
assert "search" in data
38+
# Accept both string and dict for search key for compatibility
39+
if isinstance(data["search"], dict):
40+
assert data["search"].get("searchStr") == search_term
41+
else:
42+
assert data["search"] == search_term
43+
44+
def test_orders_returns_list():
45+
response = client.get("/orders")
46+
assert response.status_code == 200
47+
data = response.json()
48+
assert "meta" in data
49+
assert "pagination" in data
50+
assert "data" in data
51+
assert isinstance(data["data"], list)

0 commit comments

Comments
 (0)