-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_cache.py
More file actions
369 lines (298 loc) · 8.79 KB
/
test_cache.py
File metadata and controls
369 lines (298 loc) · 8.79 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# -*- coding: future_fstrings -*-
import os
import time
import pytest
from caching import Cache
from caching.cache import _type_name, _function_name, _type_names, make_key
@pytest.fixture(params=[False, True], ids=['memory', 'file'])
def cache(tmpdir, request):
filepath = request.param and f'{tmpdir}/cache' or None
with Cache(filepath=filepath) as c:
yield c
def test_repr():
c = Cache(maxsize=1, ttl=1, filepath=None, policy='FIFO', only_on_errors=False, x='y')
expected = (
"Cache(maxsize=1, ttl=1, filepath=None, policy='FIFO', "
f"key={make_key}, only_on_errors=False, x='y')"
)
assert repr(c) == expected
keys_and_values = [
(1, 'one'),
(1, 2),
('1', 2),
(b'1', b'one'),
(1, 0.1),
(0.1, 1),
('a', 'b'),
('a' * 999999, 'b' * 999999),
([1, 'a'], {'b': 'c'}),
({'b': 'c'}, [1, 'a']),
({'b': 'c'}, {1, None, '555'}),
({'b', 'c'}, (1, 'z')),
]
test_names = [
None if len(str((k, v))) < 30 else f'{str(k)[:10]}_{str(v)[:10]}'
for k, v in keys_and_values
]
@pytest.mark.parametrize('key, value', keys_and_values, ids=test_names)
def test_cache_set_get_in_clear_del(cache, key, value):
with pytest.raises(KeyError):
cache[key]
cache[key] = value
assert cache[key] == value
# Assert duplicate erors are handled
cache[key] = value
assert cache[key] == value
assert key in cache
assert -999 not in cache
cache.clear()
assert cache.get(key) is None
cache[key] = value
assert cache[key] == value
del cache[key]
assert cache.get(key) is None
with pytest.raises(KeyError):
del cache[key]
def test_duplicates(cache):
assert 1 not in cache
cache[1] = 'one'
assert 1 in cache
assert cache[1] == 'one'
cache[1] = '1'
assert cache[1] == '1'
def test_function_decorator_noargs(cache):
call_count = 0
@cache
def pow(a, b):
time.sleep(0.001) # to make timestamp different in each call
nonlocal call_count
call_count += 1
return a**b
def values():
return list(cache.decode(v) for k, v in cache.storage.items())
assert call_count == 0
assert pow(2, 3) == 8
assert call_count == 1
expected_values = [8]
assert values() == expected_values
assert pow(2, 3) == 8
assert call_count == 1
assert values() == expected_values
assert pow(2, 2) == 4
assert call_count == 2
expected_values = [8, 4]
assert values() == expected_values
assert pow(2, 2) == 4
assert call_count == 2
assert values() == expected_values
def test_function_decorator_with_args(cache):
call_count = 0
@cache(max_size=-1, ttl=-1)
def pow(a, b):
time.sleep(0.001) # to make timestamp different in each call
nonlocal call_count
call_count += 1
return a**b
def values():
c = pow._cache
return list(c.decode(v) for k, v in c.storage.items())
assert call_count == 0
assert pow(2, 3) == 8
assert call_count == 1
expected_values = [8]
assert values() == expected_values
assert pow(2, 3) == 8
assert call_count == 1
assert values() == expected_values
assert pow(2, 2) == 4
assert call_count == 2
expected_values = [8, 4]
assert values() == expected_values
assert pow(2, 2) == 4
assert call_count == 2
assert values() == expected_values
def test_copy(cache):
c = cache
assert c.copy() is not c
assert c.copy(a=2) is not c
assert c.copy(ttl=c.params['ttl'] + 1) is not c
c.copy = lambda *_, **__: 1 / 0
with pytest.raises(ZeroDivisionError):
c(a=1)(lambda x: 1)
def test_raises_if_closed(cache):
cache.close()
cache.close()
with pytest.raises(Exception):
cache[1] = 1
with pytest.raises(Exception):
cache[1]
with pytest.raises(Exception):
cache.get(1)
with pytest.raises(Exception):
cache.get(1, None)
with pytest.raises(Exception):
del cache[1]
with pytest.raises(Exception):
cache[1] = 1
def test_remove(tmpdir, cache):
tmpdir = str(tmpdir)
cache.remove()
filepath = f'{tmpdir}/cache'
cache = Cache(filepath=filepath)
assert os.path.isfile(filepath)
assert os.listdir(tmpdir) == ['cache']
cache[1] = 'one'
cache[2] = 'two'
assert os.path.isfile(filepath)
assert os.listdir(tmpdir) == ['cache']
cache.remove()
assert not os.path.isfile(filepath)
assert os.listdir(tmpdir) == []
def test_types(cache):
assert 1.0 == 1
assert make_key(1.0) == make_key(1)
assert _type_name(1.0) != _type_name(1)
assert _type_names((1.0,), {}) != _type_names((1,), {})
call_count = 0
@cache
def func(a):
nonlocal call_count
call_count += 1
return a
assert call_count == 0
assert func(1) == 1
assert call_count == 1
assert func(1) == 1
assert call_count == 1
assert func(1.0) == 1.0
assert call_count == 2
assert isinstance(func(1.0), float)
assert call_count == 2
def test_make_key():
assert make_key(1) == (1,)
assert make_key() == ()
assert make_key(1, 2) == (1, 2)
assert make_key(one=1) == ((), 'one', 1)
assert make_key(1, 2, one=1) == ((1, 2), 'one', 1)
@pytest.mark.parametrize(
'obj, expected', [
(1, 'builtins.int'),
('', 'builtins.str'),
([], 'builtins.list'),
({}, 'builtins.dict'),
(set(), 'builtins.set'),
(object(), 'builtins.object'),
(os, 'builtins.module'),
(lambda: 1, 'builtins.function'),
(Cache().get, 'builtins.method'),
(Cache, 'builtins.type'),
(None, 'builtins.NoneType'),
],
)
def test__type_name(obj, expected):
assert _type_name(obj) == expected
def test__type_names():
def test(expected, *args, **kwargs):
assert _type_names(args, kwargs) == expected
expected = (
('builtins.str', 'builtins.int',),
('builtins.list', 'builtins.int'),
)
test(expected, 'a', 1, one=1, l=[1, 2])
@pytest.mark.parametrize(
'obj, expected', [
(lambda: 1, f'{__name__}.<lambda>'),
(cache, f'{__name__}.cache'),
],
)
def test__function_name(obj, expected):
assert _function_name(obj) == expected
def test_fn_not_callable(cache):
with pytest.raises(TypeError):
cache()(1)
def test_items(cache):
assert [(k, v) for k, v in cache.items()] == []
cache[1] = 'one'
assert next(cache.items()) == (1, 'one')
time.sleep(0.001)
cache[2] = 'two'
assert [(k, v) for k, v in cache.items()] == [(1, 'one'), (2, 'two')]
@pytest.mark.parametrize('storage', ['file', 'memory'])
def test_lru(tmpdir, storage):
filepath = None if storage == 'memory' else f'{tmpdir}/cache'
cache = Cache(filepath=filepath, maxsize=2, ttl=-1, policy='LRU')
@cache
def func(a):
return a
def keys():
return [arg for (fn_name, arg), v in cache.items()]
assert func(1) == 1
assert func(2) == 2
the_keys = keys()
assert len(the_keys) == 2
assert 1 in the_keys and 2 in the_keys
assert func(1) == 1
assert func(3) == 3
the_keys = keys()
assert len(the_keys) == 2
assert 1 in the_keys and 3 in the_keys
assert 2 not in the_keys
@pytest.mark.parametrize('storage', ['file', 'memory'])
def test_lfu(tmpdir, storage):
filepath = None if storage == 'memory' else f'{tmpdir}/cache'
cache = Cache(filepath=filepath, maxsize=2, ttl=-1, policy='LFU')
@cache
def func(a):
return a
def keys():
return [arg for (fn_name, arg), v in cache.items()]
assert func(1) == 1
assert func(2) == 2
the_keys = keys()
assert len(the_keys) == 2
assert 1 in the_keys and 2 in the_keys
assert func(1) == 1
assert func(1) == 1
assert func(2) == 2
assert func(2) == 2
assert func(3) == 3
the_keys = keys()
assert len(the_keys) == 2
assert 1 in the_keys and 2 in the_keys
assert 3 not in the_keys
def test_only_on_errors():
x = 2
call_count = 0
raised = False
n = 0
@Cache(only_on_errors=(ZeroDivisionError, ConnectionError))
def fn():
nonlocal x, n, call_count, raised
call_count += 1
try:
0 / x
except ZeroDivisionError:
raised = True
raise
n += 1
x -= 1
return n
assert fn() == 1
assert call_count == 1
assert fn() == 2
assert call_count == 2
assert x == 0
assert fn() == 2 # ZeroDivision and returning cached result
assert call_count == 3
assert raised
assert fn() == 2
x = 1
raised = False
assert fn() == 3
assert call_count == 5
assert fn() == 3 # ZeroDivision and returning cached result
assert raised
assert call_count == 6
fn._cache.clear()
with pytest.raises(ZeroDivisionError):
fn()