-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_metrics_api.py
More file actions
159 lines (129 loc) · 5.53 KB
/
test_metrics_api.py
File metadata and controls
159 lines (129 loc) · 5.53 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
#!/usr/bin/env python3
"""
Test script to verify metrics API endpoints
"""
import asyncio
import sys
import os
from datetime import datetime, timedelta
# Add the root directory to Python path
root_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, root_dir)
from fastapi.testclient import TestClient
from src.main import create_app
def test_metrics_endpoints():
"""Test the metrics API endpoints"""
print("Testing Metrics API Endpoints...")
try:
# Create test client
app = create_app()
client = TestClient(app)
# Note: These tests will require authentication in a real scenario
# For now, we'll test if the endpoints exist and return proper error codes
print("1. Testing system metrics endpoint...")
response = client.get("/api/v1/metrics/system")
print(f" Status: {response.status_code}")
if response.status_code in [200, 401]: # 401 is expected without auth
print(" OK - System metrics endpoint exists")
else:
print(f" ERROR - Unexpected status code: {response.status_code}")
print("2. Testing conversation metrics endpoint...")
response = client.get("/api/v1/metrics/conversations?time_range=24h")
print(f" Status: {response.status_code}")
if response.status_code in [200, 401]: # 401 is expected without auth
print(" OK - Conversation metrics endpoint exists")
else:
print(f" ERROR - Unexpected status code: {response.status_code}")
print("3. Testing performance metrics endpoint...")
response = client.get("/api/v1/metrics/performance")
print(f" Status: {response.status_code}")
if response.status_code in [200, 401]: # 401 is expected without auth
print(" OK - Performance metrics endpoint exists")
else:
print(f" ERROR - Unexpected status code: {response.status_code}")
print("4. Testing business metrics endpoint...")
response = client.get("/api/v1/metrics/business")
print(f" Status: {response.status_code}")
if response.status_code in [200, 401]: # 401 is expected without auth
print(" OK - Business metrics endpoint exists")
else:
print(f" ERROR - Unexpected status code: {response.status_code}")
print("5. Testing export metrics endpoint...")
export_params = {
"start_date": (datetime.now() - timedelta(days=1)).isoformat(),
"end_date": datetime.now().isoformat(),
"metric_types": ["system"],
"format": "json"
}
response = client.get("/api/v1/metrics/export", params=export_params)
print(f" Status: {response.status_code}")
if response.status_code in [200, 401]: # 401 is expected without auth
print(" OK - Export metrics endpoint exists")
else:
print(f" ERROR - Unexpected status code: {response.status_code}")
print("\nAPI endpoint tests completed!")
return True
except Exception as e:
print(f"ERROR - API test failed: {e}")
import traceback
traceback.print_exc()
return False
def test_health_endpoint():
"""Test the health check endpoint"""
print("\nTesting Health Check Endpoint...")
try:
app = create_app()
client = TestClient(app)
response = client.get("/health")
print(f" Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f" Service: {data.get('service', 'Unknown')}")
print(f" Status: {data.get('status', 'Unknown')}")
print(" OK - Health endpoint working")
return True
else:
print(f" ERROR - Health check failed with status: {response.status_code}")
return False
except Exception as e:
print(f"ERROR - Health check test failed: {e}")
return False
def test_basic_endpoints():
"""Test basic application endpoints"""
print("\nTesting Basic Application Endpoints...")
try:
app = create_app()
client = TestClient(app)
# Test OpenAPI docs (if debug mode)
response = client.get("/docs")
print(f" Docs endpoint status: {response.status_code}")
# Test root metrics endpoint
response = client.get("/metrics")
print(f" Root metrics endpoint status: {response.status_code}")
print(" OK - Basic endpoint tests completed")
return True
except Exception as e:
print(f"ERROR - Basic endpoint test failed: {e}")
return False
def main():
"""Main test function"""
print("Starting Metrics API Tests")
print("=" * 50)
# Test health endpoint first
health_ok = test_health_endpoint()
# Test basic endpoints
basic_ok = test_basic_endpoints()
# Test metrics endpoints
metrics_ok = test_metrics_endpoints()
print("\n" + "=" * 50)
if health_ok and basic_ok and metrics_ok:
print("SUCCESS - API tests completed successfully!")
print("\nNote: Authentication is required for full metrics access.")
print("Status codes 401 (Unauthorized) are expected without valid tokens.")
return 0
else:
print("FAILURE - Some API tests failed")
return 1
if __name__ == "__main__":
exit_code = main()
sys.exit(exit_code)