Skip to content

Commit 793af91

Browse files
Remove last vestiges of mock.mock (#3830)
* Remove last vestiges of mock.mock With the minimum Python version now being high enough to drop the usage of the external mock module, switch to unittest.mock everywhere. Switch ayncio test_credentials to AsyncMock. * Remove new mock usage This change snuck into master, change it to unittest.mock as well. * Fix async mock in credential provider tests --------- Co-authored-by: petyaslavova <[email protected]>
1 parent fb30954 commit 793af91

File tree

5 files changed

+43
-30
lines changed

5 files changed

+43
-30
lines changed

dev_requirements.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ build
22
build==1.2.2.post1 ; platform_python_implementation == "PyPy"
33
click==8.0.4
44
invoke==2.2.0
5-
mock
6-
mock==5.1.0 ; platform_python_implementation == "PyPy"
75
packaging>=20.4
86
packaging==24.2 ; platform_python_implementation == "PyPy"
97

tests/test_asyncio/test_command_policies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import random
2+
from unittest.mock import patch
23

34
import pytest
4-
from mock import patch
55

66
from redis import ResponseError
77
from redis._parsers.commands import CommandPolicies, RequestPolicy, ResponsePolicy

tests/test_asyncio/test_credentials.py

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
from asyncio import Lock as AsyncLock
55
from asyncio import sleep as async_sleep
66
from typing import Optional, Tuple, Union
7+
from unittest.mock import AsyncMock, call
78

89
import pytest
910
import pytest_asyncio
1011
import redis
11-
from mock.mock import Mock, call
1212
from redis import AuthenticationError, DataError, RedisError, ResponseError
1313
from redis.asyncio import Connection, ConnectionPool, Redis
1414
from redis.asyncio.retry import Retry
@@ -340,10 +340,10 @@ class TestStreamingCredentialProvider:
340340
indirect=True,
341341
)
342342
async def test_async_re_auth_all_connections(self, credential_provider):
343-
mock_connection = Mock(spec=Connection)
343+
mock_connection = AsyncMock(spec=Connection)
344344
mock_connection.retry = Retry(NoBackoff(), 0)
345-
mock_another_connection = Mock(spec=Connection)
346-
mock_pool = Mock(spec=ConnectionPool)
345+
mock_another_connection = AsyncMock(spec=Connection)
346+
mock_pool = AsyncMock(spec=ConnectionPool)
347347
mock_pool.connection_kwargs = {
348348
"credential_provider": credential_provider,
349349
}
@@ -391,16 +391,16 @@ async def re_auth_callback(token):
391391
indirect=True,
392392
)
393393
async def test_async_re_auth_partial_connections(self, credential_provider):
394-
mock_connection = Mock(spec=Connection)
394+
mock_connection = AsyncMock(spec=Connection)
395395
mock_connection.retry = Retry(NoBackoff(), 3)
396-
mock_another_connection = Mock(spec=Connection)
396+
mock_another_connection = AsyncMock(spec=Connection)
397397
mock_another_connection.retry = Retry(NoBackoff(), 3)
398-
mock_failed_connection = Mock(spec=Connection)
398+
mock_failed_connection = AsyncMock(spec=Connection)
399399
mock_failed_connection.read_response.side_effect = ConnectionError(
400400
"Failed auth"
401401
)
402402
mock_failed_connection.retry = Retry(NoBackoff(), 3)
403-
mock_pool = Mock(spec=ConnectionPool)
403+
mock_pool = AsyncMock(spec=ConnectionPool)
404404
mock_pool.connection_kwargs = {
405405
"credential_provider": credential_provider,
406406
}
@@ -454,21 +454,28 @@ async def re_auth_callback(token):
454454
indirect=True,
455455
)
456456
async def test_re_auth_pub_sub_in_resp3(self, credential_provider):
457-
mock_pubsub_connection = Mock(spec=Connection)
457+
mock_pubsub_connection = AsyncMock(spec=Connection)
458458
mock_pubsub_connection.get_protocol.return_value = 3
459459
mock_pubsub_connection.credential_provider = credential_provider
460460
mock_pubsub_connection.retry = Retry(NoBackoff(), 3)
461-
mock_another_connection = Mock(spec=Connection)
461+
mock_another_connection = AsyncMock(spec=Connection)
462462
mock_another_connection.retry = Retry(NoBackoff(), 3)
463463

