Skip to content

Commit 0781051

Browse files
feat: add testing client and order book (sammchardy#1456)
* feat: add testing client and order book * fix test * don't use testnet * fix tests * add async test --------- Co-authored-by: carlosmiei <43336371+carlosmiei@users.noreply.github.com>
1 parent 46417db commit 0781051

4 files changed

Lines changed: 90 additions & 11 deletions

File tree

tests/conftest.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import pytest
2+
from binance.client import Client, AsyncClient
3+
import os
4+
import asyncio
5+
6+
proxies = {}
7+
proxy = os.getenv("PROXY")
8+
9+
if proxy:
10+
proxies = {"http": proxy, 'https': proxy } # tmp: improve this in the future
11+
else:
12+
print("No proxy set")
13+
14+
15+
@pytest.fixture(scope="module")
16+
def client():
17+
return Client("test_api_key", "test_api_secret", {'proxies': proxies})
18+
19+
@pytest.fixture(scope="module")
20+
async def clientAsync():
21+
# for now this is not working inside the tests
22+
res = await AsyncClient().create(api_key="api_key", api_secret="api_secret", https_proxy=proxy, loop=asyncio.new_event_loop())
23+
return res

tests/test_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
3+
def test_client_initialization(client):
4+
assert client.API_KEY == 'test_api_key'
5+
assert client.API_SECRET == 'test_api_secret'
6+
assert client.testnet == False

tests/test_get_order_book.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import pytest
2+
from binance.exceptions import BinanceAPIException
3+
from binance import AsyncClient
4+
import os
5+
6+
proxies = {}
7+
proxy = os.getenv("PROXY")
8+
9+
10+
def assert_ob(order_book):
11+
assert isinstance(order_book, dict)
12+
assert 'lastUpdateId' in order_book
13+
assert 'bids' in order_book
14+
assert 'asks' in order_book
15+
16+
assert isinstance(order_book['bids'], list)
17+
assert isinstance(order_book['asks'], list)
18+
19+
if order_book['bids']:
20+
bid = order_book['bids'][0]
21+
assert len(bid) == 2
22+
assert all(isinstance(item, str) for item in bid[:2])
23+
24+
if order_book['asks']:
25+
ask = order_book['asks'][0]
26+
assert len(ask) == 2
27+
assert all(isinstance(item, str) for item in ask[:2])
28+
29+
def test_get_order_book(client):
30+
try:
31+
order_book = client.get_order_book(symbol='BTCUSDT')
32+
assert_ob(order_book)
33+
34+
except BinanceAPIException as e:
35+
pytest.fail(f"API request failed: {str(e)}")
36+
37+
def test_get_order_book_with_limit(client):
38+
try:
39+
order_book = client.get_order_book(symbol='BTCUSDT', limit=5)
40+
41+
assert_ob(order_book)
42+
assert len(order_book['bids']) <= 5
43+
assert len(order_book['asks']) <= 5
44+
45+
except BinanceAPIException as e:
46+
pytest.fail(f"API request failed: {str(e)}")
47+
48+
49+
async def test_get_order_book_async():
50+
try:
51+
client = AsyncClient(api_key="api_key", api_secret="api_secret", https_proxy=proxy)
52+
order_book = await client.get_order_book(symbol='BTCUSDT')
53+
54+
assert_ob(order_book)
55+
except BinanceAPIException as e:
56+
pytest.fail(f"API request failed: {str(e)}")

tests/test_ping.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,23 @@
33
import os
44
import pytest
55

6+
67
proxies = {}
78
proxy = os.getenv("PROXY")
89

9-
if proxy:
10-
proxies = {"http": proxy, 'https': proxy } # tmp: improve this in the future
11-
else:
12-
print("No proxy set")
13-
14-
client = Client("api_key", "api_secret", {'proxies': proxies})
15-
16-
def test_papi_ping_sync():
10+
def test_papi_ping_sync(client):
1711
ping_response = client.papi_ping()
1812
assert ping_response != None
1913

20-
def test_ping_sync():
14+
def test_ping_sync(client):
2115
ping_response = client.ping()
2216
assert ping_response != None
2317

24-
def test_futures_ping():
18+
def test_futures_ping(client):
2519
ping_response = client.futures_ping()
2620
assert ping_response != None
2721

28-
def test_coin_ping():
22+
def test_coin_ping(client):
2923
ping_response = client.futures_coin_ping()
3024
assert ping_response != None
3125

0 commit comments

Comments
 (0)