Spaces:
Running
Running
File size: 4,787 Bytes
049861f | 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 | """
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)
|