"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
test(longbridge): cover both SDK Config shapes in one loader test
alex10f8295840ac
diff --git a/agent/src/trading/connectors/longbridge/sdk.py b/agent/src/trading/connectors/longbridge/sdk.pyindex 31f11da16..de6025e48 100644--- a/agent/src/trading/connectors/longbridge/sdk.py+++ b/agent/src/trading/connectors/longbridge/sdk.py@@ -673,12 +673,11 @@ def _require_openapi() -> ModuleType:def build_sdk_config(openapi: Any, app_key: str, app_secret: str, access_token: str) -> Any:- """Build a ``longbridge.openapi.Config`` on any SDK in the supported range.+ """Build the SDK ``Config`` on any SDK in the supported range.From 4.0 the SDK refuses direct ``Config(...)`` construction and only offers``Config.from_apikey``; 0.2.x has no ``from_apikey``. ``longbridge>=0.2.5``- admits both, so pick by capability rather than by version number. Shared by- this connector and the backtest loader so both follow one rule.+ admits both, so pick by capability rather than by version number.Args:openapi: The SDK ``openapi`` module.diff --git a/agent/tests/test_longbridge_loader.py b/agent/tests/test_longbridge_loader.pyindex 2e267e5d8..4227f2af6 100644--- a/agent/tests/test_longbridge_loader.py+++ b/agent/tests/test_longbridge_loader.py@@ -192,8 +192,24 @@ def _configured_loader() -> loader_mod.LongbridgeLoader:return loader+class _FactoryOnlyConfig:+ """Mimics SDK 4.0+: ``Config(...)`` is refused, only ``from_apikey`` works."""++ def __new__(cls, *args: object, **kwargs: object) -> "_FactoryOnlyConfig":+ raise TypeError("cannot create 'builtins.Config' instances")++ @staticmethod+ def from_apikey(*args: str) -> tuple[str, ...]:+ return args+++ "config_cls",+ [lambda *args: args, _FactoryOnlyConfig],+ ids=["sdk-0.2-constructor", "sdk-4-from_apikey"],+)def test_fetch_combines_all_windows_and_caches_complete_frame(- monkeypatch: pytest.MonkeyPatch,+ monkeypatch: pytest.MonkeyPatch, config_cls: object) -> None:calls: list[tuple[dt.date, dt.date]] = []cached: list[pd.DataFrame] = []@@ -227,7 +243,7 @@ def test_fetch_combines_all_windows_and_caches_complete_frame(]fake_openapi = SimpleNamespace(- Config=lambda *args: args,+ Config=config_cls,QuoteContext=FakeQuoteContext,Period=SimpleNamespace(Day="day"),AdjustType=SimpleNamespace(NoAdjust="none"),@@ -255,59 +271,6 @@ def test_fetch_combines_all_windows_and_caches_complete_frame(pd.testing.assert_frame_equal(cached[0], result["AAPL"])-def test_fetch_uses_from_apikey_when_sdk_provides_it(- monkeypatch: pytest.MonkeyPatch,-) -> None:- """SDK 4.0+ refuses ``Config(...)``; the loader must go through the factory."""- factory_calls: list[tuple[str, ...]] = []- built = object()-- class Sdk4Config:- def __new__(cls, *args: object) -> "Sdk4Config":- raise TypeError("cannot create 'builtins.Config' instances")-- @staticmethod- def from_apikey(*args: str) -> object:- factory_calls.append(args)- return built-- class FakeQuoteContext:- def __init__(self, config: object) -> None:- assert config is built-- def history_candlesticks_by_date(- self, *args: object, **kwargs: object- ) -> list[SimpleNamespace]:- return [- SimpleNamespace(- timestamp=pd.Timestamp(kwargs["start"]),- open=10,- high=11,- low=9,- close=10.5,- volume=100,- )- ]-- fake_openapi = SimpleNamespace(- Config=Sdk4Config,- QuoteContext=FakeQuoteContext,- Period=SimpleNamespace(Day="day"),- AdjustType=SimpleNamespace(NoAdjust="none"),- )- monkeypatch.setattr(loader_mod, "_require_longbridge", lambda: fake_openapi)- monkeypatch.setattr(loader_mod, "loader_cache_get", lambda **kwargs: None)- monkeypatch.setattr(loader_mod, "loader_cache_put", lambda **kwargs: None)-- result = _configured_loader().fetch(- ["AAPL"], "2026-01-01", "2026-01-02", interval="1D"- )-- assert factory_calls == [("key", "secret", "token")]- assert list(result) == ["AAPL"]- assert_loader_contract(result["AAPL"], context="longbridge daily via from_apikey")--def test_fetch_rejects_partial_history_when_a_window_fails(monkeypatch: pytest.MonkeyPatch,) -> None: