-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
executable file
·176 lines (140 loc) · 4.78 KB
/
test_api.py
File metadata and controls
executable file
·176 lines (140 loc) · 4.78 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
#!/usr/bin/env python3
"""
Quick API test script.
Tests each API endpoint independently.
"""
import os
import sys
from datetime import datetime
# Add src to path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
def test_fmp():
"""Test FMP API connection."""
print("\n" + "=" * 50)
print("Testing Financial Modeling Prep (FMP) API")
print("=" * 50)
from src.config import FMP_API_KEY
if not FMP_API_KEY:
print("❌ FMP_API_KEY not set in .env")
return False
print(f"✓ API key found: {FMP_API_KEY[:8]}...")
from src.fmp_client import FMPClient
client = FMPClient()
try:
# Test biggest losers
print("\nTesting /biggest-losers endpoint...")
losers = client.get_biggest_losers()
print(f"✓ Got {len(losers)} losers")
if losers:
print(f" First 3 losers:")
for l in losers[:3]:
print(f" {l.symbol}: {l.change_percentage:+.2f}%")
# Test profile
print("\nTesting /profile endpoint...")
profile = client.get_company_profile("AAPL", use_cache=False)
if profile:
print(f"✓ Got profile for AAPL")
print(f" Company: {profile.company_name}")
print(f" Sector: {profile.sector}")
print(f" Market Cap: ${profile.market_cap:,.0f}")
else:
print("❌ Failed to get AAPL profile")
return False
print("\n✅ FMP API tests passed!")
return True
except Exception as e:
print(f"❌ FMP API error: {e}")
import traceback
traceback.print_exc()
return False
def test_twelve_data():
"""Test Twelve Data API connection."""
print("\n" + "=" * 50)
print("Testing Twelve Data API")
print("=" * 50)
from src.config import TWELVE_DATA_API_KEY
if not TWELVE_DATA_API_KEY:
print("⚠️ TWELVE_DATA_API_KEY not set (optional)")
return None
print(f"✓ API key found: {TWELVE_DATA_API_KEY[:8]}...")
from src.twelve_data_client import TwelveDataClient
client = TwelveDataClient()
try:
print("\nTesting /quote endpoint...")
quote = client.get_quote("AAPL")
if quote:
print(f"✓ Got quote for AAPL")
print(f" Close: ${quote.close_price:.2f}")
print(f" Change: {quote.change_percent:+.2f}%")
else:
print("❌ Failed to get AAPL quote")
return False
status = client.get_credit_status()
print(f"\nCredit Status:")
print(f" Daily used: {status['daily_used']}")
print(f" Daily remaining: {status['daily_remaining']}")
print("\n✅ Twelve Data API tests passed!")
return True
except Exception as e:
print(f"❌ Twelve Data API error: {e}")
import traceback
traceback.print_exc()
return False
def test_gmail():
"""Test SMTP connection."""
print("\n" + "=" * 50)
print("Testing SMTP")
print("=" * 50)
from src.config import GMAIL_SENDER, SMTP_USER, SMTP_PASSWORD
if not GMAIL_SENDER:
print("❌ GMAIL_SENDER not set in .env")
return False
if not SMTP_USER or not SMTP_PASSWORD:
print("❌ SMTP_USER/SMTP_PASSWORD not set in .env")
return False
print(f"✓ Sender configured: {GMAIL_SENDER}")
print(f"✓ SMTP user configured: {SMTP_USER}")
from src.gmail_sender import GmailSender
sender = GmailSender()
try:
print("\nTesting connection...")
if sender.test_connection():
print("✅ SMTP tests passed!")
return True
else:
print("❌ SMTP connection test failed")
return False
except Exception as e:
print(f"❌ SMTP error: {e}")
import traceback
traceback.print_exc()
return False
def main():
"""Run all tests."""
print(f"\nAPI Test Suite - {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print("=" * 60)
results = {
"FMP": test_fmp(),
"Twelve Data": test_twelve_data(),
"SMTP": test_gmail()
}
print("\n" + "=" * 50)
print("Summary")
print("=" * 50)
for api, result in results.items():
if result is True:
status = "✅ PASS"
elif result is False:
status = "❌ FAIL"
else:
status = "⚠️ SKIPPED"
print(f" {api}: {status}")
# Return 0 if all required tests pass (Gmail/FMP)
if results["FMP"] and results["Gmail"]:
print("\n✅ All required tests passed!")
return 0
else:
print("\n❌ Some tests failed - check configuration")
return 1
if __name__ == "__main__":
sys.exit(main())