88import asyncio
99from unittest .mock import patch , MagicMock , AsyncMock
1010
11+ # Import the module first so patch can resolve the path
12+ import okx .websocket .WsPrivateAsync as ws_private_module
13+ from okx .websocket .WsPrivateAsync import WsPrivateAsync
14+
1115
1216class TestWsPrivateAsyncInit (unittest .TestCase ):
1317 """Unit tests for WsPrivateAsync initialization"""
1418
1519 def test_init_with_required_params (self ):
1620 """Test initialization with required parameters"""
17- with patch ('okx.websocket.WsPrivateAsync.WebSocketFactory' ) as mock_factory :
18- from okx .websocket .WsPrivateAsync import WsPrivateAsync
21+ with patch .object (ws_private_module , 'WebSocketFactory' ) as mock_factory :
1922 ws = WsPrivateAsync (
2023 apiKey = "test_api_key" ,
2124 passphrase = "test_passphrase" ,
@@ -37,13 +40,12 @@ class TestWsPrivateAsyncSubscribe(unittest.TestCase):
3740
3841 def test_subscribe_sends_correct_payload (self ):
3942 """Test subscribe sends correct payload after login"""
40- with patch ( 'okx.websocket.WsPrivateAsync. WebSocketFactory' ), \
41- patch ( 'okx.websocket.WsPrivateAsync. WsUtils.initLoginParams ' ) as mock_init_login , \
42- patch ( 'okx.websocket.WsPrivateAsync. asyncio. sleep' , new_callable = AsyncMock ):
43+ with patch . object ( ws_private_module , ' WebSocketFactory' ), \
44+ patch . object ( ws_private_module , ' WsUtils' ) as mock_ws_utils , \
45+ patch . object ( ws_private_module . asyncio , ' sleep' , new_callable = AsyncMock ):
4346
44- mock_init_login .return_value = '{"op":"login"}'
47+ mock_ws_utils . initLoginParams .return_value = '{"op":"login"}'
4548
46- from okx .websocket .WsPrivateAsync import WsPrivateAsync
4749 ws = WsPrivateAsync (
4850 apiKey = "test_api_key" ,
4951 passphrase = "test_passphrase" ,
@@ -70,13 +72,12 @@ async def run_test():
7072
7173 def test_subscribe_with_id (self ):
7274 """Test subscribe with id parameter"""
73- with patch ( 'okx.websocket.WsPrivateAsync. WebSocketFactory' ), \
74- patch ( 'okx.websocket.WsPrivateAsync. WsUtils.initLoginParams ' ) as mock_init_login , \
75- patch ( 'okx.websocket.WsPrivateAsync. asyncio. sleep' , new_callable = AsyncMock ):
75+ with patch . object ( ws_private_module , ' WebSocketFactory' ), \
76+ patch . object ( ws_private_module , ' WsUtils' ) as mock_ws_utils , \
77+ patch . object ( ws_private_module . asyncio , ' sleep' , new_callable = AsyncMock ):
7678
77- mock_init_login .return_value = '{"op":"login"}'
79+ mock_ws_utils . initLoginParams .return_value = '{"op":"login"}'
7880
79- from okx .websocket .WsPrivateAsync import WsPrivateAsync
8081 ws = WsPrivateAsync (
8182 apiKey = "test_api_key" ,
8283 passphrase = "test_passphrase" ,
@@ -105,8 +106,7 @@ class TestWsPrivateAsyncUnsubscribe(unittest.TestCase):
105106
106107 def test_unsubscribe_sends_correct_payload (self ):
107108 """Test unsubscribe sends correct payload"""
108- with patch ('okx.websocket.WsPrivateAsync.WebSocketFactory' ):
109- from okx .websocket .WsPrivateAsync import WsPrivateAsync
109+ with patch .object (ws_private_module , 'WebSocketFactory' ):
110110 ws = WsPrivateAsync (
111111 apiKey = "test_api_key" ,
112112 passphrase = "test_passphrase" ,
@@ -131,8 +131,7 @@ async def run_test():
131131
132132 def test_unsubscribe_with_id (self ):
133133 """Test unsubscribe with id parameter"""
134- with patch ('okx.websocket.WsPrivateAsync.WebSocketFactory' ):
135- from okx .websocket .WsPrivateAsync import WsPrivateAsync
134+ with patch .object (ws_private_module , 'WebSocketFactory' ):
136135 ws = WsPrivateAsync (
137136 apiKey = "test_api_key" ,
138137 passphrase = "test_passphrase" ,
@@ -160,12 +159,11 @@ class TestWsPrivateAsyncLogin(unittest.TestCase):
160159
161160 def test_login_calls_init_login_params (self ):
162161 """Test login calls WsUtils.initLoginParams with correct parameters"""
163- with patch ( 'okx.websocket.WsPrivateAsync. WebSocketFactory' ), \
164- patch ( 'okx.websocket.WsPrivateAsync. WsUtils.initLoginParams ' ) as mock_init_login :
162+ with patch . object ( ws_private_module , ' WebSocketFactory' ), \
163+ patch . object ( ws_private_module , ' WsUtils' ) as mock_ws_utils :
165164
166- mock_init_login .return_value = '{"op":"login","args":[...]}'
165+ mock_ws_utils . initLoginParams .return_value = '{"op":"login","args":[...]}'
167166
168- from okx .websocket .WsPrivateAsync import WsPrivateAsync
169167 ws = WsPrivateAsync (
170168 apiKey = "test_api_key" ,
171169 passphrase = "test_passphrase" ,
@@ -179,7 +177,7 @@ def test_login_calls_init_login_params(self):
179177 async def run_test ():
180178 result = await ws .login ()
181179 self .assertTrue (result )
182- mock_init_login .assert_called_once_with (
180+ mock_ws_utils . initLoginParams .assert_called_once_with (
183181 useServerTime = True ,
184182 apiKey = "test_api_key" ,
185183 passphrase = "test_passphrase" ,
@@ -193,26 +191,23 @@ class TestWsPrivateAsyncStartStop(unittest.TestCase):
193191 """Unit tests for WsPrivateAsync start and stop methods"""
194192
195193 def test_stop (self ):
196- """Test stop method closes the factory and stops loop """
197- with patch ( 'okx.websocket.WsPrivateAsync. WebSocketFactory' ) as mock_factory_class :
194+ """Test stop method closes the factory"""
195+ with patch . object ( ws_private_module , ' WebSocketFactory' ) as mock_factory_class :
198196 mock_factory_instance = MagicMock ()
199197 mock_factory_instance .close = AsyncMock ()
200198 mock_factory_class .return_value = mock_factory_instance
201199
202- from okx .websocket .WsPrivateAsync import WsPrivateAsync
203200 ws = WsPrivateAsync (
204201 apiKey = "test_api_key" ,
205202 passphrase = "test_passphrase" ,
206203 secretKey = "test_secret_key" ,
207204 url = "wss://test.example.com" ,
208205 useServerTime = False
209206 )
210- ws .loop = MagicMock ()
211207
212208 async def run_test ():
213209 await ws .stop ()
214210 mock_factory_instance .close .assert_called_once ()
215- ws .loop .stop .assert_called_once ()
216211
217212 asyncio .get_event_loop ().run_until_complete (run_test ())
218213
0 commit comments