-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_endpoints.py
More file actions
206 lines (164 loc) Β· 8.28 KB
/
test_endpoints.py
File metadata and controls
206 lines (164 loc) Β· 8.28 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python3
"""
Test script for the new Gap Analysis API endpoints.
Demonstrates the async workflow that frontend should follow.
"""
import asyncio
import httpx
import json
import time
from datetime import datetime
# API base URL (adjust if needed)
BASE_URL = "http://localhost:8000/api/v1/gap-analysis"
async def test_gap_analysis_workflow():
"""
Test the complete gap analysis workflow:
1. Submit job
2. Poll status
3. Retrieve results
"""
print("π§ͺ TESTING GAP ANALYSIS API ENDPOINTS")
print("=" * 50)
async with httpx.AsyncClient(timeout=300.0) as client:
# Step 1: Submit a job
print("\nπ€ Step 1: Submitting gap analysis job...")
submission_data = {
"url": "https://arxiv.org/pdf/2506.15524", # Example URL
"max_papers": 5, # Small for testing
"validation_threshold": 1
}
try:
submit_response = await client.post(
f"{BASE_URL}/submit",
json=submission_data
)
if submit_response.status_code == 200:
submission_result = submit_response.json()
job_id = submission_result["job_id"]
print(f"β
Job submitted successfully!")
print(f" Job ID: {job_id}")
print(f" Estimated time: {submission_result['estimated_time_minutes']} minutes")
print(f" Message: {submission_result['message']}")
else:
print(f"β Job submission failed: {submit_response.status_code}")
print(f" Error: {submit_response.text}")
return
except Exception as e:
print(f"β Job submission error: {str(e)}")
return
# Step 2: Poll job status
print(f"\nπ Step 2: Polling job status...")
max_polls = 60 # Poll for up to 10 minutes
poll_interval = 10 # seconds
for poll_count in range(max_polls):
try:
status_response = await client.get(f"{BASE_URL}/status/{job_id}")
if status_response.status_code == 200:
status_data = status_response.json()
status = status_data["status"]
progress = status_data["progress_message"]
print(f" Poll {poll_count + 1}: Status = {status}")
print(f" Progress: {progress}")
if status == "completed":
print("β
Job completed successfully!")
if "processing_time_seconds" in status_data:
print(f" Processing time: {status_data['processing_time_seconds']:.1f} seconds")
break
elif status == "failed":
print(f"β Job failed: {status_data.get('error', 'Unknown error')}")
return
elif status in ["pending", "running"]:
print(f" Waiting {poll_interval} seconds before next poll...")
await asyncio.sleep(poll_interval)
continue
else:
print(f"β Status check failed: {status_response.status_code}")
return
except Exception as e:
print(f"β Status polling error: {str(e)}")
await asyncio.sleep(poll_interval)
continue
else:
print("β° Timeout: Job did not complete within expected time")
return
# Step 3: Retrieve results
print(f"\nπ Step 3: Retrieving results...")
try:
result_response = await client.get(f"{BASE_URL}/result/{job_id}")
if result_response.status_code == 200:
result_data = result_response.json()
print("β
Results retrieved successfully!")
print("\nπ ANALYSIS SUMMARY:")
print(f" Request ID: {result_data.get('request_id', 'N/A')}")
print(f" Validated Gaps: {len(result_data.get('validated_gaps', []))}")
print(f" Papers Analyzed: {result_data.get('process_metadata', {}).get('total_papers_analyzed', 'N/A')}")
print(f" Gaps Eliminated: {result_data.get('process_metadata', {}).get('gaps_eliminated', 'N/A')}")
# Show first gap as example
gaps = result_data.get('validated_gaps', [])
if gaps:
first_gap = gaps[0]
print(f"\nπ― EXAMPLE GAP:")
print(f" Title: {first_gap.get('gap_title', 'N/A')}")
print(f" Category: {first_gap.get('category', 'N/A')}")
print(f" Description: {first_gap.get('description', 'N/A')[:100]}...")
# Save full result to file
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
result_file = f"test_result_{timestamp}.json"
with open(result_file, 'w') as f:
json.dump(result_data, f, indent=2, default=str)
print(f"\nπΎ Full results saved to: {result_file}")
else:
print(f"β Result retrieval failed: {result_response.status_code}")
print(f" Error: {result_response.text}")
except Exception as e:
print(f"β Result retrieval error: {str(e)}")
async def test_health_and_info():
"""Test the health and info endpoints"""
print("\nπ₯ TESTING HEALTH AND INFO ENDPOINTS")
print("=" * 40)
async with httpx.AsyncClient() as client:
# Test health endpoint
try:
health_response = await client.get(f"{BASE_URL}/health")
if health_response.status_code == 200:
health_data = health_response.json()
print("β
Health check passed!")
print(f" Service: {health_data.get('service', 'N/A')}")
print(f" Version: {health_data.get('version', 'N/A')}")
print(f" Running jobs: {health_data.get('running_jobs', 'N/A')}")
else:
print(f"β Health check failed: {health_response.status_code}")
except Exception as e:
print(f"β Health check error: {str(e)}")
# Test info endpoint
try:
info_response = await client.get(f"{BASE_URL}/info")
if info_response.status_code == 200:
info_data = info_response.json()
print("\nπ Service Info:")
print(f" Name: {info_data.get('service_name', 'N/A')}")
print(f" Description: {info_data.get('description', 'N/A')}")
print(f" New Features: {len(info_data.get('new_features', []))} features")
print(f" Supported Domains: {len(info_data.get('supported_domains', []))} domains")
else:
print(f"β Info endpoint failed: {info_response.status_code}")
except Exception as e:
print(f"β Info endpoint error: {str(e)}")
async def main():
"""Main test function"""
start_time = time.time()
print(f"π Starting API endpoint tests at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print("π Make sure the FastAPI server is running on localhost:8000")
# Test health and info first
await test_health_and_info()
# Then test the full workflow
await test_gap_analysis_workflow()
total_time = time.time() - start_time
print(f"\nπ All tests completed in {total_time:.1f} seconds")
print("\nπ FRONTEND INTEGRATION NOTES:")
print(" 1. POST /submit to start analysis (returns job_id)")
print(" 2. Poll GET /status/{job_id} every 5-10 seconds")
print(" 3. GET /result/{job_id} when status = 'completed'")
print(" 4. Handle errors gracefully with status codes")
if __name__ == "__main__":
asyncio.run(main())