remove simplejson dependency
[plcapi.git] / cache_utils / tests.py
1 #coding: utf-8
2
3 from unittest import TestCase
4
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
8
9 def foo(a,b):
10     pass
11
12 class Foo(object):
13     def foo(self, a, b):
14         pass
15     @classmethod
16     def bar(cls, x):
17         pass
18
19 class FuncTypeTest(TestCase):
20     def assertFuncType(self, func, tp):
21         self.assertEqual(_func_type(func), tp)
22
23     def test_func(self):
24         self.assertFuncType(foo, 'function')
25
26     def test_method(self):
27         self.assertFuncType(Foo.foo, 'method')
28
29     def test_classmethod(self):
30         self.assertFuncType(Foo.bar, 'classmethod')
31
32
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)
38
39     def test_func(self):
40         self.assertFuncInfo(foo, [1,2], 'cache_utils.tests.foo', [1,2])
41
42     def test_method(self):
43         foo_obj = Foo()
44         self.assertFuncInfo(Foo.foo, [foo_obj, 1, 2],
45                             'cache_utils.tests.Foo.foo', [1,2])
46
47     def test_classmethod(self):
48         self.assertFuncInfo(Foo.bar, [Foo, 1],
49                             'cache_utils.tests.Foo.bar', [1])
50
51
52 class SanitizeTest(TestCase):
53     def test_sanitize_keys(self):
54         key = u"12345678901234567890123456789012345678901234567890"
55         self.assertTrue(len(key) >= 40)
56         key = sanitize_memcached_key(key, 40)
57         self.assertTrue(len(key) <= 40)
58
59
60 class ClearMemcachedTest(TestCase):
61     def tearDown(self):
62         cache._cache.flush_all()
63
64     def setUp(self):
65         cache._cache.flush_all()
66
67
68 class InvalidationTest(ClearMemcachedTest):
69
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')
74
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')
78
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')
83
84         cache.set('vasia', 'foo', 60, group='names')
85         self.assertEqual(cache.get('vasia', group='names'), 'foo')
86
87     def test_func_invalidation(self):
88         self.call_count = 0
89
90         @cached(60)
91         def my_func(a, b):
92             self.call_count += 1
93             return self.call_count
94
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)
103
104     def test_method_invalidation(self):
105         self.call_count = 0
106         this = self
107
108         class Foo(object):
109             @cached(60)
110             def bar(self, x):
111                 this.call_count += 1
112                 return this.call_count
113
114         foo = Foo()
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)
119
120     def test_invalidate_nonexisting(self):
121         @cached(60)
122         def foo(x):
123             return 1
124         foo.invalidate(5) # this shouldn't raise exception
125
126
127 class DecoratorTest(ClearMemcachedTest):
128
129     def test_decorator(self):
130         self._x = 0
131
132         @cached(60, group='test-group')
133         def my_func(params=""):
134             self._x = self._x + 1
135             return u"%d%s" % (self._x, params)
136
137         self.assertEqual(my_func(), "1")
138         self.assertEqual(my_func(), "1")
139
140         self.assertEqual(my_func("x"), u"2x")
141         self.assertEqual(my_func("x"), u"2x")
142
143         self.assertEqual(my_func(u"Василий"), u"3Василий")
144         self.assertEqual(my_func(u"Василий"), u"3Василий")
145
146         self.assertEqual(my_func(u"й"*240), u"4"+u"й"*240)
147         self.assertEqual(my_func(u"й"*240), u"4"+u"й"*240)
148
149         self.assertEqual(my_func(u"Ы"*500), u"5"+u"Ы"*500)
150         self.assertEqual(my_func(u"Ы"*500), u"5"+u"Ы"*500)