forked from sammchardy/python-binance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client.py
More file actions
252 lines (155 loc) · 5.83 KB
/
Copy pathtest_client.py
File metadata and controls
252 lines (155 loc) · 5.83 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
import pytest
from binance.client import Client
from binance.exceptions import BinanceAPIException, BinanceRequestException
from .conftest import proxies, api_key, api_secret, testnet
def test_client_initialization(client):
assert client.API_KEY is not None
assert client.API_SECRET is not None
@pytest.mark.skip(reason="Endpoint not documented")
def test_get_products(client):
client.get_products()
def test_get_exchange_info(client):
client.get_exchange_info()
def test_get_symbol_info(client):
client.get_symbol_info("BTCUSDT")
def test_ping(client):
client.ping()
def test_get_server_time(client):
client.get_server_time()
def test_get_all_tickers(client):
client.get_all_tickers()
def test_get_orderbook_tickers(client):
client.get_orderbook_tickers()
def test_get_order_book(client):
client.get_order_book(symbol="BTCUSDT")
def test_get_recent_trades(client):
client.get_recent_trades(symbol="BTCUSDT")
def test_get_historical_trades(client):
client.get_historical_trades(symbol="BTCUSDT")
def test_get_aggregate_trades(client):
client.get_aggregate_trades(symbol="BTCUSDT")
def test_get_klines(client):
client.get_klines(symbol="BTCUSDT", interval="1d")
def test_get_ui_klines(client):
client.get_ui_klines(symbol="BTCUSDT", interval="1d")
def test_get_avg_price(client):
client.get_avg_price(symbol="BTCUSDT")
def test_get_ticker(client):
client.get_ticker(symbol="BTCUSDT")
def test_get_symbol_ticker(client):
client.get_symbol_ticker(symbol="BTCUSDT")
def test_get_orderbook_ticker(client):
client.get_orderbook_ticker(symbol="BTCUSDT")
def test_get_account(client):
client.get_account()
def test_get_asset_balance(client):
client.get_asset_balance(asset="BTC")
def test_get_asset_balance_no_asset_provided(client):
client.get_asset_balance()
def test_get_my_trades(client):
client.get_my_trades(symbol="BTCUSDT")
def test_get_system_status(client):
client.get_system_status()
# User Stream Endpoints
def test_stream_get_listen_key_and_close(client):
listen_key = client.stream_get_listen_key()
client.stream_close(listen_key)
# Quoting interface endpoints
@pytest.mark.skip(reason="Endpoint not working on testnet")
def test_get_account_status(client):
client.get_account_status()
@pytest.mark.skip(reason="Endpoint not working on testnet")
def test_get_account_api_trading_status(client):
client.get_account_api_trading_status()
@pytest.mark.skip(reason="Endpoint not working on testnet")
def test_get_account_api_permissions(client):
client.get_account_api_permissions()
@pytest.mark.skip(reason="Endpoint not working on testnet")
def test_get_dust_assets(client):
client.get_dust_assets()
#########################
# Websocket API Requests #
#########################
def test_ws_get_order_book(client):
client.ws_get_order_book(symbol="BTCUSDT")
def test_ws_get_recent_trades(client):
client.ws_get_recent_trades(symbol="BTCUSDT")
def test_ws_get_historical_trades(client):
client.ws_get_historical_trades(symbol="BTCUSDT")
def test_ws_get_aggregate_trades(client):
client.ws_get_aggregate_trades(symbol="BTCUSDT")
def test_ws_get_klines(client):
client.ws_get_klines(symbol="BTCUSDT", interval="1m")
def test_ws_get_uiKlines(client):
client.ws_get_uiKlines(symbol="BTCUSDT", interval="1m")
def test_ws_get_avg_price(client):
client.ws_get_avg_price(symbol="BTCUSDT")
def test_ws_get_ticker(client):
ticker = client.ws_get_ticker(symbol="BTCUSDT")
def test_ws_get_trading_day_ticker(client):
client.ws_get_trading_day_ticker(symbol="BTCUSDT")
def test_ws_get_symbol_ticker_window(client):
client.ws_get_symbol_ticker_window(symbol="BTCUSDT")
def test_ws_get_symbol_ticker(client):
client.ws_get_symbol_ticker(symbol="BTCUSDT")
def test_ws_get_orderbook_ticker(client):
client.ws_get_orderbook_ticker(symbol="BTCUSDT")
def test_ws_ping(client):
client.ws_ping()
def test_ws_get_time(client):
client.ws_get_time()
def test_ws_get_exchange_info(client):
client.ws_get_exchange_info(symbol="BTCUSDT")
def test_time_unit_microseconds():
micro_client = Client(
api_key,
api_secret,
{"proxies": proxies},
testnet=testnet,
time_unit="MICROSECOND",
)
micro_trades = micro_client.get_recent_trades(symbol="BTCUSDT")
assert len(str(micro_trades[0]["time"])) >= 16, (
"Time should be in microseconds (16+ digits)"
)
def test_time_unit_milloseconds():
milli_client = Client(
api_key,
api_secret,
{"proxies": proxies},
testnet=testnet,
time_unit="MILLISECOND",
)
milli_trades = milli_client.get_recent_trades(symbol="BTCUSDT")
assert len(str(milli_trades[0]["time"])) == 13, (
"Time should be in milliseconds (13 digits)"
)
def test_handle_response(client):
# Test successful JSON response
mock_response = type('Response', (), {
'status_code': 200,
'text': '{"key": "value"}',
'json': lambda: {"key": "value"}
})
assert client._handle_response(mock_response) == {"key": "value"}
# Test empty response
mock_empty_response = type('Response', (), {
'status_code': 200,
'text': ''
})
assert client._handle_response(mock_empty_response) == {}
# Test invalid JSON response
mock_invalid_response = type('Response', (), {
'status_code': 200,
'text': 'invalid json',
'json': lambda: exec('raise ValueError()')
})
with pytest.raises(BinanceRequestException):
client._handle_response(mock_invalid_response)
# Test error status code
mock_error_response = type('Response', (), {
'status_code': 400,
'text': 'error message'
})
with pytest.raises(BinanceAPIException):
client._handle_response(mock_error_response)