forked from learningequality/sushi-chef-ubongokids
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_cache.py
65 lines (53 loc) · 1.86 KB
/
test_cache.py
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
import unittest
from cache import Db
from tempfile import mkdtemp
class TestCacheDbMethods(unittest.TestCase):
def setUp(self):
dirname = mkdtemp(suffix='caching', prefix='teststorage')
self.cache = Db(dirname, 'rawcaching').__enter__()
def tearDown(self):
self.cache.__exit__(None, None, None)
def test_add_found(self):
self.cache.add(key='hola', data=u'mundo!')
found, data = self.cache.get('hola')
assert found
assert data == 'mundo!'
stats = self.cache.stats()
assert stats['misses'] == 0
assert stats['hits'] == 1
def test_add_not_found(self):
found, data = self.cache.get('hola')
assert not found
assert not data
stats = self.cache.stats()
assert stats['misses'] == 1
assert stats['hits'] == 0
def test_remove(self):
self.cache.add(key='hola', data=u'mundo!')
self.cache.remove('hola')
found, data = self.cache.get('hola')
assert not found
assert not data
def test_change(self):
self.cache.add(key='hola', data=u'mundo!')
found, data = self.cache.get('hola')
assert found
assert data == 'mundo!'
self.cache.add(key='hola', data=u'world!')
found, data = self.cache.get('hola')
assert found
assert data == 'world!'
def test_hits_and_misses(self):
members = 10
times = 5
# Exercise the cache
for i in range(0, members * times):
found, data = self.cache.get(str(i % members))
if not found:
key = str(i)
self.cache.add(key=key, data='value_{}'.format(key))
stats = self.cache.stats()
assert stats['misses'] == members
assert stats['hits'] == (members * times) - members
if __name__ == '__main__':
unittest.main()