-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_spend_endpoint.py
More file actions
108 lines (94 loc) · 3.27 KB
/
test_spend_endpoint.py
File metadata and controls
108 lines (94 loc) · 3.27 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
import requests
BASE_URL = "http://localhost:8000"
def add_sample_transactions():
"""
Adds sample transactions to the system.
"""
print("Adding sample transactions...")
sample_transactions = [
{"payer": "DANNON", "points": 300, "timestamp": "2022-10-31T10:00:00Z"},
{"payer": "UNILEVER", "points": 200, "timestamp": "2022-10-31T11:00:00Z"},
{"payer": "MILLER COORS", "points": 10000, "timestamp": "2022-11-01T14:00:00Z"},
{"payer": "DANNON", "points": 1000, "timestamp": "2022-11-02T14:00:00Z"},
]
for transaction in sample_transactions:
response = requests.post(f"{BASE_URL}/add", json=transaction)
if response.status_code != 200:
print(f"Failed to add transaction: {transaction}")
print(f"Response: {response.text}")
return False
print("Sample transactions added successfully!")
return True
def test_spend_points():
"""
Test the /spend endpoint with various scenarios.
"""
print("\nTesting /spend Endpoint\n")
# Add sample transactions
if not add_sample_transactions():
return
# Define test cases
test_cases = [
{
"description": "Spend 5000 points",
"data": {"points": 5000},
"expected_status": 200,
},
{
"description": "Spend more points than available (20000 points)",
"data": {"points": 20000},
"expected_status": 400,
},
{
"description": "Spend a small valid amount (100 points)",
"data": {"points": 100},
"expected_status": 200,
},
{
"description": "Spend all available points (10500 points)",
"data": {"points": 10500},
"expected_status": 200,
},
{
"description": "Spend zero points",
"data": {"points": 0},
"expected_status": 200,
},
{
"description": "Spend negative points (invalid input)",
"data": {"points": -100},
"expected_status": 400,
},
{
"description": "Spend 6300 points",
"data": {"points": 6300},
"expected_status": 200,
},
{
"description": "Spend with missing 'points' field (invalid input)",
"data": {},
"expected_status": 400,
},
{
"description": "Spend 100 points",
"data": {"points": 100},
"expected_status": 200,
},
]
# Run test cases
for idx, test_case in enumerate(test_cases):
print(f"\nTest Case {idx + 1}: {test_case['description']}")
response = requests.post(f"{BASE_URL}/spend", json=test_case["data"])
# Check response status
if response.status_code == test_case["expected_status"]:
print(f"Status: {response.status_code} (Expected)")
else:
print(f"Status: {response.status_code} (Unexpected)")
print(f"Response: {response.text}")
continue
# Print response body for successful cases
if response.status_code == 200:
print("Response:")
print(response.text) # Handle plain text response
if __name__ == "__main__":
test_spend_points()