1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
import random
import time
import pytest
from flask_caching import Cache
try:
import redis # noqa
HAS_NOT_REDIS = False
except ImportError:
HAS_NOT_REDIS = True
def test_cache_set(app, cache):
cache.set("hi", "hello")
assert cache.get("hi") == "hello"
def test_cache_has(app, cache):
cache.add("hi", "hello")
assert cache.has("hi")
def test_cache_add(app, cache):
cache.add("hi", "hello")
assert cache.get("hi") == "hello"
cache.add("hi", "foobar")
assert cache.get("hi") == "hello"
def test_cache_delete(app, cache):
cache.set("hi", "hello")
cache.delete("hi")
assert cache.get("hi") is None
def test_cache_delete_many(app, cache):
cache.set("hi", "hello")
cache.delete_many("ho", "hi")
assert cache.get("hi") is not None
@pytest.mark.skipif(HAS_NOT_REDIS, reason="requires Redis")
def test_cache_unlink(app, redis_server):
cache = Cache(config={"CACHE_TYPE": "redis"})
cache.init_app(app)
cache.set("biggerkey", "test" * 100)
cache.unlink("biggerkey")
assert cache.get("biggerkey") is None
cache.set("biggerkey1", "test" * 100)
cache.set("biggerkey2", "test" * 100)
cache.unlink("biggerkey1", "biggerkey2")
assert cache.get("biggerkey1") is None
assert cache.get("biggerkey2") is None
def test_cache_unlink_if_not(app):
cache = Cache(config={"CACHE_TYPE": "simple"})
cache.init_app(app)
cache.set("biggerkey", "test" * 100)
cache.unlink("biggerkey")
assert cache.get("biggerkey") is None
cache.set("biggerkey1", "test" * 100)
cache.set("biggerkey2", "test" * 100)
cache.unlink("biggerkey1", "biggerkey2")
assert cache.get("biggerkey1") is None
assert cache.get("biggerkey2") is None
def test_cache_delete_many_ignored(app):
cache = Cache(config={"CACHE_TYPE": "simple", "CACHE_IGNORE_ERRORS": True})
cache.init_app(app)
cache.set("hi", "hello")
assert cache.get("hi") == "hello"
cache.delete_many("ho", "hi")
assert cache.get("hi") is None
def test_cache_cached_function(app, cache):
with app.test_request_context():
@cache.cached(1, key_prefix="MyBits")
def get_random_bits():
return [random.randrange(0, 2) for i in range(50)]
my_list = get_random_bits()
his_list = get_random_bits()
assert my_list == his_list
time.sleep(2)
his_list = get_random_bits()
assert my_list != his_list
def test_cache_cached_function_with_source_check_enabled(app, cache):
with app.test_request_context():
@cache.cached(key_prefix="MyBits", source_check=True)
def get_random_bits():
return [random.randrange(0, 2) for i in range(50)]
first_attempt = get_random_bits()
second_attempt = get_random_bits()
assert second_attempt == first_attempt
# ... change the source to see if the return value changes when called
@cache.cached(key_prefix="MyBits", source_check=True)
def get_random_bits():
return {"val": [random.randrange(0, 2) for i in range(50)]}
third_attempt = get_random_bits()
assert third_attempt != first_attempt
# We changed the return data type so we do a check to be sure
assert isinstance(third_attempt, dict)
# ... change the source back to what it was original and the data should
# be the same
@cache.cached(key_prefix="MyBits", source_check=True)
def get_random_bits():
return [random.randrange(0, 2) for i in range(50)]
forth_attempt = get_random_bits()
assert forth_attempt == first_attempt
def test_cache_cached_function_with_source_check_disabled(app, cache):
with app.test_request_context():
@cache.cached(key_prefix="MyBits", source_check=False)
def get_random_bits():
return [random.randrange(0, 2) for i in range(50)]
first_attempt = get_random_bits()
second_attempt = get_random_bits()
assert second_attempt == first_attempt
# ... change the source to see if the return value changes when called
@cache.cached(key_prefix="MyBits", source_check=False)
def get_random_bits():
return {"val": [random.randrange(0, 2) for i in range(50)]}
third_attempt = get_random_bits()
assert third_attempt == first_attempt
def test_cache_accepts_multiple_ciphers(app, cache, hash_method):
with app.test_request_context():
@cache.cached(1, key_prefix="MyBits", hash_method=hash_method)
def get_random_bits():
return [random.randrange(0, 2) for i in range(50)]
my_list = get_random_bits()
his_list = get_random_bits()
assert my_list == his_list
time.sleep(2)
his_list = get_random_bits()
assert my_list != his_list
def test_cached_none(app, cache):
with app.test_request_context():
from collections import Counter
call_counter = Counter()
@cache.cached(cache_none=True)
def cache_none(param):
call_counter[param] += 1
return None
cache_none(1)
assert call_counter[1] == 1
assert cache_none(1) is None
assert call_counter[1] == 1
cache.clear()
cache_none(1)
assert call_counter[1] == 2
def test_cached_doesnt_cache_none(app, cache):
"""Asserting that when cache_none is False, we always
assume a None value returned from .get() means the key is not found
"""
with app.test_request_context():
from collections import Counter
call_counter = Counter()
@cache.cached()
def cache_none(param):
call_counter[param] += 1
return None
cache_none(1)
# The cached function should have been called
assert call_counter[1] == 1
# Next time we call the function, the value should be coming from the cache…
# But the value is None and so we treat it as uncached.
assert cache_none(1) is None
# …thus, the call counter should increment to 2
assert call_counter[1] == 2
cache.clear()
cache_none(1)
assert call_counter[1] == 3
def test_cache_forced_update(app, cache):
from collections import Counter
with app.test_request_context():
need_update = False
call_counter = Counter()
@cache.cached(1, forced_update=lambda: need_update)
def cached_function(param):
call_counter[param] += 1
return 1
cached_function(1)
assert call_counter[1] == 1
assert cached_function(1) == 1
assert call_counter[1] == 1
need_update = True
assert cached_function(1) == 1
assert call_counter[1] == 2
def test_cache_forced_update_params(app, cache):
from collections import Counter
with app.test_request_context():
cached_call_counter = Counter()
call_counter = Counter()
call_params = {}
def need_update(param):
"""This helper function returns True if it has been called with
the same params for more than 2 times
"""
call_counter[param] += 1
call_params[call_counter[param] - 1] = (param,)
return call_counter[param] > 2
@cache.cached(1, forced_update=need_update)
def cached_function(param):
cached_call_counter[param] += 1
return 1
assert cached_function(1) == 1
# need_update should have been called once
assert call_counter[1] == 1
# the parameters used to call need_update should be the same as the
# parameters used to call cached_function
assert call_params[0] == (1,)
# the cached function should have been called once
assert cached_call_counter[1] == 1
assert cached_function(1) == 1
# need_update should have been called twice by now as forced_update
# should be called regardless of the arguments
assert call_counter[1] == 2
# the parameters used to call need_update should be the same as the
# parameters used to call cached_function
assert call_params[1] == (1,)
# this time the forced_update should have returned False, so
# cached_function should not have been called again
assert cached_call_counter[1] == 1
assert cached_function(1) == 1
# need_update should have been called thrice by now as forced_update
# should be called regardless of the arguments
assert call_counter[1] == 3
# the parameters used to call need_update should be the same as the
# parameters used to call cached_function
assert call_params[1] == (1,)
# this time the forced_update should have returned True, so
# cached_function should have been called again
assert cached_call_counter[1] == 2
def test_generator(app, cache):
"""test function return generator"""
with app.test_request_context():
@cache.cached()
def gen():
return (str(time.time()) for i in range(2))
time_str = gen()
time.sleep(1)
assert gen() == time_str
@cache.cached()
def gen_yield():
yield str(time.time())
yield str(time.time())
time_str = gen_yield()
time.sleep(1)
assert gen_yield() == time_str
|