464-
mock_pool = Mock(spec=ConnectionPool)
464+
mock_pool = AsyncMock(spec=ConnectionPool)
465465
mock_pool.connection_kwargs = {
466466
"credential_provider": credential_provider,
467467
}
468-
mock_pool.get_connection.side_effect = [
469-
mock_pubsub_connection,
470-
mock_another_connection,
471-
]
468+
469+
async def get_connection_side_effect():
470+
if not hasattr(get_connection_side_effect, "call_count"):
471+
get_connection_side_effect.call_count = 0
472+
result = [mock_pubsub_connection, mock_another_connection][
473+
get_connection_side_effect.call_count
474+
]
475+
get_connection_side_effect.call_count += 1
476+
return result
477+
478+
mock_pool.get_connection = AsyncMock(side_effect=get_connection_side_effect)
472479
mock_pool._available_connections = [mock_another_connection]
473480
mock_pool._lock = AsyncLock()
474481
auth_token = None
@@ -516,21 +523,28 @@ async def re_auth_callback(token):
516523
indirect=True,
517524
)
518525
async def test_do_not_re_auth_pub_sub_in_resp2(self, credential_provider):
519-
mock_pubsub_connection = Mock(spec=Connection)
526+
mock_pubsub_connection = AsyncMock(spec=Connection)
520527
mock_pubsub_connection.get_protocol.return_value = 2
521528
mock_pubsub_connection.credential_provider = credential_provider
522529
mock_pubsub_connection.retry = Retry(NoBackoff(), 3)
523-
mock_another_connection = Mock(spec=Connection)
530+
mock_another_connection = AsyncMock(spec=Connection)
524531
mock_another_connection.retry = Retry(NoBackoff(), 3)
525532

526-
mock_pool = Mock(spec=ConnectionPool)
533+
mock_pool = AsyncMock(spec=ConnectionPool)
527534
mock_pool.connection_kwargs = {
528535
"credential_provider": credential_provider,
529536
}
530-
mock_pool.get_connection.side_effect = [
531-
mock_pubsub_connection,
532-
mock_another_connection,
533-
]
537+
538+
async def get_connection_side_effect():
539+
if not hasattr(get_connection_side_effect, "call_count"):
540+
get_connection_side_effect.call_count = 0
541+
result = [mock_pubsub_connection, mock_another_connection][
542+
get_connection_side_effect.call_count
543+
]
544+
get_connection_side_effect.call_count += 1
545+
return result
546+
547+
mock_pool.get_connection = AsyncMock(side_effect=get_connection_side_effect)
534548
mock_pool._available_connections = [mock_another_connection]
535549
mock_pool._lock = AsyncLock()
536550
auth_token = None
@@ -583,10 +597,10 @@ async def test_fails_on_token_renewal(self, credential_provider):
583597
RequestTokenErr,
584598
RequestTokenErr,
585599
]
586-
mock_connection = Mock(spec=Connection)
600+
mock_connection = AsyncMock(spec=Connection)
587601
mock_connection.retry = Retry(NoBackoff(), 0)
588-
mock_another_connection = Mock(spec=Connection)
589-
mock_pool = Mock(spec=ConnectionPool)
602+
mock_another_connection = AsyncMock(spec=Connection)
603+
mock_pool = AsyncMock(spec=ConnectionPool)
590604
mock_pool.connection_kwargs = {
591605
"credential_provider": credential_provider,
592606
}

tests/test_asyncio/test_multidb/test_healthcheck.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
from unittest.mock import AsyncMock, Mock
2+
13
import pytest
2-
from mock.mock import AsyncMock, Mock
34

45
from redis.asyncio.multidb.database import Database
56
from redis.asyncio.multidb.healthcheck import (

tests/test_credentials.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import threading
55
from time import sleep
66
from typing import Optional, Tuple, Union
7+
from unittest.mock import Mock, call
78

89
import pytest
910
import redis
10-
from mock.mock import Mock, call
1111
from redis import AuthenticationError, DataError, Redis, ResponseError
1212
from redis.auth.err import RequestTokenErr
1313
from redis.backoff import NoBackoff

0 commit comments

Comments
 (0)