-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_cachecore.py
More file actions
executable file
·190 lines (156 loc) · 5.46 KB
/
test_cachecore.py
File metadata and controls
executable file
·190 lines (156 loc) · 5.46 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import time
import unittest
import tempfile
import shutil
import collections
from unittest import TestCase
import cachecore as cache
try:
import redis
try:
from redis.exceptions import ConnectionError as RedisConnectionError
cache.RedisCache(key_prefix='werkzeug-test-case:')._client.set('test','connection')
except RedisConnectionError:
redis = None
except ImportError:
redis = None
class SimpleCacheTestCase(TestCase):
def test_get_dict(self):
c = cache.SimpleCache()
c.set('a', 'a')
c.set('b', 'b')
d = c.get_dict('a', 'b')
assert 'a' in d
assert 'a' == d['a']
assert 'b' in d
assert 'b' == d['b']
def test_set_many(self):
c = cache.SimpleCache()
c.set_many({0: 0, 1: 1, 2: 4})
assert c.get(2) == 4
c.set_many((i, i*i) for i in xrange(3))
assert c.get(2) == 4
class FileSystemCacheTestCase(TestCase):
def test_set_get(self):
tmp_dir = tempfile.mkdtemp()
try:
c = cache.FileSystemCache(cache_dir=tmp_dir)
for i in range(3):
c.set(str(i), i * i)
for i in range(3):
result = c.get(str(i))
assert result == i * i
finally:
shutil.rmtree(tmp_dir)
def test_filesystemcache_prune(self):
THRESHOLD = 13
tmp_dir = tempfile.mkdtemp()
c = cache.FileSystemCache(cache_dir=tmp_dir, threshold=THRESHOLD)
for i in range(2 * THRESHOLD):
c.set(str(i), i)
cache_files = os.listdir(tmp_dir)
shutil.rmtree(tmp_dir)
assert len(cache_files) <= THRESHOLD
def test_filesystemcache_clear(self):
tmp_dir = tempfile.mkdtemp()
c = cache.FileSystemCache(cache_dir=tmp_dir)
c.set('foo', 'bar')
cache_files = os.listdir(tmp_dir)
assert len(cache_files) == 1
c.clear()
cache_files = os.listdir(tmp_dir)
assert len(cache_files) == 0
shutil.rmtree(tmp_dir)
class MappingTestCase(TestCase):
def test_should_have_MutableMapping_methods_and_inherit_from_it(self):
self.assertIn(collections.MutableMapping, cache.BaseCache.mro())
my_cache = cache.BaseCache()
expected_methods = ['setitem', 'getitem', 'delitem', 'iter', 'len']
actual_methods = dir(my_cache)
for method in expected_methods:
self.assertIn('__{}__'.format(method), actual_methods)
def test_should_be_able_to_use_it_as_a_dict(self):
my_cache = cache.SimpleCache()
my_cache['python'] = 'rules'
my_cache.set('answer', '42')
self.assertEquals(my_cache.get('python'), 'rules')
self.assertEquals(my_cache['answer'], '42')
del my_cache['python']
my_cache.delete('answer')
self.assertEquals(my_cache['python'], None)
self.assertEquals(my_cache['answer'], None)
# class RedisCacheTestCase(TestCase):
# def make_cache(self):
# return cache.RedisCache(key_prefix='werkzeug-test-case:')
# def teardown(self):
# self.make_cache().clear()
# def test_compat(self):
# c = self.make_cache()
# c._client.set(c.key_prefix + 'foo', 'Awesome')
# self.assert_equal(c.get('foo'), 'Awesome')
# c._client.set(c.key_prefix + 'foo', '42')
# self.assert_equal(c.get('foo'), 42)
# def test_get_set(self):
# c = self.make_cache()
# c.set('foo', ['bar'])
# assert c.get('foo') == ['bar']
# def test_get_many(self):
# c = self.make_cache()
# c.set('foo', ['bar'])
# c.set('spam', 'eggs')
# assert c.get_many('foo', 'spam') == [['bar'], 'eggs']
# def test_set_many(self):
# c = self.make_cache()
# c.set_many({'foo': 'bar', 'spam': ['eggs']})
# assert c.get('foo') == 'bar'
# assert c.get('spam') == ['eggs']
# def test_expire(self):
# c = self.make_cache()
# c.set('foo', 'bar', 1)
# time.sleep(2)
# assert c.get('foo') is None
# def test_add(self):
# c = self.make_cache()
# # sanity check that add() works like set()
# c.add('foo', 'bar')
# assert c.get('foo') == 'bar'
# c.add('foo', 'qux')
# assert c.get('foo') == 'bar'
# def test_delete(self):
# c = self.make_cache()
# c.add('foo', 'bar')
# assert c.get('foo') == 'bar'
# c.delete('foo')
# assert c.get('foo') is None
# def test_delete_many(self):
# c = self.make_cache()
# c.add('foo', 'bar')
# c.add('spam', 'eggs')
# c.delete_many('foo', 'spam')
# assert c.get('foo') is None
# assert c.get('spam') is None
# def test_inc_dec(self):
# c = self.make_cache()
# c.set('foo', 1)
# assert c.inc('foo') == 2
# assert c.dec('foo') == 1
# c.delete('foo')
# def test_true_false(self):
# c = self.make_cache()
# c.set('foo', True)
# assert c.get('foo') == True
# c.set('bar', False)
# assert c.get('bar') == False
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(SimpleCacheTestCase))
suite.addTest(unittest.makeSuite(FileSystemCacheTestCase))
# if redis is not None:
# suite.addTest(unittest.makeSuite(RedisCacheTestCase))
return suite
if __name__ == '__main__':
# print suite()
unittest.main()