| 1 |
import importlib.util |
| 2 |
import inspect |
| 3 |
|
| 4 |
import pytest |
| 5 |
from flask import Flask |
| 6 |
from flask import make_response |
| 7 |
|
| 8 |
from flask_caching import Cache |
| 9 |
from flask_caching import CachedResponse |
| 10 |
from flask_caching.backends import FileSystemCache |
| 11 |
from flask_caching.backends import MemcachedCache |
| 12 |
from flask_caching.backends import NullCache |
| 13 |
from flask_caching.backends import RedisCache |
| 14 |
from flask_caching.backends import RedisSentinelCache |
| 15 |
from flask_caching.backends import SASLMemcachedCache |
| 16 |
from flask_caching.backends import SimpleCache |
| 17 |
from flask_caching.backends import SpreadSASLMemcachedCache |
| 18 |
|
| 19 |
# pylibmc doesn't have any wheels for python > 3.11 |
| 20 |
pylibmc_available = False |
| 21 |
try: |
| 22 |
importlib.util.find_spec("pylibmc") |
| 23 |
except ImportError: |
| 24 |
pylibmc_available = False |
| 25 |
|
| 26 |
|
| 27 |
cache_types = [ |
| 28 |
FileSystemCache, |
| 29 |
MemcachedCache, |
| 30 |
NullCache, |
| 31 |
RedisCache, |
| 32 |
RedisSentinelCache, |
| 33 |
SimpleCache, |
| 34 |
] |
| 35 |
|
| 36 |
if pylibmc_available: |
| 37 |
cache_types.append(SASLMemcachedCache) |
| 38 |
cache_types.append(SpreadSASLMemcachedCache) |
| 39 |
|
| 40 |
|
| 41 |
@pytest.fixture |
| 42 |
def app(): |
| 43 |
app_ = Flask(__name__) |
| 44 |
|
| 45 |
return app_ |
| 46 |
|
| 47 |
|
| 48 |
@pytest.mark.parametrize( |
| 49 |
"cache_type", |
| 50 |
(cache_types), |
| 51 |
) |
| 52 |
def test_init_nullcache(cache_type, app, tmp_path): |
| 53 |
extra_config = { |
| 54 |
FileSystemCache: { |
| 55 |
"CACHE_DIR": tmp_path, |
| 56 |
}, |
| 57 |
SASLMemcachedCache: { |
| 58 |
"CACHE_MEMCACHED_USERNAME": "test", |
| 59 |
"CACHE_MEMCACHED_PASSWORD": "test", |
| 60 |
}, |
| 61 |
} |
| 62 |
app.config["CACHE_TYPE"] = "flask_caching.backends." + cache_type.__name__ |
| 63 |
app.config.update(extra_config.get(cache_type, {})) |
| 64 |
cache = Cache(app=app) |
| 65 |
|
| 66 |
assert isinstance(app.extensions["cache"][cache], cache_type) |
| 67 |
|
| 68 |
|
| 69 |
def test_cached_response_init_is_typed(): |
| 70 |
"""Regression test for issue #628. |
| 71 |
|
| 72 |
``CachedResponse.__init__`` must declare a return type and parameter |
| 73 |
types so that mypy ``--strict`` doesn't flag construction in typed |
| 74 |
code with ``[no-untyped-call]``. See |
| 75 |
https://github.com/pallets-eco/flask-caching/issues/628. |
| 76 |
""" |
| 77 |
sig = inspect.signature(CachedResponse.__init__) |
| 78 |
# __init__ should declare ``-> None`` rather than be untyped. |
| 79 |
# inspect.signature returns the literal ``None`` for ``-> None``. |
| 80 |
assert sig.return_annotation is None, ( |
| 81 |
f"CachedResponse.__init__ return annotation is {sig.return_annotation!r}; " |
| 82 |
"expected None" |
| 83 |
) |
| 84 |
# All non-self parameters should carry an annotation. |
| 85 |
for name, param in sig.parameters.items(): |
| 86 |
if name == "self": |
| 87 |
continue |
| 88 |
assert param.annotation is not inspect.Parameter.empty, ( |
| 89 |
f"CachedResponse.__init__ parameter {name!r} has no annotation" |
| 90 |
) |
| 91 |
|
| 92 |
|
| 93 |
def test_cached_response_construction_with_flask_response(): |
| 94 |
"""Sanity check that CachedResponse still wraps a flask Response.""" |
| 95 |
app = Flask(__name__) |
| 96 |
with app.test_request_context(): |
| 97 |
resp = make_response("hi") |
| 98 |
cached = CachedResponse(resp, 10) |
| 99 |
assert cached.timeout == 10 |
| 100 |
assert cached.get_data(as_text=True) == "hi" |