Skip to content

Commit b0ff8ef

Browse files
committed
-
1 parent 8a32a75 commit b0ff8ef

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

python_toolbox/caching/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
# todo: examine thread-safety
77

8-
from .cache import cache
8+
from .decorators import cache
99
from .cached_type import CachedType
1010
from .cached_property import CachedProperty
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ class CLEAR_ENTIRE_CACHE(object):
2323
'''Sentinel object for clearing the entire cache'''
2424

2525

26+
def _get_now():
27+
'''
28+
Get the current datetime.
29+
30+
This is specified as a function to make testing easier.
31+
'''
32+
return datetime_module.datetime.now()
33+
34+
2635
@decorator_tools.helpful_decorator_builder
2736
def cache(max_size=infinity, time_to_keep=None):
2837
'''
@@ -89,7 +98,7 @@ def remove_expired_entries():
8998
cutting_point = binary_search.binary_search_by_index(
9099
cached._cache.keys(),
91100
sorting_key_function,
92-
datetime_module.datetime.now(),
101+
_get_now(),
93102
rounding=binary_search.LOW
94103
)
95104
if cutting_point is not None:
@@ -107,7 +116,7 @@ def cached(function, *args, **kwargs):
107116
value = function(*args, **kwargs)
108117
cached._cache[sleek_call_args] = (
109118
value,
110-
datetime_module.datetime.now()
119+
_get_now()
111120
)
112121
cached._cache.sort(key=sorting_key_function)
113122
return value

test_python_toolbox/test_caching/test_cache.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import nose.tools
1313

14+
from python_toolbox import caching
1415
from python_toolbox.caching import cache
1516
from python_toolbox import temp_value_setting
1617
from python_toolbox import cute_testing
@@ -206,25 +207,25 @@ def _mock_now():
206207
return fixed_time
207208

208209
with temp_value_setting.TempValueSetter(
209-
(datetime_module.datetime, 'now'), _mock_now):
210-
assert map(f, 'abc') == (1, 2, 3)
210+
(caching.decorators, '_get_now'), _mock_now):
211+
assert map(f, 'abc') == [1, 2, 3]
211212
fixed_time += datetime_module.timedelta(days=100)
212-
assert map(f, 'abc') == (1, 2, 3)
213-
assert map(f, 'def') == (4, 5, 6)
213+
assert map(f, 'abc') == [1, 2, 3]
214+
assert map(f, 'def') == [4, 5, 6]
214215
fixed_time += datetime_module.timedelta(days=100)
215-
assert map(f, 'abc') == (1, 2, 3)
216-
assert map(f, 'def') == (4, 5, 6)
216+
assert map(f, 'abc') == [1, 2, 3]
217+
assert map(f, 'def') == [4, 5, 6]
217218
fixed_time += datetime_module.timedelta(days=100)
218-
assert map(f, 'abc') == (1, 2, 3)
219-
assert map(f, 'def') == (4, 5, 6)
219+
assert map(f, 'abc') == [1, 2, 3]
220+
assert map(f, 'def') == [4, 5, 6]
220221
fixed_time += datetime_module.timedelta(days=100)
221-
assert map(f, 'abc') == (7, 8, 9)
222-
assert map(f, 'def') == (4, 5, 6)
222+
assert map(f, 'abc') == [7, 8, 9]
223+
assert map(f, 'def') == [4, 5, 6]
223224
fixed_time += datetime_module.timedelta(days=100)
224-
assert map(f, 'abc') == (7, 8, 9)
225-
assert map(f, 'def') == (10, 11, 12)
225+
assert map(f, 'abc') == [7, 8, 9]
226+
assert map(f, 'def') == [10, 11, 12]
226227
assert f(a='d') == f(a='d', b=2) == f('d') == 10
227228
fixed_time += datetime_module.timedelta(days=1000)
228-
assert map(f, 'abcdef') == (13, 14, 15, 16, 17, 18)
229+
assert map(f, 'abcdef') == [13, 14, 15, 16, 17, 18]
229230
assert f(a='d', b='meow') == 19
230231

0 commit comments

Comments
 (0)