Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions test/test_ws_private_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
from okx.websocket.WsPrivateAsync import WsPrivateAsync
from test.config import get_api_credentials

# Test constants
WS_PRIVATE_URL = "wss://wspap.okx.com:8443/ws/v5/private?brokerId=9999"
WS_BUSINESS_URL = "wss://wspap.okx.com:8443/ws/v5/business?brokerId=9999"


def privateCallback(message):
print("privateCallback", message)


async def main():
api_key, api_secret_key, passphrase, _ = get_api_credentials()
url = "wss://wspap.okx.com:8443/ws/v5/private?brokerId=9999"
ws = WsPrivateAsync(
apiKey=api_key,
passphrase=passphrase,
secretKey=api_secret_key,
url=url,
url=WS_PRIVATE_URL,
debug=True
)
await ws.start()
Expand Down Expand Up @@ -52,12 +55,11 @@ async def test_place_order():
URL: /ws/v5/private (Rate limit: 60 requests/second)
"""
api_key, api_secret_key, passphrase, _ = get_api_credentials()
url = "wss://wspap.okx.com:8443/ws/v5/private?brokerId=9999"
ws = WsPrivateAsync(
apiKey=api_key,
passphrase=passphrase,
secretKey=api_secret_key,
url=url,
url=WS_PRIVATE_URL,
debug=True
)
await ws.start()
Expand Down Expand Up @@ -85,12 +87,11 @@ async def test_batch_orders():
URL: /ws/v5/private (Rate limit: 60 requests/second, max 20 orders)
"""
api_key, api_secret_key, passphrase, _ = get_api_credentials()
url = "wss://wspap.okx.com:8443/ws/v5/private?brokerId=9999"
ws = WsPrivateAsync(
apiKey=api_key,
passphrase=passphrase,
secretKey=api_secret_key,
url=url,
url=WS_PRIVATE_URL,
debug=True
)
await ws.start()
Expand Down Expand Up @@ -129,12 +130,11 @@ async def test_cancel_order():
URL: /ws/v5/private (Rate limit: 60 requests/second)
"""
api_key, api_secret_key, passphrase, _ = get_api_credentials()
url = "wss://wspap.okx.com:8443/ws/v5/private?brokerId=9999"
ws = WsPrivateAsync(
apiKey=api_key,
passphrase=passphrase,
secretKey=api_secret_key,
url=url,
url=WS_PRIVATE_URL,
debug=True
)
await ws.start()
Expand All @@ -158,12 +158,11 @@ async def test_batch_cancel_orders():
URL: /ws/v5/private (Rate limit: 60 requests/second, max 20 orders)
"""
api_key, api_secret_key, passphrase, _ = get_api_credentials()
url = "wss://wspap.okx.com:8443/ws/v5/private?brokerId=9999"
ws = WsPrivateAsync(
apiKey=api_key,
passphrase=passphrase,
secretKey=api_secret_key,
url=url,
url=WS_PRIVATE_URL,
debug=True
)
await ws.start()
Expand All @@ -185,12 +184,11 @@ async def test_amend_order():
URL: /ws/v5/private (Rate limit: 60 requests/second)
"""
api_key, api_secret_key, passphrase, _ = get_api_credentials()
url = "wss://wspap.okx.com:8443/ws/v5/private?brokerId=9999"
ws = WsPrivateAsync(
apiKey=api_key,
passphrase=passphrase,
secretKey=api_secret_key,
url=url,
url=WS_PRIVATE_URL,
debug=True
)
await ws.start()
Expand All @@ -216,12 +214,11 @@ async def test_mass_cancel():
Note: This function uses the business channel
"""
api_key, api_secret_key, passphrase, _ = get_api_credentials()
url = "wss://wspap.okx.com:8443/ws/v5/business?brokerId=9999"
ws = WsPrivateAsync(
apiKey=api_key,
passphrase=passphrase,
secretKey=api_secret_key,
url=url,
url=WS_BUSINESS_URL,
debug=True
)
await ws.start()
Expand All @@ -241,12 +238,11 @@ async def test_mass_cancel():
async def test_send_method():
"""Test generic send method"""
api_key, api_secret_key, passphrase, _ = get_api_credentials()
url = "wss://wspap.okx.com:8443/ws/v5/private?brokerId=9999"
ws = WsPrivateAsync(
apiKey=api_key,
passphrase=passphrase,
secretKey=api_secret_key,
url=url,
url=WS_PRIVATE_URL,
debug=True
)
await ws.start()
Expand Down
11 changes: 7 additions & 4 deletions test/unit/okx/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from okx.Account import AccountAPI
from okx import consts as c

