Skip to content

Commit

Permalink
Make DJANGO_REDIS_IGNORE_EXCEPTIONS more test friendly
Browse files Browse the repository at this point in the history
Calculate the constants as the class is instantiated instead of import
time. This allows overriding the constant using Django's
override_setting without monkey patching the module.
  • Loading branch information
jdufresne committed Apr 13, 2020
1 parent 3053e6b commit b7d64f0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 32 deletions.
26 changes: 12 additions & 14 deletions django_redis/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,8 @@

from .exceptions import ConnectionInterrupted

DJANGO_REDIS_IGNORE_EXCEPTIONS = getattr(
settings, "DJANGO_REDIS_IGNORE_EXCEPTIONS", False
)
DJANGO_REDIS_LOG_IGNORED_EXCEPTIONS = getattr(
settings, "DJANGO_REDIS_LOG_IGNORED_EXCEPTIONS", False
)
DJANGO_REDIS_LOGGER = getattr(settings, "DJANGO_REDIS_LOGGER", False)
DJANGO_REDIS_SCAN_ITERSIZE = getattr(settings, "DJANGO_REDIS_SCAN_ITERSIZE", 10)


if DJANGO_REDIS_LOG_IGNORED_EXCEPTIONS:
logger = logging.getLogger(DJANGO_REDIS_LOGGER or __name__)

CONNECTION_INTERRUPTED = object()


Expand All @@ -38,8 +27,8 @@ def _decorator(self, *args, **kwargs):
return method(self, *args, **kwargs)
except ConnectionInterrupted as e:
if self._ignore_exceptions:
if DJANGO_REDIS_LOG_IGNORED_EXCEPTIONS:
logger.error(str(e))
if self._log_ignored_exceptions:
self.logger.error(str(e))

return return_value
raise e.__cause__
Expand All @@ -61,7 +50,16 @@ def __init__(self, server, params):
self._client = None

self._ignore_exceptions = options.get(
"IGNORE_EXCEPTIONS", DJANGO_REDIS_IGNORE_EXCEPTIONS
"IGNORE_EXCEPTIONS",
getattr(settings, "DJANGO_REDIS_IGNORE_EXCEPTIONS", False),
)
self._log_ignored_exceptions = getattr(
settings, "DJANGO_REDIS_LOG_IGNORED_EXCEPTIONS", False
)
self.logger = (
logging.getLogger(getattr(settings, "DJANGO_REDIS_LOGGER", __name__))
if self._log_ignored_exceptions
else None
)

@property
Expand Down
27 changes: 9 additions & 18 deletions tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,18 +706,15 @@ def test_touch_default_timeout(self):

class DjangoOmitExceptionsTests(unittest.TestCase):
def setUp(self):
self._orig_setting = django_redis.cache.DJANGO_REDIS_IGNORE_EXCEPTIONS
django_redis.cache.DJANGO_REDIS_IGNORE_EXCEPTIONS = True
caches_setting = copy.deepcopy(settings.CACHES)
caches_setting["doesnotexist"]["OPTIONS"]["IGNORE_EXCEPTIONS"] = True
cm = override_settings(CACHES=caches_setting)
cm = override_settings(
CACHES=caches_setting, DJANGO_REDIS_IGNORE_EXCEPTIONS=True
)
cm.enable()
self.addCleanup(cm.disable)
self.cache = caches["doesnotexist"]

def tearDown(self):
django_redis.cache.DJANGO_REDIS_IGNORE_EXCEPTIONS = self._orig_setting

def test_get_many_returns_default_arg(self):
self.assertIs(self.cache._ignore_exceptions, True)
self.assertEqual(self.cache.get_many(["key1", "key2", "key3"]), {})
Expand All @@ -731,37 +728,31 @@ def test_get(self):

class DjangoOmitExceptionsPriority1Tests(unittest.TestCase):
def setUp(self):
self._orig_setting = django_redis.cache.DJANGO_REDIS_IGNORE_EXCEPTIONS
django_redis.cache.DJANGO_REDIS_IGNORE_EXCEPTIONS = False
caches_setting = copy.deepcopy(settings.CACHES)
caches_setting["doesnotexist"]["OPTIONS"]["IGNORE_EXCEPTIONS"] = True
cm = override_settings(CACHES=caches_setting)
cm = override_settings(
CACHES=caches_setting, DJANGO_REDIS_IGNORE_EXCEPTIONS=False
)
cm.enable()
self.addCleanup(cm.disable)
self.cache = caches["doesnotexist"]

def tearDown(self):
django_redis.cache.DJANGO_REDIS_IGNORE_EXCEPTIONS = self._orig_setting

def test_get(self):
self.assertIs(self.cache._ignore_exceptions, True)
self.assertIsNone(self.cache.get("key"))


class DjangoOmitExceptionsPriority2Tests(unittest.TestCase):
def setUp(self):
self._orig_setting = django_redis.cache.DJANGO_REDIS_IGNORE_EXCEPTIONS
django_redis.cache.DJANGO_REDIS_IGNORE_EXCEPTIONS = True
caches_setting = copy.deepcopy(settings.CACHES)
caches_setting["doesnotexist"]["OPTIONS"]["IGNORE_EXCEPTIONS"] = False
cm = override_settings(CACHES=caches_setting)
cm = override_settings(
CACHES=caches_setting, DJANGO_REDIS_IGNORE_EXCEPTIONS=True
)
cm.enable()
self.addCleanup(cm.disable)
self.cache = caches["doesnotexist"]

def tearDown(self):
django_redis.cache.DJANGO_REDIS_IGNORE_EXCEPTIONS = self._orig_setting

def test_get(self):
self.assertIs(self.cache._ignore_exceptions, False)
with self.assertRaises(ConnectionError):
Expand Down

0 comments on commit b7d64f0

Please sign in to comment.