From b862c3b7f9430f388ed7750d2c95f19f952ea268 Mon Sep 17 00:00:00 2001 From: Flora Xu Date: Sat, 5 Jul 2025 16:16:05 -0700 Subject: [PATCH] add tests --- .gitignore | 3 + tests/__init__.py | 5 ++ tests/test_env_loading.py | 91 +++++++++++++++++++++++++ tests/test_gemini_integration.py | 110 +++++++++++++++++++++++++++++++ tests/test_openai_integration.py | 110 +++++++++++++++++++++++++++++++ 5 files changed, 319 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/test_env_loading.py create mode 100644 tests/test_gemini_integration.py create mode 100644 tests/test_openai_integration.py diff --git a/.gitignore b/.gitignore index 4ebf99e3..41b4d9da 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,6 @@ eval_results/ eval_data/ *.egg-info/ .env + +results + diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..33e03e98 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,5 @@ +""" +TradingAgents Test Suite +""" + +__version__ = "0.1.0" \ No newline at end of file diff --git a/tests/test_env_loading.py b/tests/test_env_loading.py new file mode 100644 index 00000000..2ca5f086 --- /dev/null +++ b/tests/test_env_loading.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python3 +""" +Test script to demonstrate the new environment loading functionality +""" + +from tradingagents.utils.env_loader import load_environment_variables, print_environment_status, get_llm_config + +def test_environment_loading(): + """Test the environment loading functionality""" + + print("๐Ÿงช Testing Environment Loading") + print("=" * 40) + + # Test environment loading + env_info = load_environment_variables() + + print(f"\n๐Ÿ“ Environment file: {env_info['env_file_path']}") + print(f"๐Ÿ”‘ FinnHub available: {env_info['validation']['finnhub_available']}") + print(f"๐Ÿค– LLM providers: {env_info['validation']['llm_providers_available']}") + print(f"โœ… All required available: {env_info['validation']['all_required_available']}") + + if env_info['validation']['warnings']: + print("\nโš ๏ธ Warnings:") + for warning in env_info['validation']['warnings']: + print(f" โ€ข {warning}") + + return env_info + +def test_provider_configs(): + """Test provider configuration retrieval""" + + print("\n๐Ÿ”ง Testing Provider Configurations") + print("=" * 40) + + providers = ["openai", "google", "anthropic"] + + for provider in providers: + config = get_llm_config(provider) + print(f"\n๐Ÿ“‹ {provider.upper()} Configuration:") + print(f" Backend URL: {config.get('backend_url', 'Not needed')}") + print(f" Embedding Model: {config.get('embedding_model', 'Unknown')}") + print(f" API Key Env: {config.get('api_key_env', 'Unknown')}") + + quick_models = config.get('supported_models', {}).get('quick_think', []) + deep_models = config.get('supported_models', {}).get('deep_think', []) + + print(f" Quick Think Models: {len(quick_models)} available") + print(f" Deep Think Models: {len(deep_models)} available") + +def test_gemini_specific(): + """Test Gemini-specific configuration""" + + print("\n๐Ÿค– Testing Gemini Configuration") + print("=" * 40) + + google_config = get_llm_config("google") + + print(f"Backend URL: {google_config.get('backend_url')}") + print(f"Embedding Model: {google_config.get('embedding_model')}") + + if google_config.get('backend_url') is None: + print("โœ… Correct: Google doesn't need backend_url") + else: + print("โŒ Incorrect: Google should not have backend_url") + + # Test model availability + quick_models = google_config.get('supported_models', {}).get('quick_think', []) + deep_models = google_config.get('supported_models', {}).get('deep_think', []) + + print(f"\nAvailable Gemini Models:") + print(f" Quick Think: {quick_models}") + print(f" Deep Think: {deep_models}") + +if __name__ == "__main__": + print("๐Ÿš€ TradingAgents Environment Testing") + print("=" * 50) + + # Test environment loading + env_info = test_environment_loading() + + # Test provider configurations + test_provider_configs() + + # Test Gemini-specific configuration + test_gemini_specific() + + # Show full environment status + print("\n" + "=" * 50) + print_environment_status() + + print("\nโœ… Environment testing completed!") \ No newline at end of file diff --git a/tests/test_gemini_integration.py b/tests/test_gemini_integration.py new file mode 100644 index 00000000..690549bc --- /dev/null +++ b/tests/test_gemini_integration.py @@ -0,0 +1,110 @@ +#!/usr/bin/env python3 +""" +Test script to verify Gemini integration with TradingAgents +""" + +import os +import sys +from pathlib import Path + +# Add the project root to the Python path +project_root = Path(__file__).parent.parent +sys.path.insert(0, str(project_root)) + +from tradingagents.graph.trading_graph import TradingAgentsGraph +from tradingagents.default_config import DEFAULT_CONFIG + +def test_gemini_integration(): + """Test the Gemini integration""" + + # Check if Google API key is available + if not os.getenv("GOOGLE_API_KEY"): + print("โŒ GOOGLE_API_KEY environment variable not set") + print("Please set it with: export GOOGLE_API_KEY=your_key_here") + return False + + # Create a config for Gemini + config = DEFAULT_CONFIG.copy() + config["llm_provider"] = "google" + config["backend_url"] = "https://generativelanguage.googleapis.com/v1" + config["deep_think_llm"] = "gemini-2.0-flash" + config["quick_think_llm"] = "gemini-2.0-flash" + config["max_debate_rounds"] = 1 + config["online_tools"] = True + + try: + print("๐Ÿš€ Testing Gemini integration...") + + # Initialize the graph with Gemini + graph = TradingAgentsGraph( + selected_analysts=["market", "news"], # Use fewer analysts for testing + debug=True, + config=config + ) + + print("โœ… TradingAgentsGraph initialized successfully with Gemini") + + # Test a simple propagation + print("๐Ÿ”„ Testing propagation with SPY...") + _, decision = graph.propagate("SPY", "2024-12-01") + + print(f"โœ… Propagation completed successfully!") + print(f"๐Ÿ“Š Final decision: {decision}") + + return True + + except Exception as e: + print(f"โŒ Error during Gemini integration test: {str(e)}") + return False + +def test_memory_system(): + """Test the memory system with Gemini embeddings""" + + if not os.getenv("GOOGLE_API_KEY"): + print("โŒ GOOGLE_API_KEY environment variable not set") + return False + + try: + from tradingagents.agents.utils.memory import FinancialSituationMemory + + config = { + "llm_provider": "google", + "backend_url": "https://generativelanguage.googleapis.com/v1" + } + + print("๐Ÿง  Testing memory system with Gemini embeddings...") + + memory = FinancialSituationMemory("test_memory", config) + + # Test adding situations + test_data = [ + ("High inflation with rising interest rates", "Consider defensive sectors"), + ("Tech sector volatility increasing", "Reduce exposure to growth stocks") + ] + + memory.add_situations(test_data) + print("โœ… Memory system working with Gemini embeddings") + + return True + + except Exception as e: + print(f"โŒ Error in memory system test: {str(e)}") + return False + +if __name__ == "__main__": + print("๐Ÿงช Testing TradingAgents Gemini Integration") + print("=" * 50) + + # Test memory system first + memory_success = test_memory_system() + + if memory_success: + # Test full integration + integration_success = test_gemini_integration() + + if integration_success: + print("\n๐ŸŽ‰ All Gemini integration tests passed!") + else: + print("\nโŒ Gemini integration test failed") + else: + print("\nโŒ Memory system test failed") \ No newline at end of file diff --git a/tests/test_openai_integration.py b/tests/test_openai_integration.py new file mode 100644 index 00000000..c7e88cc0 --- /dev/null +++ b/tests/test_openai_integration.py @@ -0,0 +1,110 @@ +#!/usr/bin/env python3 +""" +Test script to verify OpenAI integration with TradingAgents +""" + +import os +import sys +from pathlib import Path + +# Add the project root to the Python path +project_root = Path(__file__).parent.parent +sys.path.insert(0, str(project_root)) + +from tradingagents.graph.trading_graph import TradingAgentsGraph +from tradingagents.default_config import DEFAULT_CONFIG + +def test_openai_integration(): + """Test the OpenAI integration""" + + # Check if OpenAI API key is available + if not os.getenv("OPENAI_API_KEY"): + print("โŒ OPENAI_API_KEY environment variable not set") + print("Please set it with: export OPENAI_API_KEY=your_key_here") + return False + + # Create a config for OpenAI + config = DEFAULT_CONFIG.copy() + config["llm_provider"] = "openai" + config["backend_url"] = "https://api.openai.com/v1" + config["deep_think_llm"] = "gpt-4o-mini" + config["quick_think_llm"] = "gpt-4o-mini" + config["max_debate_rounds"] = 1 + config["online_tools"] = True + + try: + print("๐Ÿš€ Testing OpenAI integration...") + + # Initialize the graph with OpenAI + graph = TradingAgentsGraph( + selected_analysts=["market", "news"], # Use fewer analysts for testing + debug=True, + config=config + ) + + print("โœ… TradingAgentsGraph initialized successfully with OpenAI") + + # Test a simple propagation + print("๐Ÿ”„ Testing propagation with SPY...") + _, decision = graph.propagate("SPY", "2024-12-01") + + print(f"โœ… Propagation completed successfully!") + print(f"๐Ÿ“Š Final decision: {decision}") + + return True + + except Exception as e: + print(f"โŒ Error during OpenAI integration test: {str(e)}") + return False + +def test_memory_system(): + """Test the memory system with OpenAI embeddings""" + + if not os.getenv("OPENAI_API_KEY"): + print("โŒ OPENAI_API_KEY environment variable not set") + return False + + try: + from tradingagents.agents.utils.memory import FinancialSituationMemory + + config = { + "llm_provider": "openai", + "backend_url": "https://api.openai.com/v1" + } + + print("๐Ÿง  Testing memory system with OpenAI embeddings...") + + memory = FinancialSituationMemory("test_memory", config) + + # Test adding situations + test_data = [ + ("High inflation with rising interest rates", "Consider defensive sectors"), + ("Tech sector volatility increasing", "Reduce exposure to growth stocks") + ] + + memory.add_situations(test_data) + print("โœ… Memory system working with OpenAI embeddings") + + return True + + except Exception as e: + print(f"โŒ Error in memory system test: {str(e)}") + return False + +if __name__ == "__main__": + print("๐Ÿงช Testing TradingAgents OpenAI Integration") + print("=" * 50) + + # Test memory system first + memory_success = test_memory_system() + + if memory_success: + # Test full integration + integration_success = test_openai_integration() + + if integration_success: + print("\n๐ŸŽ‰ All OpenAI integration tests passed!") + else: + print("\nโŒ OpenAI integration test failed") + else: + print("\nโŒ Memory system test failed") \ No newline at end of file