forked from sammchardy/python-binance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_async_client.py
More file actions
297 lines (187 loc) · 8.27 KB
/
test_async_client.py
File metadata and controls
297 lines (187 loc) · 8.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import pytest
from binance.async_client import AsyncClient
from .conftest import proxy, api_key, api_secret, testnet
from binance.exceptions import BinanceAPIException, BinanceRequestException
from aiohttp import ClientResponse, hdrs
from aiohttp.helpers import TimerNoop
from yarl import URL
pytestmark = [pytest.mark.asyncio]
async def test_clientAsync_initialization(clientAsync):
assert clientAsync.API_KEY is not None
assert clientAsync.API_SECRET is not None
@pytest.mark.skip(reason="Endpoint not documented")
async def test_get_products(clientAsync):
await clientAsync.get_products()
async def test_get_exchange_info(clientAsync):
await clientAsync.get_exchange_info()
async def test_get_symbol_info(clientAsync):
await clientAsync.get_symbol_info("BTCUSDT")
async def test_ping(clientAsync):
await clientAsync.ping()
async def test_get_server_time(clientAsync):
await clientAsync.get_server_time()
async def test_get_all_tickers(clientAsync):
await clientAsync.get_all_tickers()
async def test_get_orderbook_tickers(clientAsync):
await clientAsync.get_orderbook_tickers()
async def test_get_order_book(clientAsync):
await clientAsync.get_order_book(symbol="BTCUSDT")
async def test_get_recent_trades(clientAsync):
await clientAsync.get_recent_trades(symbol="BTCUSDT")
async def test_get_historical_trades(clientAsync):
await clientAsync.get_historical_trades(symbol="BTCUSDT")
async def test_get_aggregate_trades(clientAsync):
await clientAsync.get_aggregate_trades(symbol="BTCUSDT")
async def test_get_klines(clientAsync):
await clientAsync.get_klines(symbol="BTCUSDT", interval="1d")
async def test_get_uiklines(clientAsync):
await clientAsync.get_ui_klines(symbol="BTCUSDT", interval="1d")
async def test_futures_mark_price_klines(clientAsync):
await clientAsync.futures_mark_price_klines(symbol="BTCUSDT", interval="1h")
async def test_futures_index_price_klines(clientAsync):
await clientAsync.futures_index_price_klines(pair="BTCUSDT", interval="1h")
async def test_futures_premium_index_klines(clientAsync):
await clientAsync.futures_premium_index_klines(symbol="BTCUSDT", interval="1h")
@pytest.mark.skip(reason="network error")
async def test_futures_coin_premium_index_klines(clientAsync):
await clientAsync.futures_coin_premium_index_klines(symbol="BTCUSD", interval="1h")
async def test_get_avg_price(clientAsync):
await clientAsync.get_avg_price(symbol="BTCUSDT")
async def test_get_ticker(clientAsync):
await clientAsync.get_ticker(symbol="BTCUSDT")
async def test_get_symbol_ticker(clientAsync):
await clientAsync.get_symbol_ticker(symbol="BTCUSDT")
async def test_get_orderbook_ticker(clientAsync):
await clientAsync.get_orderbook_ticker(symbol="BTCUSDT")
async def test_get_account(clientAsync):
await clientAsync.get_account()
async def test_get_asset_balance(clientAsync):
await clientAsync.get_asset_balance(asset="BTC")
async def test_get_asset_balance_no_asset_provided(clientAsync):
await clientAsync.get_asset_balance()
async def test_get_my_trades(clientAsync):
await clientAsync.get_my_trades(symbol="BTCUSDT")
async def test_get_system_status(clientAsync):
await clientAsync.get_system_status()
# User Stream Endpoints
async def test_stream_get_listen_key_and_close(clientAsync):
listen_key = await clientAsync.stream_get_listen_key()
await clientAsync.stream_close(listen_key)
# Quoting interface endpoints
#########################
# Websocket API Requests #
#########################
async def test_ws_get_order_book(clientAsync):
await clientAsync.ws_get_order_book(symbol="BTCUSDT")
async def test_ws_get_recent_trades(clientAsync):
await clientAsync.ws_get_recent_trades(symbol="BTCUSDT")
async def test_ws_get_historical_trades(clientAsync):
await clientAsync.ws_get_historical_trades(symbol="BTCUSDT")
async def test_ws_get_aggregate_trades(clientAsync):
await clientAsync.ws_get_aggregate_trades(symbol="BTCUSDT")
async def test_ws_get_klines(clientAsync):
await clientAsync.ws_get_klines(symbol="BTCUSDT", interval="1m")
async def test_ws_get_uiKlines(clientAsync):
await clientAsync.ws_get_uiKlines(symbol="BTCUSDT", interval="1m")
async def test_ws_get_avg_price(clientAsync):
await clientAsync.ws_get_avg_price(symbol="BTCUSDT")
async def test_ws_get_ticker(clientAsync):
ticker = await clientAsync.ws_get_ticker(symbol="BTCUSDT")
async def test_ws_get_trading_day_ticker(clientAsync):
await clientAsync.ws_get_trading_day_ticker(symbol="BTCUSDT")
async def test_ws_get_symbol_ticker_window(clientAsync):
await clientAsync.ws_get_symbol_ticker_window(symbol="BTCUSDT")
async def test_ws_get_symbol_ticker(clientAsync):
await clientAsync.ws_get_symbol_ticker(symbol="BTCUSDT")
async def test_ws_get_orderbook_ticker(clientAsync):
await clientAsync.ws_get_orderbook_ticker(symbol="BTCUSDT")
async def test_ws_ping(clientAsync):
await clientAsync.ws_ping()
async def test_ws_get_time(clientAsync):
await clientAsync.ws_get_time()
async def test_ws_get_exchange_info(clientAsync):
await clientAsync.ws_get_exchange_info(symbol="BTCUSDT")
@pytest.mark.skip(reason="can't test margin endpoints")
async def test_margin_next_hourly_interest_rate(clientAsync):
await clientAsync.margin_next_hourly_interest_rate(
assets="BTC",
isIsolated="FALSE"
)
@pytest.mark.skip(reason="can't test margin endpoints")
async def test_margin_interest_history(clientAsync):
await clientAsync.margin_interest_history(
asset="BTC",
)
@pytest.mark.skip(reason="can't test margin endpoints")
async def test_margin_borrow_repay(clientAsync):
await clientAsync.margin_borrow_repay(
asset="BTC",
amount=0.1,
isIsolated="FALSE",
symbol="BTCUSDT",
type="BORROW"
)
@pytest.mark.skip(reason="can't test margin endpoints")
async def test_margin_get_borrow_repay_records(clientAsync):
await clientAsync.margin_get_borrow_repay_records(
asset="BTC",
isolatedSymbol="BTCUSDT",
)
@pytest.mark.skip(reason="can't test margin endpoints")
async def test_margin_interest_rate_history(clientAsync):
await clientAsync.margin_interest_rate_history(
asset="BTC",
)
@pytest.mark.skip(reason="can't test margin endpoints")
async def test_margin_max_borrowable(clientAsync):
await clientAsync.margin_max_borrowable(
asset="BTC",
)
async def test_time_unit_microseconds():
micro_client = AsyncClient(
api_key, api_secret, https_proxy=proxy, testnet=testnet, time_unit="MICROSECOND"
)
micro_trades = await micro_client.get_recent_trades(symbol="BTCUSDT")
assert len(str(micro_trades[0]["time"])) >= 16, (
"Time should be in microseconds (16+ digits)"
)
async def test_time_unit_milloseconds():
milli_client = AsyncClient(
api_key, api_secret, https_proxy=proxy, testnet=testnet, time_unit="MILLISECOND"
)
milli_trades = await milli_client.get_recent_trades(symbol="BTCUSDT")
assert len(str(milli_trades[0]["time"])) == 13, (
"Time should be in milliseconds (13 digits)"
)
async def test_handle_response(clientAsync):
# Create base response object
mock_response = ClientResponse(
'GET', URL('http://test.com'),
request_info=None,
writer=None,
continue100=None,
timer=TimerNoop(),
traces=[],
loop=clientAsync.loop,
session=None,
)
# Initialize headers
mock_response._headers = {hdrs.CONTENT_TYPE: 'application/json'}
# Test successful JSON response
mock_response.status = 200
mock_response._body = b'{"key": "value"}'
assert await clientAsync._handle_response(mock_response) == {"key": "value"}
# Test empty response
mock_response.status = 200
mock_response._body = b''
assert await clientAsync._handle_response(mock_response) == {}
# Test invalid JSON response
mock_response.status = 200
mock_response._body = b'invalid json'
with pytest.raises(BinanceRequestException):
await clientAsync._handle_response(mock_response)
# Test error status code
mock_response.status = 400
mock_response._body = b'error message'
with pytest.raises(BinanceAPIException):
await clientAsync._handle_response(mock_response)