-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_customer_order_workflow.py
More file actions
166 lines (140 loc) · 5.44 KB
/
Copy pathtest_customer_order_workflow.py
File metadata and controls
166 lines (140 loc) · 5.44 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
#!/usr/bin/env python3
"""
Test the customer-order workflow
"""
import requests
import json
from decimal import Decimal
BASE_URL = "http://127.0.0.1:8000"
TENANT_ID = "test_tenant_001"
def test_customer_order_workflow():
"""Test the complete customer and order workflow"""
print("Testing Customer-Order Workflow")
print("=" * 40)
# First ensure tenant exists
try:
requests.post(f"{BASE_URL}/internal/database",
params={"environment": "development"},
json={"databaseId": TENANT_ID})
except:
pass
# Step 1: Create a customer
print("\n1. Creating customer...")
customer_data = {
"first_name": "John",
"last_name": "Doe",
"email": f"john.doe.{int(__import__('time').time())}@example.com",
"phone": "+1-555-0123",
"address_line1": "123 Main St",
"city": "New York",
"state": "NY",
"postal_code": "10001",
"country": "USA",
"accepts_marketing": True,
"notes": "VIP customer"
}
response = requests.post(f"{BASE_URL}/tenant/{TENANT_ID}/customers", json=customer_data)
print(f"Status: {response.status_code}")
if response.status_code != 201:
print(f"Failed to create customer: {response.text}")
return False
customer = response.json()
customer_id = customer["id"]
print(f"✓ Created customer: {customer['first_name']} {customer['last_name']} (ID: {customer_id})")
# Step 2: Create a product for the order
print("\n2. Creating product...")
product_data = {
"name": f"Test Product {int(__import__('time').time())}",
"slug": f"test-product-{int(__import__('time').time())}",
"description": "A test product for order testing",
"sku": f"TEST-PROD-{int(__import__('time').time())}",
"price": 25.99,
"inventory_quantity": 100,
"status": "active"
}
# Create multipart form data
files = {
'product_data': (None, json.dumps(product_data), 'application/json'),
}
response = requests.post(f"{BASE_URL}/tenant/{TENANT_ID}/products", files=files)
print(f"Status: {response.status_code}")
if response.status_code != 201:
print(f"Failed to create product: {response.text}")
return False
product = response.json()
product_id = product["id"]
print(f"✓ Created product: {product['name']} (ID: {product_id})")
# Step 3: Create an order
print("\n3. Creating order...")
order_data = {
"customer_id": customer_id,
"order_number": f"ORD-{int(__import__('time').time())}",
"status": "pending",
"payment_status": "pending",
"fulfillment_status": "unfulfilled",
"notes": "Test order from automated test",
"items": [
{
"product_id": product_id,
"quantity": 2,
"unit_price": 25.99
}
]
}
response = requests.post(f"{BASE_URL}/tenant/{TENANT_ID}/orders", json=order_data)
print(f"Status: {response.status_code}")
if response.status_code != 201:
print(f"Failed to create order: {response.text}")
return False
order = response.json()
order_id = order["id"]
print(f"✓ Created order: {order['order_number']} (ID: {order_id})")
print(f" Customer ID: {order['customer_id']}")
print(f" Total Amount: ${float(order['total_amount']):.2f}")
# Step 4: Verify customer can be retrieved
print("\n4. Verifying customer...")
response = requests.get(f"{BASE_URL}/tenant/{TENANT_ID}/customers/{customer_id}")
if response.status_code == 200:
retrieved_customer = response.json()
print(f"✓ Retrieved customer: {retrieved_customer['first_name']} {retrieved_customer['last_name']}")
else:
print(f"✗ Failed to retrieve customer: {response.text}")
return False
# Step 5: Verify order can be retrieved
print("\n5. Verifying order...")
response = requests.get(f"{BASE_URL}/tenant/{TENANT_ID}/orders/{order_id}")
if response.status_code == 200:
retrieved_order = response.json()
print(f"✓ Retrieved order: {retrieved_order['order_number']}")
print(f" Status: {retrieved_order['status']}")
print(f" Customer ID: {retrieved_order['customer_id']}")
else:
print(f"✗ Failed to retrieve order: {response.text}")
return False
# Step 6: List customers
print("\n6. Testing customer list...")
response = requests.get(f"{BASE_URL}/tenant/{TENANT_ID}/customers")
if response.status_code == 200:
customers_response = response.json()
customers_list = customers_response.get("data", [])
total = customers_response.get("meta", {}).get("total", 0)
print(f"✓ Listed {len(customers_list)} customers (total: {total})")
else:
print(f"✗ Failed to list customers: {response.text}")
return False
# Step 7: List orders
print("\n7. Testing orders list...")
response = requests.get(f"{BASE_URL}/tenant/{TENANT_ID}/orders")
if response.status_code == 200:
orders_list = response.json()
print(f"✓ Listed {len(orders_list)} orders")
else:
print(f"✗ Failed to list orders: {response.text}")
return False
print(f"\n{'='*40}")
print("✅ Customer-Order workflow test PASSED!")
print(f"{'='*40}")
return True
if __name__ == "__main__":
success = test_customer_order_workflow()
exit(0 if success else 1)