diff --git a/cachalot/tests.py b/cachalot/tests.py index 4253cad..e9672be 100644 --- a/cachalot/tests.py +++ b/cachalot/tests.py @@ -2,9 +2,9 @@ from __future__ import unicode_literals try: - from unittest import skip + from unittest import skip, skipIf except ImportError: # For Python 2.6 - from unittest2 import skip + from unittest2 import skip, skipIf import datetime from django.conf import settings from django.contrib.auth.models import User, Permission, Group @@ -1178,3 +1178,22 @@ class AtomicTestCase(TestCase): pass data3 = list(Test.objects.all()) self.assertListEqual(data3, [t1]) + + +class SettingsTestCase(TestCase): + @skipIf(len(settings.CACHES) == 1, + 'We can’t change the cache used since there’s only one configured') + def test_cache(self): + with self.settings(CACHALOT_CACHE='default'): + with self.assertNumQueries(1): + list(Test.objects.all()) + with self.assertNumQueries(0): + list(Test.objects.all()) + + other_cache = [k for k in settings.CACHES if k != 'default'][0] + + with self.settings(CACHALOT_CACHE=other_cache): + with self.assertNumQueries(1): + list(Test.objects.all()) + with self.assertNumQueries(0): + list(Test.objects.all()) diff --git a/runtests.py b/runtests.py index b9e7bef..3ed6b8f 100755 --- a/runtests.py +++ b/runtests.py @@ -37,6 +37,9 @@ CACHES = { 'LOCATION': '127.0.0.1:6379:0', } } +DEFAULT_CACHE_KEY = os.environ.get('CACHE_BACKEND', 'locmem') +CACHES['default'] = CACHES[DEFAULT_CACHE_KEY] +del CACHES[DEFAULT_CACHE_KEY] settings.configure( DEBUG=True, @@ -46,7 +49,7 @@ settings.configure( 'django.contrib.auth', 'django.contrib.contenttypes', ), - CACHES={'default': CACHES[os.environ.get('CACHE_BACKEND', 'locmem')]}, + CACHES=CACHES, MIDDLEWARE_CLASSES=(), )