Skip to content

Commit 8429575

Browse files
committed
fix tests
1 parent 9f4caae commit 8429575

10 files changed

Lines changed: 69 additions & 401 deletions

tests/conftest.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,16 @@ def manager():
9090
api_key="test_key", api_secret="test_secret", https_proxy=proxy, testnet=True
9191
)
9292

93-
@pytest_asyncio.fixture(autouse=True, scope="function")
94-
async def event_loop():
93+
@pytest.fixture(autouse=True, scope="function")
94+
def event_loop():
9595
"""Create new event loop for each test"""
9696
loop = asyncio.new_event_loop()
97-
asyncio.set_event_loop(loop)
9897
yield loop
9998
# Clean up pending tasks
10099
pending = asyncio.all_tasks(loop)
101100
for task in pending:
102101
task.cancel()
103-
# Wait for all tasks to complete
104-
await asyncio.gather(*pending, return_exceptions=True)
105-
# Run the loop one more time to ensure all cleanup is done
106-
await asyncio.sleep(0)
102+
loop.run_until_complete(asyncio.gather(*pending, return_exceptions=True))
107103
loop.close()
108104

109105

tests/test_async_client.py

Lines changed: 49 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -18,117 +18,94 @@ async def test_clientAsync_initialization(clientAsync):
1818
@pytest.mark.skip(reason="Endpoint not documented")
1919
async def test_get_products(clientAsync):
2020
await clientAsync.get_products()
21-
21+
2222

2323
async def test_get_exchange_info(clientAsync):
2424
await clientAsync.get_exchange_info()
25-
26-
25+
2726
async def test_get_symbol_info(clientAsync):
2827
await clientAsync.get_symbol_info("BTCUSDT")
29-
28+
3029

3130
async def test_ping(clientAsync):
3231
await clientAsync.ping()
33-
34-
32+
3533
async def test_get_server_time(clientAsync):
3634
await clientAsync.get_server_time()
37-
38-
35+
3936
async def test_get_all_tickers(clientAsync):
4037
await clientAsync.get_all_tickers()
4138

42-
4339
async def test_get_orderbook_tickers(clientAsync):
4440
await clientAsync.get_orderbook_tickers()
45-
46-
41+
4742
async def test_get_order_book(clientAsync):
4843
await clientAsync.get_order_book(symbol="BTCUSDT")
49-
50-
44+
5145
async def test_get_recent_trades(clientAsync):
5246
await clientAsync.get_recent_trades(symbol="BTCUSDT")
5347

5448

5549
async def test_get_historical_trades(clientAsync):
5650
await clientAsync.get_historical_trades(symbol="BTCUSDT")
57-
51+
5852

5953
async def test_get_aggregate_trades(clientAsync):
6054
await clientAsync.get_aggregate_trades(symbol="BTCUSDT")
61-
62-
55+
6356
async def test_get_klines(clientAsync):
6457
await clientAsync.get_klines(symbol="BTCUSDT", interval="1d")
65-
66-
58+
6759
async def test_get_uiklines(clientAsync):
6860
await clientAsync.get_ui_klines(symbol="BTCUSDT", interval="1d")
69-
70-
61+
7162
async def test_futures_mark_price_klines(clientAsync):
7263
await clientAsync.futures_mark_price_klines(symbol="BTCUSDT", interval="1h")
73-
74-
64+
7565
async def test_futures_index_price_klines(clientAsync):
7666
await clientAsync.futures_index_price_klines(pair="BTCUSDT", interval="1h")
77-
78-
67+
7968
async def test_futures_premium_index_klines(clientAsync):
8069
await clientAsync.futures_premium_index_klines(symbol="BTCUSDT", interval="1h")
81-
82-
70+
8371
@pytest.mark.skip(reason="network error")
8472
async def test_futures_coin_premium_index_klines(clientAsync):
8573
await clientAsync.futures_coin_premium_index_klines(symbol="BTCUSD", interval="1h")
86-
87-
74+
8875
async def test_get_avg_price(clientAsync):
8976
await clientAsync.get_avg_price(symbol="BTCUSDT")
90-
91-
77+
9278
async def test_get_ticker(clientAsync):
9379
await clientAsync.get_ticker(symbol="BTCUSDT")
94-
95-
80+
9681
async def test_get_symbol_ticker(clientAsync):
9782
await clientAsync.get_symbol_ticker(symbol="BTCUSDT")
98-
99-
83+
10084
async def test_get_orderbook_ticker(clientAsync):
10185
await clientAsync.get_orderbook_ticker(symbol="BTCUSDT")
102-
103-
86+
10487
async def test_get_account(clientAsync):
10588
await clientAsync.get_account()
106-
107-
89+
10890
async def test_get_asset_balance(clientAsync):
10991
await clientAsync.get_asset_balance(asset="BTC")
110-
111-
92+
11293
async def test_get_asset_balance_no_asset_provided(clientAsync):
11394
await clientAsync.get_asset_balance()
114-
115-
95+
11696
async def test_get_my_trades(clientAsync):
11797
await clientAsync.get_my_trades(symbol="BTCUSDT")
118-
119-
98+
12099
async def test_get_system_status(clientAsync):
121100
await clientAsync.get_system_status()
122-
123-
101+
124102
# User Stream Endpoints
125103

126104

127105
async def test_stream_get_listen_key_and_close(clientAsync):
128106
listen_key = await clientAsync.stream_get_listen_key()
129107
await clientAsync.stream_close(listen_key)
130-
131-
108+
132109
# Quoting interface endpoints
133110

