import random import time import pytest from flask_caching import Cache from flask_caching import function_namespace def test_memoize(app, cache, clock): with app.test_request_context(): @cache.memoize(3) def big_foo(a, b): return a + b + random.randrange(0, 100000) result = big_foo(5, 2) clock.advance(1) assert big_foo(5, 2) == result result2 = big_foo(5, 3) assert result2 != result clock.advance(3) assert big_foo(5, 2) != result clock.advance(1) assert big_foo(5, 3) != result2 def test_memoize_falsy_default_argument(app, cache): with app.test_request_context(): runs = [] @cache.memoize() def f(a, b=0): runs.append((a, b)) return f"{a}:{b}" # A falsy default (0) must not collide with an explicit None. assert f(1, None) == "1:None" assert f(1) == "1:0" assert f(1, 0) == "1:0" assert f(1, b=0) == "1:0" # None and the default 0 are distinct; the three b=0 forms share a key. assert runs == [(1, None), (1, 0)] def test_memoize_hashes(app, cache, hash_method, clock): with app.test_request_context(): @cache.memoize(3, hash_method=hash_method) def big_foo(a, b): return a + b + random.randrange(0, 100000) result = big_foo(5, 2) clock.advance(1) assert big_foo(5, 2) == result result2 = big_foo(5, 3) assert result2 != result clock.advance(3) assert big_foo(5, 2) != result clock.advance(1) assert big_foo(5, 3) != result2 def test_memoize_timeout(app, clock): app.config["CACHE_DEFAULT_TIMEOUT"] = 1 cache = Cache(app) with app.test_request_context(): @cache.memoize() def big_foo(a, b): return a + b + random.randrange(0, 100000) result = big_foo(5, 2) assert big_foo(5, 2) == result clock.advance(2) assert big_foo(5, 2) != result def test_memoize_entry_written_late_keeps_its_own_timeout(app, cache, clock): with app.test_request_context(): runs = [] @cache.memoize(timeout=60) def f(param): runs.append(param) return param.upper() assert f("icecream") == "ICECREAM" clock.advance(59) assert f("waffles") == "WAFFLES" # t=61: the version key used to expire here and orphan both entries. clock.advance(2) assert f("waffles") == "WAFFLES" assert runs == ["icecream", "waffles"] # t=120: past this entry's own timeout, so it expires normally. clock.advance(59) assert f("waffles") == "WAFFLES" assert runs == ["icecream", "waffles", "waffles"] def test_memoize_version_key_outlives_newest_entry(app, cache, clock): with app.test_request_context(): @cache.memoize(timeout=60) def f(param): return param.upper() version_key = cache._memvname(function_namespace(f.uncached)[0]) f("a") clock.advance(50) # t=50: this entry expires at t=110, so the version key has to as well. f("b") clock.advance(59) # t=109: entry "b" still has a second left, so its version must be live. assert cache.get(version_key) is not None def test_memoize_version_key_expires_once_its_entries_have(app, cache, clock): with app.test_request_context(): @cache.memoize(timeout=60) def f(param): return param.upper() f("a") version_key = cache._memvname(function_namespace(f.uncached)[0]) assert cache.get(version_key) is not None clock.advance(61) assert cache.get(version_key) is None def test_memoize_delete_resets_version_key(app, cache, clock): with app.test_request_context(): runs = [] @cache.memoize(timeout=60) def f(param): runs.append(param) return param.upper() f("a") assert runs == ["a"] cache.delete_memoized(f) assert f("a") == "A" assert runs == ["a", "a"] clock.advance(3600) f("b") clock.advance(30) assert f("b") == "B" assert runs == ["a", "a", "b"] def test_memoize_annotated(app, cache): with app.test_request_context(): @cache.memoize(50) def big_foo_annotated(a, b): return a + b + random.randrange(0, 100000) big_foo_annotated.__annotations__ = { "a": int, "b": int, "return": int, } result = big_foo_annotated(5, 2) assert big_foo_annotated(5, 2) == result def test_memoize_utf8_arguments(app, cache): with app.test_request_context(): @cache.memoize() def big_foo(a, b): return f"{a}-{b}" big_foo("æøå", "chars") def test_memoize_unicode_arguments(app, cache): with app.test_request_context(): @cache.memoize() def big_foo(a, b): return f"{a}-{b}" big_foo("æøå", "chars") def test_memoize_delete(app, cache): with app.test_request_context(): @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) assert big_foo(5, 2) == result assert big_foo(5, 2) == result assert big_foo(5, 3) != result assert big_foo(5, 3) == result2 cache.delete_memoized(big_foo) assert big_foo(5, 2) != result assert big_foo(5, 3) != result2 def test_memoize_delete_via_attribute(app, cache): with app.test_request_context(): @cache.memoize(5) def big_foo(a, b): return a + b + random.randrange(0, 100000) result = big_foo(5, 2) assert big_foo(5, 2) == result big_foo.delete_memoized() assert big_foo(5, 2) != result def test_memoize_classfunc_delete_via_attribute(app, cache): with app.test_request_context(): class Adder: def __init__(self, initial): self.initial = initial @cache.memoize(5) def add(self, b): return self.initial + b + random.random() adder1 = Adder(1) adder2 = Adder(2) a1 = adder1.add(3) a2 = adder2.add(3) assert adder1.add(3) == a1 assert adder2.add(3) == a2 # the attribute takes no arguments, so it resets the version for # every instance, not just adder1 adder1.add.delete_memoized() assert adder1.add(3) != a1 assert adder2.add(3) != a2 def test_memoize_no_timeout_delete(app, cache): with app.test_request_context(): @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 cache.delete_memoized(big_foo, 5, 2) assert big_foo(5, 1) == result_a assert big_foo(5, 2) != result_b # Cleanup bigfoo 5,1 5,2 or it might conflict with # following run if it also uses memecache cache.delete_memoized(big_foo, 5, 2) cache.delete_memoized(big_foo, 5, 1) def test_memoize_verhash_delete(app, cache): with app.test_request_context(): @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) assert big_foo(5, 2) == result assert big_foo(5, 2) == result assert big_foo(5, 3) != result assert big_foo(5, 3) == result2 cache.delete_memoized_verhash(big_foo) _fname, _fname_instance = function_namespace(big_foo) version_key = cache._memvname(_fname) assert cache.get(version_key) is None assert big_foo(5, 2) != result assert big_foo(5, 3) != result2 assert cache.get(version_key) is not None def test_memoize_annotated_delete(app, cache): with app.test_request_context(): @cache.memoize(5) def big_foo_annotated(a, b): return a + b + random.randrange(0, 100000) big_foo_annotated.__annotations__ = {"a": int, "b": int, "return": int} result = big_foo_annotated(5, 2) result2 = big_foo_annotated(5, 3) assert big_foo_annotated(5, 2) == result assert big_foo_annotated(5, 2) == result assert big_foo_annotated(5, 3) != result assert big_foo_annotated(5, 3) == result2 cache.delete_memoized_verhash(big_foo_annotated) _fname, _fname_instance = function_namespace(big_foo_annotated) version_key = cache._memvname(_fname) assert cache.get(version_key) is None assert big_foo_annotated(5, 2) != result assert big_foo_annotated(5, 3) != result2 assert cache.get(version_key) is not None def test_memoize_args(app, cache): with app.test_request_context(): @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 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 # Cleanup bigfoo 5,1 5,2 or it might conflict with # following run if it also uses memecache cache.delete_memoized(big_foo, [5, 3, 2], [1]) cache.delete_memoized(big_foo, [3, 3], [1]) def test_memoize_kwargs(app, cache): with app.test_request_context(): @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 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_memoize_kwargonly(app, cache): with app.test_request_context(): @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_memoize_arg_kwarg(app, cache): with app.test_request_context(): @cache.memoize() def f(a, b, c=1): return a + b + c + random.randrange(0, 100000) assert f(1, 2) == f(1, 2, c=1) assert f(1, 2) == f(1, 2, 1) assert f(1, 2) == f(1, 2) assert f(1, 2, 3) != f(1, 2) with pytest.raises(TypeError): f(1) def test_memoize_arg_kwarg_var_keyword(app, cache): with app.test_request_context(): @cache.memoize() def f(a, b, c=1, **kwargs): return a + b + c + random.randrange(0, 100000) + sum(list(kwargs.values())) assert f(1, 2) == f(1, 2, c=1) assert f(1, 2) == f(1, 2, 1) assert f(1, 2) == f(1, 2) assert f(1, 2, d=5, e=8) == f(1, 2, e=8, d=5) assert f(1, b=2, c=3, d=5, e=8) == f(1, 2, e=8, d=5, b=2, c=3) assert f(1, 2, 3) != f(1, 2) assert f(1, 2, 3) != f(1, 2) with pytest.raises(TypeError): f(1) def test_memoize_classarg(app, cache): @cache.memoize() def bar(a): return a.value + random.random() class Adder: 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_memoize_classfunc(app, cache): class Adder: def __init__(self, initial): self.initial = initial @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_memoize_classfunc_repr(app, cache): class Adder: def __init__(self, initial): self.initial = initial @cache.memoize() def add(self, b): return self.initial + b + random.random() def __repr__(self): return "42" def __caching_id__(self): return self.initial 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_memoize_classfunc_delete(app, cache): with app.test_request_context(): class Adder: def __init__(self, initial): self.initial = initial @cache.memoize() def add(self, b): return self.initial + b + random.random() adder1 = Adder(1) adder2 = Adder(2) a1 = adder1.add(3) a2 = adder2.add(3) assert a1 != a2 assert adder1.add(3) == a1 assert adder2.add(3) == a2 cache.delete_memoized(adder1.add) a3 = adder1.add(3) a4 = adder2.add(3) assert not a1 == a3 # self.assertNotEqual(a1, a3) assert a1 != a3 assert a2 == a4 # self.assertEqual(a2, a4) cache.delete_memoized(Adder.add) a5 = adder1.add(3) a6 = adder2.add(3) assert not a5 == a6 # self.assertNotEqual(a5, a6) assert not a3 == a5 # self.assertNotEqual(a3, a5) assert not a4 == a6 # self.assertNotEqual(a4, a6) def test_memoize_classfunc_delete_with_args(app, cache): """A bound method carries its own instance, so ``delete_memoized`` only needs the arguments that follow it.""" with app.test_request_context(): class Adder: @cache.memoize(5) def add(self, b): return b + random.random() adder1 = Adder() adder2 = Adder() a1 = adder1.add(3) a2 = adder1.add(4) a3 = adder2.add(3) cache.delete_memoized(adder1.add, 3) # only ``adder1.add(3)`` is gone assert adder1.add(3) != a1 assert adder1.add(4) == a2 assert adder2.add(3) == a3 def test_memoize_classfunc_delete_with_explicit_instance(app, cache): """Passing the instance explicitly keeps working, so that code written against the previous behaviour is not broken.""" with app.test_request_context(): class Adder: @cache.memoize(5) def add(self, b): return b + random.random() adder1 = Adder() adder2 = Adder() a1 = adder1.add(3) a2 = adder1.add(4) a3 = adder2.add(3) cache.delete_memoized(adder1.add, adder1, 3) assert adder1.add(3) != a1 assert adder1.add(4) == a2 assert adder2.add(3) == a3 # ... and so does reaching the method through the class a1 = adder1.add(3) cache.delete_memoized(Adder.add, adder1, 3) assert adder1.add(3) != a1 assert adder2.add(3) == a3 def test_memoize_classfunc_delete_with_args_to_ignore_self(app, cache): with app.test_request_context(): class Adder: @cache.memoize(5, args_to_ignore=["self"]) def add(self, b): return b + random.random() adder1 = Adder() adder2 = Adder() a1 = adder1.add(3) a2 = adder1.add(4) # ``self`` is not part of the key, so both instances share it assert adder2.add(3) == a1 cache.delete_memoized(adder1.add, 3) assert adder1.add(3) != a1 assert adder1.add(4) == a2 def test_memoize_classfunc_delete_with_kwargs_only(app, cache): with app.test_request_context(): class Adder: @cache.memoize(5) def add(self, b): return b + random.random() adder1 = Adder() a1 = adder1.add(3) cache.delete_memoized(adder1.add, b=3) assert adder1.add(3) != a1 # reached through the class there is no instance to fall back on with pytest.raises(ValueError): cache.delete_memoized(Adder.add, b=3) def test_memoize_classmethod_delete(app, cache): with app.test_request_context(): class Mock: @classmethod @cache.memoize(5) def big_foo(cls, a, b): return a + b + random.randrange(0, 100000) result = Mock.big_foo(5, 2) result2 = Mock.big_foo(5, 3) assert Mock.big_foo(5, 2) == result assert Mock.big_foo(5, 2) == result assert Mock.big_foo(5, 3) != result assert Mock.big_foo(5, 3) == result2 cache.delete_memoized(Mock.big_foo) assert Mock.big_foo(5, 2) != result assert Mock.big_foo(5, 3) != result2 def test_memoize_classmethod_delete_with_args(app, cache): with app.test_request_context(): class Mock: @classmethod @cache.memoize(5) def big_foo(cls, a, b): return a + b + random.randrange(0, 100000) result = Mock.big_foo(5, 2) result2 = Mock.big_foo(5, 3) assert Mock.big_foo(5, 2) == result assert Mock.big_foo(5, 2) == result assert Mock.big_foo(5, 3) != result assert Mock.big_foo(5, 3) == result2 with pytest.raises(ValueError): cache.delete_memoized(Mock.big_foo, 5, 2) assert Mock.big_foo(5, 2) == result assert Mock.big_foo(5, 3) == result2 cache.delete_memoized(Mock.big_foo, Mock, 5, 2) assert Mock.big_foo(5, 2) != result assert Mock.big_foo(5, 3) == result2 def test_memoize_forced_update(app, cache): with app.test_request_context(): forced_update = False @cache.memoize(5, forced_update=lambda: forced_update) def big_foo(a, b): return a + b + random.randrange(0, 100000) result = big_foo(5, 2) assert big_foo(5, 2) == result forced_update = True new_result = big_foo(5, 2) assert new_result != result forced_update = False assert big_foo(5, 2) == new_result def test_memoize_forced_update_parameters(app, cache): from collections import Counter with app.test_request_context(): call_counter = Counter() call_params = {} forced_update = False def forced_update_func(a, b): call_counter[1] += 1 call_params[call_counter[1] - 1] = (a, b) return forced_update @cache.memoize(5, forced_update=forced_update_func) def memoized_func(a, b): return a + b + random.randrange(0, 100000) # Save the value for later inspection result = memoized_func(5, 2) # forced_update_func should have been called once by memoize itself. assert call_counter[1] == 1 # …with the values we called the function with assert call_params[0] == (5, 2) # Calling the function again should return the cached value assert memoized_func(5, 2) == result # forced_update_func should have been called one more time… assert call_counter[1] == 2 # …with the values we called the function with assert call_params[1] == (5, 2) # Tell forced_update_func to return True next time forced_update = True # Save the new result… new_result = memoized_func(5, 2) # …which, due to the random number in the function, should be different # from the old one assert new_result != result # forced_update_func should have been called one more time again… assert call_counter[1] == 3 # …with the values we called the function with assert call_params[2] == (5, 2) # Now stop forced updating again forced_update = False # The function should return the same value as it did last time assert memoized_func(5, 2) == new_result # forced_update_func should have been called one more time again… assert call_counter[1] == 4 # …with the values we called the function with assert call_params[3] == (5, 2) def test_memoize_is_stale(app, cache): with app.test_request_context(): stale = False cached_values = [] def is_stale(value): cached_values.append(value) return stale @cache.memoize(5, is_stale=is_stale) def big_foo(a, b): return a + b + random.randrange(0, 100000) result = big_foo(5, 2) # is_stale is only consulted on a cache hit assert cached_values == [] assert big_foo(5, 2) == result assert cached_values == [result] stale = True new_result = big_foo(5, 2) assert new_result != result assert cached_values == [result, result] stale = False assert big_foo(5, 2) == new_result assert cached_values == [result, result, new_result] def test_memoize_is_stale_parameters(app, cache): with app.test_request_context(): call_params = [] def is_stale(value, a, b): call_params.append((value, a, b)) return False @cache.memoize(5, is_stale=is_stale) def memoized_func(a, b): return a + b + random.randrange(0, 100000) result = memoized_func(5, 2) assert call_params == [] assert memoized_func(5, 2) == result # the cached value comes first, followed by the call's own arguments assert call_params == [(result, 5, 2)] def test_memoize_multiple_arg_kwarg_calls(app, cache): with app.test_request_context(): @cache.memoize() def big_foo(a, b, c=None, d=None): if c is None: c = [1, 1] if d is None: 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_memoize_multiple_arg_kwarg_delete(app, cache): with app.test_request_context(): @cache.memoize() def big_foo(a, b, c=None, d=None): if c is None: c = [1, 1] if d is None: 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]) 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 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 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 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 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 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_memoize_kwargs_to_args(app, cache): with 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 = cache._memoize_kwargs_to_args(big_foo, 1, 2, "foo", "bar") assert args == expected args, kwargs = cache._memoize_kwargs_to_args(big_foo, 2, "foo", "bar", a=1) assert args == expected args, kwargs = cache._memoize_kwargs_to_args( big_foo, a=1, b=2, c="foo", d="bar" ) assert args == expected args, kwargs = cache._memoize_kwargs_to_args( big_foo, d="bar", b=2, a=1, c="foo" ) assert args == expected args, kwargs = cache._memoize_kwargs_to_args(big_foo, 1, 2, d="bar", c="foo") assert args == expected def test_memoize_when_using_args_unpacking(app, cache): with app.test_request_context(): @cache.memoize() def big_foo(*args): return sum(args) + random.randrange(0, 100000) result_a = big_foo(1, 2) result_b = big_foo(1, 3) assert big_foo(1, 2) == result_a assert big_foo(1, 3) == result_b assert big_foo(1, 2) != result_b assert big_foo(1, 3) != result_a cache.delete_memoized(big_foo) assert big_foo(1, 2) != result_a assert big_foo(1, 3) != result_b def test_memoize_when_using_variable_mix_args_unpacking(app, cache): with app.test_request_context(): @cache.memoize() def big_foo(a, b, *args, **kwargs): return ( sum([a, b]) + sum(args) + sum(kwargs.values()) + random.randrange(0, 100000) ) result_a = big_foo(1, 2, 3, 4, x=2, y=5) result_b = big_foo(4, 7, 7, 2, x=1, y=4) assert big_foo(1, 2, 3, 4, x=2, y=5) == result_a assert big_foo(4, 7, 7, 2, x=1, y=4) == result_b assert big_foo(1, 2, 3, 4, x=2, y=5) != result_b assert big_foo(4, 7, 7, 2, x=1, y=4) != result_a cache.delete_memoized(big_foo) assert big_foo(1, 2, 3, 4, x=2, y=5) != result_a assert big_foo(4, 7, 7, 2, x=1, y=4) != result_b def test_memoize_none(app, cache): with app.test_request_context(): from collections import Counter call_counter = Counter() @cache.memoize(cache_none=True) def memoize_none(param): call_counter[param] += 1 memoize_none(1) # The memoized function should have been called assert call_counter[1] == 1 # Next time we call the function, the value should be coming from the # cache... assert memoize_none(1) is None # …thus, the call counter should remain 1 assert call_counter[1] == 1 cache.clear() memoize_none(1) assert call_counter[1] == 2 def test_memoize_never_accept_none(app, cache): """Asserting that when cache_none is False, we always assume a None value returned from .get() means the key is not found """ with app.test_request_context(): from collections import Counter call_counter = Counter() @cache.memoize() def memoize_none(param): call_counter[param] += 1 memoize_none(1) # The memoized function should have been called assert call_counter[1] == 1 # Next time we call the function, the value should be coming from the # cache… # But the value is None and so we treat it as uncached. assert memoize_none(1) is None # …thus, the call counter should increment to 2 assert call_counter[1] == 2 cache.clear() memoize_none(1) assert call_counter[1] == 3 def test_memoize_with_source_check_enabled(app, cache): with app.test_request_context(): @cache.memoize(source_check=True) def big_foo(a, b): return str(time.time()) first_try = big_foo(5, 2) second_try = big_foo(5, 2) assert second_try == first_try @cache.memoize(source_check=True) def big_foo(a, b): return str(time.time()) third_try = big_foo(5, 2) assert third_try[0] != first_try @cache.memoize(source_check=True) def big_foo(a, b): return str(time.time()) forth_try = big_foo(5, 2) assert forth_try == first_try def test_memoize_with_source_check_disabled(app, cache): with app.test_request_context(): @cache.memoize(source_check=False) def big_foo(a, b): return str(time.time()) first_try = big_foo(5, 2) second_try = big_foo(5, 2) assert second_try == first_try @cache.memoize(source_check=False) def big_foo(a, b): return time.time() third_try = big_foo(5, 2) assert third_try == first_try def test_memoize_source_check_from_config(app): """``CACHE_SOURCE_CHECK`` applies when the decorator doesn't override it.""" app.config["CACHE_SOURCE_CHECK"] = True cache = Cache(app) with app.test_request_context(): @cache.memoize() def big_foo(a, b): return str(time.time()) first_try = big_foo(5, 2) assert big_foo(5, 2) == first_try @cache.memoize() def big_foo(a, b): # noqa: F811 return str(time.time()) assert big_foo(5, 2) != first_try def test_memoize_delete_with_source_check_from_config(app): """``CACHE_SOURCE_CHECK`` must reach the ``make_cache_key`` attribute. ``delete_memoized`` builds the key through ``make_cache_key``, which is created when the decorator is applied. Resolving the config value later than that made the two keys disagree, so nothing was deleted. """ app.config["CACHE_SOURCE_CHECK"] = True cache = Cache(app) with app.test_request_context(): @cache.memoize(50) def big_foo(a, b): return a + b + random.randrange(0, 100000) first_try = big_foo(5, 2) assert big_foo(5, 2) == first_try cache.delete_memoized(big_foo, 5, 2) assert big_foo(5, 2) != first_try def test_cached_source_check_from_config_applies_to_first_call(app): """The source hash must be part of the key from the very first call.""" app.config["CACHE_SOURCE_CHECK"] = True cache = Cache(app) @cache.cached(key_prefix="MyBits") def get_random_bits(): return [random.randrange(0, 2) for i in range(50)] with app.test_request_context(): key_before_call = get_random_bits.make_cache_key() get_random_bits() assert key_before_call != "MyBits" assert get_random_bits.make_cache_key() == key_before_call def test_memoize_ignore_args(app, cache): with app.test_request_context(): @cache.memoize(50, args_to_ignore=["b"]) def big_foo(a, b): return a + b + random.randrange(0, 100000) result = big_foo(5, 2) assert big_foo(5, 3) == result def test_memoize_method_ignore_self_arg(app, cache): with app.test_request_context(): class Foo: @cache.memoize(50, args_to_ignore=["self"]) def big_foo(self, a, b): return a + b + random.randrange(0, 100000) assert Foo().big_foo(5, 2) == Foo().big_foo(5, 2) def test_memoize_function_ignore_kwarg(app, cache): with app.test_request_context(): @cache.memoize(50, args_to_ignore=["b"]) def big_foo(a, b): return a + b + random.randrange(0, 100000) assert big_foo(5, 2) == big_foo(5, b=3)