| 1 |
"""Tests for the cachelib integration. |
| 2 |
|
| 3 |
Flask-Caching runs on top of cachelib: the built-in backends subclass their |
| 4 |
cachelib counterparts, and ``CACHE_TYPE`` may also point straight at a |
| 5 |
cachelib class, in which case the class is instantiated directly with |
| 6 |
``CACHE_ARGS``/``CACHE_OPTIONS`` instead of going through |
| 7 |
:meth:`.BaseCache.factory`. |
| 8 |
""" |
| 9 |
|
| 10 |
import cachelib |
| 11 |
import pytest |
| 12 |
from cachelib.serializers import SimpleSerializer |
| 13 |
|
| 14 |
from flask_caching import Cache |
| 15 |
|
| 16 |
|
| 17 |
@pytest.mark.parametrize( |
| 18 |
"cache_type", |
| 19 |
("cachelib.SimpleCache", "cachelib.simple.SimpleCache"), |
| 20 |
) |
| 21 |
def test_cachelib_class_as_cache_type(app, cache_type): |
| 22 |
"""A cachelib class is used as-is, not wrapped in a subclass.""" |
| 23 |
app.config["CACHE_TYPE"] = cache_type |
| 24 |
cache = Cache(app) |
| 25 |
|
| 26 |
backend = app.extensions["cache"][cache] |
| 27 |
assert type(backend) is cachelib.SimpleCache |
| 28 |
|
| 29 |
|
| 30 |
def test_cachelib_backend_gets_default_timeout(app): |
| 31 |
app.config["CACHE_TYPE"] = "cachelib.SimpleCache" |
| 32 |
app.config["CACHE_DEFAULT_TIMEOUT"] = 111 |
| 33 |
cache = Cache(app) |
| 34 |
|
| 35 |
assert app.extensions["cache"][cache].default_timeout == 111 |
| 36 |
|
| 37 |
|
| 38 |
def test_cachelib_backend_gets_cache_options(app): |
| 39 |
"""CACHE_OPTIONS are passed as keyword arguments to the cachelib class.""" |
| 40 |
app.config["CACHE_TYPE"] = "cachelib.SimpleCache" |
| 41 |
app.config["CACHE_OPTIONS"] = {"threshold": 7} |
| 42 |
cache = Cache(app) |
| 43 |
|
| 44 |
assert app.extensions["cache"][cache]._threshold == 7 |
| 45 |
|
| 46 |
|
| 47 |
def test_cachelib_backend_gets_cache_args(app, tmp_path): |
| 48 |
"""CACHE_ARGS are passed as positional arguments to the cachelib class.""" |
| 49 |
app.config["CACHE_TYPE"] = "cachelib.FileSystemCache" |
| 50 |
app.config["CACHE_ARGS"] = [str(tmp_path)] |
| 51 |
cache = Cache(app) |
| 52 |
|
| 53 |
backend = app.extensions["cache"][cache] |
| 54 |
assert type(backend) is cachelib.FileSystemCache |
| 55 |
assert backend._path == str(tmp_path) |
| 56 |
|
| 57 |
|
| 58 |
def test_cachelib_backend_proxy_methods(app): |
| 59 |
app.config["CACHE_TYPE"] = "cachelib.SimpleCache" |
| 60 |
cache = Cache(app) |
| 61 |
|
| 62 |
with app.app_context(): |
| 63 |
cache.set("hi", "hello") |
| 64 |
assert cache.get("hi") == "hello" |
| 65 |
assert cache.has("hi") |
| 66 |
|
| 67 |
cache.add("hi", "foobar") |
| 68 |
assert cache.get("hi") == "hello" |
| 69 |
|
| 70 |
cache.set_many({"a": 1, "b": 2}) |
| 71 |
assert cache.get_many("a", "b") == [1, 2] |
| 72 |
|
| 73 |
cache.delete("hi") |
| 74 |
assert cache.get("hi") is None |
| 75 |
|
| 76 |
cache.clear() |
| 77 |
assert cache.get("a") is None |
| 78 |
|
| 79 |
|
| 80 |
def test_cachelib_backend_with_cached_view(app): |
| 81 |
app.config["CACHE_TYPE"] = "cachelib.SimpleCache" |
| 82 |
cache = Cache(app) |
| 83 |
calls = [] |
| 84 |
|
| 85 |
@app.route("/") |
| 86 |
@cache.cached() |
| 87 |
def view(): |
| 88 |
calls.append(1) |
| 89 |
return "ok" |
| 90 |
|
| 91 |
client = app.test_client() |
| 92 |
assert client.get("/").data == b"ok" |
| 93 |
assert client.get("/").data == b"ok" |
| 94 |
assert len(calls) == 1 |
| 95 |
|
| 96 |
|
| 97 |
def test_cachelib_backend_with_memoize(app): |
| 98 |
app.config["CACHE_TYPE"] = "cachelib.SimpleCache" |
| 99 |
cache = Cache(app) |
| 100 |
calls = [] |
| 101 |
|
| 102 |
@cache.memoize() |
| 103 |
def double(x): |
| 104 |
calls.append(x) |
| 105 |
return x * 2 |
| 106 |
|
| 107 |
with app.app_context(): |
| 108 |
assert double(2) == 4 |
| 109 |
assert double(2) == 4 |
| 110 |
assert len(calls) == 1 |
| 111 |
|
| 112 |
cache.delete_memoized(double) |
| 113 |
assert double(2) == 4 |
| 114 |
assert len(calls) == 2 |
| 115 |
|
| 116 |
|
| 117 |
def test_backend_uses_cachelib_serializer(app): |
| 118 |
"""The backend holds a cachelib serializer that can be replaced.""" |
| 119 |
cache = Cache(app) |
| 120 |
backend = app.extensions["cache"][cache] |
| 121 |
assert isinstance(backend.serializer, SimpleSerializer) |
| 122 |
|
| 123 |
class CountingSerializer(SimpleSerializer): |
| 124 |
def __init__(self): |
| 125 |
self.dumped = 0 |
| 126 |
|
| 127 |
def dumps(self, value, protocol=None): |
| 128 |
self.dumped += 1 |
| 129 |
return super().dumps(value) |
| 130 |
|
| 131 |
serializer = CountingSerializer() |
| 132 |
backend.serializer = serializer |
| 133 |
|
| 134 |
with app.app_context(): |
| 135 |
cache.set("hi", "hello") |
| 136 |
assert cache.get("hi") == "hello" |
| 137 |
|
| 138 |
assert serializer.dumped == 1 |