#!/usr/bin/env python3 """ Test script to verify if API key switching is effective """ import os import sys from datetime import datetime from dotenv import load_dotenv # Load environment variables load_dotenv() # Add the current directory to Python path for imports sys.path.append(os.path.dirname(os.path.abspath(__file__))) from main import SantimentDataFetcher def test_api_key_switching(): """ Test if API key switching actually works by attempting multiple requests """ print("šŸ”§ Testing API Key Switching Effectiveness") print("=" * 60) # Get API keys api_keys = os.getenv('SANTIMENT_API_KEY') if not api_keys: print("āŒ No SANTIMENT_API_KEY found in environment") return key_list = [key.strip() for key in api_keys.split(',')] print(f"šŸ“Š Testing with {len(key_list)} API keys") # Create fetcher fetcher = SantimentDataFetcher() # Track switches initial_switches = fetcher.rate_limit_switches print(f"\nšŸš€ Starting test at {datetime.now().strftime('%H:%M:%S')}") print(f"Initial API key switches: {initial_switches}") # Attempt to fetch a simple metric multiple times test_slug = 'bitcoin' test_metric = 'price_usd' success_count = 0 attempt_count = 5 # Try 5 requests for i in range(attempt_count): print(f"\n--- Attempt {i+1}/{attempt_count} ---") print(f"Current API key: #{fetcher.current_key_index + 1}") try: result = fetcher.fetch_single_metric(test_metric, test_slug) if result is not None and not result.empty: success_count += 1 print(f"āœ… Success! Got {len(result)} data points") else: print("āš ļø No data returned") except Exception as e: print(f"āŒ Error: {e}") print(f"API key switches so far: {fetcher.rate_limit_switches}") # Small delay between requests import time time.sleep(1) # Final report print(f"\nšŸ“ˆ FINAL RESULTS") print("=" * 40) print(f"Successful requests: {success_count}/{attempt_count}") print(f"Total API key switches: {fetcher.rate_limit_switches}") print(f"Final API key: #{fetcher.current_key_index + 1}") # Interpret results if fetcher.rate_limit_switches > 0: print("\nāœ… API key switching IS working!") print("āœ… Your keys appear to be from different accounts.") elif success_count == attempt_count: print("\nāœ… All requests successful without switching!") print("ā„¹ļø Either keys are from different accounts OR current key still has quota.") else: print("\nāŒ No switching occurred and some requests failed.") print("āš ļø All keys might be from the same exhausted account.") return fetcher.rate_limit_switches > 0 if __name__ == "__main__": test_api_key_switching()