# Test constants
IDX_VOL_NEGATIVE_5_PERCENT = '-0.05'


class TestAccountAPIPositionBuilder(unittest.TestCase):
"""Unit tests for the position_builder method"""
Expand Down Expand Up @@ -100,11 +103,11 @@ def test_position_builder_negative_idxVol(self, mock_request):
mock_request.return_value = mock_response

# Act
result = self.account_api.position_builder(idxVol='-0.05')
result = self.account_api.position_builder(idxVol=IDX_VOL_NEGATIVE_5_PERCENT)

# Assert
expected_params = {
'idxVol': '-0.05'
'idxVol': IDX_VOL_NEGATIVE_5_PERCENT
}
mock_request.assert_called_once_with(c.POST, c.POSITION_BUILDER, expected_params)
self.assertEqual(result, mock_response)
Expand Down Expand Up @@ -326,7 +329,7 @@ def test_position_builder_complex_scenario(self, mock_request):
greeksType='PA',
simPos=sim_pos,
simAsset=sim_asset,
idxVol='-0.05'
idxVol=IDX_VOL_NEGATIVE_5_PERCENT
)

# Assert
Expand All @@ -337,7 +340,7 @@ def test_position_builder_complex_scenario(self, mock_request):
'greeksType': 'PA',
'simPos': sim_pos,
'simAsset': sim_asset,
'idxVol': '-0.05'
'idxVol': IDX_VOL_NEGATIVE_5_PERCENT
}
mock_request.assert_called_once_with(c.POST, c.POSITION_BUILDER, expected_params)
self.assertEqual(result['code'], '0')
Expand Down
39 changes: 22 additions & 17 deletions test/unit/okx/test_okxclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
import warnings
from unittest.mock import patch, MagicMock

# Test constants
MOCK_CLIENT_INIT = 'okx.okxclient.Client.__init__'
TEST_PROXY_URL = 'http://proxy.example.com:8080'
TEST_API_ENDPOINT = '/api/v5/test'


class TestOkxClientInit(unittest.TestCase):
"""Unit tests for OkxClient initialization"""

def test_init_with_default_parameters(self):
"""Test initialization with default parameters"""
with patch('okx.okxclient.Client.__init__') as mock_init:
with patch(MOCK_CLIENT_INIT) as mock_init:
mock_init.return_value = None

from okx.okxclient import OkxClient
Expand All @@ -27,7 +32,7 @@ def test_init_with_default_parameters(self):

def test_init_with_custom_parameters(self):
"""Test initialization with custom parameters"""
with patch('okx.okxclient.Client.__init__') as mock_init:
with patch(MOCK_CLIENT_INIT) as mock_init:
mock_init.return_value = None

from okx.okxclient import OkxClient
Expand All @@ -47,7 +52,7 @@ def test_init_with_custom_parameters(self):

def test_init_with_deprecated_use_server_time_shows_warning(self):
"""Test that using deprecated use_server_time parameter shows warning"""
with patch('okx.okxclient.Client.__init__') as mock_init:
with patch(MOCK_CLIENT_INIT) as mock_init:
mock_init.return_value = None

from okx.okxclient import OkxClient
Expand All @@ -66,18 +71,18 @@ class TestOkxClientHttpxCompatibility(unittest.TestCase):

def test_init_with_new_httpx_proxy_parameter(self):
"""Test initialization with new httpx version using proxy parameter"""
with patch('okx.okxclient.Client.__init__') as mock_init:
with patch(MOCK_CLIENT_INIT) as mock_init:
# Simulate new httpx version (accepts proxy parameter)
mock_init.return_value = None

