1
+ """These tests are forked from Django's tests/cache/tests.py."""
1
2
import os
2
3
import pickle
3
4
import time
9
10
from django .core import management
10
11
from django .core .cache import DEFAULT_CACHE_ALIAS , CacheKeyWarning , cache , caches
11
12
from 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
19
15
from django .test import RequestFactory , TestCase , modify_settings , override_settings
20
16
21
17
from .models import Poll , expensive_calculation
@@ -97,12 +93,8 @@ def caches_setting_for_tests(base=None, exclude=None, **params):
97
93
98
94
99
95
class BaseCacheTests :
100
- # A common set of tests to apply to all cache backends
101
96
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."
106
98
107
99
def tearDown (self ):
108
100
cache .clear ()
@@ -186,12 +178,12 @@ def test_incr(self):
186
178
self .assertEqual (cache .incr ("answer" , 10 ), 52 )
187
179
self .assertEqual (cache .get ("answer" ), 52 )
188
180
self .assertEqual (cache .incr ("answer" , - 10 ), 42 )
189
- with self .assertRaises (ValueError ):
181
+ with self .assertRaisesMessage (ValueError , "Key 'does_not_exist' not found." ):
190
182
cache .incr ("does_not_exist" )
191
- with self .assertRaises (ValueError ):
183
+ with self .assertRaisesMessage (ValueError , "Key 'does_not_exist' not found." ):
192
184
cache .incr ("does_not_exist" , - 1 )
193
185
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" ):
195
187
cache .incr ("null" )
196
188
197
189
def test_decr (self ):
@@ -202,12 +194,12 @@ def test_decr(self):
202
194
self .assertEqual (cache .decr ("answer" , 10 ), 32 )
203
195
self .assertEqual (cache .get ("answer" ), 32 )
204
196
self .assertEqual (cache .decr ("answer" , - 10 ), 42 )
205
- with self .assertRaises (ValueError ):
197
+ with self .assertRaisesMessage (ValueError , "Key 'does_not_exist' not found." ):
206
198
cache .decr ("does_not_exist" )
207
- with self .assertRaises (ValueError ):
199
+ with self .assertRaisesMessage (ValueError , "Key 'does_not_exist' not found." ):
208
200
cache .incr ("does_not_exist" , - 1 )
209
201
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" ):
211
203
cache .decr ("null" )
212
204
213
205
def test_close (self ):
@@ -846,12 +838,7 @@ def test_custom_key_func(self):
846
838
self .assertEqual (caches ["custom_key" ].get ("answer2" ), 42 )
847
839
self .assertEqual (caches ["custom_key2" ].get ("answer2" ), 42 )
848
840
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 )
855
842
def test_cache_write_unpicklable_object (self ):
856
843
fetch_middleware = FetchFromCacheMiddleware (empty_response )
857
844
@@ -1003,7 +990,7 @@ def allow_migrate(self, db, app_label, **hints):
1003
990
@modify_settings (
1004
991
INSTALLED_APPS = {"prepend" : "django_mongodb_backend" },
1005
992
)
1006
- class CreateCacheTableForDBCacheTests (TestCase ):
993
+ class CreateCacheCollectionTests (TestCase ):
1007
994
databases = {"default" , "other" }
1008
995
1009
996
@override_settings (DATABASE_ROUTERS = [DBCacheRouter ()])
0 commit comments