From b7d64f0a80180ecfe8900407fbdf5e4196fa7a2a Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Mon, 13 Apr 2020 11:38:27 -0700 Subject: [PATCH] Make DJANGO_REDIS_IGNORE_EXCEPTIONS more test friendly 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. --- django_redis/cache.py | 26 ++++++++++++-------------- tests/test_backend.py | 27 +++++++++------------------ 2 files changed, 21 insertions(+), 32 deletions(-) diff --git a/django_redis/cache.py b/django_redis/cache.py index d752fbf7..a595103d 100644 --- a/django_redis/cache.py +++ b/django_redis/cache.py @@ -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() @@ -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__ @@ -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 diff --git a/tests/test_backend.py b/tests/test_backend.py index 31a80931..c7eee33d 100644 --- a/tests/test_backend.py +++ b/tests/test_backend.py @@ -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"]), {}) @@ -731,18 +728,15 @@ 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")) @@ -750,18 +744,15 @@ def test_get(self): 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):