diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 000000000..8ae19a167 --- /dev/null +++ b/tests/conftest.py @@ -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 \ No newline at end of file diff --git a/tests/test_client.py b/tests/test_client.py new file mode 100644 index 000000000..c3f725fff --- /dev/null +++ b/tests/test_client.py @@ -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 diff --git a/tests/test_get_order_book.py b/tests/test_get_order_book.py new file mode 100644 index 000000000..dd2b823a8 --- /dev/null +++ b/tests/test_get_order_book.py @@ -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)}") diff --git a/tests/test_ping.py b/tests/test_ping.py index 99888fe67..6bdfbd1c3 100644 --- a/tests/test_ping.py +++ b/tests/test_ping.py @@ -3,29 +3,23 @@ import os import pytest + proxies = {} proxy = os.getenv("PROXY") -if proxy: - proxies = {"http": proxy, 'https': proxy } # tmp: improve this in the future -else: - print("No proxy set") - -client = Client("api_key", "api_secret", {'proxies': proxies}) - -def test_papi_ping_sync(): +def test_papi_ping_sync(client): ping_response = client.papi_ping() assert ping_response != None -def test_ping_sync(): +def test_ping_sync(client): ping_response = client.ping() assert ping_response != None -def test_futures_ping(): +def test_futures_ping(client): ping_response = client.futures_ping() assert ping_response != None -def test_coin_ping(): +def test_coin_ping(client): ping_response = client.futures_coin_ping() assert ping_response != None