17
17
from django_redis .serializers .msgpack import MSGPackSerializer
18
18
19
19
20
+ @pytest .fixture
21
+ def patch_herd_settings ():
22
+ default_cache = caches ["default" ]
23
+ if not isinstance (default_cache .client , herd .HerdClient ):
24
+ yield
25
+
26
+ # destroy cache to force recreation with updated settings
27
+ del caches ["default" ]
28
+ with override_settings (CACHE_HERD_TIMEOUT = 2 ):
29
+ yield
30
+ # destroy cache force recreation with original settings
31
+ del caches ["default" ]
32
+
33
+
20
34
class TestDjangoRedisCache :
21
35
def test_setnx (self , cache : RedisCache ):
22
36
# we should ensure there is no test_key_nx in redis
@@ -36,7 +50,7 @@ def test_setnx(self, cache: RedisCache):
36
50
res = cache .get ("test_key_nx" )
37
51
assert res is None
38
52
39
- def test_setnx_timeout (self , cache : RedisCache ):
53
+ def test_setnx_timeout (self , patch_herd_settings , cache : RedisCache ):
40
54
# test that timeout still works for nx=True
41
55
res = cache .set ("test_key_nx" , 1 , timeout = 2 , nx = True )
42
56
assert res is True
@@ -115,7 +129,7 @@ def test_save_float(self, cache: RedisCache):
115
129
assert isinstance (res , float )
116
130
assert res == float_val
117
131
118
- def test_timeout (self , cache : RedisCache ):
132
+ def test_timeout (self , patch_herd_settings , cache : RedisCache ):
119
133
cache .set ("test_key" , 222 , timeout = 3 )
120
134
time .sleep (4 )
121
135
@@ -127,7 +141,7 @@ def test_timeout_0(self, cache: RedisCache):
127
141
res = cache .get ("test_key" )
128
142
assert res is None
129
143
130
- def test_timeout_parameter_as_positional_argument (self , cache : RedisCache ):
144
+ def test_timeout_parameter_as_positional_argument (self , patch_herd_settings , cache : RedisCache ):
131
145
cache .set ("test_key" , 222 , - 1 )
132
146
res = cache .get ("test_key" )
133
147
assert res is None
@@ -198,7 +212,11 @@ def test_set_many(self, cache: RedisCache):
198
212
assert res == {"a" : 1 , "b" : 2 , "c" : 3 }
199
213
200
214
def test_set_call_empty_pipeline (
201
- self , cache : RedisCache , mocker : MockerFixture , settings : SettingsWrapper
215
+ self ,
216
+ patch_herd_settings ,
217
+ cache : RedisCache ,
218
+ mocker : MockerFixture ,
219
+ settings : SettingsWrapper ,
202
220
):
203
221
if isinstance (cache .client , ShardClient ):
204
222
pytest .skip ("ShardClient doesn't support get_client" )
@@ -496,10 +514,7 @@ def test_delete_pattern_with_custom_count(self, client_mock, cache: RedisCache):
496
514
@patch ("django_redis.cache.RedisCache.client" )
497
515
@override_settings (DJANGO_REDIS_SCAN_ITERSIZE = 30 )
498
516
def test_delete_pattern_with_settings_default_scan_count (
499
- self ,
500
- client_mock ,
501
- cache : RedisCache ,
502
- settings : SettingsWrapper ,
517
+ self , client_mock , patch_herd_settings , cache : RedisCache , settings : SettingsWrapper ,
503
518
):
504
519
for key in ["foo-aa" , "foo-ab" , "foo-bb" , "foo-bc" ]:
505
520
cache .set (key , "foo" )
@@ -521,7 +536,7 @@ def test_close_client(self, cache: RedisCache, mocker: MockerFixture):
521
536
cache .close ()
522
537
assert mock .called
523
538
524
- def test_ttl (self , cache : RedisCache ):
539
+ def test_ttl (self , patch_herd_settings , cache : RedisCache ):
525
540
cache .set ("foo" , "bar" , 10 )
526
541
ttl = cache .ttl ("foo" )
527
542
@@ -728,7 +743,7 @@ def test_touch_zero_timeout(self, cache: RedisCache):
728
743
res = cache .get ("test_key" )
729
744
assert res is None
730
745
731
- def test_touch_positive_timeout (self , cache : RedisCache ):
746
+ def test_touch_positive_timeout (self , patch_herd_settings , cache : RedisCache ):
732
747
cache .set ("test_key" , 222 , timeout = 10 )
733
748
734
749
assert cache .touch ("test_key" , 2 ) is True
0 commit comments