-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_hybrid_forecast.py
More file actions
317 lines (250 loc) · 11 KB
/
test_hybrid_forecast.py
File metadata and controls
317 lines (250 loc) · 11 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
"""
Test script for hybrid forecasting system
Tests both short-term (weather API) and long-term (climate patterns) forecasting
"""
import requests
import json
from datetime import datetime
# API endpoints
BASE_URL = "http://127.0.0.1:8081"
def test_climate_service():
"""Test the climate service directly"""
print("\n" + "="*60)
print("TEST 1: Climate Service Direct Test")
print("="*60)
try:
from src.climate_service import get_climate_service
# Test location: Mumbai, India
lat, lon = 19.076, 72.877
climate_service = get_climate_service()
# Test region detection
print(f"\n📍 Testing location: Mumbai ({lat}, {lon})")
region_info = climate_service.get_region_info(lat, lon)
print(f" Region detected: {region_info}")
# Test 6-month forecast generation
print("\n📅 Generating 6-month forecast...")
forecasts = climate_service.generate_six_month_forecast(lat, lon)
print(f"\n✅ Generated {len(forecasts)} monthly forecasts")
# Display first forecast
if forecasts:
first = forecasts[0]
print(f"\nFirst forecast (Month 1):")
print(f" Month: {first.get('month')}")
print(f" Temperature: {first.get('temperature')}")
print(f" Humidity: {first.get('humidity')}")
print(f" Data Source: {first.get('data_source')}")
print(f" Confidence: {first.get('confidence')}")
print(f" Temperature Chart Days: {len(first.get('temperature_chart', []))}")
print("\n✅ Climate Service Test PASSED")
return True
except Exception as e:
print(f"\n❌ Climate Service Test FAILED: {e}")
import traceback
traceback.print_exc()
return False
def test_short_term_forecast():
"""Test short-term forecast (1-5 days) via API"""
print("\n" + "="*60)
print("TEST 2: Short-Term Forecast API (1-5 days)")
print("="*60)
try:
url = f"{BASE_URL}/forecast/hybrid"
payload = {
"latitude": 40.7128,
"longitude": -74.0060,
"days_ahead": 5,
"forecast_type": "short"
}
print(f"\n📡 Sending request to {url}")
print(f" Payload: {json.dumps(payload, indent=2)}")
response = requests.post(url, json=payload, timeout=30)
if response.status_code == 200:
data = response.json()
print(f"\n✅ Response received (Status: {response.status_code})")
print(f" Forecast Type: {data.get('forecast_type')}")
print(f" Number of forecasts: {len(data.get('forecasts', []))}")
# Display first forecast
if data.get('forecasts'):
first = data['forecasts'][0]
print(f"\n First forecast:")
print(f" Date: {first.get('date')}")
print(f" Temperature: {first.get('temperature')}")
print(f" Data Source: {first.get('data_source')}")
print("\n✅ Short-Term Forecast Test PASSED")
return True
else:
print(f"\n❌ API returned status {response.status_code}")
print(f" Response: {response.text}")
return False
except requests.exceptions.ConnectionError:
print(f"\n⚠️ Cannot connect to API at {BASE_URL}")
print(" Make sure the API server is running:")
print(" > python -m uvicorn src.api:app --host 127.0.0.1 --port 8081")
return False
except Exception as e:
print(f"\n❌ Short-Term Forecast Test FAILED: {e}")
import traceback
traceback.print_exc()
return False
def test_long_term_forecast():
"""Test long-term forecast (6 months) via API"""
print("\n" + "="*60)
print("TEST 3: Long-Term Forecast API (6 months)")
print("="*60)
try:
url = f"{BASE_URL}/forecast/hybrid"
payload = {
"latitude": 19.076,
"longitude": 72.877,
"days_ahead": 180,
"forecast_type": "long"
}
print(f"\n📡 Sending request to {url}")
print(f" Payload: {json.dumps(payload, indent=2)}")
response = requests.post(url, json=payload, timeout=30)
if response.status_code == 200:
data = response.json()
print(f"\n✅ Response received (Status: {response.status_code})")
print(f" Forecast Type: {data.get('forecast_type')}")
print(f" Number of forecasts: {len(data.get('forecasts', []))}")
# Display first two forecasts
forecasts = data.get('forecasts', [])
for i, forecast in enumerate(forecasts[:2]):
print(f"\n Forecast {i+1}:")
print(f" Month: {forecast.get('month')}")
print(f" Temperature: {forecast.get('temperature')}")
print(f" Humidity: {forecast.get('humidity')}")
print(f" Data Source: {forecast.get('data_source')}")
print(f" Confidence: {forecast.get('confidence')}")
chart = forecast.get('temperature_chart', [])
if chart:
print(f" Temp Chart: {len(chart)} daily values, avg={sum(chart)/len(chart):.1f}°C")
print("\n✅ Long-Term Forecast Test PASSED")
return True
else:
print(f"\n❌ API returned status {response.status_code}")
print(f" Response: {response.text}")
return False
except requests.exceptions.ConnectionError:
print(f"\n⚠️ Cannot connect to API at {BASE_URL}")
print(" Make sure the API server is running:")
print(" > python -m uvicorn src.api:app --host 127.0.0.1 --port 8081")
return False
except Exception as e:
print(f"\n❌ Long-Term Forecast Test FAILED: {e}")
import traceback
traceback.print_exc()
return False
def test_auto_forecast():
"""Test auto-detection of forecast type"""
print("\n" + "="*60)
print("TEST 4: Auto Forecast Type Detection")
print("="*60)
try:
url = f"{BASE_URL}/forecast/hybrid"
# Test with 3 days (should use short-term)
payload = {
"latitude": 35.6762,
"longitude": 139.6503,
"days_ahead": 3,
"forecast_type": "auto"
}
print(f"\n📡 Test 4a: Auto with days_ahead=3 (should be 'short')")
response = requests.post(url, json=payload, timeout=30)
if response.status_code == 200:
data = response.json()
forecast_type = data.get('forecast_type')
print(f" ✅ Detected type: {forecast_type}")
if forecast_type == "short":
print(" ✅ Correctly detected as short-term")
else:
print(f" ❌ Expected 'short', got '{forecast_type}'")
# Test with 90 days (should use long-term)
payload['days_ahead'] = 90
print(f"\n📡 Test 4b: Auto with days_ahead=90 (should be 'long')")
response = requests.post(url, json=payload, timeout=30)
if response.status_code == 200:
data = response.json()
forecast_type = data.get('forecast_type')
print(f" ✅ Detected type: {forecast_type}")
if forecast_type == "long":
print(" ✅ Correctly detected as long-term")
else:
print(f" ❌ Expected 'long', got '{forecast_type}'")
print("\n✅ Auto Forecast Detection Test PASSED")
return True
except requests.exceptions.ConnectionError:
print(f"\n⚠️ Cannot connect to API at {BASE_URL}")
return False
except Exception as e:
print(f"\n❌ Auto Forecast Test FAILED: {e}")
return False
def test_climate_summary():
"""Test climate summary endpoint"""
print("\n" + "="*60)
print("TEST 5: Climate Summary Endpoint")
print("="*60)
try:
url = f"{BASE_URL}/climate/summary"
params = {
"latitude": -33.8688,
"longitude": 151.2093
}
print(f"\n📡 Sending request to {url}")
print(f" Location: Sydney, Australia ({params['latitude']}, {params['longitude']})")
response = requests.get(url, params=params, timeout=10)
if response.status_code == 200:
data = response.json()
print(f"\n✅ Response received")
print(f" Region: {data.get('region')}")
print(f" Continent: {data.get('continent_name')}")
print(f" Data Available: {data.get('data_available')}")
cities = data.get('representative_cities', [])
if cities:
print(f"\n Representative cities:")
for city in cities[:3]:
print(f" - {city.get('name')}: {city.get('climate_zone')}")
print("\n✅ Climate Summary Test PASSED")
return True
else:
print(f"\n❌ API returned status {response.status_code}")
return False
except requests.exceptions.ConnectionError:
print(f"\n⚠️ Cannot connect to API at {BASE_URL}")
return False
except Exception as e:
print(f"\n❌ Climate Summary Test FAILED: {e}")
return False
def main():
"""Run all tests"""
print("\n" + "="*60)
print("🧪 HYBRID FORECAST SYSTEM TEST SUITE")
print("="*60)
print(f"\nTesting API at: {BASE_URL}")
print(f"Current time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
results = []
# Test 1: Climate Service (direct, no API needed)
results.append(("Climate Service", test_climate_service()))
# Test 2-5: API tests (requires running server)
results.append(("Short-Term Forecast API", test_short_term_forecast()))
results.append(("Long-Term Forecast API", test_long_term_forecast()))
results.append(("Auto Forecast Detection", test_auto_forecast()))
results.append(("Climate Summary API", test_climate_summary()))
# Summary
print("\n" + "="*60)
print("📊 TEST SUMMARY")
print("="*60)
for test_name, passed in results:
status = "✅ PASSED" if passed else "❌ FAILED"
print(f"{status} - {test_name}")
total = len(results)
passed = sum(1 for _, p in results if p)
print(f"\nTotal: {passed}/{total} tests passed")
if passed == total:
print("\n🎉 All tests passed!")
else:
print(f"\n⚠️ {total - passed} test(s) failed")
print("\nNote: API tests require the server to be running:")
print(" > python -m uvicorn src.api:app --host 127.0.0.1 --port 8081")
if __name__ == "__main__":
main()