diff --git a/test/test_ws_private_async.py b/test/test_ws_private_async.py index 10f34bf..082f61a 100644 --- a/test/test_ws_private_async.py +++ b/test/test_ws_private_async.py @@ -3,6 +3,10 @@ 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) @@ -10,12 +14,11 @@ def 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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() diff --git a/test/unit/okx/test_account.py b/test/unit/okx/test_account.py index 57f949d..949a189 100644 --- a/test/unit/okx/test_account.py +++ b/test/unit/okx/test_account.py @@ -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""" @@ -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) @@ -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 @@ -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') diff --git a/test/unit/okx/test_okxclient.py b/test/unit/okx/test_okxclient.py index 463507b..da8f900 100644 --- a/test/unit/okx/test_okxclient.py +++ b/test/unit/okx/test_okxclient.py @@ -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 @@ -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 @@ -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 @@ -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""" @@ -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) @@ -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) @@ -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 @@ -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 @@ -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( @@ -166,9 +171,9 @@ 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""" @@ -176,9 +181,9 @@ def test_request_with_params(self): 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__': diff --git a/test/unit/okx/websocket/test_ws_private_async.py b/test/unit/okx/websocket/test_ws_private_async.py index 531f5a8..9641909 100644 --- a/test/unit/okx/websocket/test_ws_private_async.py +++ b/test/unit/okx/websocket/test_ws_private_async.py @@ -13,6 +13,10 @@ import okx.websocket.WsPrivateAsync as ws_private_module from okx.websocket.WsPrivateAsync import WsPrivateAsync +# Test constants +TEST_WS_URL = 'wss://test.example.com' +MOCK_WS_FACTORY = 'okx.websocket.WsPrivateAsync.WebSocketFactory' + class TestWsPrivateAsyncInit(unittest.TestCase): """Unit tests for WsPrivateAsync initialization""" @@ -24,26 +28,26 @@ def test_init_with_required_params(self): apiKey="test_api_key", passphrase="test_passphrase", secretKey="test_secret_key", - url="wss://test.example.com" + url=TEST_WS_URL ) self.assertEqual(ws.apiKey, "test_api_key") self.assertEqual(ws.passphrase, "test_passphrase") self.assertEqual(ws.secretKey, "test_secret_key") - self.assertEqual(ws.url, "wss://test.example.com") + self.assertEqual(ws.url, TEST_WS_URL) self.assertFalse(ws.useServerTime) self.assertFalse(ws.debug) - mock_factory.assert_called_once_with("wss://test.example.com") + mock_factory.assert_called_once_with(TEST_WS_URL) def test_init_with_debug_enabled(self): """Test initialization with debug mode enabled""" - with patch('okx.websocket.WsPrivateAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): from okx.websocket.WsPrivateAsync import WsPrivateAsync ws = WsPrivateAsync( apiKey="test_api_key", passphrase="test_passphrase", secretKey="test_secret_key", - url="wss://test.example.com", + url=TEST_WS_URL, debug=True ) @@ -51,7 +55,7 @@ def test_init_with_debug_enabled(self): def test_init_with_deprecated_useServerTime_shows_warning(self): """Test that using deprecated useServerTime parameter shows warning""" - with patch('okx.websocket.WsPrivateAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): from okx.websocket.WsPrivateAsync import WsPrivateAsync with warnings.catch_warnings(record=True) as w: @@ -60,7 +64,7 @@ def test_init_with_deprecated_useServerTime_shows_warning(self): apiKey="test_api_key", passphrase="test_passphrase", secretKey="test_secret_key", - url="wss://test.example.com", + url=TEST_WS_URL, useServerTime=True ) @@ -70,7 +74,7 @@ def test_init_with_deprecated_useServerTime_shows_warning(self): def test_init_without_useServerTime_no_warning(self): """Test that not using useServerTime parameter shows no warning""" - with patch('okx.websocket.WsPrivateAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): from okx.websocket.WsPrivateAsync import WsPrivateAsync with warnings.catch_warnings(record=True) as w: @@ -79,7 +83,7 @@ def test_init_without_useServerTime_no_warning(self): apiKey="test_api_key", passphrase="test_passphrase", secretKey="test_secret_key", - url="wss://test.example.com" + url=TEST_WS_URL ) # No deprecation warning expected @@ -102,7 +106,7 @@ def test_subscribe_sends_correct_payload(self): apiKey="test_api_key", passphrase="test_passphrase", secretKey="test_secret_key", - url="wss://test.example.com" + url=TEST_WS_URL ) mock_websocket = AsyncMock() ws.websocket = mock_websocket @@ -133,7 +137,7 @@ def test_subscribe_with_id(self): apiKey="test_api_key", passphrase="test_passphrase", secretKey="test_secret_key", - url="wss://test.example.com" + url=TEST_WS_URL ) mock_websocket = AsyncMock() ws.websocket = mock_websocket @@ -161,7 +165,7 @@ def test_unsubscribe_sends_correct_payload(self): apiKey="test_api_key", passphrase="test_passphrase", secretKey="test_secret_key", - url="wss://test.example.com" + url=TEST_WS_URL ) mock_websocket = AsyncMock() ws.websocket = mock_websocket @@ -185,7 +189,7 @@ def test_unsubscribe_with_id(self): apiKey="test_api_key", passphrase="test_passphrase", secretKey="test_secret_key", - url="wss://test.example.com" + url=TEST_WS_URL ) mock_websocket = AsyncMock() ws.websocket = mock_websocket @@ -207,13 +211,13 @@ class TestWsPrivateAsyncSend(unittest.TestCase): def test_send_without_id(self): """Test generic send method without id""" - with patch('okx.websocket.WsPrivateAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): from okx.websocket.WsPrivateAsync import WsPrivateAsync ws = WsPrivateAsync( apiKey="test_api_key", passphrase="test_passphrase", secretKey="test_secret_key", - url="wss://test.example.com" + url=TEST_WS_URL ) mock_websocket = AsyncMock() ws.websocket = mock_websocket @@ -233,13 +237,13 @@ async def run_test(): def test_send_with_id(self): """Test generic send method with id""" - with patch('okx.websocket.WsPrivateAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): from okx.websocket.WsPrivateAsync import WsPrivateAsync ws = WsPrivateAsync( apiKey="test_api_key", passphrase="test_passphrase", secretKey="test_secret_key", - url="wss://test.example.com" + url=TEST_WS_URL ) mock_websocket = AsyncMock() ws.websocket = mock_websocket @@ -260,13 +264,13 @@ class TestWsPrivateAsyncOrderMethods(unittest.TestCase): def _create_ws_instance(self): """Helper to create WsPrivateAsync instance with mocked websocket""" - with patch('okx.websocket.WsPrivateAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): from okx.websocket.WsPrivateAsync import WsPrivateAsync ws = WsPrivateAsync( apiKey="test_api_key", passphrase="test_passphrase", secretKey="test_secret_key", - url="wss://test.example.com" + url=TEST_WS_URL ) mock_websocket = AsyncMock() ws.websocket = mock_websocket @@ -274,7 +278,7 @@ def _create_ws_instance(self): def test_place_order_sends_correct_payload(self): """Test place_order sends correct operation""" - with patch('okx.websocket.WsPrivateAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): ws, mock_websocket = self._create_ws_instance() callback = MagicMock() order_args = [{ @@ -299,7 +303,7 @@ async def run_test(): def test_place_order_without_id(self): """Test place_order without id parameter""" - with patch('okx.websocket.WsPrivateAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): ws, mock_websocket = self._create_ws_instance() order_args = [{"instId": "BTC-USDT"}] @@ -314,7 +318,7 @@ async def run_test(): def test_batch_orders_sends_correct_payload(self): """Test batch_orders sends correct operation""" - with patch('okx.websocket.WsPrivateAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): ws, mock_websocket = self._create_ws_instance() callback = MagicMock() order_args = [ @@ -334,7 +338,7 @@ async def run_test(): def test_batch_orders_without_id(self): """Test batch_orders without id parameter""" - with patch('okx.websocket.WsPrivateAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): ws, mock_websocket = self._create_ws_instance() order_args = [{"instId": "BTC-USDT"}, {"instId": "ETH-USDT"}] @@ -349,7 +353,7 @@ async def run_test(): def test_cancel_order_sends_correct_payload(self): """Test cancel_order sends correct operation""" - with patch('okx.websocket.WsPrivateAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): ws, mock_websocket = self._create_ws_instance() callback = MagicMock() cancel_args = [{"instId": "BTC-USDT", "ordId": "12345"}] @@ -366,7 +370,7 @@ async def run_test(): def test_cancel_order_without_id(self): """Test cancel_order without id parameter""" - with patch('okx.websocket.WsPrivateAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): ws, mock_websocket = self._create_ws_instance() cancel_args = [{"instId": "BTC-USDT", "ordId": "12345"}] @@ -381,7 +385,7 @@ async def run_test(): def test_batch_cancel_orders_sends_correct_payload(self): """Test batch_cancel_orders sends correct operation""" - with patch('okx.websocket.WsPrivateAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): ws, mock_websocket = self._create_ws_instance() callback = MagicMock() cancel_args = [ @@ -401,7 +405,7 @@ async def run_test(): def test_batch_cancel_orders_without_id(self): """Test batch_cancel_orders without id parameter""" - with patch('okx.websocket.WsPrivateAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): ws, mock_websocket = self._create_ws_instance() cancel_args = [{"instId": "BTC-USDT", "ordId": "12345"}] @@ -416,7 +420,7 @@ async def run_test(): def test_amend_order_sends_correct_payload(self): """Test amend_order sends correct operation""" - with patch('okx.websocket.WsPrivateAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): ws, mock_websocket = self._create_ws_instance() callback = MagicMock() amend_args = [{ @@ -438,7 +442,7 @@ async def run_test(): def test_amend_order_without_id(self): """Test amend_order without id parameter""" - with patch('okx.websocket.WsPrivateAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): ws, mock_websocket = self._create_ws_instance() amend_args = [{"instId": "BTC-USDT", "ordId": "12345", "newSz": "0.002"}] @@ -453,7 +457,7 @@ async def run_test(): def test_batch_amend_orders_sends_correct_payload(self): """Test batch_amend_orders sends correct operation""" - with patch('okx.websocket.WsPrivateAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): ws, mock_websocket = self._create_ws_instance() callback = MagicMock() amend_args = [ @@ -473,7 +477,7 @@ async def run_test(): def test_batch_amend_orders_without_id(self): """Test batch_amend_orders without id parameter""" - with patch('okx.websocket.WsPrivateAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): ws, mock_websocket = self._create_ws_instance() amend_args = [{"instId": "BTC-USDT", "ordId": "12345", "newSz": "0.002"}] @@ -488,7 +492,7 @@ async def run_test(): def test_mass_cancel_sends_correct_payload(self): """Test mass_cancel sends correct operation""" - with patch('okx.websocket.WsPrivateAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): ws, mock_websocket = self._create_ws_instance() callback = MagicMock() mass_cancel_args = [{ @@ -508,7 +512,7 @@ async def run_test(): def test_mass_cancel_without_id(self): """Test mass_cancel without id parameter""" - with patch('okx.websocket.WsPrivateAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): ws, mock_websocket = self._create_ws_instance() mass_cancel_args = [{"instType": "SPOT", "instFamily": "BTC-USDT"}] @@ -536,7 +540,7 @@ def test_login_calls_init_login_params(self): apiKey="test_api_key", passphrase="test_passphrase", secretKey="test_secret_key", - url="wss://test.example.com" + url=TEST_WS_URL ) mock_websocket = AsyncMock() ws.websocket = mock_websocket @@ -568,7 +572,7 @@ def test_stop(self): apiKey="test_api_key", passphrase="test_passphrase", secretKey="test_secret_key", - url="wss://test.example.com" + url=TEST_WS_URL ) async def run_test(): diff --git a/test/unit/okx/websocket/test_ws_public_async.py b/test/unit/okx/websocket/test_ws_public_async.py index ca27b38..86bef23 100644 --- a/test/unit/okx/websocket/test_ws_public_async.py +++ b/test/unit/okx/websocket/test_ws_public_async.py @@ -12,6 +12,10 @@ import okx.websocket.WsPublicAsync as ws_public_module from okx.websocket.WsPublicAsync import WsPublicAsync +# Test constants +TEST_WS_URL = 'wss://test.example.com' +MOCK_WS_FACTORY = 'okx.websocket.WsPublicAsync.WebSocketFactory' + class TestWsPublicAsyncInit(unittest.TestCase): """Unit tests for WsPublicAsync initialization""" @@ -19,9 +23,9 @@ class TestWsPublicAsyncInit(unittest.TestCase): def test_init_with_url(self): """Test initialization with url parameter""" with patch.object(ws_public_module, 'WebSocketFactory') as mock_factory: - ws = WsPublicAsync(url="wss://test.example.com") + ws = WsPublicAsync(url=TEST_WS_URL) - self.assertEqual(ws.url, "wss://test.example.com") + self.assertEqual(ws.url, TEST_WS_URL) self.assertEqual(ws.apiKey, '') self.assertEqual(ws.passphrase, '') self.assertEqual(ws.secretKey, '') @@ -30,10 +34,10 @@ def test_init_with_url(self): def test_init_with_credentials(self): """Test initialization with all credentials for business channel""" - with patch('okx.websocket.WsPublicAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): from okx.websocket.WsPublicAsync import WsPublicAsync ws = WsPublicAsync( - url="wss://test.example.com", + url=TEST_WS_URL, apiKey="test_api_key", passphrase="test_passphrase", secretKey="test_secret_key" @@ -45,17 +49,17 @@ def test_init_with_credentials(self): def test_init_with_debug_enabled(self): """Test initialization with debug mode enabled""" - with patch('okx.websocket.WsPublicAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): from okx.websocket.WsPublicAsync import WsPublicAsync - ws = WsPublicAsync(url="wss://test.example.com", debug=True) + ws = WsPublicAsync(url=TEST_WS_URL, debug=True) self.assertTrue(ws.debug) def test_init_with_debug_disabled(self): """Test initialization with debug mode disabled (default)""" - with patch('okx.websocket.WsPublicAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): from okx.websocket.WsPublicAsync import WsPublicAsync - ws = WsPublicAsync(url="wss://test.example.com", debug=False) + ws = WsPublicAsync(url=TEST_WS_URL, debug=False) self.assertFalse(ws.debug) @@ -65,9 +69,9 @@ class TestWsPublicAsyncLogin(unittest.TestCase): def test_login_without_credentials_raises_error(self): """Test that login raises ValueError when credentials are missing""" - with patch('okx.websocket.WsPublicAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): from okx.websocket.WsPublicAsync import WsPublicAsync - ws = WsPublicAsync(url="wss://test.example.com") + ws = WsPublicAsync(url=TEST_WS_URL) async def run_test(): with self.assertRaises(ValueError) as context: @@ -78,14 +82,14 @@ async def run_test(): def test_login_with_credentials_success(self): """Test successful login with valid credentials""" - with patch('okx.websocket.WsPublicAsync.WebSocketFactory') as mock_factory, \ + with patch(MOCK_WS_FACTORY) as mock_factory, \ patch('okx.websocket.WsPublicAsync.WsUtils.initLoginParams') as mock_init_login: mock_init_login.return_value = '{"op":"login","args":[...]}' from okx.websocket.WsPublicAsync import WsPublicAsync ws = WsPublicAsync( - url="wss://test.example.com", + url=TEST_WS_URL, apiKey="test_api_key", passphrase="test_passphrase", secretKey="test_secret_key" @@ -113,9 +117,9 @@ class TestWsPublicAsyncSubscribe(unittest.TestCase): def test_subscribe_without_id(self): """Test subscribe without id parameter""" - with patch('okx.websocket.WsPublicAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): from okx.websocket.WsPublicAsync import WsPublicAsync - ws = WsPublicAsync(url="wss://test.example.com") + ws = WsPublicAsync(url=TEST_WS_URL) mock_websocket = AsyncMock() ws.websocket = mock_websocket callback = MagicMock() @@ -138,7 +142,7 @@ async def run_test(): def test_subscribe_with_id(self): """Test subscribe with id parameter""" with patch.object(ws_public_module, 'WebSocketFactory'): - ws = WsPublicAsync(url="wss://test.example.com") + ws = WsPublicAsync(url=TEST_WS_URL) mock_websocket = AsyncMock() ws.websocket = mock_websocket callback = MagicMock() @@ -159,7 +163,7 @@ async def run_test(): def test_subscribe_with_multiple_channels(self): """Test subscribe with multiple channels""" with patch.object(ws_public_module, 'WebSocketFactory'): - ws = WsPublicAsync(url="wss://test.example.com") + ws = WsPublicAsync(url=TEST_WS_URL) mock_websocket = AsyncMock() ws.websocket = mock_websocket callback = MagicMock() @@ -184,7 +188,7 @@ class TestWsPublicAsyncUnsubscribe(unittest.TestCase): def test_unsubscribe_without_id(self): """Test unsubscribe without id parameter""" with patch.object(ws_public_module, 'WebSocketFactory'): - ws = WsPublicAsync(url="wss://test.example.com") + ws = WsPublicAsync(url=TEST_WS_URL) mock_websocket = AsyncMock() ws.websocket = mock_websocket callback = MagicMock() @@ -203,7 +207,7 @@ async def run_test(): def test_unsubscribe_with_id(self): """Test unsubscribe with id parameter""" with patch.object(ws_public_module, 'WebSocketFactory'): - ws = WsPublicAsync(url="wss://test.example.com") + ws = WsPublicAsync(url=TEST_WS_URL) mock_websocket = AsyncMock() ws.websocket = mock_websocket callback = MagicMock() @@ -224,9 +228,9 @@ class TestWsPublicAsyncSend(unittest.TestCase): def test_send_without_id(self): """Test generic send method without id""" - with patch('okx.websocket.WsPublicAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): from okx.websocket.WsPublicAsync import WsPublicAsync - ws = WsPublicAsync(url="wss://test.example.com") + ws = WsPublicAsync(url=TEST_WS_URL) mock_websocket = AsyncMock() ws.websocket = mock_websocket callback = MagicMock() @@ -245,9 +249,9 @@ async def run_test(): def test_send_with_id(self): """Test generic send method with id""" - with patch('okx.websocket.WsPublicAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): from okx.websocket.WsPublicAsync import WsPublicAsync - ws = WsPublicAsync(url="wss://test.example.com") + ws = WsPublicAsync(url=TEST_WS_URL) mock_websocket = AsyncMock() ws.websocket = mock_websocket args = [{"instId": "BTC-USDT"}] @@ -263,9 +267,9 @@ async def run_test(): def test_send_without_callback(self): """Test send method without callback (preserves existing callback)""" - with patch('okx.websocket.WsPublicAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): from okx.websocket.WsPublicAsync import WsPublicAsync - ws = WsPublicAsync(url="wss://test.example.com") + ws = WsPublicAsync(url=TEST_WS_URL) mock_websocket = AsyncMock() ws.websocket = mock_websocket existing_callback = MagicMock() @@ -281,9 +285,9 @@ async def run_test(): def test_send_with_new_callback_replaces_existing(self): """Test send method with new callback replaces existing callback""" - with patch('okx.websocket.WsPublicAsync.WebSocketFactory'): + with patch(MOCK_WS_FACTORY): from okx.websocket.WsPublicAsync import WsPublicAsync - ws = WsPublicAsync(url="wss://test.example.com") + ws = WsPublicAsync(url=TEST_WS_URL) mock_websocket = AsyncMock() ws.websocket = mock_websocket old_callback = MagicMock() @@ -308,7 +312,7 @@ def test_stop(self): mock_factory_instance.close = AsyncMock() mock_factory_class.return_value = mock_factory_instance - ws = WsPublicAsync(url="wss://test.example.com") + ws = WsPublicAsync(url=TEST_WS_URL) async def run_test(): await ws.stop()