| 1 |
"""Tests for the ``CACHE_SERIALIZER`` configuration option. |
| 2 |
|
| 3 |
Every cachelib backed backend holds a ``serializer`` instance that turns |
| 4 |
values into bytes before they reach the store. ``CACHE_SERIALIZER`` replaces |
| 5 |
it for the backend Flask-Caching builds. |
| 6 |
""" |
| 7 |
|
| 8 |
import pytest |
| 9 |
from cachelib.serializers import JSONSerializer |
| 10 |
from cachelib.serializers import SimpleSerializer |
| 11 |
|
| 12 |
from flask_caching import Cache |
| 13 |
|
| 14 |
|
| 15 |
class CountingSerializer(SimpleSerializer): |
| 16 |
def __init__(self): |
| 17 |
self.dumped = 0 |
| 18 |
self.loaded = 0 |
| 19 |
|
| 20 |
def dumps(self, value, protocol=None): |
| 21 |
self.dumped += 1 |
| 22 |
return super().dumps(value) |
| 23 |
|
| 24 |
def loads(self, bvalue): |
| 25 |
self.loaded += 1 |
| 26 |
return super().loads(bvalue) |
| 27 |
|
| 28 |
|
| 29 |
class NeedsArguments(SimpleSerializer): |
| 30 |
def __init__(self, protocol): |
| 31 |
self.protocol = protocol |
| 32 |
|
| 33 |
|
| 34 |
def test_default_keeps_the_cachelib_serializer(app): |
| 35 |
cache = Cache(app) |
| 36 |
|
| 37 |
assert isinstance(app.extensions["cache"][cache].serializer, SimpleSerializer) |
| 38 |
|
| 39 |
|
| 40 |
def test_instance_is_used_as_is(app): |
| 41 |
serializer = JSONSerializer() |
| 42 |
app.config["CACHE_SERIALIZER"] = serializer |
| 43 |
cache = Cache(app) |
| 44 |
|
| 45 |
assert app.extensions["cache"][cache].serializer is serializer |
| 46 |
|
| 47 |
|
| 48 |
def test_class_is_instantiated(app): |
| 49 |
app.config["CACHE_SERIALIZER"] = JSONSerializer |
| 50 |
cache = Cache(app) |
| 51 |
|
| 52 |
assert isinstance(app.extensions["cache"][cache].serializer, JSONSerializer) |
| 53 |
|
| 54 |
|
| 55 |
def test_serializer_read_from_extension_config(app): |
| 56 |
cache = Cache(app, config={"CACHE_SERIALIZER": JSONSerializer}) |
| 57 |
|
| 58 |
assert isinstance(app.extensions["cache"][cache].serializer, JSONSerializer) |
| 59 |
|
| 60 |
|
| 61 |
def test_configured_serializer_is_used_for_get_and_set(app): |
| 62 |
serializer = CountingSerializer() |
| 63 |
app.config["CACHE_SERIALIZER"] = serializer |
| 64 |
cache = Cache(app) |
| 65 |
|
| 66 |
with app.app_context(): |
| 67 |
cache.set("hi", "hello") |
| 68 |
assert cache.get("hi") == "hello" |
| 69 |
|
| 70 |
assert serializer.dumped == 1 |
| 71 |
assert serializer.loaded == 1 |
| 72 |
|
| 73 |
|
| 74 |
def test_values_are_stored_in_the_configured_format(app): |
| 75 |
app.config["CACHE_SERIALIZER"] = JSONSerializer |
| 76 |
cache = Cache(app) |
| 77 |
|
| 78 |
with app.app_context(): |
| 79 |
cache.set("hi", {"a": 1}) |
| 80 |
assert cache.cache._cache["hi"][1] == b'{"a": 1}' |
| 81 |
assert cache.get("hi") == {"a": 1} |
| 82 |
|
| 83 |
|
| 84 |
def test_cachelib_cache_type_gets_the_serializer(app): |
| 85 |
app.config["CACHE_TYPE"] = "cachelib.SimpleCache" |
| 86 |
app.config["CACHE_SERIALIZER"] = JSONSerializer |
| 87 |
cache = Cache(app) |
| 88 |
|
| 89 |
assert isinstance(app.extensions["cache"][cache].serializer, JSONSerializer) |
| 90 |
|
| 91 |
|
| 92 |
def test_backend_without_serializer_warns(app): |
| 93 |
app.config.update( |
| 94 |
CACHE_TYPE="NullCache", |
| 95 |
CACHE_NO_NULL_WARNING=True, |
| 96 |
CACHE_SERIALIZER=JSONSerializer, |
| 97 |
) |
| 98 |
|
| 99 |
with pytest.warns(UserWarning, match="CACHE_SERIALIZER"): |
| 100 |
Cache(app) |
| 101 |
|
| 102 |
|
| 103 |
def test_string_serializer_raises(app): |
| 104 |
app.config["CACHE_SERIALIZER"] = "json" |
| 105 |
|
| 106 |
with pytest.raises(ValueError, match="CACHE_SERIALIZER"): |
| 107 |
Cache(app) |
| 108 |
|
| 109 |
|
| 110 |
def test_non_cachelib_serializer_raises(app): |
| 111 |
class DuckTyped: |
| 112 |
def dumps(self, value): |
| 113 |
return b"" |
| 114 |
|
| 115 |
def loads(self, bvalue): |
| 116 |
return None |
| 117 |
|
| 118 |
app.config["CACHE_SERIALIZER"] = DuckTyped() |
| 119 |
|
| 120 |
with pytest.raises(ValueError, match="BaseSerializer"): |
| 121 |
Cache(app) |
| 122 |
|
| 123 |
|
| 124 |
def test_non_cachelib_serializer_class_raises(app): |
| 125 |
class DuckTyped: |
| 126 |
def dumps(self, value): |
| 127 |
return b"" |
| 128 |
|
| 129 |
def loads(self, bvalue): |
| 130 |
return None |
| 131 |
|
| 132 |
app.config["CACHE_SERIALIZER"] = DuckTyped |
| 133 |
|
| 134 |
with pytest.raises(ValueError, match="BaseSerializer"): |
| 135 |
Cache(app) |
| 136 |
|
| 137 |
|
| 138 |
def test_serializer_class_needing_arguments_raises(app): |
| 139 |
app.config["CACHE_SERIALIZER"] = NeedsArguments |
| 140 |
|
| 141 |
with pytest.raises(ValueError, match="Couldn't instantiate serializer"): |
| 142 |
Cache(app) |
| 143 |
|
| 144 |
|
| 145 |
def test_serializer_is_rejected_before_the_backend_is_built(app): |
| 146 |
app.config["CACHE_TYPE"] = "flask_caching.backends.RedisCache" |
| 147 |
app.config["CACHE_SERIALIZER"] = "json" |
| 148 |
|
| 149 |
with pytest.raises(ValueError, match="CACHE_SERIALIZER"): |
| 150 |
Cache(app) |
| 151 |
|
| 152 |
assert "cache" not in app.extensions |