22import socket
33import types
44from errno import ECONNREFUSED
5- from unittest .mock import patch
65
76import pytest
87import redis
8+ from mock import mock
99from redis ._parsers import (
1010 _AsyncHiredisParser ,
1111 _AsyncRESP2Parser ,
2525from redis .utils import HIREDIS_AVAILABLE
2626from tests .conftest import skip_if_server_version_lt
2727
28- from .compat import mock
2928from .mocks import MockStream
3029
3130
@@ -43,7 +42,7 @@ async def test_invalid_response(create_redis):
4342 else :
4443 exp_err = f'Protocol error, got "{ raw .decode ()} " as reply type byte'
4544
46- with mock .patch .object (parser , "_stream" , fake_stream ):
45+ with mock .mock . patch .object (parser , "_stream" , fake_stream ):
4746 with pytest .raises (InvalidResponse , match = exp_err ):
4847 await parser .read_response ()
4948
@@ -86,8 +85,8 @@ async def get_conn():
8685 init_call_count += 1
8786 return mock_conn
8887
89- with mock .patch .object (r .connection_pool , "get_connection" , get_conn ):
90- with mock .patch .object (r .connection_pool , "release" ):
88+ with mock .mock . patch .object (r .connection_pool , "get_connection" , get_conn ):
89+ with mock .mock . patch .object (r .connection_pool , "release" ):
9190 await asyncio .gather (r .set ("a" , "b" ), r .set ("c" , "d" ))
9291
9392 assert init_call_count == 1
@@ -157,7 +156,7 @@ async def mock_connect():
157156
158157async def test_connect_without_retry_on_os_error ():
159158 """Test that the _connect function is not being retried in case of a OSError"""
160- with patch .object (Connection , "_connect" ) as _connect :
159+ with mock . patch .object (Connection , "_connect" ) as _connect :
161160 _connect .side_effect = OSError ("" )
162161 conn = Connection (retry_on_timeout = True , retry = Retry (NoBackoff (), 2 ))
163162 with pytest .raises (ConnectionError ):
@@ -281,9 +280,9 @@ async def dummy_method(*args, **kwargs):
281280 pass
282281
283282 # get dummy stream objects for the connection
284- with patch .object (asyncio , "open_connection" , open_connection ):
283+ with mock . patch .object (asyncio , "open_connection" , open_connection ):
285284 # disable the initial version handshake
286- with patch .multiple (
285+ with mock . patch .multiple (
287286 conn , send_command = dummy_method , read_response = dummy_method
288287 ):
289288 await conn .connect ()
@@ -325,7 +324,7 @@ async def mock_aclose(self):
325324
326325 url : str = request .config .getoption ("--redis-url" )
327326 r1 = await Redis .from_url (url )
328- with patch .object (r1 , "aclose" , mock_aclose ):
327+ with mock . patch .object (r1 , "aclose" , mock_aclose ):
329328 with pytest .deprecated_call ():
330329 await r1 .close ()
331330 assert calls == 1
@@ -382,7 +381,7 @@ async def mock_disconnect(_):
382381 nonlocal called
383382 called += 1
384383
385- with patch .object (ConnectionPool , "disconnect" , mock_disconnect ):
384+ with mock . patch .object (ConnectionPool , "disconnect" , mock_disconnect ):
386385 async with await get_redis_connection () as r1 :
387386 assert r1 .auto_close_connection_pool is False
388387
@@ -414,7 +413,7 @@ async def mock_disconnect(_):
414413 nonlocal called
415414 called += 1
416415
417- with patch .object (ConnectionPool , "disconnect" , mock_disconnect ):
416+ with mock . patch .object (ConnectionPool , "disconnect" , mock_disconnect ):
418417 async with await get_redis_connection () as r1 :
419418 assert r1 .auto_close_connection_pool is True
420419
@@ -441,7 +440,7 @@ async def mock_disconnect(_):
441440 nonlocal called
442441 called += 1
443442
444- with patch .object (ConnectionPool , "disconnect" , mock_disconnect ):
443+ with mock . patch .object (ConnectionPool , "disconnect" , mock_disconnect ):
445444 async with await get_redis_connection () as r1 :
446445 assert r1 .auto_close_connection_pool is False
447446
@@ -461,7 +460,7 @@ async def test_client_garbage_collection(request):
461460 # create a client with a connection from the pool
462461 client = Redis (connection_pool = pool , single_connection_client = True )
463462 await client .initialize ()
464- with mock .patch .object (client , "connection" ) as a :
463+ with mock .mock . patch .object (client , "connection" ) as a :
465464 # we cannot, in unittests, or from asyncio, reliably trigger garbage collection
466465 # so we must just invoke the handler
467466 with pytest .warns (ResourceWarning ):
@@ -486,8 +485,8 @@ async def test_connection_garbage_collection(request):
486485 await client .initialize ()
487486 conn = client .connection
488487
489- with mock .patch .object (conn , "_reader" ):
490- with mock .patch .object (conn , "_writer" ) as a :
488+ with mock .mock . patch .object (conn , "_reader" ):
489+ with mock .mock . patch .object (conn , "_writer" ) as a :
491490 # we cannot, in unittests, or from asyncio, reliably trigger
492491 # garbage collection so we must just invoke the handler
493492 with pytest .warns (ResourceWarning ):
0 commit comments