forked from cool-RR/python_toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cache.py
More file actions
232 lines (159 loc) · 6.21 KB
/
test_cache.py
File metadata and controls
232 lines (159 loc) · 6.21 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
# Copyright 2009-2017 Ram Rachum.
# This program is distributed under the MIT license.
'''Testing module for `python_toolbox.caching.cache`.'''
import datetime as datetime_module
import re
import weakref
from python_toolbox import caching
from python_toolbox.caching import cache
from python_toolbox import misc_tools
from python_toolbox import temp_value_setting
from python_toolbox import cute_testing
from python_toolbox import gc_tools
@misc_tools.set_attributes(i=0)
def counting_func(a=1, b=2, *args, **kwargs):
'''Function that returns a bigger number every time.'''
try:
return counting_func.i
finally:
counting_func.i += 1
def test_basic():
'''Test basic workings of `cache`.'''
f = cache()(counting_func)
assert f() == f() == f(1, 2) == f(a=1, b=2)
assert f() != f('boo')
assert f('boo') == f('boo') == f(a='boo')
assert f('boo') != f(meow='frrr')
assert f(meow='frrr') == f(1, meow='frrr') == f(a=1, meow='frrr')
def test_weakref():
'''Test that `cache` weakrefs weakreffable arguments.'''
f = cache()(counting_func)
class A: pass
a = A()
result = f(a)
assert result == f(a) == f(a) == f(a)
a_ref = weakref.ref(a)
del a
gc_tools.collect()
assert a_ref() is None
a = A()
result = f(meow=a)
assert result == f(meow=a) == f(meow=a) == f(meow=a)
a_ref = weakref.ref(a)
del a
gc_tools.collect()
assert a_ref() is None
def test_lru():
'''Test the least-recently-used algorithm for forgetting cached results.'''
f = cache(max_size=3)(counting_func)
r0, r1, r2 = f(0), f(1), f(2)
assert f(0) == f(0) == r0 == f(0)
assert f(1) == f(1) == r1 == f(1)
assert f(2) == f(2) == r2 == f(2)
r3 = f(3)
assert f(0) != r0 # Now we recalculated `f(0)` so we forgot `f(1)`
assert f(2) == f(2) == r2 == f(2)
assert f(3) == f(3) == r3 == f(3)
new_r1 = f(1)
# Requesting these:
f(3)
f(1)
# So `f(2)` will be the least-recently-used.
r4 = f(4) # Now `f(2)` has been thrown out.
new_r2 = f(2) # And now `f(3)` is thrown out
assert f(2) != r2
assert f(1) == new_r1 == f(1)
assert f(4) == r4 == f(4)
assert f(2) == new_r2 == f(2)
# Now `f(1)` is the least-recently-used.
r5 = f(5) # Now `f(1)` has been thrown out.
assert f(4) == r4 == f(4)
assert f(5) == r5 == f(5)
assert f(1) != new_r1
def test_unhashable_arguments():
'''Test `cache` works with unhashable arguments.'''
f = cache()(counting_func)
x = {1, 2}
assert f(x) == f(x)
assert f(7, x) != f(8, x)
assert f('boo') != f(meow='frrr')
y = {1: [1, 2], 2: frozenset([3, 'b'])}
assert f(meow=y) == f(1, meow=y)
def test_helpful_message_when_forgetting_parentheses():
'''Test user gets a helpful exception when when forgetting parentheses.'''
def confusedly_forget_parentheses():
@cache
def f(): pass
with cute_testing.RaiseAssertor(
TypeError,
'It seems that you forgot to add parentheses after @cache when '
'decorating the f function.'
):
confusedly_forget_parentheses()
def test_signature_preservation():
'''Test that a function's signature is preserved after decorating.'''
f = cache()(counting_func)
assert f() == f() == f(1, 2) == f(a=1, b=2)
cute_testing.assert_same_signature(f, counting_func)
def my_func(qq, zz=1, yy=2, *args): pass
my_func_cached = cache(max_size=7)(my_func)
cute_testing.assert_same_signature(my_func, my_func_cached)
def my_other_func(**kwargs): pass
my_func_cached = cache()(my_func)
cute_testing.assert_same_signature(my_func, my_func_cached)
def test_api():
'''Test the API of cached functions.'''
f = cache()(counting_func)
g = cache(max_size=3)(counting_func)
for cached_function in (f, g):
assert not hasattr(cached_function, 'cache')
cute_testing.assert_polite_wrapper(cached_function, counting_func)
result_1 = cached_function(1)
assert cached_function(1) == result_1 == cached_function(1)
cached_function.cache_clear()
result_2 = cached_function(1)
assert cached_function(1) == result_2 == cached_function(1)
assert result_1 != result_2 == cached_function(1) != result_1
# Asserting we're not using `dict.clear` or something:
assert cached_function.cache_clear.__name__ == 'cache_clear'
def test_double_caching():
'''Test that `cache` detects and prevents double-caching of functions.'''
f = cache()(counting_func)
g = cache()(f)
assert f is g
def test_time_to_keep():
counting_func.i = 0 # Resetting so we could refer to hard numbers
# without worrying whether other tests made `i` higher.
f = cache(time_to_keep={'days': 356})(counting_func)
print(f('zero'))
assert f('zero') == 0 # Just to get rid of zero
assert f('a') == 1
assert f('b') == 2
assert f('c') == 3
assert f('b') == 2
start_datetime = datetime_module.datetime.now()
fixed_time = start_datetime
def _mock_now():
return fixed_time
with temp_value_setting.TempValueSetter(
(caching.decorators, '_get_now'), _mock_now):
assert list(map(f, 'abc')) == [1, 2, 3]
fixed_time += datetime_module.timedelta(days=100)
assert list(map(f, 'abc')) == [1, 2, 3]
assert list(map(f, 'def')) == [4, 5, 6]
fixed_time += datetime_module.timedelta(days=100)
assert list(map(f, 'abc')) == [1, 2, 3]
assert list(map(f, 'def')) == [4, 5, 6]
fixed_time += datetime_module.timedelta(days=100)
assert list(map(f, 'abc')) == [1, 2, 3]
assert list(map(f, 'def')) == [4, 5, 6]
fixed_time += datetime_module.timedelta(days=100)
assert list(map(f, 'abc')) == [7, 8, 9]
assert list(map(f, 'def')) == [4, 5, 6]
fixed_time += datetime_module.timedelta(days=100)
assert list(map(f, 'abc')) == [7, 8, 9]
assert list(map(f, 'def')) == [10, 11, 12]
assert f(a='d') == f(a='d', b=2) == f('d') == 10
fixed_time += datetime_module.timedelta(days=1000)
assert list(map(f, 'abcdef')) == [13, 14, 15, 16, 17, 18]
assert f(a='d', b='meow') == 19