Ticket #16708: cached_db_exists_check_cache_v2.diff
File cached_db_exists_check_cache_v2.diff, 2.5 KB (added by , 13 years ago) |
---|
-
django/contrib/sessions/tests.py
12 12 from django.contrib.sessions.backends.signed_cookies import SessionStore as CookieSession 13 13 from django.contrib.sessions.models import Session 14 14 from django.contrib.sessions.middleware import SessionMiddleware 15 from django.core.cache import cache 15 16 from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation 16 17 from django.http import HttpResponse 17 18 from django.test import TestCase, RequestFactory … … 276 277 self.assertEqual(self.session['y'], 2) 277 278 278 279 280 class MockDatabaseStore(DatabaseSession): 281 db_exists_called = False 282 db_exists_call_count = 0 283 284 def exists(self, session_key): 285 self.db_exists_called = True 286 self.db_exists_call_count += 1 287 return super(MockDatabaseStore, self).exists(session_key) 288 289 class MockCacheDBSession(CacheDBSession, MockDatabaseStore): 290 pass 291 279 292 class CacheDBSessionTests(SessionTestsMixin, TestCase): 280 293 281 backend = CacheDBSession294 backend = MockCacheDBSession 282 295 296 def test_exists_cached(self): 297 self.session['cat'] = "dog" 298 self.session.save() 299 300 self.assertTrue(self.session.exists(self.session.session_key)) 301 302 self.assertTrue(self.session.db_exists_called) 303 self.assertEqual(self.session.db_exists_call_count, 1) 304 305 def test_exists_not_cached(self): 306 self.session['cat'] = "dog" 307 self.session.save() 308 309 cache.delete(self.session.session_key) 310 311 self.assertTrue(self.session.exists(self.session.session_key)) 312 313 self.assertTrue(self.session.db_exists_called) 314 self.assertEqual(self.session.db_exists_call_count, 2) 315 283 316 # Don't need DB flushing for these tests, so can use unittest.TestCase as base class 284 317 class FileSessionTests(SessionTestsMixin, unittest.TestCase): 285 318 -
django/contrib/sessions/backends/cached_db.py
22 22 return data 23 23 24 24 def exists(self, session_key): 25 if cache.get(session_key) is not None: 26 return True 25 27 return super(SessionStore, self).exists(session_key) 26 28 27 29 def save(self, must_create=False):