| 1 |
# -*- coding: utf-8 -*- |
|---|
| 2 |
|
|---|
| 3 |
# Unit tests for cache framework |
|---|
| 4 |
# Uses whatever cache backend is set in the test settings file. |
|---|
| 5 |
|
|---|
| 6 |
import os |
|---|
| 7 |
import shutil |
|---|
| 8 |
import tempfile |
|---|
| 9 |
import time |
|---|
| 10 |
import unittest |
|---|
| 11 |
|
|---|
| 12 |
from django.core.cache import cache, get_cache |
|---|
| 13 |
from django.core.cache.backends.filebased import CacheClass as FileCache |
|---|
| 14 |
from django.http import HttpResponse |
|---|
| 15 |
from django.utils.cache import patch_vary_headers |
|---|
| 16 |
from django.utils.hashcompat import md5_constructor |
|---|
| 17 |
|
|---|
| 18 |
# functions/classes for complex data type tests |
|---|
| 19 |
def f(): |
|---|
| 20 |
return 42 |
|---|
| 21 |
class C: |
|---|
| 22 |
def m(n): |
|---|
| 23 |
return 24 |
|---|
| 24 |
|
|---|
| 25 |
class Cache(unittest.TestCase): |
|---|
| 26 |
def setUp(self): |
|---|
| 27 |
# Special-case the file cache so we can clean up after ourselves. |
|---|
| 28 |
if isinstance(cache, FileCache): |
|---|
| 29 |
self.cache_dir = tempfile.mkdtemp() |
|---|
| 30 |
self.cache = get_cache("file:///%s" % self.cache_dir) |
|---|
| 31 |
else: |
|---|
| 32 |
self.cache_dir = None |
|---|
| 33 |
self.cache = cache |
|---|
| 34 |
|
|---|
| 35 |
def tearDown(self): |
|---|
| 36 |
if self.cache_dir is not None: |
|---|
| 37 |
shutil.rmtree(self.cache_dir) |
|---|
| 38 |
|
|---|
| 39 |
def test_simple(self): |
|---|
| 40 |
# simple set/get |
|---|
| 41 |
self.cache.set("key", "value") |
|---|
| 42 |
self.assertEqual(self.cache.get("key"), "value") |
|---|
| 43 |
|
|---|
| 44 |
def test_add(self): |
|---|
| 45 |
# test add (only add if key isn't already in cache) |
|---|
| 46 |
self.cache.add("addkey1", "value") |
|---|
| 47 |
result = self.cache.add("addkey1", "newvalue") |
|---|
| 48 |
self.assertEqual(result, False) |
|---|
| 49 |
self.assertEqual(self.cache.get("addkey1"), "value") |
|---|
| 50 |
|
|---|
| 51 |
def test_non_existent(self): |
|---|
| 52 |
# get with non-existent keys |
|---|
| 53 |
self.assertEqual(self.cache.get("does_not_exist"), None) |
|---|
| 54 |
self.assertEqual(self.cache.get("does_not_exist", "bang!"), "bang!") |
|---|
| 55 |
|
|---|
| 56 |
def test_get_many(self): |
|---|
| 57 |
# get_many |
|---|
| 58 |
self.cache.set('a', 'a') |
|---|
| 59 |
self.cache.set('b', 'b') |
|---|
| 60 |
self.cache.set('c', 'c') |
|---|
| 61 |
self.cache.set('d', 'd') |
|---|
| 62 |
self.assertEqual(self.cache.get_many(['a', 'c', 'd']), {'a' : 'a', 'c' : 'c', 'd' : 'd'}) |
|---|
| 63 |
self.assertEqual(self.cache.get_many(['a', 'b', 'e']), {'a' : 'a', 'b' : 'b'}) |
|---|
| 64 |
|
|---|
| 65 |
def test_delete(self): |
|---|
| 66 |
# delete |
|---|
| 67 |
self.cache.set("key1", "spam") |
|---|
| 68 |
self.cache.set("key2", "eggs") |
|---|
| 69 |
self.assertEqual(self.cache.get("key1"), "spam") |
|---|
| 70 |
self.cache.delete("key1") |
|---|
| 71 |
self.assertEqual(self.cache.get("key1"), None) |
|---|
| 72 |
self.assertEqual(self.cache.get("key2"), "eggs") |
|---|
| 73 |
|
|---|
| 74 |
def test_has_key(self): |
|---|
| 75 |
# has_key |
|---|
| 76 |
self.cache.set("hello1", "goodbye1") |
|---|
| 77 |
self.assertEqual(self.cache.has_key("hello1"), True) |
|---|
| 78 |
self.assertEqual(self.cache.has_key("goodbye1"), False) |
|---|
| 79 |
|
|---|
| 80 |
def test_in(self): |
|---|
| 81 |
self.cache.set("hello2", "goodbye2") |
|---|
| 82 |
self.assertEqual("hello2" in self.cache, True) |
|---|
| 83 |
self.assertEqual("goodbye2" in self.cache, False) |
|---|
| 84 |
|
|---|
| 85 |
def test_data_types(self): |
|---|
| 86 |
stuff = { |
|---|
| 87 |
'string' : 'this is a string', |
|---|
| 88 |
'int' : 42, |
|---|
| 89 |
'list' : [1, 2, 3, 4], |
|---|
| 90 |
'tuple' : (1, 2, 3, 4), |
|---|
| 91 |
'dict' : {'A': 1, 'B' : 2}, |
|---|
| 92 |
'function' : f, |
|---|
| 93 |
'class' : C, |
|---|
| 94 |
} |
|---|
| 95 |
self.cache.set("stuff", stuff) |
|---|
| 96 |
self.assertEqual(self.cache.get("stuff"), stuff) |
|---|
| 97 |
|
|---|
| 98 |
def test_expiration(self): |
|---|
| 99 |
self.cache.set('expire1', 'very quickly', 1) |
|---|
| 100 |
self.cache.set('expire2', 'very quickly', 1) |
|---|
| 101 |
self.cache.set('expire3', 'very quickly', 1) |
|---|
| 102 |
|
|---|
| 103 |
time.sleep(2) |
|---|
| 104 |
self.assertEqual(self.cache.get("expire1"), None) |
|---|
| 105 |
|
|---|
| 106 |
self.cache.add("expire2", "newvalue") |
|---|
| 107 |
self.assertEqual(self.cache.get("expire2"), "newvalue") |
|---|
| 108 |
self.assertEqual(self.cache.has_key("expire3"), False) |
|---|
| 109 |
|
|---|
| 110 |
def test_unicode(self): |
|---|
| 111 |
stuff = { |
|---|
| 112 |
u'ascii': u'ascii_value', |
|---|
| 113 |
u'unicode_ascii': u'Iñtërnâtiônàlizætiøn1', |
|---|
| 114 |
u'Iñtërnâtiônàlizætiøn': u'Iñtërnâtiônàlizætiøn2', |
|---|
| 115 |
u'ascii': {u'x' : 1 } |
|---|
| 116 |
} |
|---|
| 117 |
for (key, value) in stuff.items(): |
|---|
| 118 |
self.cache.set(key, value) |
|---|
| 119 |
self.assertEqual(self.cache.get(key), value) |
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
class FileBasedCacheTests(unittest.TestCase): |
|---|
| 123 |
""" |
|---|
| 124 |
Specific test cases for the file-based cache. |
|---|
| 125 |
""" |
|---|
| 126 |
def setUp(self): |
|---|
| 127 |
self.dirname = tempfile.mkdtemp() |
|---|
| 128 |
self.cache = FileCache(self.dirname, {}) |
|---|
| 129 |
|
|---|
| 130 |
def tearDown(self): |
|---|
| 131 |
shutil.rmtree(self.dirname) |
|---|
| 132 |
|
|---|
| 133 |
def test_hashing(self): |
|---|
| 134 |
"""Test that keys are hashed into subdirectories correctly""" |
|---|
| 135 |
self.cache.set("foo", "bar") |
|---|
| 136 |
keyhash = md5_constructor("foo").hexdigest() |
|---|
| 137 |
keypath = os.path.join(self.dirname, keyhash[:2], keyhash[2:4], keyhash[4:]) |
|---|
| 138 |
self.assert_(os.path.exists(keypath)) |
|---|
| 139 |
|
|---|
| 140 |
def test_subdirectory_removal(self): |
|---|
| 141 |
""" |
|---|
| 142 |
Make sure that the created subdirectories are correctly removed when empty. |
|---|
| 143 |
""" |
|---|
| 144 |
self.cache.set("foo", "bar") |
|---|
| 145 |
keyhash = md5_constructor("foo").hexdigest() |
|---|
| 146 |
keypath = os.path.join(self.dirname, keyhash[:2], keyhash[2:4], keyhash[4:]) |
|---|
| 147 |
self.assert_(os.path.exists(keypath)) |
|---|
| 148 |
|
|---|
| 149 |
self.cache.delete("foo") |
|---|
| 150 |
self.assert_(not os.path.exists(keypath)) |
|---|
| 151 |
self.assert_(not os.path.exists(os.path.dirname(keypath))) |
|---|
| 152 |
self.assert_(not os.path.exists(os.path.dirname(os.path.dirname(keypath)))) |
|---|
| 153 |
|
|---|
| 154 |
class CacheUtils(unittest.TestCase): |
|---|
| 155 |
"""TestCase for django.utils.cache functions.""" |
|---|
| 156 |
|
|---|
| 157 |
def test_patch_vary_headers(self): |
|---|
| 158 |
headers = ( |
|---|
| 159 |
# Initial vary, new headers, resulting vary. |
|---|
| 160 |
(None, ('Accept-Encoding',), 'Accept-Encoding'), |
|---|
| 161 |
('Accept-Encoding', ('accept-encoding',), 'Accept-Encoding'), |
|---|
| 162 |
('Accept-Encoding', ('ACCEPT-ENCODING',), 'Accept-Encoding'), |
|---|
| 163 |
('Cookie', ('Accept-Encoding',), 'Cookie, Accept-Encoding'), |
|---|
| 164 |
('Cookie, Accept-Encoding', ('Accept-Encoding',), 'Cookie, Accept-Encoding'), |
|---|
| 165 |
('Cookie, Accept-Encoding', ('Accept-Encoding', 'cookie'), 'Cookie, Accept-Encoding'), |
|---|
| 166 |
(None, ('Accept-Encoding', 'COOKIE'), 'Accept-Encoding, COOKIE'), |
|---|
| 167 |
('Cookie, Accept-Encoding', ('Accept-Encoding', 'cookie'), 'Cookie, Accept-Encoding'), |
|---|
| 168 |
('Cookie , Accept-Encoding', ('Accept-Encoding', 'cookie'), 'Cookie, Accept-Encoding'), |
|---|
| 169 |
) |
|---|
| 170 |
for initial_vary, newheaders, resulting_vary in headers: |
|---|
| 171 |
response = HttpResponse() |
|---|
| 172 |
if initial_vary is not None: |
|---|
| 173 |
response['Vary'] = initial_vary |
|---|
| 174 |
patch_vary_headers(response, newheaders) |
|---|
| 175 |
self.assertEqual(response['Vary'], resulting_vary) |
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
if __name__ == '__main__': |
|---|
| 179 |
unittest.main() |
|---|