Spaces:
No application file
No application file
| #!/usr/bin/env python3 | |
| """ | |
| Test the API endpoints | |
| """ | |
| import requests | |
| import json | |
| import time | |
| import sys | |
| # Wait for server to start | |
| time.sleep(3) | |
| API_URL = "http://127.0.0.1:5000" | |
| API_KEY = "castor_d167aa169b5e4219a66779e45fbaaefe" | |
| def test_health(): | |
| """Test health endpoint""" | |
| print("\n" + "="*60) | |
| print("1. Testing Health Endpoint") | |
| print("="*60) | |
| try: | |
| response = requests.get(f"{API_URL}/api/health") | |
| print(f"Status: {response.status_code}") | |
| print(json.dumps(response.json(), indent=2)) | |
| return response.status_code == 200 | |
| except Exception as e: | |
| print(f"Error: {e}") | |
| return False | |
| def test_forecast(): | |
| """Test forecast endpoint""" | |
| print("\n" + "="*60) | |
| print("2. Testing Forecast Endpoint") | |
| print("="*60) | |
| try: | |
| headers = { | |
| "X-API-Key": API_KEY, | |
| "Content-Type": "application/json" | |
| } | |
| payload = { | |
| "product": "Castor", | |
| "start_date": "2025-12-01", | |
| "end_date": "2025-12-10" | |
| } | |
| response = requests.post( | |
| f"{API_URL}/api/forecast", | |
| headers=headers, | |
| json=payload | |
| ) | |
| print(f"Status: {response.status_code}") | |
| data = response.json() | |
| print(f"Product: {data.get('product')}") | |
| print(f"Last Known Price: {data.get('last_known_price')}") | |
| print(f"Forecast Days: {data.get('forecast_period', {}).get('days')}") | |
| print(f"\nFirst 3 forecasts:") | |
| for forecast in data.get('forecast', [])[:3]: | |
| print(f" {forecast['date']}: ARIMA={forecast['arima_price']}, LSTM={forecast['lstm_price']}") | |
| return response.status_code == 200 | |
| except Exception as e: | |
| print(f"Error: {e}") | |
| return False | |
| def test_arima_only(): | |
| """Test ARIMA forecast endpoint""" | |
| print("\n" + "="*60) | |
| print("3. Testing ARIMA Forecast Endpoint") | |
| print("="*60) | |
| try: | |
| headers = { | |
| "X-API-Key": API_KEY, | |
| "Content-Type": "application/json" | |
| } | |
| payload = { | |
| "product": "Castor", | |
| "start_date": "2025-12-01", | |
| "end_date": "2025-12-05" | |
| } | |
| response = requests.post( | |
| f"{API_URL}/api/forecast/arima", | |
| headers=headers, | |
| json=payload | |
| ) | |
| print(f"Status: {response.status_code}") | |
| data = response.json() | |
| print(f"Model: {data.get('model')}") | |
| print(f"Product: {data.get('product')}") | |
| print(f"Forecasts: {len(data.get('forecast', []))}") | |
| return response.status_code == 200 | |
| except Exception as e: | |
| print(f"Error: {e}") | |
| return False | |
| if __name__ == "__main__": | |
| print("\nπ Castor Price Forecasting API - Test Suite") | |
| print(f"API URL: {API_URL}") | |
| print(f"API Key: {API_KEY[:20]}...{API_KEY[-10:]}") | |
| results = { | |
| "Health": test_health(), | |
| "Forecast": test_forecast(), | |
| "ARIMA": test_arima_only() | |
| } | |
| print("\n" + "="*60) | |
| print("TEST RESULTS") | |
| print("="*60) | |
| for test_name, result in results.items(): | |
| status = "β PASSED" if result else "β FAILED" | |
| print(f"{test_name}: {status}") | |
| all_passed = all(results.values()) | |
| print("\n" + "="*60) | |
| if all_passed: | |
| print("β All tests passed! API is ready for deployment.") | |
| else: | |
| print("β Some tests failed. Check the output above.") | |
| print("="*60 + "\n") | |
| sys.exit(0 if all_passed else 1) | |