Created
July 6, 2015 12:57
-
-
Save igniteflow/4e97eb41e2b9a80cd5de to your computer and use it in GitHub Desktop.
Mock Django's cache in unit tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
myapp/mymodule.py: | |
from django.core.cache import cache | |
def set_cache(): | |
cache.set('foo', 'bar') | |
def get_cache(): | |
return cache.get('foo') | |
""" | |
from myapp.mymodule import set_cache, get_cache | |
with patch('myapp.mymodule.cache') as mock_cache: | |
cache = {} | |
def get(key, default=None): | |
return cache.get(key, default) | |
def _set(key, value, timeout=60): | |
cache[key] = value | |
mock_cache.get = get | |
mock_cache.set = _set | |
set_cache() | |
self.assertEqual(cache['foo'], 'bar') | |
self.assertEqual(get_cache(), 'bar') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment