refactor(V2): bybit client da pybit a httpx puro (parità V1)

This commit is contained in:
AdrianoDev
2026-05-01 01:35:26 +02:00
parent 95b8bcfe96
commit 6097dde4e4
4 changed files with 1149 additions and 620 deletions
+20 -32
View File
@@ -29,15 +29,8 @@ async def test_build_client_bybit_returns_correct_env(monkeypatch):
for k, v in _minimal_env().items():
monkeypatch.setenv(k, v)
# Stub pybit HTTP per evitare connessione reale durante __init__
from cerbero_mcp.exchanges.bybit import client as bybit_client
class _FakeHTTP:
def __init__(self, **kwargs):
self.kwargs = kwargs
monkeypatch.setattr(bybit_client, "HTTP", _FakeHTTP)
# BybitClient costruisce internamente httpx.AsyncClient: nessuna
# connessione reale finché non si invoca un metodo di rete.
from cerbero_mcp.exchanges import build_client
from cerbero_mcp.settings import Settings
@@ -78,28 +71,22 @@ async def test_build_client_alpaca_returns_correct_env(monkeypatch):
for k, v in _minimal_env().items():
monkeypatch.setenv(k, v)
# Stub alpaca SDK clients per evitare connessioni reali in __init__
from cerbero_mcp.exchanges.alpaca import client as alpaca_client
class _FakeSdk:
def __init__(self, **kwargs):
self.kwargs = kwargs
monkeypatch.setattr(alpaca_client, "TradingClient", _FakeSdk)
monkeypatch.setattr(alpaca_client, "StockHistoricalDataClient", _FakeSdk)
monkeypatch.setattr(alpaca_client, "CryptoHistoricalDataClient", _FakeSdk)
monkeypatch.setattr(alpaca_client, "OptionHistoricalDataClient", _FakeSdk)
# AlpacaClient (V2) usa httpx puro: il costruttore non apre connessioni
# reali (httpx.AsyncClient è lazy fino alla prima request), quindi nessuno
# stub SDK è necessario.
from cerbero_mcp.exchanges import build_client
from cerbero_mcp.settings import Settings
s = Settings()
c_test = await build_client(s, "alpaca", "testnet")
c_live = await build_client(s, "alpaca", "mainnet")
assert c_test is not c_live
assert c_test.paper is True
assert c_live.paper is False
try:
assert c_test is not c_live
assert c_test.paper is True
assert c_live.paper is False
finally:
await c_test.aclose()
await c_live.aclose()
@pytest.mark.asyncio
@@ -202,8 +189,8 @@ async def test_hyperliquid_url_from_env_overrides_default(monkeypatch):
@pytest.mark.asyncio
async def test_bybit_url_from_env_overrides_default(monkeypatch):
"""Bybit: pybit non accetta `endpoint` come kwarg, ma setting di
`_http.endpoint` post-init rispecchia l'override."""
"""Bybit (httpx): override BYBIT_URL_TESTNET applica direttamente a
`self.base_url`, usato come base di ogni richiesta REST V5."""
from tests.unit.test_settings import _minimal_env
env = _minimal_env(BYBIT_URL_TESTNET="https://bybit-custom.example.com")
@@ -216,14 +203,12 @@ async def test_bybit_url_from_env_overrides_default(monkeypatch):
s = Settings()
c = await build_client(s, "bybit", "testnet")
assert c.base_url == "https://bybit-custom.example.com"
# override applicato all'istanza pybit HTTP via attributo `endpoint`
assert getattr(c._http, "endpoint", None) == "https://bybit-custom.example.com"
@pytest.mark.asyncio
async def test_alpaca_url_from_env_overrides_default(monkeypatch):
"""Alpaca: TradingClient supporta url_override per trading API.
Data clients (Stock/Crypto/Option) non supportano override sul costruttore."""
"""Alpaca V2 (httpx): `base_url` override applica al solo trading
endpoint; data endpoints (data.alpaca.markets) restano hardcoded."""
from tests.unit.test_settings import _minimal_env
env = _minimal_env(ALPACA_URL_TESTNET="https://alpaca-custom.example.com")
@@ -235,7 +220,10 @@ async def test_alpaca_url_from_env_overrides_default(monkeypatch):
s = Settings()
c = await build_client(s, "alpaca", "testnet")
assert c.base_url == "https://alpaca-custom.example.com"
try:
assert c.base_url == "https://alpaca-custom.example.com"
finally:
await c.aclose()
@pytest.mark.asyncio