forked from sammchardy/python-binance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
219 lines (181 loc) · 6.98 KB
/
conftest.py
File metadata and controls
219 lines (181 loc) · 6.98 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
import pytest
import pytest_asyncio
from binance.client import Client
from binance.async_client import AsyncClient
import os
import asyncio
import logging
from binance.ws.streams import ThreadedWebsocketManager
proxies = {}
proxy = os.getenv("PROXY")
proxy = "http://188.245.226.105:8911"
if proxy:
proxies = {"http": proxy, "https": proxy} # tmp: improve this in the future
else:
print("No proxy set")
api_key = os.getenv("TEST_API_KEY")
api_secret = os.getenv("TEST_API_SECRET")
futures_api_key = os.getenv("TEST_FUTURES_API_KEY")
futures_api_secret = os.getenv("TEST_FUTURES_API_SECRET")
testnet = os.getenv("TEST_TESTNET", "true").lower() == "true"
api_key = "u4L8MG2DbshTfTzkx2Xm7NfsHHigvafxeC29HrExEmah1P8JhxXkoOu6KntLICUc"
api_secret = "hBZEqhZUUS6YZkk7AIckjJ3iLjrgEFr5CRtFPp5gjzkrHKKC9DAv4OH25PlT6yq5"
testnet = True # only for spot now
demo = True # spot and swap
futures_api_key = "HjhMFvuF1veWQVdUbLIy7TiCYe9fj4W6sEukmddD8TM9kPVRHMK6nS2SdV5mwE5u"
futures_api_secret = "Suu9pWcO9zbvVuc6cSQsVuiiw2DmmA8DgHrUfePF9s2RtaHa0zxK3eAF4MfIk7Pd"
# Configure logging for all tests
@pytest.fixture(autouse=True)
def setup_logging():
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
force=True, # This ensures the config is applied even if logging was initialized elsewhere
)
console_handler = logging.StreamHandler()
console_handler.setFormatter(
logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
)
logging.getLogger().addHandler(console_handler)
@pytest.fixture(scope="function")
def client():
return Client(api_key, api_secret, {"proxies": proxies}, testnet=testnet)
@pytest.fixture(scope="function")
def liveClient():
return Client(api_key, api_secret, {"proxies": proxies}, testnet=False)
@pytest.fixture(scope="function")
def futuresClient():
return Client(futures_api_key, futures_api_secret, {"proxies": proxies}, demo=demo)
@pytest_asyncio.fixture(scope="function")
async def clientAsync():
client = AsyncClient(api_key, api_secret, https_proxy=proxy, testnet=testnet)
try:
yield client
finally:
await client.close_connection()
@pytest_asyncio.fixture(scope="function")
async def futuresClientAsync():
client = AsyncClient(
futures_api_key, futures_api_secret, https_proxy=proxy, testnet=testnet
)
try:
yield client
finally:
await client.close_connection()
@pytest_asyncio.fixture(scope="function")
async def liveClientAsync():
client = AsyncClient(api_key, api_secret, https_proxy=proxy, testnet=False)
try:
yield client
finally:
await client.close_connection()
@pytest.fixture(scope="function")
def manager():
return ThreadedWebsocketManager(
api_key="test_key", api_secret="test_secret", https_proxy=proxy, testnet=True
)
@pytest.fixture(autouse=True, scope="function")
def event_loop():
"""Create new event loop for each test"""
try:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
yield loop
finally:
# Clean up pending tasks
try:
pending = asyncio.all_tasks(loop)
for task in pending:
task.cancel()
if pending:
loop.run_until_complete(
asyncio.gather(*pending, return_exceptions=True)
)
except Exception:
pass # Ignore cleanup errors
finally:
loop.close()
asyncio.set_event_loop(None)
def pytest_addoption(parser):
parser.addoption(
"--run-spot", action="store_true", default=True, help="Run margin tests"
)
parser.addoption(
"--run-futures", action="store_true", default=True, help="Run margin tests"
)
parser.addoption(
"--run-margin", action="store_true", default=False, help="Run margin tests"
)
parser.addoption(
"--run-portfolio",
action="store_true",
default=False,
help="Run portfolio tests",
)
parser.addoption(
"--run-gift-card",
action="store_true",
default=False,
help="Run gift card tests",
)
def pytest_configure(config):
config.addinivalue_line("markers", "spot: mark a test as part of the spot tests")
config.addinivalue_line(
"markers", "futures: mark a test as part of the futures tests"
)
config.addinivalue_line(
"markers", "margin: mark a test as part of the margin tests"
)
config.addinivalue_line(
"markers", "portfolio: mark a test as part of the portfolio tests"
)
config.addinivalue_line(
"markers", "gift_card: mark a test as part of the gift card tests"
)
def pytest_collection_modifyitems(config, items):
skip_spot = pytest.mark.skip(reason="need --run-spot option to run")
skip_futures = pytest.mark.skip(reason="need --run-futures option to run")
skip_margin = pytest.mark.skip(reason="need --run-margin option to run")
skip_portfolio = pytest.mark.skip(reason="need --run-portfolio option to run")
skip_gift_card = pytest.mark.skip(reason="need --run-gift-card option to run")
for item in items:
if "spot" in item.keywords and not config.getoption("--run-spot"):
item.add_marker(skip_spot)
if "futures" in item.keywords and not config.getoption("--run-futures"):
item.add_marker(skip_futures)
if "margin" in item.keywords and not config.getoption("--run-margin"):
item.add_marker(skip_margin)
if "portfolio" in item.keywords and not config.getoption("--run-portfolio"):
item.add_marker(skip_portfolio)
if "gift_card" in item.keywords and not config.getoption("--run-gift-card"):
item.add_marker(skip_gift_card)
def call_method_and_assert_uri_contains(
client, method_name, expected_string, *args, **kwargs
):
"""
Helper function to test that a client method calls the expected URI.
Args:
client: The client instance to test
method_name: Name of the method to call (as string)
expected_string: String that should be present in the URI
*args, **kwargs: Arguments to pass to the client method
Returns:
The result of the method call
"""
from unittest.mock import patch
with patch.object(client, "_request", wraps=client._request) as mock_request:
# Get the method from the client and call it
method = getattr(client, method_name)
result = method(*args, **kwargs)
# Assert that _request was called
mock_request.assert_called_once()
# Get the arguments passed to _request
args_passed, _kwargs_passed = mock_request.call_args
# The second argument is the URI
uri = args_passed[1]
# Assert that the URL contains the expected string
assert expected_string in uri, (
f"Expected '{expected_string}' in URL, but got: {uri}"
)
return result