Adds a test for CACHALOT_CACHE.

This commit is contained in:
Bertrand Bordage 2014-10-05 01:26:51 +02:00
parent c4ed66b710
commit 5888560850
2 changed files with 25 additions and 3 deletions

View file

@ -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 cant change the cache used since theres 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())

View file

@ -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=(),
)