Skip to content

Commit 06a7cae

Browse files
committed
Use local fixture to patch herd settings,
1 parent 5e5d095 commit 06a7cae

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

tests/conftest.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,10 @@
22

33
import pytest
44
from django.core.cache import cache as default_cache
5-
from pytest_django.fixtures import SettingsWrapper
65

76
from django_redis.cache import BaseCache
87

98

10-
@pytest.fixture(autouse=True)
11-
def patch_settings(settings: SettingsWrapper):
12-
settings.CACHE_HERD_TIMEOUT = 2
13-
14-
159
@pytest.fixture
1610
def cache() -> Iterable[BaseCache]:
1711
yield default_cache

tests/test_backend.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@
1717
from django_redis.serializers.msgpack import MSGPackSerializer
1818

1919

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+
2034
class TestDjangoRedisCache:
2135
def test_setnx(self, cache: RedisCache):
2236
# we should ensure there is no test_key_nx in redis
@@ -36,7 +50,7 @@ def test_setnx(self, cache: RedisCache):
3650
res = cache.get("test_key_nx")
3751
assert res is None
3852

39-
def test_setnx_timeout(self, cache: RedisCache):
53+
def test_setnx_timeout(self, patch_herd_settings, cache: RedisCache):
4054
# test that timeout still works for nx=True
4155
res = cache.set("test_key_nx", 1, timeout=2, nx=True)
4256
assert res is True
@@ -115,7 +129,7 @@ def test_save_float(self, cache: RedisCache):
115129
assert isinstance(res, float)
116130
assert res == float_val
117131

118-
def test_timeout(self, cache: RedisCache):
132+
def test_timeout(self, patch_herd_settings, cache: RedisCache):
119133
cache.set("test_key", 222, timeout=3)
120134
time.sleep(4)
121135

@@ -127,7 +141,7 @@ def test_timeout_0(self, cache: RedisCache):
127141
res = cache.get("test_key")
128142
assert res is None
129143

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):
131145
cache.set("test_key", 222, -1)
132146
res = cache.get("test_key")
133147
assert res is None
@@ -198,7 +212,11 @@ def test_set_many(self, cache: RedisCache):
198212
assert res == {"a": 1, "b": 2, "c": 3}
199213

200214
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,
202220
):
203221
if isinstance(cache.client, ShardClient):
204222
pytest.skip("ShardClient doesn't support get_client")
@@ -496,10 +514,7 @@ def test_delete_pattern_with_custom_count(self, client_mock, cache: RedisCache):
496514
@patch("django_redis.cache.RedisCache.client")
497515
@override_settings(DJANGO_REDIS_SCAN_ITERSIZE=30)
498516
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,
503518
):
504519
for key in ["foo-aa", "foo-ab", "foo-bb", "foo-bc"]:
505520
cache.set(key, "foo")
@@ -521,7 +536,7 @@ def test_close_client(self, cache: RedisCache, mocker: MockerFixture):
521536
cache.close()
522537
assert mock.called
523538

524-
def test_ttl(self, cache: RedisCache):
539+
def test_ttl(self, patch_herd_settings, cache: RedisCache):
525540
cache.set("foo", "bar", 10)
526541
ttl = cache.ttl("foo")
527542

@@ -728,7 +743,7 @@ def test_touch_zero_timeout(self, cache: RedisCache):
728743
res = cache.get("test_key")
729744
assert res is None
730745

731-
def test_touch_positive_timeout(self, cache: RedisCache):
746+
def test_touch_positive_timeout(self, patch_herd_settings, cache: RedisCache):
732747
cache.set("test_key", 222, timeout=10)
733748

734749
assert cache.touch("test_key", 2) is True

0 commit comments

Comments
 (0)