| 1 |
"""Tests that the decorators go through the public ``Cache`` API. |
| 2 |
|
| 3 |
Third-party instrumentation (tracing, metrics) works by subclassing ``Cache`` |
| 4 |
and wrapping its public methods. Those wrappers only see the traffic caused by |
| 5 |
``@cached``/``@memoize`` if the decorators call ``self.get()``/``self.set()`` |
| 6 |
rather than reaching into ``self.cache`` directly. |
| 7 |
""" |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
from flask_caching import Cache |
| 12 |
|
| 13 |
|
| 14 |
class RecordingCache(Cache): |
| 15 |
"""A ``Cache`` subclass that records the public methods it was asked for.""" |
| 16 |
|
| 17 |
def __init__(self, *args, **kwargs): |
| 18 |
self.calls = [] |
| 19 |
super().__init__(*args, **kwargs) |
| 20 |
|
| 21 |
def get(self, *args, **kwargs): |
| 22 |
self.calls.append("get") |
| 23 |
return super().get(*args, **kwargs) |
| 24 |
|
| 25 |
def has(self, *args, **kwargs): |
| 26 |
self.calls.append("has") |
| 27 |
return super().has(*args, **kwargs) |
| 28 |
|
| 29 |
def set(self, *args, **kwargs): |
| 30 |
self.calls.append("set") |
| 31 |
return super().set(*args, **kwargs) |
| 32 |
|
| 33 |
def delete(self, *args, **kwargs): |
| 34 |
self.calls.append("delete") |
| 35 |
return super().delete(*args, **kwargs) |
| 36 |
|
| 37 |
def delete_many(self, *args, **kwargs): |
| 38 |
self.calls.append("delete_many") |
| 39 |
return super().delete_many(*args, **kwargs) |
| 40 |
|
| 41 |
def get_many(self, *args, **kwargs): |
| 42 |
self.calls.append("get_many") |
| 43 |
return super().get_many(*args, **kwargs) |
| 44 |
|
| 45 |
def set_many(self, *args, **kwargs): |
| 46 |
self.calls.append("set_many") |
| 47 |
return super().set_many(*args, **kwargs) |
| 48 |
|
| 49 |
|
| 50 |
@pytest.fixture |
| 51 |
def recording_cache(app): |
| 52 |
return RecordingCache(app) |
| 53 |
|
| 54 |
|
| 55 |
def test_cached_uses_public_methods(app, recording_cache): |
| 56 |
@app.route("/") |
| 57 |
@recording_cache.cached() |
| 58 |
def cached_view(): |
| 59 |
return "hello" |
| 60 |
|
| 61 |
tc = app.test_client() |
| 62 |
|
| 63 |
tc.get("/") |
| 64 |
assert recording_cache.calls == ["get", "set"] |
| 65 |
|
| 66 |
recording_cache.calls.clear() |
| 67 |
tc.get("/") |
| 68 |
assert recording_cache.calls == ["get"] |
| 69 |
|
| 70 |
|
| 71 |
def test_cached_cache_none_uses_public_has(app, recording_cache): |
| 72 |
@app.route("/") |
| 73 |
@recording_cache.cached(cache_none=True) |
| 74 |
def cached_view(): |
| 75 |
return "hello" |
| 76 |
|
| 77 |
tc = app.test_client() |
| 78 |
|
| 79 |
tc.get("/") |
| 80 |
assert recording_cache.calls == ["get", "has", "set"] |
| 81 |
|
| 82 |
|
| 83 |
def test_memoize_uses_public_methods(app, recording_cache): |
| 84 |
with app.test_request_context(): |
| 85 |
|
| 86 |
@recording_cache.memoize() |
| 87 |
def big_foo(a, b): |
| 88 |
return a + b |
| 89 |
|
| 90 |
big_foo(5, 2) |
| 91 |
assert recording_cache.calls == [ |
| 92 |
"get_many", |
| 93 |
"set_many", |
| 94 |
"get", |
| 95 |
"set", |
| 96 |
"get_many", # memoize version gets read |
| 97 |
"set_many", # memoize version gets updated |
| 98 |
] |
| 99 |
|
| 100 |
recording_cache.calls.clear() |
| 101 |
big_foo(5, 2) |
| 102 |
assert recording_cache.calls == ["get_many", "get"] |
| 103 |
|
| 104 |
|
| 105 |
def test_delete_memoized_uses_public_delete(app, recording_cache): |
| 106 |
with app.test_request_context(): |
| 107 |
|
| 108 |
@recording_cache.memoize() |
| 109 |
def big_foo(a, b): |
| 110 |
return a + b |
| 111 |
|
| 112 |
big_foo(5, 2) |
| 113 |
|
| 114 |
recording_cache.calls.clear() |
| 115 |
recording_cache.delete_memoized(big_foo, 5, 2) |
| 116 |
assert "delete" in recording_cache.calls |
| 117 |
|
| 118 |
|
| 119 |
def test_delete_memoized_without_args_uses_public_set_many(app, recording_cache): |
| 120 |
with app.test_request_context(): |
| 121 |
|
| 122 |
@recording_cache.memoize() |
| 123 |
def big_foo(a, b): |
| 124 |
return a + b |
| 125 |
|
| 126 |
big_foo(5, 2) |
| 127 |
|
| 128 |
recording_cache.calls.clear() |
| 129 |
recording_cache.delete_memoized(big_foo) |
| 130 |
assert "set_many" in recording_cache.calls |
| 131 |
|
| 132 |
|
| 133 |
def test_delete_memoized_verhash_uses_public_delete_many(app, recording_cache): |
| 134 |
with app.test_request_context(): |
| 135 |
|
| 136 |
@recording_cache.memoize() |
| 137 |
def big_foo(a, b): |
| 138 |
return a + b |
| 139 |
|
| 140 |
big_foo(5, 2) |
| 141 |
|
| 142 |
recording_cache.calls.clear() |
| 143 |
recording_cache.delete_memoized_verhash(big_foo) |
| 144 |
assert "delete_many" in recording_cache.calls |
| 145 |
|
| 146 |
|
| 147 |
def test_subclass_overrides_still_cache_correctly(app, recording_cache): |
| 148 |
"""The recording subclass must not change the caching behaviour itself.""" |
| 149 |
with app.test_request_context(): |
| 150 |
runs = [] |
| 151 |
|
| 152 |
@recording_cache.memoize() |
| 153 |
def big_foo(a, b): |
| 154 |
runs.append((a, b)) |
| 155 |
return a + b |
| 156 |
|
| 157 |
assert big_foo(5, 2) == 7 |
| 158 |
assert big_foo(5, 2) == 7 |
| 159 |
assert runs == [(5, 2)] |