-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_api.py
More file actions
182 lines (156 loc) · 5.45 KB
/
test_api.py
File metadata and controls
182 lines (156 loc) · 5.45 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
#!/usr/bin/env python3
"""
Comprehensive test suite for Exoplanet Classifier API
Tests all endpoints and functionality
"""
import requests
import json
import time
import sys
# Test configuration
BASE_URL = "http://localhost:8000" # Change to your deployed URL for production testing
API_URL = f"{BASE_URL}/api"
def test_endpoint(endpoint, method="GET", data=None, expected_status=200):
"""Test a single endpoint"""
url = f"{API_URL}{endpoint}"
print(f"Testing {method} {endpoint}...")
try:
if method == "GET":
response = requests.get(url, timeout=30)
elif method == "POST":
response = requests.post(url, json=data, timeout=30)
if response.status_code == expected_status:
print(f"✅ {endpoint} - Status: {response.status_code}")
return True, response.json() if response.content else None
else:
print(f"❌ {endpoint} - Status: {response.status_code}, Error: {response.text}")
return False, None
except Exception as e:
print(f"❌ {endpoint} - Exception: {str(e)}")
return False, None
def test_all_features():
"""Test all API endpoints"""
print("🚀 Testing Exoplanet Classifier API")
print("=" * 50)
tests = [
# Basic endpoints
("/", "GET"),
("/health", "GET"),
("/features", "GET"),
# Prediction endpoints
("/predict", "POST", {
"features": {
"koi_dikco_msky": 0.1,
"koi_dicco_msky": 0.2,
"koi_max_mult_ev": 0.3,
"koi_model_snr": 0.4,
"koi_dikco_mra": 0.5,
"koi_fwm_srao": 0.6,
"koi_fwm_sdeco": 0.7,
"koi_fwm_sra_err": 0.8,
"koi_fwm_sdec_err": 0.9,
"koi_fwm_srao_err": 1.0,
"koi_period": 10.5,
"koi_depth": 1000.0,
"koi_duration": 2.5,
"koi_prad": 1.2,
"koi_impact": 0.3,
"koi_steff": 5500.0,
"koi_srad": 1.0,
"koi_slogg": 4.4,
"koi_kepmag": 12.0
}
}),
# Metrics and analysis
("/metrics", "GET"),
("/feature-correlations", "GET"),
("/models", "GET"),
# Dataset endpoints
("/datasets/koi", "GET"),
("/datasets/koi/columns", "GET"),
("/random-example/koi", "GET"),
# Batch prediction
("/batch-predict", "POST", {
"records": [
{
"koi_dikco_msky": 0.1,
"koi_dicco_msky": 0.2,
"koi_max_mult_ev": 0.3,
"koi_model_snr": 0.4,
"koi_dikco_mra": 0.5,
"koi_fwm_srao": 0.6,
"koi_fwm_sdeco": 0.7,
"koi_fwm_sra_err": 0.8,
"koi_fwm_sdec_err": 0.9,
"koi_fwm_srao_err": 1.0,
"koi_period": 10.5,
"koi_depth": 1000.0,
"koi_duration": 2.5,
"koi_prad": 1.2,
"koi_impact": 0.3,
"koi_steff": 5500.0,
"koi_srad": 1.0,
"koi_slogg": 4.4,
"koi_kepmag": 12.0
}
]
})
]
passed = 0
total = len(tests)
for test in tests:
if len(test) == 2:
endpoint, method = test
data = None
else:
endpoint, method, data = test
success, response = test_endpoint(endpoint, method, data)
if success:
passed += 1
time.sleep(0.5) # Small delay between requests
print("=" * 50)
print(f"📊 Test Results: {passed}/{total} tests passed")
if passed == total:
print("🎉 All tests passed! Your API is working perfectly.")
return True
else:
print("⚠️ Some tests failed. Check the errors above.")
return False
def test_performance():
"""Test performance of key endpoints"""
print("\n⚡ Performance Testing")
print("=" * 30)
performance_tests = [
("/features", "GET"),
("/metrics", "GET"),
("/feature-correlations", "GET"),
("/models", "GET")
]
for endpoint, method in performance_tests:
start_time = time.time()
success, _ = test_endpoint(endpoint, method)
end_time = time.time()
if success:
duration = end_time - start_time
print(f"⏱️ {endpoint}: {duration:.2f}s")
if duration > 10:
print(f"⚠️ {endpoint} is slow ({duration:.2f}s)")
elif duration > 5:
print(f"⚡ {endpoint} is moderate ({duration:.2f}s)")
else:
print(f"🚀 {endpoint} is fast ({duration:.2f}s)")
if __name__ == "__main__":
print("Exoplanet Classifier API Test Suite")
print("Make sure your API is running on", BASE_URL)
print()
# Test all functionality
all_passed = test_all_features()
# Test performance
test_performance()
print("\n" + "=" * 50)
if all_passed:
print("✅ All tests passed! Your API is ready for production.")
sys.exit(0)
else:
print("❌ Some tests failed. Please fix the issues above.")
sys.exit(1)