-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add testing client and order book (#1456)
* feat: add testing client and order book * fix test * don't use testnet * fix tests * add async test --------- Co-authored-by: carlosmiei <[email protected]>
- Loading branch information
1 parent
46417db
commit 0781051
Showing
4 changed files
with
90 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import pytest | ||
from binance.client import Client, AsyncClient | ||
import os | ||
import asyncio | ||
|
||
proxies = {} | ||
proxy = os.getenv("PROXY") | ||
|
||
if proxy: | ||
proxies = {"http": proxy, 'https': proxy } # tmp: improve this in the future | ||
else: | ||
print("No proxy set") | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def client(): | ||
return Client("test_api_key", "test_api_secret", {'proxies': proxies}) | ||
|
||
@pytest.fixture(scope="module") | ||
async def clientAsync(): | ||
# for now this is not working inside the tests | ||
res = await AsyncClient().create(api_key="api_key", api_secret="api_secret", https_proxy=proxy, loop=asyncio.new_event_loop()) | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
|
||
def test_client_initialization(client): | ||
assert client.API_KEY == 'test_api_key' | ||
assert client.API_SECRET == 'test_api_secret' | ||
assert client.testnet == False | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import pytest | ||
from binance.exceptions import BinanceAPIException | ||
from binance import AsyncClient | ||
import os | ||
|
||
proxies = {} | ||
proxy = os.getenv("PROXY") | ||
|
||
|
||
def assert_ob(order_book): | ||
assert isinstance(order_book, dict) | ||
assert 'lastUpdateId' in order_book | ||
assert 'bids' in order_book | ||
assert 'asks' in order_book | ||
|
||
assert isinstance(order_book['bids'], list) | ||
assert isinstance(order_book['asks'], list) | ||
|
||
if order_book['bids']: | ||
bid = order_book['bids'][0] | ||
assert len(bid) == 2 | ||
assert all(isinstance(item, str) for item in bid[:2]) | ||
|
||
if order_book['asks']: | ||
ask = order_book['asks'][0] | ||
assert len(ask) == 2 | ||
assert all(isinstance(item, str) for item in ask[:2]) | ||
|
||
def test_get_order_book(client): | ||
try: | ||
order_book = client.get_order_book(symbol='BTCUSDT') | ||
assert_ob(order_book) | ||
|
||
except BinanceAPIException as e: | ||
pytest.fail(f"API request failed: {str(e)}") | ||
|
||
def test_get_order_book_with_limit(client): | ||
try: | ||
order_book = client.get_order_book(symbol='BTCUSDT', limit=5) | ||
|
||
assert_ob(order_book) | ||
assert len(order_book['bids']) <= 5 | ||
assert len(order_book['asks']) <= 5 | ||
|
||
except BinanceAPIException as e: | ||
pytest.fail(f"API request failed: {str(e)}") | ||
|
||
|
||
async def test_get_order_book_async(): | ||
try: | ||
client = AsyncClient(api_key="api_key", api_secret="api_secret", https_proxy=proxy) | ||
order_book = await client.get_order_book(symbol='BTCUSDT') | ||
|
||
assert_ob(order_book) | ||
except BinanceAPIException as e: | ||
pytest.fail(f"API request failed: {str(e)}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters