1+ """These tests are forked from Django's tests/cache/tests.py."""
12import os
23import pickle
34import time
910from django .core import management
1011from django .core .cache import DEFAULT_CACHE_ALIAS , CacheKeyWarning , cache , caches
1112from django .core .cache .backends .base import InvalidCacheBackendError
12- from django .http import (
13- HttpResponse ,
14- )
15- from django .middleware .cache import (
16- FetchFromCacheMiddleware ,
17- UpdateCacheMiddleware ,
18- )
13+ from django .http import HttpResponse
14+ from django .middleware .cache import FetchFromCacheMiddleware , UpdateCacheMiddleware
1915from django .test import RequestFactory , TestCase , modify_settings , override_settings
2016
2117from .models import Poll , expensive_calculation
@@ -97,12 +93,8 @@ def caches_setting_for_tests(base=None, exclude=None, **params):
9793
9894
9995class BaseCacheTests :
100- # A common set of tests to apply to all cache backends
10196 factory = RequestFactory ()
102-
103- # Some clients raise custom exceptions when .incr() or .decr() are called
104- # with a non-integer value.
105- incr_decr_type_error = TypeError
97+ incr_decr_type_error_msg = "Cannot apply %s() to a value of non-numeric type."
10698
10799 def tearDown (self ):
108100 cache .clear ()
@@ -186,12 +178,12 @@ def test_incr(self):
186178 self .assertEqual (cache .incr ("answer" , 10 ), 52 )
187179 self .assertEqual (cache .get ("answer" ), 52 )
188180 self .assertEqual (cache .incr ("answer" , - 10 ), 42 )
189- with self .assertRaises (ValueError ):
181+ with self .assertRaisesMessage (ValueError , "Key 'does_not_exist' not found." ):
190182 cache .incr ("does_not_exist" )
191- with self .assertRaises (ValueError ):
183+ with self .assertRaisesMessage (ValueError , "Key 'does_not_exist' not found." ):
192184 cache .incr ("does_not_exist" , - 1 )
193185 cache .set ("null" , None )
194- with self .assertRaises ( self .incr_decr_type_error ):
186+ with self .assertRaisesMessage ( TypeError , self .incr_decr_type_error_msg % "incr" ):
195187 cache .incr ("null" )
196188
197189 def test_decr (self ):
@@ -202,12 +194,12 @@ def test_decr(self):
202194 self .assertEqual (cache .decr ("answer" , 10 ), 32 )
203195 self .assertEqual (cache .get ("answer" ), 32 )
204196 self .assertEqual (cache .decr ("answer" , - 10 ), 42 )
205- with self .assertRaises (ValueError ):
197+ with self .assertRaisesMessage (ValueError , "Key 'does_not_exist' not found." ):
206198 cache .decr ("does_not_exist" )
207- with self .assertRaises (ValueError ):
199+ with self .assertRaisesMessage (ValueError , "Key 'does_not_exist' not found." ):
208200 cache .incr ("does_not_exist" , - 1 )
209201 cache .set ("null" , None )
210- with self .assertRaises ( self .incr_decr_type_error ):
202+ with self .assertRaisesMessage ( TypeError , self .incr_decr_type_error_msg % "decr" ):
211203 cache .decr ("null" )
212204
213205 def test_close (self ):
@@ -846,12 +838,7 @@ def test_custom_key_func(self):
846838 self .assertEqual (caches ["custom_key" ].get ("answer2" ), 42 )
847839 self .assertEqual (caches ["custom_key2" ].get ("answer2" ), 42 )
848840
849- @override_settings (
850- CACHE_MIDDLEWARE_ALIAS = DEFAULT_CACHE_ALIAS ,
851- )
852- @modify_settings (
853- INSTALLED_APPS = {"prepend" : "django_mongodb_backend" },
854- )
841+ @override_settings (CACHE_MIDDLEWARE_ALIAS = DEFAULT_CACHE_ALIAS )
855842 def test_cache_write_unpicklable_object (self ):
856843 fetch_middleware = FetchFromCacheMiddleware (empty_response )
857844
@@ -1003,7 +990,7 @@ def allow_migrate(self, db, app_label, **hints):
1003990@modify_settings (
1004991 INSTALLED_APPS = {"prepend" : "django_mongodb_backend" },
1005992)
1006- class CreateCacheTableForDBCacheTests (TestCase ):
993+ class CreateCacheCollectionTests (TestCase ):
1007994 databases = {"default" , "other" }
1008995
1009996 @override_settings (DATABASE_ROUTERS = [DBCacheRouter ()])
0 commit comments