-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_api_robustness.py
More file actions
100 lines (90 loc) · 3.41 KB
/
test_api_robustness.py
File metadata and controls
100 lines (90 loc) · 3.41 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
#!/usr/bin/env python3
"""
Test script for API robustness verification
Tests all endpoints with valid and edge case scenarios
"""
import requests
import json
import time
def test_api_endpoints():
base_url = "http://localhost:8000"
# Test coordinates from your original example
test_coordinates = [
[77.2090, 28.6139], # Delhi area
[77.2100, 28.6149],
[77.2110, 28.6159],
[77.2120, 28.6169]
]
print("🧪 Testing API Robustness...")
# Test 1: Health Check
try:
response = requests.get(f"{base_url}/")
print(f"✅ Health check: {response.status_code} - {response.json()}")
except Exception as e:
print(f"❌ Health check failed: {e}")
# Test 2: Predict endpoint
try:
predict_payload = {
"coordinates": test_coordinates,
"t1": 2000,
"t2": 3000
}
response = requests.post(f"{base_url}/predict", json=predict_payload, timeout=30)
print(f"✅ Predict endpoint: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f" Predicted yield: {data.get('predicted_yield', 'N/A')}")
else:
print(f" Response: {response.text}")
except Exception as e:
print(f"❌ Predict endpoint failed: {e}")
# Test 3: Generate heatmap endpoint
try:
heatmap_payload = {
"coordinates": test_coordinates,
"t1": 2000,
"t2": 3000
}
response = requests.post(f"{base_url}/generate_heatmap", json=heatmap_payload, timeout=60)
print(f"✅ Heatmap endpoint: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f" Predicted yield: {data.get('predicted_yield', 'N/A')}")
print(f" Has heatmap data: {'heatmap_data' in data}")
print(f" Suggestions count: {len(data.get('suggestions', []))}")
else:
print(f" Response: {response.text}")
except Exception as e:
print(f"❌ Heatmap endpoint failed: {e}")
# Test 4: Edge case - Empty coordinates
try:
edge_payload = {
"coordinates": [],
"t1": 2000,
"t2": 3000
}
response = requests.post(f"{base_url}/predict", json=edge_payload, timeout=30)
print(f"✅ Edge case (empty coords): {response.status_code}")
except Exception as e:
print(f"❌ Edge case test failed: {e}")
# Test 5: Edge case - Invalid coordinates
try:
edge_payload = {
"coordinates": [[999, 999]],
"t1": 2000,
"t2": 3000
}
response = requests.post(f"{base_url}/predict", json=edge_payload, timeout=30)
print(f"✅ Edge case (invalid coords): {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f" Fallback yield: {data.get('predicted_yield', 'N/A')}")
except Exception as e:
print(f"❌ Invalid coords test failed: {e}")
if __name__ == "__main__":
print("🚀 Starting API robustness tests...")
print("Make sure the server is running on localhost:8000")
print("-" * 50)
test_api_endpoints()
print("-" * 50)
print("✨ API robustness testing completed!")