Add Binance REST client with OHLCV, ticker, and volume methods
Implements BinanceRestClient wrapping python-binance with get_top_coins, get_ohlcv, get_all_prices, and get_24h_volume. Includes mocked unit tests for all public methods. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
42
tests/test_binance_rest.py
Normal file
42
tests/test_binance_rest.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import pytest
|
||||
from unittest.mock import patch, MagicMock
|
||||
from data.binance_rest import BinanceRestClient
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
with patch("data.binance_rest.Client") as mock_client_cls:
|
||||
mock_client_cls.return_value = MagicMock()
|
||||
c = BinanceRestClient(api_key="test", api_secret="test")
|
||||
return c
|
||||
|
||||
def test_get_top_coins_returns_symbols(client):
|
||||
mock_tickers = [
|
||||
{"symbol": "BTCUSDT", "quoteVolume": "1000000"},
|
||||
{"symbol": "ETHUSDT", "quoteVolume": "500000"},
|
||||
{"symbol": "BTCETH", "quoteVolume": "200000"},
|
||||
]
|
||||
with patch.object(client.client, "get_ticker", return_value=mock_tickers):
|
||||
result = client.get_top_coins(limit=2)
|
||||
assert "BTCUSDT" in result
|
||||
assert "ETHUSDT" in result
|
||||
assert "BTCETH" not in result
|
||||
|
||||
def test_get_ohlcv_returns_dataframe(client):
|
||||
mock_klines = [
|
||||
[1700000000000, "40000", "41000", "39000", "40500", "100",
|
||||
1700003599999, "4050000", 500, "50", "2025000", "0"]
|
||||
]
|
||||
with patch.object(client.client, "get_klines", return_value=mock_klines):
|
||||
df = client.get_ohlcv("BTCUSDT", interval="1h", limit=1)
|
||||
assert len(df) == 1
|
||||
assert "close" in df.columns
|
||||
assert df.iloc[0]["close"] == 40500.0
|
||||
|
||||
def test_get_all_tickers(client):
|
||||
mock_prices = [
|
||||
{"symbol": "BTCUSDT", "price": "40000.00"},
|
||||
{"symbol": "ETHUSDT", "price": "3500.00"},
|
||||
]
|
||||
with patch.object(client.client, "get_all_tickers", return_value=mock_prices):
|
||||
result = client.get_all_prices()
|
||||
assert result["BTCUSDT"] == 40000.0
|
||||
Reference in New Issue
Block a user