Debian Codegraph @

tests/test_init.py

raw
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 MemcachedCache: {
58 "CACHE_MEMCACHED_SERVERS": ["127.0.0.1:11212"],
59 },
60 RedisCache: {
61 "CACHE_REDIS_PORT": 6360,
62 },
63 SASLMemcachedCache: {
64 "CACHE_MEMCACHED_SERVERS": ["127.0.0.1:11212"],
65 "CACHE_MEMCACHED_USERNAME": "test",
66 "CACHE_MEMCACHED_PASSWORD": "test",
67 },
68 SpreadSASLMemcachedCache: {
69 "CACHE_MEMCACHED_SERVERS": ["127.0.0.1:11212"],
70 "CACHE_MEMCACHED_USERNAME": "test",
71 "CACHE_MEMCACHED_PASSWORD": "test",
72 },
73 }
74 app.config["CACHE_TYPE"] = "flask_caching.backends." + cache_type.__name__
75 app.config.update(extra_config.get(cache_type, {}))
76 cache = Cache(app=app)
77
78 assert isinstance(app.extensions["cache"][cache], cache_type)
79
80
81 def test_cached_response_init_is_typed():
82 """Regression test for issue #628.
83
84 ``CachedResponse.__init__`` must declare a return type and parameter
85 types so that mypy ``--strict`` doesn't flag construction in typed
86 code with ``[no-untyped-call]``. See
87 https://github.com/pallets-eco/flask-caching/issues/628.
88 """
89 sig = inspect.signature(CachedResponse.__init__)
90 # __init__ should declare ``-> None`` rather than be untyped.
91 # inspect.signature returns the literal ``None`` for ``-> None``.
92 assert sig.return_annotation is None, (
93 f"CachedResponse.__init__ return annotation is {sig.return_annotation!r}; "
94 "expected None"
95 )
96 # All non-self parameters should carry an annotation.
97 for name, param in sig.parameters.items():
98 if name == "self":
99 continue
100 assert param.annotation is not inspect.Parameter.empty, (
101 f"CachedResponse.__init__ parameter {name!r} has no annotation"
102 )
103
104
105 def test_cached_response_construction_with_flask_response():
106 """Sanity check that CachedResponse still wraps a flask Response."""
107 app = Flask(__name__)
108 with app.test_request_context():
109 resp = make_response("hi")
110 cached = CachedResponse(resp, 10)
111 assert cached.timeout == 10
112 assert cached.get_data(as_text=True) == "hi"