Repository navigation
Expand file tree
/
Copy pathtest_smoke.py
More file actions
248 lines (205 loc) · 7.83 KB
/
Copy pathtest_smoke.py
File metadata and controls
248 lines (205 loc) · 7.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import pytest
from fastapi.testclient import TestClient
from src.api.server import app
client = TestClient(app)
def test_smoke_flow():
# 1. Add Product
res = client.post("/api/products", json={
"name": "Smoke Item",
"priceCents": 500,
"stock": 10
})
assert res.status_code == 201
product = res.json()
pid = product["id"]
# 2. Add to Cart
res = client.post("/api/cart/items", json={
"userId": "user_dev",
"productId": pid,
"quantity": 2
})
assert res.status_code == 200
cart = res.json()
assert len(cart["items"]) == 1
# 3. Create Order
res = client.post("/api/orders", json={
"userId": "user_dev"
})
assert res.status_code == 201
order = res.json()
assert order["totalCents"] == 1000
assert order["status"] == "PENDING_PAYMENT"
# 4. Verify Stock Deducted
# Note: In real integration test we might check GET /products or GET /products/:id
# Here we just rely on previous steps success
def test_out_of_stock():
user_id = "user_2"
# Add product with stock of 5
res = client.post("/api/products", json={
"name": "Limited Item",
"priceCents": 200,
"stock": 5
})
assert res.status_code == 201
product = res.json()
pid = product["id"]
# Try to add 6 units (exceeds stock of 5)
client.post("/api/cart/items", json={
"userId": user_id,
"productId": pid,
"quantity": 6
})
# Verify 409 is returned
resp = client.post("/api/orders", json={"userId": user_id})
assert resp.status_code == 409
assert "out of stock" in resp.json()["detail"].lower()
def test_cart_query_and_remove():
user_id = "user_cart_crud"
# Add two products
ids = []
for name, price in [("Cart Item A", 100), ("Cart Item B", 200)]:
res = client.post("/api/products", json={"name": name, "priceCents": price, "stock": 10})
assert res.status_code == 201
ids.append(res.json()["id"])
# Query empty cart (lazy-created for a fresh user)
res = client.get("/api/cart/items", params={"userId": user_id})
assert res.status_code == 200
assert res.json()["items"] == []
# Add both products
for pid in ids:
res = client.post("/api/cart/items", json={"userId": user_id, "productId": pid, "quantity": 1})
assert res.status_code == 200
# Query returns both items
res = client.get("/api/cart/items", params={"userId": user_id})
assert res.status_code == 200
cart = res.json()
assert len(cart["items"]) == 2
# Remove one: only the target item is removed
res = client.delete(f"/api/cart/items/{ids[0]}", params={"userId": user_id})
assert res.status_code == 200
cart = res.json()
assert len(cart["items"]) == 1
assert cart["items"][0]["productId"] == ids[1]
# Remove the same item again -> 404
res = client.delete(f"/api/cart/items/{ids[0]}", params={"userId": user_id})
assert res.status_code == 404
assert "Cart item not found" in res.json()["detail"]
def test_order_list_query():
# Add product with enough stock
res = client.post("/api/products", json={"name": "Order List Item", "priceCents": 100, "stock": 10})
assert res.status_code == 201
pid = res.json()["id"]
def order_once(user_id):
r = client.post("/api/cart/items", json={"userId": user_id, "productId": pid, "quantity": 1})
assert r.status_code == 200
r = client.post("/api/orders", json={"userId": user_id})
assert r.status_code == 201
order = r.json()
# userId field is present and aligned with Node implementation
assert order["userId"] == user_id
return order
order_once("user_oa")
order_once("user_oa")
order_once("user_ob")
# user_oa has exactly 2 orders, others not mixed in
res = client.get("/api/orders", params={"userId": "user_oa"})
assert res.status_code == 200
orders = res.json()
assert len(orders) == 2
assert all(o["userId"] == "user_oa" for o in orders)
# User with no orders -> empty array
res = client.get("/api/orders", params={"userId": "nobody"})
assert res.status_code == 200
assert res.json() == []
# Missing userId -> 400 MISSING_USER_ID
res = client.get("/api/orders")
assert res.status_code == 400
assert "Missing userId" in res.json()["detail"]
def test_get_product_by_id():
# Add product
res = client.post("/api/products", json={
"name": "Test Product",
"priceCents": 300,
"stock": 5
})
assert res.status_code == 201
pid = res.json()["id"]
# Query existing product
res = client.get(f"/api/products/{pid}")
assert res.status_code == 200
assert res.json()["name"] == "Test Product"
assert res.json()["priceCents"] == 300
# Query non-existent product
res = client.get("/api/products/non-existent")
assert res.status_code == 404
assert "Product not found" in res.json()["detail"]
def test_search_and_sort():
# Add products
for name, price in [("iPhone 15", 5999), ("iPad Pro", 7999), ("MacBook", 9999)]:
client.post("/api/products", json={"name": name, "priceCents": price, "stock": 5})
# Search by name (case-insensitive)
res = client.get("/api/products", params={"name": "ipad"})
assert res.status_code == 200
assert len(res.json()) == 1
assert res.json()[0]["name"] == "iPad Pro"
# No params -> all products (shared app instance: previous tests also added products)
res = client.get("/api/products")
assert res.status_code == 200
all_products = res.json()
assert len(all_products) >= 3
names = {p["name"] for p in all_products}
assert {"iPhone 15", "iPad Pro", "MacBook"} <= names
# No results
res = client.get("/api/products", params={"name": "nonexistent"})
assert res.status_code == 200
assert res.json() == []
# Sort ascending
res = client.get("/api/products", params={"sort": "price_asc"})
prices = [p["priceCents"] for p in res.json()]
assert prices == sorted(prices)
# Sort descending
res = client.get("/api/products", params={"sort": "price_desc"})
prices = [p["priceCents"] for p in res.json()]
assert prices == sorted(prices, reverse=True)
# Invalid sort value -> natural order, still 200
res = client.get("/api/products", params={"sort": "invalid"})
assert res.status_code == 200
assert len(res.json()) >= 3
# Search + sort combination
res = client.get("/api/products", params={"name": "a", "sort": "price_desc"})
prices = [p["priceCents"] for p in res.json()]
assert prices == sorted(prices, reverse=True)
assert all("a" in p["name"].lower() for p in res.json())
def test_order_get_by_id():
user_id = "user_order_detail"
# Create product -> cart -> order
res = client.post("/api/products", json={
"name": "Order Detail Item",
"priceCents": 300,
"stock": 5
})
assert res.status_code == 201
pid = res.json()["id"]
res = client.post("/api/cart/items", json={
"userId": user_id,
"productId": pid,
"quantity": 2
})
assert res.status_code == 200
res = client.post("/api/orders", json={"userId": user_id})
assert res.status_code == 201
order = res.json()
# Query existing order -> 200 with camelCase fields
res = client.get(f"/api/orders/{order['id']}")
assert res.status_code == 200
fetched = res.json()
assert fetched["id"] == order["id"]
assert fetched["userId"] == user_id
assert fetched["status"] == "PENDING_PAYMENT"
assert fetched["totalCents"] == 600
assert fetched["items"][0]["productId"] == pid
assert fetched["items"][0]["priceCents"] == 300
# Query non-existent order -> 404
res = client.get("/api/orders/order_does_not_exist")
assert res.status_code == 404
assert "Order not found" in res.json()["detail"]