Skip to content

Commit

Permalink
feat: add testing client and order book (#1456)
Browse files Browse the repository at this point in the history
* 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
pcriadoperez and carlosmiei authored Oct 27, 2024
1 parent 46417db commit 0781051
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 11 deletions.
23 changes: 23 additions & 0 deletions tests/conftest.py
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
6 changes: 6 additions & 0 deletions tests/test_client.py
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

Check failure on line 6 in tests/test_client.py

View workflow job for this annotation

GitHub Actions / lint

Ruff (E712)

tests/test_client.py:6:12: E712 Avoid equality comparisons to `False`; use `if not client.testnet:` for false checks
56 changes: 56 additions & 0 deletions tests/test_get_order_book.py
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)}")
16 changes: 5 additions & 11 deletions tests/test_ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 0781051

Please sign in to comment.