|
| 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)}") |
0 commit comments