from okx.okxclient import OkxClient
client = OkxClient(proxy='http://proxy.example.com:8080')
client = OkxClient(proxy=TEST_PROXY_URL)

# Should call super().__init__ with proxy parameter
mock_init.assert_called_once()
call_kwargs = mock_init.call_args
self.assertIn('proxy', call_kwargs.kwargs)
self.assertEqual(call_kwargs.kwargs['proxy'], 'http://proxy.example.com:8080')
self.assertEqual(call_kwargs.kwargs['proxy'], TEST_PROXY_URL)

def test_init_with_old_httpx_falls_back_to_proxies(self):
"""Test initialization falls back to proxies for old httpx version"""
Expand All @@ -91,11 +96,11 @@ def mock_init_side_effect(*args, **kwargs):
# Second call should work (with proxies or without)
return None

with patch('okx.okxclient.Client.__init__') as mock_init:
with patch(MOCK_CLIENT_INIT) as mock_init:
mock_init.side_effect = mock_init_side_effect

from okx.okxclient import OkxClient
client = OkxClient(proxy='http://proxy.example.com:8080')
client = OkxClient(proxy=TEST_PROXY_URL)

# Should have been called twice
self.assertEqual(mock_init.call_count, 2)
Expand All @@ -104,8 +109,8 @@ def mock_init_side_effect(*args, **kwargs):
second_call = mock_init.call_args_list[1]
self.assertIn('proxies', second_call.kwargs)
expected_proxies = {
'http://': 'http://proxy.example.com:8080',
'https://': 'http://proxy.example.com:8080'
'http://': TEST_PROXY_URL,
'https://': TEST_PROXY_URL
}
self.assertEqual(second_call.kwargs['proxies'], expected_proxies)

Expand All @@ -120,7 +125,7 @@ def mock_init_side_effect(*args, **kwargs):
raise TypeError("__init__() got an unexpected keyword argument 'proxy'")
return None

with patch('okx.okxclient.Client.__init__') as mock_init:
with patch(MOCK_CLIENT_INIT) as mock_init:
mock_init.side_effect = mock_init_side_effect

from okx.okxclient import OkxClient
Expand All @@ -135,7 +140,7 @@ def mock_init_side_effect(*args, **kwargs):

def test_init_without_proxy(self):
"""Test initialization without proxy parameter"""
with patch('okx.okxclient.Client.__init__') as mock_init:
with patch(MOCK_CLIENT_INIT) as mock_init:
mock_init.return_value = None

from okx.okxclient import OkxClient
Expand All @@ -151,7 +156,7 @@ class TestOkxClientRequest(unittest.TestCase):

def setUp(self):
"""Set up test fixtures"""
with patch('okx.okxclient.Client.__init__') as mock_init:
with patch(MOCK_CLIENT_INIT) as mock_init:
mock_init.return_value = None
from okx.okxclient import OkxClient
self.client = OkxClient(
Expand All @@ -166,19 +171,19 @@ def test_request_without_params(self):
with patch.object(self.client, '_request') as mock_request:
mock_request.return_value = {'code': '0'}

result = self.client._request_without_params('GET', '/api/v5/test')
result = self.client._request_without_params('GET', TEST_API_ENDPOINT)

mock_request.assert_called_once_with('GET', '/api/v5/test', {})
mock_request.assert_called_once_with('GET', TEST_API_ENDPOINT, {})

def test_request_with_params(self):
"""Test _request_with_params passes params correctly"""
with patch.object(self.client, '_request') as mock_request:
mock_request.return_value = {'code': '0'}
params = {'instId': 'BTC-USDT'}

result = self.client._request_with_params('GET', '/api/v5/test', params)
result = self.client._request_with_params('GET', TEST_API_ENDPOINT, params)

mock_request.assert_called_once_with('GET', '/api/v5/test', params)
mock_request.assert_called_once_with('GET', TEST_API_ENDPOINT, params)


if __name__ == '__main__':
Expand Down
Loading
Loading