Skip to content

Commit

Permalink
Prevent cache persistence between tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sarayourfriend committed May 23, 2024
1 parent bdc0dec commit ef7ffe2
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions api/test/fixtures/cache.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.core.cache import caches

import pytest
from django_redis.cache import RedisCache
from fakeredis import FakeRedis, FakeServer
Expand Down Expand Up @@ -41,18 +43,20 @@ def get_redis_connection(*args, **kwargs):


@pytest.fixture(autouse=True)
def django_cache(redis, monkeypatch) -> RedisCache:
def django_cache(redis) -> RedisCache:
"""Use the fake Redis fixture ``redis`` as Django's default cache."""

original_default_cache = caches["default"]
cache = RedisCache(" ", {})
client = cache.client
client._clients = [redis]
monkeypatch.setattr("django.core.cache.cache", cache)
caches["default"] = cache
yield cache
caches["default"] = original_default_cache


@pytest.fixture
def unreachable_django_cache(unreachable_redis, monkeypatch) -> RedisCache:
def unreachable_django_cache(unreachable_redis) -> RedisCache:
"""
Use the fake Redis fixture ``unreachable_redis`` as Django's default cache.
Expand All @@ -61,8 +65,10 @@ def unreachable_django_cache(unreachable_redis, monkeypatch) -> RedisCache:
``ConnectionError``.
"""

original_default_cache = caches["default"]
cache = RedisCache(" ", {})
client = cache.client
client._clients = [unreachable_redis]
monkeypatch.setattr("django.core.cache.cache", cache)
caches["default"] = unreachable_redis
yield cache
caches["default"] = original_default_cache

0 comments on commit ef7ffe2

Please sign in to comment.