ETF_Suite_Portal/tests/test_api_config.py

74 lines
3.0 KiB
Python

import streamlit as st
import logging
from api import APIFactory
import pandas as pd
def test_api_configuration():
"""Test the API configuration and secrets."""
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
try:
# Initialize API factory
api_factory = APIFactory()
# Test FMP client
logger.info("Testing FMP client...")
fmp_client = api_factory.get_client('fmp')
# Test with a known ETF
test_symbol = "SPY"
# Test profile data
logger.info(f"Getting profile data for {test_symbol}...")
profile = fmp_client.get_etf_profile(test_symbol)
if isinstance(profile, (dict, list)) and (isinstance(profile, dict) and not profile.get('error') or isinstance(profile, list) and len(profile) > 0):
logger.info("✅ Profile data retrieved successfully")
if isinstance(profile, list):
logger.info(f"Retrieved {len(profile)} profile entries")
else:
logger.error("❌ Failed to get profile data")
if isinstance(profile, dict):
logger.error(f"Error: {profile.get('message', 'Unknown error')}")
else:
logger.error(f"Error: Unexpected response type: {type(profile)}")
# Test historical data
logger.info(f"Getting historical data for {test_symbol}...")
historical = fmp_client.get_historical_data(test_symbol, period='1mo')
if isinstance(historical, pd.DataFrame) and not historical.empty:
logger.info("✅ Historical data retrieved successfully")
logger.info(f"Data points: {len(historical)}")
else:
logger.error("❌ Failed to get historical data")
if isinstance(historical, dict):
logger.error(f"Error: {historical.get('message', 'Unknown error')}")
else:
logger.error(f"Error: Unexpected response type: {type(historical)}")
# Test cache
logger.info("Testing cache...")
cache_stats = api_factory.get_cache_stats()
logger.info(f"Cache stats: {cache_stats}")
# Test fallback to yfinance
logger.info("Testing fallback to yfinance...")
yfinance_data = api_factory.get_data(test_symbol, 'etf_profile', provider='yfinance')
if isinstance(yfinance_data, (dict, list)) and (isinstance(yfinance_data, dict) and not yfinance_data.get('error') or isinstance(yfinance_data, list) and len(yfinance_data) > 0):
logger.info("✅ YFinance fallback working")
else:
logger.error("❌ YFinance fallback failed")
logger.info("\n✅ All tests passed!")
return True
except Exception as e:
logger.error(f"❌ Test failed: {str(e)}")
return False
if __name__ == "__main__":
success = test_api_configuration()
if success:
print("\n✅ All tests passed!")
else:
print("\n❌ Some tests failed. Check the logs for details.")