1
# -*- coding: utf-8 -*-
3
# Unit tests for cache framework
4
# Uses whatever cache backend is set in the test settings file.
12
from django.core.cache import 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
18
# functions/classes for complex data type tests
25
class Cache(unittest.TestCase):
26
def test_simple(self):
28
cache.set("key", "value")
29
self.assertEqual(cache.get("key"), "value")
32
# test add (only add if key isn't already in cache)
33
cache.add("addkey1", "value")
34
result = cache.add("addkey1", "newvalue")
35
self.assertEqual(result, False)
36
self.assertEqual(cache.get("addkey1"), "value")
38
def test_non_existent(self):
39
# get with non-existent keys
40
self.assertEqual(cache.get("does_not_exist"), None)
41
self.assertEqual(cache.get("does_not_exist", "bang!"), "bang!")
43
def test_get_many(self):
49
self.assertEqual(cache.get_many(['a', 'c', 'd']), {'a' : 'a', 'c' : 'c', 'd' : 'd'})
50
self.assertEqual(cache.get_many(['a', 'b', 'e']), {'a' : 'a', 'b' : 'b'})
52
def test_delete(self):
54
cache.set("key1", "spam")
55
cache.set("key2", "eggs")
56
self.assertEqual(cache.get("key1"), "spam")
58
self.assertEqual(cache.get("key1"), None)
59
self.assertEqual(cache.get("key2"), "eggs")
61
def test_has_key(self):
63
cache.set("hello1", "goodbye1")
64
self.assertEqual(cache.has_key("hello1"), True)
65
self.assertEqual(cache.has_key("goodbye1"), False)
68
cache.set("hello2", "goodbye2")
69
self.assertEqual("hello2" in cache, True)
70
self.assertEqual("goodbye2" in cache, False)
72
def test_data_types(self):
74
'string' : 'this is a string',
76
'list' : [1, 2, 3, 4],
77
'tuple' : (1, 2, 3, 4),
78
'dict' : {'A': 1, 'B' : 2},
82
cache.set("stuff", stuff)
83
self.assertEqual(cache.get("stuff"), stuff)
85
def test_expiration(self):
86
cache.set('expire1', 'very quickly', 1)
87
cache.set('expire2', 'very quickly', 1)
88
cache.set('expire3', 'very quickly', 1)
91
self.assertEqual(cache.get("expire1"), None)
93
cache.add("expire2", "newvalue")
94
self.assertEqual(cache.get("expire2"), "newvalue")
95
self.assertEqual(cache.has_key("expire3"), False)
97
def test_unicode(self):
99
u'ascii': u'ascii_value',
100
u'unicode_ascii': u'Iñtërnâtiônàlizætiøn1',
101
u'Iñtërnâtiônàlizætiøn': u'Iñtërnâtiônàlizætiøn2',
102
u'ascii': {u'x' : 1 }
104
for (key, value) in stuff.items():
105
cache.set(key, value)
106
self.assertEqual(cache.get(key), value)
109
class FileBasedCacheTests(unittest.TestCase):
111
Specific test cases for the file-based cache.
114
self.dirname = tempfile.mktemp()
115
os.mkdir(self.dirname)
116
self.cache = FileCache(self.dirname, {})
119
shutil.rmtree(self.dirname)
121
def test_hashing(self):
122
"""Test that keys are hashed into subdirectories correctly"""
123
self.cache.set("foo", "bar")
124
keyhash = md5_constructor("foo").hexdigest()
125
keypath = os.path.join(self.dirname, keyhash[:2], keyhash[2:4], keyhash[4:])
126
self.assert_(os.path.exists(keypath))
128
def test_subdirectory_removal(self):
130
Make sure that the created subdirectories are correctly removed when empty.
132
self.cache.set("foo", "bar")
133
keyhash = md5_constructor("foo").hexdigest()
134
keypath = os.path.join(self.dirname, keyhash[:2], keyhash[2:4], keyhash[4:])
135
self.assert_(os.path.exists(keypath))
137
self.cache.delete("foo")
138
self.assert_(not os.path.exists(keypath))
139
self.assert_(not os.path.exists(os.path.dirname(keypath)))
140
self.assert_(not os.path.exists(os.path.dirname(os.path.dirname(keypath))))
142
class CacheUtils(unittest.TestCase):
143
"""TestCase for django.utils.cache functions."""
145
def test_patch_vary_headers(self):
147
# Initial vary, new headers, resulting vary.
148
(None, ('Accept-Encoding',), 'Accept-Encoding'),
149
('Accept-Encoding', ('accept-encoding',), 'Accept-Encoding'),
150
('Accept-Encoding', ('ACCEPT-ENCODING',), 'Accept-Encoding'),
151
('Cookie', ('Accept-Encoding',), 'Cookie, Accept-Encoding'),
152
('Cookie, Accept-Encoding', ('Accept-Encoding',), 'Cookie, Accept-Encoding'),
153
('Cookie, Accept-Encoding', ('Accept-Encoding', 'cookie'), 'Cookie, Accept-Encoding'),
154
(None, ('Accept-Encoding', 'COOKIE'), 'Accept-Encoding, COOKIE'),
155
('Cookie, Accept-Encoding', ('Accept-Encoding', 'cookie'), 'Cookie, Accept-Encoding'),
156
('Cookie , Accept-Encoding', ('Accept-Encoding', 'cookie'), 'Cookie, Accept-Encoding'),
158
for initial_vary, newheaders, resulting_vary in headers:
159
response = HttpResponse()
160
if initial_vary is not None:
161
response['Vary'] = initial_vary
162
patch_vary_headers(response, newheaders)
163
self.assertEqual(response['Vary'], resulting_vary)
166
if __name__ == '__main__':