~ubuntu-branches/debian/squeeze/python-django/squeeze

« back to all changes in this revision

Viewing changes to tests/regressiontests/cache/tests.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb, Chris Lamb, David Spreen, Sandro Tosi
  • Date: 2008-11-19 21:31:00 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20081119213100-gp0lqhxl1qxa6dgl
Tags: 1.0.2-1
[ Chris Lamb ]
* New upstream bugfix release. Closes: #505783
* Add myself to Uploaders with ACK from Brett.

[ David Spreen ]
* Remove python-pysqlite2 from Recommends because Python 2.5 includes
  sqlite library used by Django. Closes: 497886

[ Sandro Tosi ]
* debian/control
  - switch Vcs-Browser field to viewsvn

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
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 test_simple(self):
27
 
        # simple set/get
28
 
        cache.set("key", "value")
29
 
        self.assertEqual(cache.get("key"), "value")
30
 
 
31
 
    def test_add(self):
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")
37
 
 
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!")
42
 
 
43
 
    def test_get_many(self):
44
 
        # get_many
45
 
        cache.set('a', 'a')
46
 
        cache.set('b', 'b')
47
 
        cache.set('c', 'c')
48
 
        cache.set('d', 'd')
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'})
51
 
 
52
 
    def test_delete(self):
53
 
        # delete
54
 
        cache.set("key1", "spam")
55
 
        cache.set("key2", "eggs")
56
 
        self.assertEqual(cache.get("key1"), "spam")
57
 
        cache.delete("key1")
58
 
        self.assertEqual(cache.get("key1"), None)
59
 
        self.assertEqual(cache.get("key2"), "eggs")
60
 
 
61
 
    def test_has_key(self):
62
 
        # has_key
63
 
        cache.set("hello1", "goodbye1")
64
 
        self.assertEqual(cache.has_key("hello1"), True)
65
 
        self.assertEqual(cache.has_key("goodbye1"), False)
66
 
 
67
 
    def test_in(self):
68
 
        cache.set("hello2", "goodbye2")
69
 
        self.assertEqual("hello2" in cache, True)
70
 
        self.assertEqual("goodbye2" in cache, False)
71
 
 
72
 
    def test_data_types(self):
73
 
        stuff = {
74
 
            'string'    : 'this is a string',
75
 
            'int'       : 42,
76
 
            'list'      : [1, 2, 3, 4],
77
 
            'tuple'     : (1, 2, 3, 4),
78
 
            'dict'      : {'A': 1, 'B' : 2},
79
 
            'function'  : f,
80
 
            'class'     : C,
81
 
        }
82
 
        cache.set("stuff", stuff)
83
 
        self.assertEqual(cache.get("stuff"), stuff)
84
 
 
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)
89
 
 
90
 
        time.sleep(2)
91
 
        self.assertEqual(cache.get("expire1"), None)
92
 
 
93
 
        cache.add("expire2", "newvalue")
94
 
        self.assertEqual(cache.get("expire2"), "newvalue")
95
 
        self.assertEqual(cache.has_key("expire3"), False)
96
 
 
97
 
    def test_unicode(self):
98
 
        stuff = {
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 }
103
 
            }
104
 
        for (key, value) in stuff.items():
105
 
            cache.set(key, value)
106
 
            self.assertEqual(cache.get(key), value)
107
 
 
108
 
 
109
 
class FileBasedCacheTests(unittest.TestCase):
110
 
    """
111
 
    Specific test cases for the file-based cache.
112
 
    """
113
 
    def setUp(self):
114
 
        self.dirname = tempfile.mktemp()
115
 
        os.mkdir(self.dirname)
116
 
        self.cache = FileCache(self.dirname, {})
117
 
 
118
 
    def tearDown(self):
119
 
        shutil.rmtree(self.dirname)
120
 
 
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))
127
 
 
128
 
    def test_subdirectory_removal(self):
129
 
        """
130
 
        Make sure that the created subdirectories are correctly removed when empty.
131
 
        """
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))
136
 
 
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))))
141
 
 
142
 
class CacheUtils(unittest.TestCase):
143
 
    """TestCase for django.utils.cache functions."""
144
 
 
145
 
    def test_patch_vary_headers(self):
146
 
        headers = (
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'),
157
 
        )
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)
164
 
 
165
 
 
166
 
if __name__ == '__main__':
167
 
    unittest.main()