summaryrefslogtreecommitdiff
path: root/django/tests/cache_tests.py
blob: a9cce041fa7e9488afa05f19baabe507008ea40f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""
Unit tests for django.core.cache

If you don't have memcached running on localhost port 11211, the memcached tests
will fail.
"""

from django.core import cache
import unittest
import time

# functions/classes for complex data type tests        
def f():
    return 42
class C:
    def m(n):
        return 24
        
class CacheBackendsTest(unittest.TestCase):
    
    def testBackends(self):
        sc = cache.get_cache('simple://')
        mc = cache.get_cache('memcached://127.0.0.1:11211/')
        self.failUnless(isinstance(sc, cache._SimpleCache))
        self.failUnless(isinstance(mc, cache._MemcachedCache))

    def testInvalidBackends(self):
        self.assertRaises(cache.InvalidCacheBackendError, cache.get_cache, 'nothing://foo/')
        self.assertRaises(cache.InvalidCacheBackendError, cache.get_cache, 'not a uri')
        
    def testDefaultTimeouts(self):
        sc = cache.get_cache('simple:///?timeout=15')
        mc = cache.get_cache('memcached://127.0.0.1:11211/?timeout=15')
        self.assertEquals(sc.default_timeout, 15)
        self.assertEquals(sc.default_timeout, 15)

class SimpleCacheTest(unittest.TestCase):
    
    def setUp(self):
        self.cache = cache.get_cache('simple://')
    
    def testGetSet(self):
        self.cache.set('key', 'value')
        self.assertEqual(self.cache.get('key'), 'value')
        
    def testNonExistantKeys(self):
        self.assertEqual(self.cache.get('does not exist'), None)
        self.assertEqual(self.cache.get('does not exist', 'bang!'), 'bang!')

    def testGetMany(self):
        self.cache.set('a', 'a')
        self.cache.set('b', 'b')
        self.cache.set('c', 'c')
        self.cache.set('d', 'd')
        self.assertEqual(self.cache.get_many(['a', 'c', 'd']), {'a' : 'a', 'c' : 'c', 'd' : 'd'})
        self.assertEqual(self.cache.get_many(['a', 'b', 'e']), {'a' : 'a', 'b' : 'b'})

    def testDelete(self):
        self.cache.set('key1', 'spam')
        self.cache.set('key2', 'eggs')
        self.assertEqual(self.cache.get('key1'), 'spam')
        self.cache.delete('key1')
        self.assertEqual(self.cache.get('key1'), None)
        self.assertEqual(self.cache.get('key2'), 'eggs')
        
    def testHasKey(self):
        self.cache.set('hello', 'goodbye')
        self.assertEqual(self.cache.has_key('hello'), True)
        self.assertEqual(self.cache.has_key('goodbye'), False)

    def testDataTypes(self):
        items = {
            'string'    : 'this is a string',
            'int'       : 42,
            'list'      : [1, 2, 3, 4],
            'tuple'     : (1, 2, 3, 4),
            'dict'      : {'A': 1, 'B' : 2},
            'function'  : f,
            'class'     : C,
        }
        for (key, value) in items.items():
            self.cache.set(key, value)
            self.assertEqual(self.cache.get(key), value)
            
    def testExpiration(self):
        self.cache.set('expire', 'very quickly', 1)
        time.sleep(2)
        self.assertEqual(self.cache.get('expire'), None)
        
    def testCull(self):
        c = cache.get_cache('simple://?max_entries=9&cull_frequency=3')
        for i in range(10):
            c.set('culltest%i' % i, i)
        n = 0
        for i in range(10):
            if c.get('culltest%i' % i):
                n += 1
        self.assertEqual(n, 6)
        
    def testCullAll(self):
        c = cache.get_cache('simple://?max_entries=9&cull_frequency=0')
        for i in range(10):
            c.set('cullalltest%i' % i, i)
        for i in range(10):
            self.assertEqual(self.cache.get('cullalltest%i' % i), None)
            
class MemcachedCacheTest(SimpleCacheTest):
    
    def setUp(self):
        self.cache = cache.get_cache('memcached://127.0.0.1:11211/')
        
    testCull = testCullAll = lambda s: None        

def tests():
    s = unittest.TestLoader().loadTestsFromName(__name__)
    unittest.TextTestRunner(verbosity=0).run(s)

if __name__ == "__main__":
    tests()