File size: 3,023 Bytes
7c850fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/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)