-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_simplecache.py
More file actions
141 lines (107 loc) · 3.75 KB
/
test_simplecache.py
File metadata and controls
141 lines (107 loc) · 3.75 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
import time
import simplecache
import pytest
class TestBasics:
"""Test core functionality."""
def test_set(self):
cache = simplecache.Cache()
cache.set('pin', 1077)
v, expires = cache.storage['pin']
assert v == 1077
def test_get(self):
cache = simplecache.Cache()
cache.set('pin', 1077)
v = cache.get('pin')
assert v == 1077
v = cache.get('balance', 0.93)
assert v == 0.93
def test_expiration(self):
cache = simplecache.Cache()
cache.set('pin', 1077, ttl=.1)
time.sleep(.1)
with pytest.raises(KeyError, match=r"pin"):
cache.get('pin')
def test_exists(self):
cache = simplecache.Cache()
cache.set('pin', 1077)
assert cache.exists('pin')
assert not cache.exists('balance')
def test_clear(self):
cache = simplecache.Cache()
cache.set('pin', 1077)
cache.clear()
assert len(cache.storage) == 0
def test_iter(self):
cache = simplecache.Cache()
cache.set('captain', 'leela')
cache.set('delivery-boy', 'fry')
cache.set('cook', 'bender')
d = dict(cache)
assert d == {'captain': 'leela', 'delivery-boy': 'fry', 'cook': 'bender'}
class TestConstructor:
"""Test constructor args."""
def test_bad_ttl(self):
with pytest.raises(ValueError, match=r"ttl must be greater than 0"):
simplecache.Cache(ttl=-1)
def test_bad_maxsize(self):
with pytest.raises(ValueError, match=r"maxsize must be greater than 0"):
simplecache.Cache(maxsize=-1)
class TestExpiration:
"""Test expiration works correctly."""
def test_default_ttl(self):
cache = simplecache.Cache(ttl=.1)
cache.set('pin', 1077)
v = cache.get('pin')
assert v == 1077
time.sleep(.1)
with pytest.raises(KeyError, match=r"pin"):
cache.get('pin')
# make sure storage objects get cleared
assert not cache.storage
assert not cache.index
def test_per_key_ttl(self):
cache = simplecache.Cache(ttl=.1)
cache.set('pin', 1077, ttl=.2) # set ttl explicitly
time.sleep(.1)
v = cache.get('pin')
assert v == 1077
time.sleep(.2)
with pytest.raises(KeyError, match=r"pin"):
cache.get('pin')
def test_per_key_none(self):
cache = simplecache.Cache(ttl=.1)
cache.set('pin', 1077, ttl=None)
time.sleep(.2)
v = cache.get('pin')
assert v == 1077
def test_sorting(self):
cache = simplecache.Cache(ttl=1)
cache.set('captain', 'leela')
cache.set('delivery-boy', 'fry')
cache.set('pin', 1077, ttl=.1) # insert in middle
cache.set('cook', 'bender')
time.sleep(.1)
with pytest.raises(KeyError, match=r"pin"):
cache.get('pin')
assert cache.get('captain') == 'leela'
class TestMaxSize:
"""Test maxsize eviction."""
def test_eviction(self):
cache = simplecache.Cache(maxsize=1, ttl=1)
cache.set('captain', 'leela')
cache.set('delivery-boy', 'fry')
cache.set('cook', 'bender')
# most stale entry should get evicted first
with pytest.raises(KeyError, match=r"captain"):
cache.get('captain')
with pytest.raises(KeyError, match=r"delivery-boy"):
cache.get('delivery-boy')
assert cache.get('cook') == 'bender'
class TestSetKeyAgain:
"""Test index doesn't end up with duplicate entries."""
def test_set_twice(self):
cache = simplecache.Cache(ttl=60)
cache.set('pin', 1077)
cache.set('pin', 1077)
assert len(cache.storage) == 1
assert len(cache.index) == 1