﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
17995	Forcing a Specific Caching Backend in Tests	dcooper	nobody	"I declared a specific caching backend in my settings file (`DatabaseCache` in development).

{{{#!python
# settings.py

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
        'LOCATION': 'example_db_cache',
    }
}
}}}

I'd like to unit test my code forcing the `LocMemCache` backend.

{{{#!python
# myapp/tests.py

from django.conf import settings
from django.core.cache.backends.locmem import LocMemCache
from django.test import TestCase
from django.test.utils import override_settings


@override_settings(
    CACHES={
        'default': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        },
    },
)
class CacheTests(TestCase):
    def test_check_cache_backend(self):
        from django.core.cache import cache

        self.assertEqual(settings.CACHES, {
            'default': {
                'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
            },
        })

        # This fails, since cache is DatabaseCache from settings.py!
        self.assertIsInstance(cache, LocMemCache)
}}}

which gives exception,

{{{
david@devbox:~/project$ ./manage.py test myapp/tests.py
Creating test database for alias 'default'...
F
======================================================================
FAIL: test_check_cache_backend (myapp.tests.CacheTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""/home/david/project/myapp/tests.py"", line 24, in test_check_cache_backend
    self.assertIsInstance(cache, LocMemCache)
AssertionError: <django.core.cache.backends.db.DatabaseCache object at 0x1ca4c10> is not an instance of <class 'django.core.cache.backends.locmem.LocMemCache'>

----------------------------------------------------------------------
Ran 1 test in 0.002s

FAILED (failures=1)
Destroying test database for alias 'default'...
}}}

So the `cache` object is an instance of `DatabaseCache`, which is the setting from my master `settings.py`, even though the `@override_settings` properly overrode `CACHES`.

Is it possible to test using `LocMemCache`? Is this expected behaviour?

Thanks and keep up the great work!

David Cooper <dcooper@trapeze.com>"	Bug	closed	Testing framework	1.4	Normal	duplicate	cache test		Unreviewed	0	0	0	0	0	0
