BackendPayactiv / test_api.py
3v324v23's picture
Deploy backend: add FastAPI app and Dockerfile
7c850fc
Raw History Blame Contribute Delete
3.02 kB
#!/usr/bin/env python3
"""
Test script to verify the unified backend API endpoints.
Run this before deploying to verify both EWA and Distress endpoints work.
"""
import json
import subprocess
import time
import sys
def test_endpoints():
"""Test all API endpoints with sample requests."""
tests = [
{
"name": "Health Check",
"endpoint": "/health",
"method": "GET",
},
{
"name": "Model Info",
"endpoint": "/model-info",
"method": "GET",
},
{
"name": "EWA Forecast (Simple)",
"endpoint": "/predict",
"method": "POST",
"data": {
"history": [10, 12, 15, 14, 16, 18, 20],
"horizon": 3,
"alpha": 0.3
}
},
{
"name": "Financial Distress Prediction",
"endpoint": "/distress/predict",
"method": "POST",
"data": {
"user_id": "test_user_001",
"total_spend": 5000,
"txn_count": 50,
"total_spend_30d": 1500,
"max_daily_spend_30d": 300,
"txn_count_30d": 15,
"ewa_usage_count": 5,
"ewa_total_amount": 500
}
}
]
base_url = "http://localhost:8000"
passed = 0
failed = 0
print("\n" + "=" * 60)
print("Testing Unified Financial & Demand Forecast API")
print("=" * 60 + "\n")
for test in tests:
try:
if test["method"] == "GET":
cmd = f'curl -s {base_url}{test["endpoint"]}'
else:
data_json = json.dumps(test.get("data", {}))
cmd = f'curl -s -X {test["method"]} {base_url}{test["endpoint"]} -H "Content-Type: application/json" -d \'{data_json}\''
result = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=5)
if result.returncode == 0:
response = json.loads(result.stdout)
print(f"✅ {test['name']}")
print(f" Endpoint: {test['method']} {test['endpoint']}")
print(f" Response: {json.dumps(response, indent=2)[:200]}...")
passed += 1
else:
print(f"❌ {test['name']}")
print(f" Error: {result.stderr}")
failed += 1
except Exception as e:
print(f"❌ {test['name']}")
print(f" Exception: {str(e)}")
failed += 1
print()
print("=" * 60)
print(f"Results: {passed} passed, {failed} failed")
print("=" * 60 + "\n")
return failed == 0
if __name__ == "__main__":
print("Make sure the backend is running: python main.py")
print("Waiting 3 seconds before starting tests...\n")
time.sleep(3)
success = test_endpoints()
sys.exit(0 if success else 1)