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(
61 {
62 "CACHE_TYPE": "RedisCache",
63 "CACHE_KEY_PREFIX": "foo",
64 "CACHE_REDIS_PORT": 6360,
65 }
66 )
67
68 app2 = Flask(__name__)
69 app2.config.from_mapping(
70 {
71 "CACHE_TYPE": "RedisCache",
72 "CACHE_KEY_PREFIX": "bar",
73 "CACHE_REDIS_PORT": 6360,
74 }
75 )
76 cache.init_app(app1)
77 cache.init_app(app2)
78
79 # When we have the app context, the prefix should be
80 # different for each app.
81 with app1.app_context():
82 assert cache.cache.key_prefix == "foo"
83
84 with app2.app_context():
85 assert cache.cache.key_prefix == "bar"
86
87
88 @pytest.mark.skipif(HAS_NOT_REDIS, reason="requires Redis")
89 def test_app_redis_cache_backend_url_default_db(app, redis_server):
90 config = {
91 "CACHE_TYPE": "RedisCache",
92 "CACHE_REDIS_URL": "redis://localhost:6360",
93 }
94 cache = Cache()
95 cache.init_app(app, config=config)
96 from flask_caching.backends.rediscache import RedisCache
97
98 assert isinstance(app.extensions["cache"][cache], RedisCache)
99 rconn = app.extensions["cache"][
100 cache
101 ]._write_client.connection_pool.get_connection()
102 assert rconn.db == 0
103
104
105 @pytest.mark.skipif(HAS_NOT_REDIS, reason="requires Redis")
106 def test_app_redis_cache_backend_url_custom_db(app, redis_server):
107 config = {
108 "CACHE_TYPE": "RedisCache",
109 "CACHE_REDIS_URL": "redis://localhost:6360/2",
110 }
111 cache = Cache()
112 cache.init_app(app, config=config)
113 rconn = app.extensions["cache"][
114 cache
115 ]._write_client.connection_pool.get_connection()
116 assert rconn.db == 2
117
118
119 @pytest.mark.skipif(HAS_NOT_REDIS, reason="requires Redis")
120 def test_app_redis_cache_backend_url_explicit_db_arg(app, redis_server):
121 config = {
122 "CACHE_TYPE": "RedisCache",
123 "CACHE_REDIS_URL": "redis://localhost:6360",
124 "CACHE_REDIS_DB": 1,
125 }
126 cache = Cache()
127 cache.init_app(app, config=config)
128 rconn = app.extensions["cache"][
129 cache
130 ]._write_client.connection_pool.get_connection()
131 assert rconn.db == 1
132
133
134 def test_app_custom_cache_backend(app):
135 cache = Cache()
136 app.config["CACHE_TYPE"] = "test_basic_app.newsimple"
137 cache.init_app(app)
138
139 with app.app_context():
140 assert isinstance(cache.cache, CustomSimpleCache)
141
142
143 def test_subclassed_cache_class(app):
144 # just invoking it here proofs that everything worked when subclassing
145 # otherwise an werkzeug.utils.ImportStringError exception will be raised
146 # because flask-caching can't find the backend
147
148 # testing for "not raises" looked more hacky like this..
149 CustomCache(app)