summaryrefslogtreecommitdiffstats
path: root/keyedcache/tests.py
blob: 8abb8dd3a8af2a505983d53b4f3f24d9551fffd1 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import keyedcache
import random
from django.test import TestCase
import time

CACHE_HIT=0

def cachetest(a,b,c):
    global CACHE_HIT
    CACHE_HIT += 1
    r = [random.randrange(0,1000) for x in range(0,3)]
    ret = [r, a + r[0], b + r[1], c + r[2]]
    return ret

cachetest = keyedcache.cache_function(2)(cachetest)

class DecoratorTest(TestCase):

    def testCachePut(self):
        d = cachetest(1,2,3)
        self.assertEqual(CACHE_HIT,1)

        d2 = cachetest(1,2,3)
        self.assertEqual(CACHE_HIT,1)
        self.assertEqual(d, d2)

        seeds = d[0]
        self.assertEqual(seeds[0] + 1, d[1])
        self.assertEqual(seeds[1] + 2, d[2])
        self.assertEqual(seeds[2] + 3, d[3])

        time.sleep(3)
        d3 = cachetest(1,2,3)
        self.assertEqual(CACHE_HIT,2)
        self.assertNotEqual(d, d3)

    def testDeleteCachedFunction(self):
        orig = cachetest(10,20,30)
        keyedcache.cache_delete_function(cachetest)
        after = cachetest(10,20,30)
        self.assertNotEqual(orig,keyedcache)

class CachingTest(TestCase):

    def testCacheGetFail(self):
        try:
            keyedcache.cache_get('x')
            self.fail('should have raised NotCachedError')
        except keyedcache.NotCachedError:
            pass

    def testCacheGetOK(self):
        one = [1,2,3,4]
        keyedcache.cache_set('ok', value=one, length=2)
        two = keyedcache.cache_get('ok')
        self.assertEqual(one, two)

        time.sleep(5)
        try:
            three = keyedcache.cache_get('ok')
            self.fail('should have raised NotCachedError, got %s' % three)
        except keyedcache.NotCachedError:
            pass

    def testCacheGetDefault(self):
        chk = keyedcache.cache_get('default',default='-')
        self.assertEqual(chk, '-')


    def testDelete(self):
        keyedcache.cache_set('del', value=True)

        for x in range(0,10):
            keyedcache.cache_set('del', 'x', x, value=True)
            for y in range(0,5):
                keyedcache.cache_set('del', 'x', x, 'y', y, value=True)

        # check to make sure all the values are in the cache
        self.assert_(keyedcache.cache_get('del', default=False))
        for x in range(0,10):
            self.assert_(keyedcache.cache_get('del', 'x', x, default=False))
            for y in range(0,5):
                self.assert_(keyedcache.cache_get('del', 'x', x, 'y', y, default=False))

        # try to delete just one
        killed = keyedcache.cache_delete('del','x',1)
        self.assertEqual([keyedcache.CACHE_PREFIX + "::del::x::1"], killed)
        self.assertFalse(keyedcache.cache_get('del', 'x', 1, default=False))

        # but the others are still there
        self.assert_(keyedcache.cache_get('del', 'x', 2, default=False))

        # now kill all of del::x::1
        killed = keyedcache.cache_delete('del','x', 1, children=True)
        for y in range(0,5):
            self.assertFalse(keyedcache.cache_get('del', 'x', 1, 'y', y, default=False))

        # but del::x::2 and children are there
        self.assert_(keyedcache.cache_get('del','x',2,'y',1, default=False))

        # kill the rest
        killed = keyedcache.cache_delete('del', children=True)
        self.assertFalse(keyedcache.cache_get('del',default=False))
        for x in range(0,10):
            self.assertFalse(keyedcache.cache_get('del', 'x', x, default=False))
            for y in range(0,5):
                self.assertFalse(keyedcache.cache_get('del', 'x', x, 'y', y, default=False))


class TestCacheDisable(TestCase):

    def testDisable(self):
        keyedcache.cache_set('disabled', value=False)
        v = keyedcache.cache_get('disabled')
        self.assertEqual(v, False)

        keyedcache.cache_enable(False)
        keyedcache.cache_set('disabled', value=True)
        try:
            keyedcache.cache_get('disabled')
            self.fail('should have raised NotCachedError')
        except keyedcache.NotCachedError, nce:
            key = keyedcache.cache_key('disabled')
            self.assertEqual(nce.key, key)

        keyedcache.cache_enable()
        v2 = keyedcache.cache_get('disabled')
        # should still be False, since the cache was disabled
        self.assertEqual(v2, False)

class TestKeyMaker(TestCase):

    def testSimpleKey(self):
        v = keyedcache.cache_key('test')
        self.assertEqual(v, keyedcache.CACHE_PREFIX + '::test')

    def testDualKey(self):
        v = keyedcache.cache_key('test', 2)
        self.assertEqual(v, keyedcache.CACHE_PREFIX + '::test::2')

    def testPairedKey(self):
        v = keyedcache.cache_key('test', more='yes')
        self.assertEqual(v, keyedcache.CACHE_PREFIX + '::test::more::yes')

    def testPairedDualKey(self):
        v = keyedcache.cache_key('test', 3, more='yes')
        self.assertEqual(v, keyedcache.CACHE_PREFIX + '::test::3::more::yes')