-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
158 lines (139 loc) Β· 5.18 KB
/
Copy pathtest_api.py
File metadata and controls
158 lines (139 loc) Β· 5.18 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
import requests
import json
# API base URL
BASE_URL = "http://localhost:5000"
def test_division_api():
"""Test the division API with various scenarios"""
print("π§ͺ Testing Division API")
print("=" * 50)
# Test cases
test_cases = [
{
"name": "Basic division (10 / 2)",
"data": {"numerator": 10, "denominator": 2},
"expected_result": 5.0
},
{
"name": "Decimal division (15.5 / 3)",
"data": {"numerator": 15.5, "denominator": 3},
"expected_result": 15.5 / 3
},
{
"name": "Negative numbers (-20 / 4)",
"data": {"numerator": -20, "denominator": 4},
"expected_result": -5.0
},
{
"name": "Zero numerator (0 / 5)",
"data": {"numerator": 0, "denominator": 5},
"expected_result": 0.0
}
]
# Test successful cases
for test_case in test_cases:
print(f"\nπ Testing: {test_case['name']}")
try:
response = requests.post(
f"{BASE_URL}/divide",
json=test_case['data'],
headers={'Content-Type': 'application/json'}
)
if response.status_code == 200:
result = response.json()
print(f"β
Success: {result['numerator']} / {result['denominator']} = {result['result']}")
if abs(result['result'] - test_case['expected_result']) < 0.001:
print(" β Result matches expected value")
else:
print(f" β οΈ Expected: {test_case['expected_result']}, Got: {result['result']}")
else:
print(f"β Error: {response.status_code} - {response.text}")
except requests.exceptions.ConnectionError:
print("β Connection Error: Make sure the server is running on http://localhost:5000")
return
except Exception as e:
print(f"β Unexpected error: {str(e)}")
# Test error cases
print("\n" + "=" * 50)
print("π¨ Testing Error Cases")
print("=" * 50)
error_cases = [
{
"name": "Division by zero",
"data": {"numerator": 10, "denominator": 0}
},
{
"name": "Missing denominator",
"data": {"numerator": 10}
},
{
"name": "Invalid input (string)",
"data": {"numerator": "abc", "denominator": 2}
},
{
"name": "Empty request body",
"data": {}
}
]
for error_case in error_cases:
print(f"\nπ Testing: {error_case['name']}")
try:
response = requests.post(
f"{BASE_URL}/divide",
json=error_case['data'],
headers={'Content-Type': 'application/json'}
)
if response.status_code >= 400:
result = response.json()
print(f"β
Expected error: {response.status_code} - {result.get('error', 'Unknown error')}")
else:
print(f"β Unexpected success: {response.status_code}")
except requests.exceptions.ConnectionError:
print("β Connection Error: Make sure the server is running")
return
except Exception as e:
print(f"β Unexpected error: {str(e)}")
def test_health_endpoint():
"""Test the health check endpoint"""
print("\n" + "=" * 50)
print("π₯ Testing Health Endpoint")
print("=" * 50)
try:
response = requests.get(f"{BASE_URL}/health")
if response.status_code == 200:
result = response.json()
print(f"β
Health check: {result['status']} - {result['message']}")
else:
print(f"β Health check failed: {response.status_code}")
except requests.exceptions.ConnectionError:
print("β Connection Error: Make sure the server is running")
except Exception as e:
print(f"β Unexpected error: {str(e)}")
def test_home_endpoint():
"""Test the home endpoint"""
print("\n" + "=" * 50)
print("π Testing Home Endpoint")
print("=" * 50)
try:
response = requests.get(f"{BASE_URL}/")
if response.status_code == 200:
result = response.json()
print(f"β
Home endpoint: {result['message']}")
print("Available endpoints:")
for endpoint, description in result['endpoints'].items():
print(f" - {endpoint}: {description}")
else:
print(f"β Home endpoint failed: {response.status_code}")
except requests.exceptions.ConnectionError:
print("β Connection Error: Make sure the server is running")
except Exception as e:
print(f"β Unexpected error: {str(e)}")
if __name__ == "__main__":
print("π Division API Test Suite")
print("Make sure the server is running with: python app.py")
print("=" * 50)
test_health_endpoint()
test_home_endpoint()
test_division_api()
print("\n" + "=" * 50)
print("β¨ Test suite completed!")
print("=" * 50)