Skip to content

Home Information - Your home, your data, organized.

Python

Fix Redis test isolation — tests no longer modify real Redis state (Issue #269) (#272)

Tony C440010f78dec
diff --git a/src/hi/apps/common/redis_client.py b/src/hi/apps/common/redis_client.py
index 8fb66a4c..5cac7eb7 100644
--- a/src/hi/apps/common/redis_client.py
+++ b/src/hi/apps/common/redis_client.py
@@ -70,11 +70,11 @@ def get_redis_client():
def clear_redis_client():
- global _g_global_redis_client_initialized
+ global _g_global_redis_initialized_attempted
global _g_global_redis_client
if _g_global_redis_client:
logger.info( "Clearing existing Redis connection" )
- _g_global_redis_client_initialized = False
+ _g_global_redis_initialized_attempted = False
_g_global_redis_client = None # No good way to explicitly "close" this
return
diff --git a/src/hi/environment/server.py b/src/hi/environment/server.py
index f99ea004..e0aed365 100644
--- a/src/hi/environment/server.py
+++ b/src/hi/environment/server.py
@@ -33,7 +33,6 @@ class EnvironmentSettings:
MEDIA_ROOT : str = None
REDIS_HOST : str = 'localhost'
REDIS_PORT : int = 6379
- REDIS_KEY_PREFIX : str = ''
SUPPRESS_AUTHENTICATION : bool = True
EMAIL_SUBJECT_PREFIX : str = ''
DEFAULT_FROM_EMAIL : str = ''
@@ -125,10 +124,6 @@ class EnvironmentSettings:
env_settings.REDIS_PORT = int( cls.get_env_variable('HI_REDIS_PORT') )
except ( TypeError, ValueError ):
pass
- env_settings.REDIS_KEY_PREFIX = cls.get_env_variable(
- 'HI_REDIS_KEY_PREFIX',
- env_settings.REDIS_KEY_PREFIX,
- )
###########
# Email-related
diff --git a/src/hi/requirements/development.txt b/src/hi/requirements/development.txt
index a398ecdd..3172692a 100644
--- a/src/hi/requirements/development.txt
+++ b/src/hi/requirements/development.txt
@@ -1,6 +1,7 @@
-r base.txt
autopep8==2.3.2
+fakeredis==2.35.1
black==26.3.1
flake8==7.3.0
grip==4.6.2
diff --git a/src/hi/settings/base.py b/src/hi/settings/base.py
index a15d74d3..e034b5e0 100644
--- a/src/hi/settings/base.py
+++ b/src/hi/settings/base.py
@@ -312,7 +312,6 @@ CONSTANCE_CONFIG = {
REDIS_HOST = ENV.REDIS_HOST
REDIS_PORT = ENV.REDIS_PORT
-REDIS_KEY_PREFIX = ENV.REDIS_KEY_PREFIX
CACHES = {
'default': {
@@ -321,10 +320,12 @@ CACHES = {
'LOCATION': [
f'redis://{REDIS_HOST}:{REDIS_PORT}',
],
- "KEY_PREFIX": f'main:{REDIS_KEY_PREFIX}',
+ "KEY_PREFIX": 'main:',
}
}
+TEST_RUNNER = 'hi.testing.runner.HiTestRunner'
+
AUTH_USER_MODEL = "custom.CustomUser"
SUPPRESS_AUTHENTICATION = ENV.SUPPRESS_AUTHENTICATION
diff --git a/src/hi/testing/base_test_case.py b/src/hi/testing/base_test_case.py
index 42d85970..b2f7a0a8 100644
--- a/src/hi/testing/base_test_case.py
+++ b/src/hi/testing/base_test_case.py
@@ -19,7 +19,7 @@ class BaseTestCase(TestCase):
"""
Common testing utilties.
"""
-
+
def setUp(self):
# With the APPEND_SLASHES feature, you can see a lot of warnings as
# it does its work to add/remove slashes. We are not so interested
diff --git a/src/hi/testing/runner.py b/src/hi/testing/runner.py
new file mode 100644
index 00000000..074a4f48
--- /dev/null
+++ b/src/hi/testing/runner.py
@@ -0,0 +1,48 @@
+"""
+Custom test runner that isolates tests from real Redis.
+
+Replaces the direct Redis client with fakeredis and Django's cache backend
+with LocMemCache before any test runs. This ensures no test touches the
+real Redis instance regardless of which test base class is used.
+
+Configured via TEST_RUNNER in settings/base.py.
+"""
+import fakeredis
+
+from django.test import override_settings
+from django.test.runner import DiscoverRunner
+
+import hi.apps.common.redis_client as redis_client_module
+
+LOCMEM_CACHE = {
+ 'default': {
+ 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
+ }
+}
+
+
+class HiTestRunner( DiscoverRunner ):
+
+ def setup_test_environment( self, **kwargs ):
+ super().setup_test_environment( **kwargs )
+
+ # Replace the global Redis client with an in-memory fake so no
+ # test hits real Redis. Mark as initialized so the real
+ # initialize_global_cache_client() is never called.
+ self._original_redis_client = redis_client_module._g_global_redis_client
+ self._original_redis_initialized = redis_client_module._g_global_redis_initialized_attempted
+ redis_client_module._g_global_redis_client = fakeredis.FakeRedis( decode_responses = True )
+ redis_client_module._g_global_redis_initialized_attempted = True
+
+ # Override Django's cache to use in-memory backend so cache.clear()
+ # does not execute FLUSHDB on real Redis.
+ self._cache_override = override_settings( CACHES = LOCMEM_CACHE )
+ self._cache_override.enable()
+ return
+
+ def teardown_test_environment( self, **kwargs ):
+ self._cache_override.disable()
+ redis_client_module._g_global_redis_client = self._original_redis_client
+ redis_client_module._g_global_redis_initialized_attempted = self._original_redis_initialized
+ super().teardown_test_environment( **kwargs )
+ return
diff --git a/unraid/home-information.xml b/unraid/home-information.xml
index a12477ab..8e849d23 100644
--- a/unraid/home-information.xml
+++ b/unraid/home-information.xml
@@ -36,7 +36,6 @@ Perfect for homeowners who want to stay organized and maintain their property ef
<Config Name="Suppress Authentication" Target="HI_SUPPRESS_AUTHENTICATION" Default="true|false" Mode="" Description="Skip login screen for local network use (set to false for multi-user setup)" Type="Variable" Display="advanced" Required="false" Mask="false"/>
<Config Name="Redis Host" Target="HI_REDIS_HOST" Default="127.0.0.1" Mode="" Description="Redis hostname (use 127.0.0.1 for internal Redis)" Type="Variable" Display="advanced" Required="false" Mask="false"></Config>
<Config Name="Redis Port" Target="HI_REDIS_PORT" Default="6379" Mode="" Description="Redis port" Type="Variable" Display="advanced" Required="false" Mask="false"></Config>
- <Config Name="Redis Key Prefix" Target="HI_REDIS_KEY_PREFIX" Default="" Mode="" Description="Prefix for Redis keys (leave empty unless running multiple instances)" Type="Variable" Display="advanced" Required="false" Mask="false"/>
<Config Name="Extra Host URLs" Target="HI_EXTRA_HOST_URLS" Default="" Mode="" Description="Additional URLs (ALLOWED_HOSTS) for network access (e.g., http://192.168.1.100:9411 http://nas.local:9411)" Type="Variable" Display="advanced" Required="false" Mask="false"/>
<Config Name="Extra CSP URLs" Target="HI_EXTRA_CSP_URLS" Default="" Mode="" Description="Additional URLs for Content Security Policy (usually same as Extra Host URLs)" Type="Variable" Display="advanced" Required="false" Mask="false"/>
<Config Name="Email Subject Prefix" Target="HI_EMAIL_SUBJECT_PREFIX" Default="" Mode="" Description="Prefix for email subjects (e.g., [Home Info])" Type="Variable" Display="advanced" Required="false" Mask="false"/>