2
2
import random
3
3
import time
4
4
from typing import Callable , TypeVar
5
+ from unittest import mock
5
6
from unittest .mock import Mock
6
7
from urllib .parse import urlparse
7
8
8
9
import pytest
9
10
import redis
10
11
from packaging .version import Version
11
12
from redis .backoff import NoBackoff
12
- from redis .connection import parse_url
13
+ from redis .connection import Connection , parse_url
13
14
from redis .exceptions import RedisClusterException
14
15
from redis .retry import Retry
15
16
@@ -39,7 +40,6 @@ def __init__(
39
40
help = None ,
40
41
metavar = None ,
41
42
):
42
-
43
43
_option_strings = []
44
44
for option_string in option_strings :
45
45
_option_strings .append (option_string )
@@ -72,7 +72,6 @@ def format_usage(self):
72
72
73
73
74
74
def pytest_addoption (parser ):
75
-
76
75
parser .addoption (
77
76
"--redis-url" ,
78
77
default = default_redis_url ,
@@ -354,23 +353,23 @@ def sslclient(request):
354
353
355
354
356
355
def _gen_cluster_mock_resp (r , response ):
357
- connection = Mock ()
356
+ connection = Mock (spec = Connection )
358
357
connection .retry = Retry (NoBackoff (), 0 )
359
358
connection .read_response .return_value = response
360
- r . connection = connection
361
- return r
359
+ with mock . patch . object ( r , " connection" , connection ):
360
+ yield r
362
361
363
362
364
363
@pytest .fixture ()
365
364
def mock_cluster_resp_ok (request , ** kwargs ):
366
365
r = _get_client (redis .Redis , request , ** kwargs )
367
- return _gen_cluster_mock_resp (r , "OK" )
366
+ yield from _gen_cluster_mock_resp (r , "OK" )
368
367
369
368
370
369
@pytest .fixture ()
371
370
def mock_cluster_resp_int (request , ** kwargs ):
372
371
r = _get_client (redis .Redis , request , ** kwargs )
373
- return _gen_cluster_mock_resp (r , 2 )
372
+ yield from _gen_cluster_mock_resp (r , 2 )
374
373
375
374
376
375
@pytest .fixture ()
@@ -384,7 +383,7 @@ def mock_cluster_resp_info(request, **kwargs):
384
383
"cluster_my_epoch:2\r \n cluster_stats_messages_sent:170262\r \n "
385
384
"cluster_stats_messages_received:105653\r \n "
386
385
)
387
- return _gen_cluster_mock_resp (r , response )
386
+ yield from _gen_cluster_mock_resp (r , response )
388
387
389
388
390
389
@pytest .fixture ()
@@ -408,7 +407,7 @@ def mock_cluster_resp_nodes(request, **kwargs):
408
407
"fbb23ed8cfa23f17eaf27ff7d0c410492a1093d6 172.17.0.7:7002 "
409
408
"master,fail - 1447829446956 1447829444948 1 disconnected\n "
410
409
)
411
- return _gen_cluster_mock_resp (r , response )
410
+ yield from _gen_cluster_mock_resp (r , response )
412
411
413
412
414
413
@pytest .fixture ()
@@ -419,7 +418,7 @@ def mock_cluster_resp_slaves(request, **kwargs):
419
418
"slave 19efe5a631f3296fdf21a5441680f893e8cc96ec 0 "
420
419
"1447836789290 3 connected']"
421
420
)
422
- return _gen_cluster_mock_resp (r , response )
421
+ yield from _gen_cluster_mock_resp (r , response )
423
422
424
423
425
424
@pytest .fixture (scope = "session" )
0 commit comments