Skip to content

Commit fbc5696

Browse files
committed
Normalize to using mock.mock everywhere instead of mixing and matching.
1 parent 91b2344 commit fbc5696

17 files changed

+30
-41
lines changed

dev_requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ flake8-isort
55
flake8
66
flynt~=0.69.0
77
invoke==2.2.0
8-
mock
8+
mock~=5.1.0
99
packaging>=20.4
1010
pytest
1111
pytest-asyncio>=0.23.0,<0.24.0

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
from enum import Enum
88
from typing import Callable, TypeVar, Union
99
from unittest import mock
10-
from unittest.mock import Mock
1110
from urllib.parse import urlparse
1211

1312
import pytest
1413
import redis
14+
from mock.mock import Mock
1515
from packaging.version import Version
1616
from redis import Sentinel
1717
from redis.auth.idp import IdentityProviderInterface

tests/test_asyncio/compat.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
import asyncio
2-
from unittest import mock
3-
4-
try:
5-
mock.AsyncMock
6-
except AttributeError:
7-
from unittest import mock
8-
91
try:
102
from contextlib import aclosing
113
except ImportError:
@@ -17,7 +9,3 @@ async def aclosing(thing):
179
yield thing
1810
finally:
1911
await thing.aclose()
20-
21-
22-
def create_task(coroutine):
23-
return asyncio.create_task(coroutine)

tests/test_asyncio/conftest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from datetime import datetime, timezone
44
from enum import Enum
55
from typing import Union
6+
from unittest import mock
67

78
import pytest
89
import pytest_asyncio
@@ -36,8 +37,6 @@
3637
)
3738
from tests.conftest import REDIS_INFO
3839

39-
from .compat import mock
40-
4140

4241
class AuthType(Enum):
4342
MANAGED_IDENTITY = "managed_identity"

tests/test_asyncio/test_cluster.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import ssl
55
import warnings
66
from typing import Any, Awaitable, Callable, Dict, List, Optional, Type, Union
7+
from unittest import mock
78
from urllib.parse import urlparse
89

910
import pytest
@@ -38,7 +39,7 @@
3839
)
3940

4041
from ..ssl_utils import get_tls_certificates
41-
from .compat import aclosing, mock
42+
from .compat import aclosing
4243

4344
pytestmark = pytest.mark.onlycluster
4445

tests/test_asyncio/test_connection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
import socket
33
import types
44
from errno import ECONNREFUSED
5-
from unittest.mock import patch
5+
from unittest import mock
66

77
import pytest
88
import redis
9+
from mock.mock import patch
910
from redis._parsers import (
1011
_AsyncHiredisParser,
1112
_AsyncRESP2Parser,
@@ -25,7 +26,6 @@
2526
from redis.utils import HIREDIS_AVAILABLE
2627
from tests.conftest import skip_if_server_version_lt
2728

28-
from .compat import mock
2929
from .mocks import MockStream
3030

3131

tests/test_asyncio/test_connection_pool.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22
import re
33
from contextlib import asynccontextmanager
4+
from unittest import mock
45

56
import pytest
67
import pytest_asyncio
@@ -9,7 +10,7 @@
910
from redis.auth.token import TokenInterface
1011
from tests.conftest import skip_if_redis_enterprise, skip_if_server_version_lt
1112

12-
from .compat import aclosing, mock
13+
from .compat import aclosing
1314
from .test_pubsub import wait_for_message
1415

1516

tests/test_asyncio/test_pipeline.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
from unittest import mock
2+
13
import pytest
24
import redis
35
from tests.conftest import skip_if_server_version_lt
46

5-
from .compat import aclosing, mock
7+
from .compat import aclosing
68
from .conftest import wait_for_command
79

810

tests/test_asyncio/test_pubsub.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import socket
44
import sys
55
from typing import Optional
6-
from unittest.mock import patch
6+
7+
from mock.mock import patch
78

89
# the functionality is available in 3.11.x but has a major issue before
910
# 3.11.3. See https://github.com/redis/redis-py/issues/2633
@@ -20,7 +21,7 @@
2021
from redis.utils import HIREDIS_AVAILABLE
2122
from tests.conftest import get_protocol_version, skip_if_server_version_lt
2223

23-
from .compat import aclosing, create_task, mock
24+
from .compat import aclosing
2425

2526

2627
def with_timeout(t):
@@ -733,7 +734,7 @@ async def loop_step():
733734
await messages.put(message)
734735
break
735736

736-
task = asyncio.get_running_loop().create_task(loop())
737+
task = asyncio.create_task(loop())
737738
# get the initial connect message
738739
async with async_timeout(1):
739740
message = await messages.get()
@@ -782,7 +783,7 @@ def callback(message):
782783
messages = asyncio.Queue()
783784
p = pubsub
784785
await self._subscribe(p, foo=callback)
785-
task = asyncio.get_running_loop().create_task(p.run())
786+
task = asyncio.create_task(p.run())
786787
await r.publish("foo", "bar")
787788
message = await messages.get()
788789
task.cancel()
@@ -805,8 +806,8 @@ def exception_handler_callback(e, pubsub) -> None:
805806
exceptions = asyncio.Queue()
806807
p = pubsub
807808
await self._subscribe(p, foo=lambda x: None)
808-
with mock.patch.object(p, "get_message", side_effect=Exception("error")):
809-
task = asyncio.get_running_loop().create_task(
809+
with patch.object(p, "get_message", side_effect=Exception("error")):
810+
task = asyncio.create_task(
810811
p.run(exception_handler=exception_handler_callback)
811812
)
812813
e = await exceptions.get()
@@ -823,7 +824,7 @@ def callback(message):
823824

824825
messages = asyncio.Queue()
825826
p = pubsub
826-
task = asyncio.get_running_loop().create_task(p.run())
827+
task = asyncio.create_task(p.run())
827828
# wait until loop gets settled. Add a subscription
828829
await asyncio.sleep(0.1)
829830
await p.subscribe(foo=callback)
@@ -867,7 +868,7 @@ async def mysetup(self, r, method):
867868
else:
868869
self.get_message = self.loop_step_listen
869870

870-
self.task = create_task(self.loop())
871+
self.task = asyncio.create_task(self.loop())
871872
# get the initial connect message
872873
message = await self.messages.get()
873874
assert message == {
@@ -903,7 +904,7 @@ async def test_reconnect_socket_error(self, r: redis.Redis, method):
903904
async with self.cond:
904905
assert self.state == 0
905906
self.state = 1
906-
with mock.patch.object(self.pubsub.connection, "_parser") as m:
907+
with patch.object(self.pubsub.connection, "_parser") as m:
907908
m.read_response.side_effect = socket.error
908909
m.can_read_destructive.side_effect = socket.error
909910
# wait until task noticies the disconnect until we

tests/test_asyncio/test_sentinel_managed_connection.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import socket
2+
from unittest import mock
23

34
import pytest
45
from redis.asyncio.retry import Retry
56
from redis.asyncio.sentinel import SentinelManagedConnection
67
from redis.backoff import NoBackoff
78

8-
from .compat import mock
9-
109
pytestmark = pytest.mark.asyncio
1110

1211

0 commit comments

Comments
 (0)