forked from thadeusb/flask-cache
/
test_cache.py
451 lines (319 loc) · 13 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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
from __future__ import with_statement
import unittest
import time
import random
from flask import Flask
from flask.ext.cache import Cache
class CacheTestCase(unittest.TestCase):
def setUp(self):
app = Flask(__name__)
app.debug = False
app.config['CACHE_TYPE'] = 'simple'
self.cache = Cache(app)
self.app = app
def tearDown(self):
self.app = None
self.cache = None
self.tc = None
def test_00_set(self):
self.cache.set('hi', 'hello')
assert self.cache.get('hi') == 'hello'
def test_01_add(self):
self.cache.add('hi', 'hello')
assert self.cache.get('hi') == 'hello'
self.cache.add('hi', 'foobar')
assert self.cache.get('hi') == 'hello'
def test_02_delete(self):
self.cache.set('hi', 'hello')
self.cache.delete('hi')
assert self.cache.get('hi') is None
def test_03_cached_view(self):
@self.app.route('/')
@self.cache.cached(5)
def cached_view():
return str(time.time())
tc = self.app.test_client()
rv = tc.get('/')
the_time = rv.data
time.sleep(2)
rv = tc.get('/')
assert the_time == rv.data
time.sleep(5)
rv = tc.get('/')
assert the_time != rv.data
def test_04_cached_view_unless(self):
@self.app.route('/a')
@self.cache.cached(5, unless=lambda: True)
def non_cached_view():
return str(time.time())
@self.app.route('/b')
@self.cache.cached(5, unless=lambda: False)
def cached_view():
return str(time.time())
tc = self.app.test_client()
rv = tc.get('/a')
the_time = rv.data
time.sleep(1)
rv = tc.get('/a')
assert the_time != rv.data
rv = tc.get('/b')
the_time = rv.data
time.sleep(1)
rv = tc.get('/b')
assert the_time == rv.data
def test_05_cached_function(self):
with self.app.test_request_context():
@self.cache.cached(2, key_prefix='MyBits')
def get_random_bits():
return [random.randrange(0, 2) for i in range(50)]
my_list = get_random_bits()
his_list = get_random_bits()
assert my_list == his_list
time.sleep(4)
his_list = get_random_bits()
assert my_list != his_list
def test_06_memoize(self):
with self.app.test_request_context():
@self.cache.memoize(5)
def big_foo(a, b):
return a+b+random.randrange(0, 100000)
result = big_foo(5, 2)
time.sleep(1)
assert big_foo(5, 2) == result
result2 = big_foo(5, 3)
assert result2 != result
time.sleep(4)
assert big_foo(5, 2) != result
assert big_foo(5, 3) == result2
time.sleep(1)
assert big_foo(5, 3) != result2
def test_07_delete_memoize(self):
with self.app.test_request_context():
@self.cache.memoize(5)
def big_foo(a, b):
return a+b+random.randrange(0, 100000)
result = big_foo(5, 2)
result2 = big_foo(5, 3)
time.sleep(1)
assert big_foo(5, 2) == result
assert big_foo(5, 2) == result
assert big_foo(5, 3) != result
assert big_foo(5, 3) == result2
self.cache.delete_memoized(big_foo)
assert big_foo(5, 2) != result
assert big_foo(5, 3) != result2
def test_08_delete_memoize(self):
with self.app.test_request_context():
@self.cache.memoize()
def big_foo(a, b):
return a+b+random.randrange(0, 100000)
result_a = big_foo(5, 1)
result_b = big_foo(5, 2)
assert big_foo(5, 1) == result_a
assert big_foo(5, 2) == result_b
self.cache.delete_memoized(big_foo, 5, 2)
assert big_foo(5, 1) == result_a
assert big_foo(5, 2) != result_b
def test_09_args_memoize(self):
with self.app.test_request_context():
@self.cache.memoize()
def big_foo(a, b):
return sum(a)+sum(b)+random.randrange(0, 100000)
result_a = big_foo([5,3,2], [1])
result_b = big_foo([3,3], [3,1])
assert big_foo([5,3,2], [1]) == result_a
assert big_foo([3,3], [3,1]) == result_b
self.cache.delete_memoized(big_foo, [5,3,2], [1])
assert big_foo([5,3,2], [1]) != result_a
assert big_foo([3,3], [3,1]) == result_b
def test_10_kwargs_memoize(self):
with self.app.test_request_context():
@self.cache.memoize()
def big_foo(a, b=None):
return a+sum(b.values())+random.randrange(0, 100000)
result_a = big_foo(1, dict(one=1,two=2))
result_b = big_foo(5, dict(three=3,four=4))
assert big_foo(1, dict(one=1,two=2)) == result_a
assert big_foo(5, dict(three=3,four=4)) == result_b
self.cache.delete_memoized(big_foo, 1, dict(one=1,two=2))
assert big_foo(1, dict(one=1,two=2)) != result_a
assert big_foo(5, dict(three=3,four=4)) == result_b
def test_10a_kwargonly_memoize(self):
@self.cache.memoize()
def big_foo(a=None):
if a is None:
a = 0
return a+random.random()
result_a = big_foo()
result_b = big_foo(5)
assert big_foo() == result_a
assert big_foo() < 1
assert big_foo(5) == result_b
assert big_foo(5) >= 5 and big_foo(5) < 6
def test_10a_arg_kwarg_memoize(self):
@self.cache.memoize()
def f(a, b=0):
pass
f(1)
f(1)
def test_10b_classarg_memoize(self):
@self.cache.memoize()
def bar(a):
return a.value + random.random()
class Adder(object):
def __init__(self, value):
self.value = value
adder = Adder(15)
adder2 = Adder(20)
y = bar(adder)
z = bar(adder2)
assert y != z
assert bar(adder) == y
assert bar(adder) != z
adder.value = 14
assert bar(adder) == y
assert bar(adder) != z
assert bar(adder) != bar(adder2)
assert bar(adder2) == z
def test_10c_classfunc_memoize(self):
class Adder(object):
def __init__(self, initial):
self.initial = initial
@self.cache.memoize()
def add(self, b):
return self.initial + b
adder1 = Adder(1)
adder2 = Adder(2)
x = adder1.add(3)
assert adder1.add(3) == x
assert adder1.add(4) != x
assert adder1.add(3) != adder2.add(3)
def test_11_cache_key_property(self):
@self.app.route('/')
@self.cache.cached(5)
def cached_view():
return str(time.time())
assert hasattr(cached_view, "make_cache_key")
assert callable(cached_view.make_cache_key)
tc = self.app.test_client()
rv = tc.get('/')
the_time = rv.data
with self.app.test_request_context():
cache_data = self.cache.get(cached_view.make_cache_key())
assert the_time == cache_data
def test_12_make_cache_key_function_property(self):
@self.app.route('/<foo>/<bar>')
@self.cache.memoize(5)
def cached_view(foo, bar):
return str(time.time())
assert hasattr(cached_view, "make_cache_key")
assert callable(cached_view.make_cache_key)
tc = self.app.test_client()
rv = tc.get('/a/b')
the_time = rv.data
cache_key = cached_view.make_cache_key(cached_view.uncached, foo=u"a", bar=u"b")
cache_data = self.cache.get(cache_key)
assert the_time == cache_data
different_key = cached_view.make_cache_key(cached_view.uncached, foo=u"b", bar=u"a")
different_data = self.cache.get(different_key)
assert the_time != different_data
def test_13_cache_timeout_property(self):
@self.app.route('/')
@self.cache.memoize(5)
def cached_view1():
return str(time.time())
@self.app.route('/<foo>/<bar>')
@self.cache.memoize(10)
def cached_view2(foo, bar):
return str(time.time())
assert hasattr(cached_view1, "cache_timeout")
assert hasattr(cached_view2, "cache_timeout")
assert cached_view1.cache_timeout == 5
assert cached_view2.cache_timeout == 10
# test that this is a read-write property
cached_view1.cache_timeout = 2
cached_view2.cache_timeout = 3
assert cached_view1.cache_timeout == 2
assert cached_view2.cache_timeout == 3
tc = self.app.test_client()
rv1 = tc.get('/')
time1 = rv1.data
time.sleep(1)
rv2 = tc.get('/a/b')
time2 = rv2.data
# VIEW1
# it's been 1 second, cache is still active
assert time1 == tc.get('/').data
time.sleep(2)
# it's been 3 seconds, cache is not still active
assert time1 != tc.get('/').data
# VIEW2
# it's been 2 seconds, cache is still active
assert time2 == tc.get('/a/b').data
time.sleep(2)
# it's been 4 seconds, cache is not still active
assert time2 != tc.get('/a/b').data
def test_14_memoized_multiple_arg_kwarg_calls(self):
with self.app.test_request_context():
@self.cache.memoize()
def big_foo(a, b,c=[1,1],d=[1,1]):
return sum(a)+sum(b)+sum(c)+sum(d)+random.randrange(0, 100000)
result_a = big_foo([5,3,2], [1], c=[3,3], d=[3,3])
assert big_foo([5,3,2], [1], d=[3,3], c=[3,3]) == result_a
assert big_foo(b=[1],a=[5,3,2],c=[3,3],d=[3,3]) == result_a
assert big_foo([5,3,2], [1], [3,3], [3,3]) == result_a
def test_15_memoize_multiple_arg_kwarg_delete(self):
with self.app.test_request_context():
@self.cache.memoize()
def big_foo(a, b,c=[1,1],d=[1,1]):
return sum(a)+sum(b)+sum(c)+sum(d)+random.randrange(0, 100000)
result_a = big_foo([5,3,2], [1], c=[3,3], d=[3,3])
self.cache.delete_memoized(big_foo, [5,3,2],[1],[3,3],[3,3])
result_b = big_foo([5,3,2], [1], c=[3,3], d=[3,3])
assert result_a != result_b
self.cache.delete_memoized(big_foo, [5,3,2],b=[1],c=[3,3],d=[3,3])
result_b = big_foo([5,3,2], [1], c=[3,3], d=[3,3])
assert result_a != result_b
self.cache.delete_memoized(big_foo, [5,3,2],[1],c=[3,3],d=[3,3])
result_a = big_foo([5,3,2], [1], c=[3,3], d=[3,3])
assert result_a != result_b
self.cache.delete_memoized(big_foo, [5,3,2],b=[1],c=[3,3],d=[3,3])
result_a = big_foo([5,3,2], [1], c=[3,3], d=[3,3])
assert result_a != result_b
self.cache.delete_memoized(big_foo, [5,3,2],[1],c=[3,3],d=[3,3])
result_b = big_foo([5,3,2], [1], c=[3,3], d=[3,3])
assert result_a != result_b
self.cache.delete_memoized(big_foo, [5,3,2],[1],[3,3],[3,3])
result_a = big_foo([5,3,2], [1], c=[3,3], d=[3,3])
assert result_a != result_b
def test_16_memoize_kwargs_to_args(self):
with self.app.test_request_context():
def big_foo(a, b, c=None, d=None):
return sum(a)+sum(b)+random.randrange(0, 100000)
expected = (1,2,'foo','bar')
args, kwargs = self.cache.memoize_kwargs_to_args(big_foo, 1,2,'foo','bar')
assert (args == expected)
args, kwargs = self.cache.memoize_kwargs_to_args(big_foo, 2,'foo','bar',a=1)
assert (args == expected)
args, kwargs = self.cache.memoize_kwargs_to_args(big_foo, a=1,b=2,c='foo',d='bar')
assert (args == expected)
args, kwargs = self.cache.memoize_kwargs_to_args(big_foo, d='bar',b=2,a=1,c='foo')
assert (args == expected)
args, kwargs = self.cache.memoize_kwargs_to_args(big_foo, 1,2,d='bar',c='foo')
assert (args == expected)
def test_17_dict_config(self):
cache = Cache(config={'CACHE_TYPE': 'simple'})
cache.init_app(self.app)
assert cache.config['CACHE_TYPE'] == 'simple'
def test_18_dict_config_initapp(self):
cache = Cache()
cache.init_app(self.app, config={'CACHE_TYPE': 'simple'})
from werkzeug.contrib.cache import SimpleCache
assert isinstance(self.app.extensions['cache'], SimpleCache)
def test_19_dict_config_both(self):
cache = Cache(config={'CACHE_TYPE': 'null'})
cache.init_app(self.app, config={'CACHE_TYPE': 'simple'})
from werkzeug.contrib.cache import SimpleCache
assert isinstance(self.app.extensions['cache'], SimpleCache)
if __name__ == '__main__':
unittest.main()