134111

@@ -139,76 +116,62 @@ async def test_stream_get_listen_key_and_close(clientAsync):
139116

140117
async def test_ws_get_order_book(clientAsync):
141118
await clientAsync.ws_get_order_book(symbol="BTCUSDT")
142-
143-
119+
144120
async def test_ws_get_recent_trades(clientAsync):
145121
await clientAsync.ws_get_recent_trades(symbol="BTCUSDT")
146-
147-
122+
148123
async def test_ws_get_historical_trades(clientAsync):
149124
await clientAsync.ws_get_historical_trades(symbol="BTCUSDT")
150-
151-
125+
152126
async def test_ws_get_aggregate_trades(clientAsync):
153127
await clientAsync.ws_get_aggregate_trades(symbol="BTCUSDT")
154-
155-
128+
156129
async def test_ws_get_klines(clientAsync):
157130
await clientAsync.ws_get_klines(symbol="BTCUSDT", interval="1m")
158-
159-
131+
160132
async def test_ws_get_uiKlines(clientAsync):
161133
await clientAsync.ws_get_uiKlines(symbol="BTCUSDT", interval="1m")
162-
163-
134+
164135
async def test_ws_get_avg_price(clientAsync):
165136
await clientAsync.ws_get_avg_price(symbol="BTCUSDT")
166-
167-
137+
168138
async def test_ws_get_ticker(clientAsync):
169139
ticker = await clientAsync.ws_get_ticker(symbol="BTCUSDT")
170-
171-
140+
172141
async def test_ws_get_trading_day_ticker(clientAsync):
173142
await clientAsync.ws_get_trading_day_ticker(symbol="BTCUSDT")
174-
175-
143+
176144
async def test_ws_get_symbol_ticker_window(clientAsync):
177145
await clientAsync.ws_get_symbol_ticker_window(symbol="BTCUSDT")
178-
179-
146+
180147
async def test_ws_get_symbol_ticker(clientAsync):
181148
await clientAsync.ws_get_symbol_ticker(symbol="BTCUSDT")
182-
183-
149+
184150
async def test_ws_get_orderbook_ticker(clientAsync):
185151
await clientAsync.ws_get_orderbook_ticker(symbol="BTCUSDT")
186-
187-
152+
188153
async def test_ws_ping(clientAsync):
189154
await clientAsync.ws_ping()
190-
191-
155+
192156
async def test_ws_get_time(clientAsync):
193157
await clientAsync.ws_get_time()
194-
195-
158+
196159
async def test_ws_get_exchange_info(clientAsync):
197160
await clientAsync.ws_get_exchange_info(symbol="BTCUSDT")
198-
161+
199162
@pytest.mark.skip(reason="can't test margin endpoints")
200163
async def test_margin_next_hourly_interest_rate(clientAsync):
201164
await clientAsync.margin_next_hourly_interest_rate(
202165
assets="BTC",
203166
isIsolated="FALSE"
204167
)
205-
168+
206169
@pytest.mark.skip(reason="can't test margin endpoints")
207170
async def test_margin_interest_history(clientAsync):
208171
await clientAsync.margin_interest_history(
209172
asset="BTC",
210173
)
211-
174+
212175
@pytest.mark.skip(reason="can't test margin endpoints")
213176
async def test_margin_borrow_repay(clientAsync):
214177
await clientAsync.margin_borrow_repay(
@@ -218,27 +181,26 @@ async def test_margin_borrow_repay(clientAsync):
218181
symbol="BTCUSDT",
219182
type="BORROW"
220183
)
221-
184+
222185
@pytest.mark.skip(reason="can't test margin endpoints")
223186
async def test_margin_get_borrow_repay_records(clientAsync):
224187
await clientAsync.margin_get_borrow_repay_records(
225188
asset="BTC",
226189
isolatedSymbol="BTCUSDT",
227190
)
228-
191+
229192
@pytest.mark.skip(reason="can't test margin endpoints")
230193
async def test_margin_interest_rate_history(clientAsync):
231194
await clientAsync.margin_interest_rate_history(
232195
asset="BTC",
233196
)
234-
197+
235198
@pytest.mark.skip(reason="can't test margin endpoints")
236199
async def test_margin_max_borrowable(clientAsync):
237200
await clientAsync.margin_max_borrowable(
238201
asset="BTC",
239202
)
240-
241-
203+
242204
async def test_time_unit_microseconds():
243205
micro_client = AsyncClient(
244206
api_key, api_secret, https_proxy=proxy, testnet=testnet, time_unit="MICROSECOND"
@@ -247,7 +209,7 @@ async def test_time_unit_microseconds():
247209
assert len(str(micro_trades[0]["time"])) >= 16, (
248210
"Time should be in microseconds (16+ digits)"
249211
)
250-
212+
await micro_client.close_connection()
251213

252214
async def test_time_unit_milloseconds():
253215
milli_client = AsyncClient(
@@ -257,6 +219,7 @@ async def test_time_unit_milloseconds():
257219
assert len(str(milli_trades[0]["time"])) == 13, (
258220
"Time should be in milliseconds (13 digits)"
259221
)
222+
await milli_client.close_connection()
260223

261224

262225
async def test_handle_response(clientAsync):
@@ -295,3 +258,4 @@ async def test_handle_response(clientAsync):
295258
mock_response._body = b'error message'
296259
with pytest.raises(BinanceAPIException):
297260
await clientAsync._handle_response(mock_response)
261+

0 commit comments

Comments
 (0)