Debian Codegraph @

tests/test_basic_app.py

raw
1 import pytest
2 from flask import Flask
3
4 from flask_caching import Cache
5 from flask_caching.backends.simplecache import SimpleCache
6
7 try:
8 import redis # noqa
9
10 HAS_NOT_REDIS = False
11 except ImportError:
12 HAS_NOT_REDIS = True
13
14
15 class CustomCache(Cache):
16 pass
17
18
19 class CustomSimpleCache(SimpleCache):
20 pass
21
22
23 def newsimple(app, config, args, kwargs):
24 return CustomSimpleCache(*args, **kwargs)
25
26
27 def test_dict_config(app):
28 cache = Cache(config={"CACHE_TYPE": "SimpleCache"})
29 cache.init_app(app)
30
31 assert cache.config["CACHE_TYPE"] == "SimpleCache"
32
33
34 def test_dict_config_initapp(app):
35 cache = Cache()
36 cache.init_app(app, config={"CACHE_TYPE": "SimpleCache"})
37 from flask_caching.backends.simplecache import SimpleCache
38
39 assert isinstance(app.extensions["cache"][cache], SimpleCache)
40
41
42 def test_dict_config_both(app):
43 cache = Cache(config={"CACHE_TYPE": "NullCache"})
44 cache.init_app(app, config={"CACHE_TYPE": "SimpleCache"})
45 from flask_caching.backends.simplecache import SimpleCache
46
47 assert isinstance(app.extensions["cache"][cache], SimpleCache)
48
49
50 def test_init_app_sets_app_attribute(app):
51 cache = Cache()
52 cache.init_app(app)
53 assert cache.app == app
54
55
56 @pytest.mark.skipif(HAS_NOT_REDIS, reason="requires Redis")
57 def test_init_app_multi_apps(app, redis_server):
58 cache = Cache()
59 app1 = Flask(__name__)
60 app1.config.from_mapping({"CACHE_TYPE": "RedisCache", "CACHE_KEY_PREFIX": "foo"})
61
62 app2 = Flask(__name__)
63 app2.config.from_mapping({"CACHE_TYPE": "RedisCache", "CACHE_KEY_PREFIX": "bar"})
64 cache.init_app(app1)
65 cache.init_app(app2)
66
67 # When we have the app context, the prefix should be
68 # different for each app.
69 with app1.app_context():
70 assert cache.cache.key_prefix == "foo"
71
72 with app2.app_context():
73 assert cache.cache.key_prefix == "bar"
74
75
76 @pytest.mark.skipif(HAS_NOT_REDIS, reason="requires Redis")
77 def test_app_redis_cache_backend_url_default_db(app, redis_server):
78 config = {
79 "CACHE_TYPE": "RedisCache",
80 "CACHE_REDIS_URL": "redis://localhost:6379",
81 }
82 cache = Cache()
83 cache.init_app(app, config=config)
84 from flask_caching.backends.rediscache import RedisCache
85
86 assert isinstance(app.extensions["cache"][cache], RedisCache)
87 rconn = app.extensions["cache"][cache]._write_client.connection_pool.get_connection(
88 "foo"
89 )
90 assert rconn.db == 0
91
92
93 @pytest.mark.skipif(HAS_NOT_REDIS, reason="requires Redis")
94 def test_app_redis_cache_backend_url_custom_db(app, redis_server):
95 config = {
96 "CACHE_TYPE": "RedisCache",
97 "CACHE_REDIS_URL": "redis://localhost:6379/2",
98 }
99 cache = Cache()
100 cache.init_app(app, config=config)
101 rconn = app.extensions["cache"][cache]._write_client.connection_pool.get_connection(
102 "foo"
103 )
104 assert rconn.db == 2
105
106
107 @pytest.mark.skipif(HAS_NOT_REDIS, reason="requires Redis")
108 def test_app_redis_cache_backend_url_explicit_db_arg(app, redis_server):
109 config = {
110 "CACHE_TYPE": "RedisCache",
111 "CACHE_REDIS_URL": "redis://localhost:6379",
112 "CACHE_REDIS_DB": 1,
113 }
114 cache = Cache()
115 cache.init_app(app, config=config)
116 rconn = app.extensions["cache"][cache]._write_client.connection_pool.get_connection(
117 "foo"
118 )
119 assert rconn.db == 1
120
121
122 def test_app_custom_cache_backend(app):
123 cache = Cache()
124 app.config["CACHE_TYPE"] = "test_basic_app.newsimple"
125 cache.init_app(app)
126
127 with app.app_context():
128 assert isinstance(cache.cache, CustomSimpleCache)
129
130
131 def test_subclassed_cache_class(app):
132 # just invoking it here proofs that everything worked when subclassing
133 # otherwise an werkzeug.utils.ImportStringError exception will be raised
134 # because flask-caching can't find the backend
135
136 # testing for "not raises" looked more hacky like this..
137 CustomCache(app)