Spaces:
Running
Running
| """ | |
| Integration test script for PortIQ API. | |
| Validates all major endpoints sequentially using the default login credentials. | |
| """ | |
| import os | |
| import sys | |
| import json | |
| import requests | |
| API_URL = "http://127.0.0.1:8000/api" | |
| def run_tests(): | |
| print("π Starting PortIQ Integration Tests...") | |
| # 1. Test Login | |
| print("\nπ 1. Testing Login Endpoint (/api/auth/login)...") | |
| login_payload = { | |
| "role": "owner", | |
| "password": "tcr-owner" | |
| } | |
| r = requests.post(f"{API_URL}/auth/login", json=login_payload) | |
| if r.status_code != 200: | |
| print(f"β Login failed with status code {r.status_code}: {r.text}") | |
| return False | |
| auth_data = r.json() | |
| token = auth_data.get("token") | |
| print(f"β Login successful! Token: {token[:10]}...") | |
| headers = { | |
| "Authorization": f"Bearer {token}" | |
| } | |
| # 2. Test Get Holdings | |
| print("\nπ 2. Testing Get Holdings (/api/portfolio/holdings)...") | |
| r = requests.get(f"{API_URL}/portfolio/holdings", headers=headers) | |
| if r.status_code != 200: | |
| print(f"β Get holdings failed: {r.text}") | |
| return False | |
| print(f"β Get holdings successful! Found {len(r.json().get('holdings', []))} assets.") | |
| # 3. Test Upload CSV | |
| print("\nπ€ 3. Testing Upload CSV (/api/portfolio/upload)...") | |
| csv_data = ( | |
| "Instrument,Qty.,Avg. cost,LTP,Invested,Cur. val,P&L,Net chg.,Day chg.,\n" | |
| "TCS,10,3200.00,3400.00,32000.00,34000.00,2000.00,6.25,1.20,\n" | |
| "INFY,20,1500.00,1400.00,30000.00,28000.00,-2000.00,-6.67,-0.80,\n" | |
| ) | |
| files = { | |
| "file": ("test_holdings.csv", csv_data, "text/csv") | |
| } | |
| r = requests.post(f"{API_URL}/portfolio/upload", headers=headers, files=files) | |
| if r.status_code != 200: | |
| print(f"β CSV upload failed: {r.text}") | |
| return False | |
| print("β CSV upload successful!") | |
| # 4. Test Briefing Generation | |
| print("\nπ€ 4. Testing Briefing Generation (/api/briefings/generate)...") | |
| print("β Generating briefing (this uses Gemini API, may take 5-15 seconds)...") | |
| r = requests.post(f"{API_URL}/briefings/generate?enable_finbert=false&save_history=true", headers=headers) | |
| if r.status_code != 200: | |
| print(f"β Briefing generation failed: {r.text}") | |
| return False | |
| briefing_data = r.json() | |
| date_str = briefing_data.get("date") | |
| print(f"β Briefing generated successfully for date: {date_str}!") | |
| print(f" Market Mood: {briefing_data.get('mood', {}).get('market')}") | |
| print(f" Opening Call: {briefing_data.get('mood', {}).get('nifty_call')}") | |
| print(f" Top Pick: {briefing_data.get('top_picks', [{}])[0].get('symbol')}") | |
| # 5. Test Briefing History Logs | |
| print("\nπ 5. Testing Briefing History logs (/api/briefings/history)...") | |
| r = requests.get(f"{API_URL}/briefings/history", headers=headers) | |
| if r.status_code != 200: | |
| print(f"β History list failed: {r.text}") | |
| return False | |
| print(f"β History retrieved! Found {len(r.json())} briefings.") | |
| # 6. Test Specific Briefing | |
| print(f"\nπ 6. Testing Fetch Briefing for {date_str} (/api/briefings/{{date}})...") | |
| r = requests.get(f"{API_URL}/briefings/{date_str}", headers=headers) | |
| if r.status_code != 200: | |
| print(f"β Fetch specific briefing failed: {r.text}") | |
| return False | |
| print("β Specific briefing details retrieved successfully!") | |
| # 7. Test AI Chat Companion | |
| print("\n㪠7. Testing AI Chat Assistant (/api/chat/ask)...") | |
| chat_payload = { | |
| "query": "Hi, what are my holdings and what is today's top pick?", | |
| "active_date": date_str, | |
| "chat_history": [] | |
| } | |
| r = requests.post(f"{API_URL}/chat/ask", headers=headers, json=chat_payload) | |
| if r.status_code != 200: | |
| print(f"β Chat request failed: {r.text}") | |
| return False | |
| print("β Chat response received successfully!") | |
| print(f" AI response: {r.json().get('response')[:150]}...") | |
| # 8. Test Performance Engine | |
| print("\nπ 8. Testing Performance metrics (/api/performance)...") | |
| r = requests.get(f"{API_URL}/performance?refresh=true", headers=headers) | |
| if r.status_code != 200: | |
| print(f"β Performance calculation failed: {r.text}") | |
| return False | |
| perf_data = r.json() | |
| print("β Performance metrics computed successfully!") | |
| print(f" Composite AI Score: {perf_data.get('summary', {}).get('ai_score')}") | |
| print(f" Buy Win Rate: {perf_data.get('summary', {}).get('buy_win_rate')}%") | |
| print(f" Signals tracked: {perf_data.get('summary', {}).get('total_signals')}") | |
| print("\nπ ALL TESTS PASSED SUCCESSFULLY! π§ ") | |
| return True | |
| if __name__ == "__main__": | |
| success = run_tests() | |
| sys.exit(0 if success else 1) | |