3 from unittest import TestCase
5 from django.core.cache import cache
6 from cache_utils.decorators import cached
7 from cache_utils.utils import sanitize_memcached_key, _func_type, _func_info
19 class FuncTypeTest(TestCase):
20 def assertFuncType(self, func, tp):
21 self.assertEqual(_func_type(func), tp)
24 self.assertFuncType(foo, 'function')
26 def test_method(self):
27 self.assertFuncType(Foo.foo, 'method')
29 def test_classmethod(self):
30 self.assertFuncType(Foo.bar, 'classmethod')
33 class FuncInfoTest(TestCase):
34 def assertFuncInfo(self, func, args_in, name, args_out):
35 info = _func_info(func, args_in)
36 self.assertEqual(info[0], name)
37 self.assertEqual(info[1], args_out)
40 self.assertFuncInfo(foo, [1,2], 'cache_utils.tests.foo', [1,2])
42 def test_method(self):
44 self.assertFuncInfo(Foo.foo, [foo_obj, 1, 2],
45 'cache_utils.tests.Foo.foo', [1,2])
47 def test_classmethod(self):
48 self.assertFuncInfo(Foo.bar, [Foo, 1],
49 'cache_utils.tests.Foo.bar', [1])
52 class SanitizeTest(TestCase):
53 def test_sanitize_keys(self):
54 key = "12345678901234567890123456789012345678901234567890"
55 self.assertTrue(len(key) >= 40)
56 key = sanitize_memcached_key(key, 40)
57 self.assertTrue(len(key) <= 40)
60 class ClearMemcachedTest(TestCase):
62 cache._cache.flush_all()
65 cache._cache.flush_all()
68 class InvalidationTest(ClearMemcachedTest):
70 def test_group_invalidation(self):
71 cache.set('vasia', 'foo', 60, group='names')
72 cache.set('petya', 'bar', 60, group='names')
73 cache.set('red', 'good', 60, group='colors')
75 self.assertEqual(cache.get('vasia', group='names'), 'foo')
76 self.assertEqual(cache.get('petya', group='names'), 'bar')
77 self.assertEqual(cache.get('red', group='colors'), 'good')
79 cache.invalidate_group('names')
80 self.assertEqual(cache.get('petya', group='names'), None)
81 self.assertEqual(cache.get('vasia', group='names'), None)
82 self.assertEqual(cache.get('red', group='colors'), 'good')
84 cache.set('vasia', 'foo', 60, group='names')
85 self.assertEqual(cache.get('vasia', group='names'), 'foo')
87 def test_func_invalidation(self):
93 return self.call_count
95 self.assertEqual(my_func(1,2), 1)
96 self.assertEqual(my_func(1,2), 1)
97 self.assertEqual(my_func(3,2), 2)
98 self.assertEqual(my_func(3,2), 2)
99 my_func.invalidate(3,2)
100 self.assertEqual(my_func(1,2), 1)
101 self.assertEqual(my_func(3,2), 3)
102 self.assertEqual(my_func(3,2), 3)
104 def test_method_invalidation(self):
112 return this.call_count
115 self.assertEqual(foo.bar(1), 1)
116 self.assertEqual(foo.bar(1), 1)
117 Foo.bar.invalidate(1)
118 self.assertEqual(foo.bar(1), 2)
120 def test_invalidate_nonexisting(self):
124 foo.invalidate(5) # this shouldn't raise exception
127 class DecoratorTest(ClearMemcachedTest):
129 def test_decorator(self):
132 @cached(60, group='test-group')
133 def my_func(params=""):
134 self._x = self._x + 1
135 return "%d%s" % (self._x, params)
137 self.assertEqual(my_func(), "1")
138 self.assertEqual(my_func(), "1")
140 self.assertEqual(my_func("x"), "2x")
141 self.assertEqual(my_func("x"), "2x")
143 self.assertEqual(my_func("Василий"), "3Василий")
144 self.assertEqual(my_func("Василий"), "3Василий")
146 self.assertEqual(my_func("й"*240), "4"+"й"*240)
147 self.assertEqual(my_func("й"*240), "4"+"й"*240)
149 self.assertEqual(my_func("Ы"*500), "5"+"Ы"*500)
150 self.assertEqual(my_func("Ы"*500), "5"+"Ы"*500)