-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_quick.py
More file actions
137 lines (121 loc) · 4.74 KB
/
test_quick.py
File metadata and controls
137 lines (121 loc) · 4.74 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
#!/usr/bin/env python3
"""
Quick test for UPI Fraud Detection System
Tests the simplified API
"""
import requests
import json
import time
from datetime import datetime
def test_api():
"""Test the API endpoints"""
base_url = "http://localhost:8000"
print("🧪 Testing UPI Fraud Detection API...")
print("=" * 50)
# Test 1: Health Check
print("1. Testing health check...")
try:
response = requests.get(f"{base_url}/health", timeout=5)
if response.status_code == 200:
data = response.json()
print(f" ✅ Health check passed: {data['status']}")
else:
print(f" ❌ Health check failed: {response.status_code}")
return False
except Exception as e:
print(f" ❌ Health check error: {e}")
return False
# Test 2: Model Status
print("2. Testing model status...")
try:
response = requests.get(f"{base_url}/models/status", timeout=5)
if response.status_code == 200:
data = response.json()
print(f" ✅ Model status: {data['model_type']} loaded")
else:
print(f" ❌ Model status failed: {response.status_code}")
except Exception as e:
print(f" ❌ Model status error: {e}")
# Test 3: Fraud Prediction - Low Risk
print("3. Testing low-risk transaction...")
try:
test_data = {
"transaction_id": "TXN_LOW_001",
"upi_id": "user@paytm",
"amount": 500.0,
"merchant_id": "MERCHANT_001",
"merchant_category": "food",
"device_id": "device_123",
"ip_address": "192.168.1.100",
"location": {"lat": 28.6139, "lon": 77.2090},
"timestamp": "2024-01-15T14:30:25Z",
"payment_method": "UPI"
}
response = requests.post(f"{base_url}/predict", json=test_data, timeout=10)
if response.status_code == 200:
data = response.json()
print(f" ✅ Low-risk prediction: {data['decision']} (Risk: {data['risk_score']:.3f})")
else:
print(f" ❌ Low-risk prediction failed: {response.status_code}")
except Exception as e:
print(f" ❌ Low-risk prediction error: {e}")
# Test 4: Fraud Prediction - High Risk
print("4. Testing high-risk transaction...")
try:
test_data = {
"transaction_id": "TXN_HIGH_001",
"upi_id": "user@paytm",
"amount": 75000.0,
"merchant_id": "MERCHANT_002",
"merchant_category": "crypto",
"device_id": "device_456",
"ip_address": "192.168.1.101",
"location": {"lat": 28.6139, "lon": 77.2090},
"timestamp": "2024-01-15T02:30:25Z", # Night time
"payment_method": "UPI"
}
response = requests.post(f"{base_url}/predict", json=test_data, timeout=10)
if response.status_code == 200:
data = response.json()
print(f" ✅ High-risk prediction: {data['decision']} (Risk: {data['risk_score']:.3f})")
print(f" 📝 Explanation: {data['explanation']['human_readable']}")
print(f" 🚨 Alerts: {data['alerts']}")
else:
print(f" ❌ High-risk prediction failed: {response.status_code}")
except Exception as e:
print(f" ❌ High-risk prediction error: {e}")
# Test 5: Performance Test
print("5. Testing performance...")
try:
test_data = {
"transaction_id": "TXN_PERF_001",
"upi_id": "perf@paytm",
"amount": 2500.0,
"merchant_id": "MERCHANT_003",
"merchant_category": "ecommerce",
"device_id": "device_789",
"ip_address": "192.168.1.102",
"location": {"lat": 28.6139, "lon": 77.2090},
"timestamp": "2024-01-15T10:30:25Z",
"payment_method": "UPI"
}
times = []
for i in range(5):
start_time = time.time()
response = requests.post(f"{base_url}/predict", json=test_data, timeout=10)
end_time = time.time()
if response.status_code == 200:
times.append((end_time - start_time) * 1000)
if times:
avg_time = sum(times) / len(times)
print(f" ✅ Performance test: {avg_time:.2f}ms average response time")
else:
print(f" ❌ Performance test failed")
except Exception as e:
print(f" ❌ Performance test error: {e}")
print("\n" + "=" * 50)
print("🎉 API testing completed!")
print("📊 Access the API documentation at: http://localhost:8000/docs")
return True
if __name__ == "__main__":
test_api()