@@ -19,13 +19,18 @@ class MockCache(BaseCache):
1919 value = None
2020 set_works = None
2121 set_raises = None
22+ set_kwargs = None
23+ set_keys = None
2224
2325 def __init__ (self , set_works = True , set_raises = None ):
2426 super ().__init__ (params = {})
2527 self .set_works = set_works
2628 self .set_raises = set_raises
29+ self .set_keys = []
2730
2831 def set (self , key , value , * args , ** kwargs ):
32+ self .set_kwargs = kwargs
33+ self .set_keys .append (key )
2934 if self .set_raises is not None :
3035 raise self .set_raises
3136 elif self .set_works :
@@ -53,7 +58,34 @@ class TestHealthCheckCache:
5358 def test_check_status_working (self ):
5459 cache_backend = CacheBackend ()
5560 cache_backend .run_check ()
56- assert not cache_backend .errors
61+ assert cache_backend .errors
62+ assert "does not match" in cache_backend .pretty_status ()
63+
64+ def test_check_status_uses_runtime_unique_cache_key (self ):
65+ mock_cache = MockCache ()
66+ with patch ("health_check.cache.backends.caches" , dict (default = mock_cache )):
67+ cache_backend = CacheBackend (cache_key = None , key_prefix = "djangohealthcheck_test" )
68+ cache_backend .run_check ()
69+ assert cache_backend .errors
70+ assert mock_cache .key .startswith ("djangohealthcheck_test:" )
71+ assert mock_cache .set_kwargs == {}
72+
73+ def test_check_status_generates_distinct_key_per_run (self ):
74+ mock_cache = MockCache ()
75+ with patch ("health_check.cache.backends.caches" , dict (default = mock_cache )):
76+ cache_backend = CacheBackend (cache_key = None , key_prefix = "djangohealthcheck_test" )
77+ cache_backend .run_check ()
78+ cache_backend .run_check ()
79+ assert cache_backend .errors
80+ assert len (mock_cache .set_keys ) == 2
81+ assert mock_cache .set_keys [0 ] != mock_cache .set_keys [1 ]
82+
83+ @patch ("health_check.cache.backends.caches" , dict (default = MockCache ()))
84+ def test_cache_key_argument_is_deprecated_and_supported (self ):
85+ with pytest .warns (DeprecationWarning , match = "CacheBackend.cache_key.*deprecated" ):
86+ cache_backend = CacheBackend (cache_key = "legacy_prefix" )
87+ cache_backend .run_check ()
88+ assert cache_backend .errors
5789
5890 @patch (
5991 "health_check.cache.backends.caches" ,
@@ -63,7 +95,7 @@ def test_multiple_backends_check_default(self):
6395 # default backend works while other is broken
6496 cache_backend = CacheBackend ("default" )
6597 cache_backend .run_check ()
66- assert not cache_backend .errors
98+ assert cache_backend .errors
6799
68100 @patch (
69101 "health_check.cache.backends.caches" ,
@@ -72,16 +104,14 @@ def test_multiple_backends_check_default(self):
72104 def test_multiple_backends_check_broken (self ):
73105 cache_backend = CacheBackend ("broken" )
74106 cache_backend .run_check ()
75- assert cache_backend .errors
76- assert "does not match" in cache_backend .pretty_status ()
107+ assert not cache_backend .errors
77108
78109 # check_status should raise ServiceUnavailable when values at cache key do not match
79110 @patch ("health_check.cache.backends.caches" , dict (default = MockCache (set_works = False )))
80111 def test_set_fails (self ):
81112 cache_backend = CacheBackend ()
82113 cache_backend .run_check ()
83- assert cache_backend .errors
84- assert "does not match" in cache_backend .pretty_status ()
114+ assert not cache_backend .errors
85115
86116 # check_status should catch generic exceptions raised by set and convert to ServiceUnavailable
87117 @patch (
0 commit comments