mirror of
https://github.com/TauricResearch/TradingAgents.git
synced 2026-05-01 14:33:10 +03:00
refactor: simplify api_key mapping and consolidate tests
Apply review suggestions: use concise `or` pattern for API key resolution, consolidate tests into parameterized subTest, move import to module level per PEP 8.
This commit is contained in:
@@ -1,38 +1,27 @@
|
|||||||
import unittest
|
import unittest
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from tradingagents.llm_clients.google_client import GoogleClient
|
||||||
|
|
||||||
|
|
||||||
class TestGoogleApiKeyStandardization(unittest.TestCase):
|
class TestGoogleApiKeyStandardization(unittest.TestCase):
|
||||||
"""Verify GoogleClient accepts unified api_key parameter."""
|
"""Verify GoogleClient accepts unified api_key parameter."""
|
||||||
|
|
||||||
@patch("tradingagents.llm_clients.google_client.NormalizedChatGoogleGenerativeAI")
|
@patch("tradingagents.llm_clients.google_client.NormalizedChatGoogleGenerativeAI")
|
||||||
def test_api_key_mapped_to_google_api_key(self, mock_chat):
|
def test_api_key_handling(self, mock_chat):
|
||||||
from tradingagents.llm_clients.google_client import GoogleClient
|
test_cases = [
|
||||||
|
("unified api_key is mapped", {"api_key": "test-key-123"}, "test-key-123"),
|
||||||
|
("legacy google_api_key still works", {"google_api_key": "legacy-key-456"}, "legacy-key-456"),
|
||||||
|
("unified api_key takes precedence", {"api_key": "unified", "google_api_key": "legacy"}, "unified"),
|
||||||
|
]
|
||||||
|
|
||||||
client = GoogleClient("gemini-2.5-flash", api_key="test-key-123")
|
for msg, kwargs, expected_key in test_cases:
|
||||||
|
with self.subTest(msg=msg):
|
||||||
|
mock_chat.reset_mock()
|
||||||
|
client = GoogleClient("gemini-2.5-flash", **kwargs)
|
||||||
client.get_llm()
|
client.get_llm()
|
||||||
call_kwargs = mock_chat.call_args[1]
|
call_kwargs = mock_chat.call_args[1]
|
||||||
self.assertEqual(call_kwargs["google_api_key"], "test-key-123")
|
self.assertEqual(call_kwargs.get("google_api_key"), expected_key)
|
||||||
|
|
||||||
@patch("tradingagents.llm_clients.google_client.NormalizedChatGoogleGenerativeAI")
|
|
||||||
def test_legacy_google_api_key_still_works(self, mock_chat):
|
|
||||||
from tradingagents.llm_clients.google_client import GoogleClient
|
|
||||||
|
|
||||||
client = GoogleClient("gemini-2.5-flash", google_api_key="legacy-key-456")
|
|
||||||
client.get_llm()
|
|
||||||
call_kwargs = mock_chat.call_args[1]
|
|
||||||
self.assertEqual(call_kwargs["google_api_key"], "legacy-key-456")
|
|
||||||
|
|
||||||
@patch("tradingagents.llm_clients.google_client.NormalizedChatGoogleGenerativeAI")
|
|
||||||
def test_api_key_takes_precedence_over_google_api_key(self, mock_chat):
|
|
||||||
from tradingagents.llm_clients.google_client import GoogleClient
|
|
||||||
|
|
||||||
client = GoogleClient(
|
|
||||||
"gemini-2.5-flash", api_key="unified", google_api_key="legacy"
|
|
||||||
)
|
|
||||||
client.get_llm()
|
|
||||||
call_kwargs = mock_chat.call_args[1]
|
|
||||||
self.assertEqual(call_kwargs["google_api_key"], "unified")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -32,10 +32,9 @@ class GoogleClient(BaseLLMClient):
|
|||||||
llm_kwargs[key] = self.kwargs[key]
|
llm_kwargs[key] = self.kwargs[key]
|
||||||
|
|
||||||
# Unified api_key maps to provider-specific google_api_key
|
# Unified api_key maps to provider-specific google_api_key
|
||||||
if "api_key" in self.kwargs:
|
google_api_key = self.kwargs.get("api_key") or self.kwargs.get("google_api_key")
|
||||||
llm_kwargs["google_api_key"] = self.kwargs["api_key"]
|
if google_api_key:
|
||||||
elif "google_api_key" in self.kwargs:
|
llm_kwargs["google_api_key"] = google_api_key
|
||||||
llm_kwargs["google_api_key"] = self.kwargs["google_api_key"]
|
|
||||||
|
|
||||||
# Map thinking_level to appropriate API param based on model
|
# Map thinking_level to appropriate API param based on model
|
||||||
# Gemini 3 Pro: low, high
|
# Gemini 3 Pro: low, high
|
||||||
|
|||||||
Reference in New Issue
Block a user