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