Files
Cerbero-mcp/tests/unit/test_exchanges_builder.py
T
AdrianoDev b71c66917c chore(V2): cleanup quality gate
- ruff: contextlib.suppress al posto di try/except/pass (client_registry, test_env_routing)
- rimozione services/ legacy (residuo da git rm)
- fix integration test fixture: rimosso sys.modules.pop che inquinava module references nei test successivi (test_audit, test_client_init_default_http)

254 test passano. Ruff: clean. Mypy: 68 warning preesistenti dal codice V1 migrato (strict=false).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-30 19:02:55 +02:00

164 lines
5.2 KiB
Python

from __future__ import annotations
import pytest
@pytest.mark.asyncio
async def test_build_client_deribit_returns_correct_url(monkeypatch):
from tests.unit.test_settings import _minimal_env
for k, v in _minimal_env().items():
monkeypatch.setenv(k, v)
from cerbero_mcp.exchanges import build_client
from cerbero_mcp.settings import Settings
s = Settings()
c_test = await build_client(s, "deribit", "testnet")
c_live = await build_client(s, "deribit", "mainnet")
# DeribitClient espone base_url come property derivata da self.testnet
assert "test" in c_test.base_url.lower()
assert "test" not in c_live.base_url.lower()
@pytest.mark.asyncio
async def test_build_client_bybit_returns_correct_env(monkeypatch):
from tests.unit.test_settings import _minimal_env
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)
from cerbero_mcp.exchanges import build_client
from cerbero_mcp.settings import Settings
s = Settings()
c_test = await build_client(s, "bybit", "testnet")
c_live = await build_client(s, "bybit", "mainnet")
assert c_test is not c_live
assert c_test.testnet is True
assert c_live.testnet is False
@pytest.mark.asyncio
async def test_build_client_hyperliquid_returns_correct_env(monkeypatch):
from tests.unit.test_settings import _minimal_env
for k, v in _minimal_env().items():
monkeypatch.setenv(k, v)
from cerbero_mcp.exchanges import build_client
from cerbero_mcp.settings import Settings
s = Settings()
c_test = await build_client(s, "hyperliquid", "testnet")
c_live = await build_client(s, "hyperliquid", "mainnet")
assert c_test is not c_live
assert c_test.testnet is True
assert c_live.testnet is False
assert "test" in c_test.base_url.lower()
assert "test" not in c_live.base_url.lower()
@pytest.mark.asyncio
async def test_build_client_alpaca_returns_correct_env(monkeypatch):
from tests.unit.test_settings import _minimal_env
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)
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
@pytest.mark.asyncio
async def test_build_client_macro_no_env_distinction(monkeypatch):
from tests.unit.test_settings import _minimal_env
for k, v in _minimal_env().items():
monkeypatch.setenv(k, v)
from cerbero_mcp.exchanges import build_client
from cerbero_mcp.exchanges.macro.client import MacroClient
from cerbero_mcp.settings import Settings
s = Settings()
c_test = await build_client(s, "macro", "testnet")
c_live = await build_client(s, "macro", "mainnet")
# entrambi sono MacroClient validi (env ignorato)
assert isinstance(c_test, MacroClient)
assert isinstance(c_live, MacroClient)
# Stesse credenziali (env ignorato)
assert c_test.fred_api_key == c_live.fred_api_key
assert c_test.finnhub_api_key == c_live.finnhub_api_key
@pytest.mark.asyncio
async def test_build_client_sentiment_no_env_distinction(monkeypatch):
from tests.unit.test_settings import _minimal_env
for k, v in _minimal_env().items():
monkeypatch.setenv(k, v)
from cerbero_mcp.exchanges import build_client
from cerbero_mcp.exchanges.sentiment.client import SentimentClient
from cerbero_mcp.settings import Settings
s = Settings()
c_test = await build_client(s, "sentiment", "testnet")
c_live = await build_client(s, "sentiment", "mainnet")
# entrambi sono SentimentClient validi (env ignorato)
assert isinstance(c_test, SentimentClient)
assert isinstance(c_live, SentimentClient)
# Stesse credenziali (env ignorato)
assert c_test.cryptopanic_key == c_live.cryptopanic_key
assert c_test.lunarcrush_key == c_live.lunarcrush_key
@pytest.mark.asyncio
async def test_build_client_unknown_exchange_raises(monkeypatch):
from tests.unit.test_settings import _minimal_env
for k, v in _minimal_env().items():
monkeypatch.setenv(k, v)
from cerbero_mcp.exchanges import build_client
from cerbero_mcp.settings import Settings
s = Settings()
with pytest.raises(ValueError):
await build_client(s, "ftx", "testnet")