| 1 |
import hashlib |
| 2 |
import itertools |
| 3 |
import time |
| 4 |
|
| 5 |
from flask import abort |
| 6 |
from flask import make_response |
| 7 |
from flask import request |
| 8 |
from flask.views import View |
| 9 |
|
| 10 |
from flask_caching import CachedResponse |
| 11 |
|
| 12 |
|
| 13 |
def test_cached_view(app, cache, clock): |
| 14 |
counter = itertools.count() |
| 15 |
|
| 16 |
@app.route("/") |
| 17 |
@cache.cached(2) |
| 18 |
def cached_view(): |
| 19 |
return str(next(counter)) |
| 20 |
|
| 21 |
tc = app.test_client() |
| 22 |
|
| 23 |
rv = tc.get("/") |
| 24 |
first = rv.data.decode("utf-8") |
| 25 |
|
| 26 |
clock.advance(1) |
| 27 |
|
| 28 |
rv = tc.get("/") |
| 29 |
|
| 30 |
assert first == rv.data.decode("utf-8") |
| 31 |
|
| 32 |
clock.advance(1) |
| 33 |
|
| 34 |
rv = tc.get("/") |
| 35 |
assert first != rv.data.decode("utf-8") |
| 36 |
|
| 37 |
|
| 38 |
def test_cached_view_class(app, cache, clock): |
| 39 |
counter = itertools.count() |
| 40 |
|
| 41 |
class CachedView(View): |
| 42 |
@cache.cached(2) |
| 43 |
def dispatch_request(self): |
| 44 |
return str(next(counter)) |
| 45 |
|
| 46 |
app.add_url_rule("/", view_func=CachedView.as_view("name")) |
| 47 |
|
| 48 |
tc = app.test_client() |
| 49 |
|
| 50 |
rv = tc.get("/") |
| 51 |
first = rv.data.decode("utf-8") |
| 52 |
|
| 53 |
clock.advance(1) |
| 54 |
|
| 55 |
rv = tc.get("/") |
| 56 |
|
| 57 |
assert first == rv.data.decode("utf-8") |
| 58 |
|
| 59 |
clock.advance(1) |
| 60 |
|
| 61 |
rv = tc.get("/") |
| 62 |
assert first != rv.data.decode("utf-8") |
| 63 |
|
| 64 |
|
| 65 |
def test_async_cached_view(app, cache): |
| 66 |
import asyncio |
| 67 |
|
| 68 |
@app.route("/test-async") |
| 69 |
@cache.cached(2) |
| 70 |
async def cached_async_view(): |
| 71 |
await asyncio.sleep(0) |
| 72 |
return str(time.time()) |
| 73 |
|
| 74 |
tc = app.test_client() |
| 75 |
rv = tc.get("/test-async") |
| 76 |
the_time = rv.data.decode("utf-8") |
| 77 |
|
| 78 |
rv = tc.get("/test-async") |
| 79 |
assert the_time == rv.data.decode("utf-8") |
| 80 |
|
| 81 |
|
| 82 |
def test_cached_view_unless(app, cache): |
| 83 |
counter = itertools.count() |
| 84 |
|
| 85 |
@app.route("/a") |
| 86 |
@cache.cached(5, unless=lambda: True) |
| 87 |
def non_cached_view(): |
| 88 |
return str(next(counter)) |
| 89 |
|
| 90 |
@app.route("/b") |
| 91 |
@cache.cached(5, unless=lambda: False) |
| 92 |
def cached_view(): |
| 93 |
return str(next(counter)) |
| 94 |
|
| 95 |
tc = app.test_client() |
| 96 |
|
| 97 |
rv = tc.get("/a") |
| 98 |
first = rv.data.decode("utf-8") |
| 99 |
|
| 100 |
rv = tc.get("/a") |
| 101 |
assert first != rv.data.decode("utf-8") |
| 102 |
|
| 103 |
rv = tc.get("/b") |
| 104 |
first = rv.data.decode("utf-8") |
| 105 |
|
| 106 |
rv = tc.get("/b") |
| 107 |
|
| 108 |
assert first == rv.data.decode("utf-8") |
| 109 |
|
| 110 |
|
| 111 |
def test_cached_view_response_filter(app, cache): |
| 112 |
counter = itertools.count() |
| 113 |
|
| 114 |
@app.route("/a") |
| 115 |
@cache.cached(5, response_filter=lambda x: x[1] < 400) |
| 116 |
def cached_view(): |
| 117 |
return (str(next(counter)), app.return_code) |
| 118 |
|
| 119 |
tc = app.test_client() |
| 120 |
|
| 121 |
# 500 response does not cache |
| 122 |
app.return_code = 500 |
| 123 |
rv = tc.get("/a") |
| 124 |
first = rv.data.decode("utf-8") |
| 125 |
|
| 126 |
rv = tc.get("/a") |
| 127 |
assert first != rv.data.decode("utf-8") |
| 128 |
|
| 129 |
# 200 response caches |
| 130 |
app.return_code = 200 |
| 131 |
rv = tc.get("/a") |
| 132 |
first = rv.data.decode("utf-8") |
| 133 |
|
| 134 |
rv = tc.get("/a") |
| 135 |
assert first == rv.data.decode("utf-8") |
| 136 |
|
| 137 |
|
| 138 |
def test_cached_view_forced_update(app, cache): |
| 139 |
forced_update = False |
| 140 |
counter = itertools.count() |
| 141 |
|
| 142 |
@app.route("/a") |
| 143 |
@cache.cached(5, forced_update=lambda: forced_update) |
| 144 |
def view(): |
| 145 |
return str(next(counter)) |
| 146 |
|
| 147 |
tc = app.test_client() |
| 148 |
|
| 149 |
rv = tc.get("/a") |
| 150 |
first = rv.data.decode("utf-8") |
| 151 |
rv = tc.get("/a") |
| 152 |
assert first == rv.data.decode("utf-8") |
| 153 |
|
| 154 |
forced_update = True |
| 155 |
rv = tc.get("/a") |
| 156 |
second = rv.data.decode("utf-8") |
| 157 |
assert second != first |
| 158 |
|
| 159 |
forced_update = False |
| 160 |
rv = tc.get("/a") |
| 161 |
assert second == rv.data.decode("utf-8") |
| 162 |
|
| 163 |
|
| 164 |
def test_cached_view_is_stale(app, cache): |
| 165 |
stale = False |
| 166 |
counter = itertools.count() |
| 167 |
|
| 168 |
@app.route("/a") |
| 169 |
@cache.cached(5, is_stale=lambda response: stale) |
| 170 |
def view(): |
| 171 |
return str(next(counter)) |
| 172 |
|
| 173 |
tc = app.test_client() |
| 174 |
|
| 175 |
rv = tc.get("/a") |
| 176 |
first = rv.data.decode("utf-8") |
| 177 |
rv = tc.get("/a") |
| 178 |
assert first == rv.data.decode("utf-8") |
| 179 |
|
| 180 |
stale = True |
| 181 |
rv = tc.get("/a") |
| 182 |
second = rv.data.decode("utf-8") |
| 183 |
assert second != first |
| 184 |
|
| 185 |
stale = False |
| 186 |
rv = tc.get("/a") |
| 187 |
assert second == rv.data.decode("utf-8") |
| 188 |
|
| 189 |
|
| 190 |
def test_generate_cache_key_from_different_view(app, cache): |
| 191 |
@app.route("/cake/<flavor>") |
| 192 |
@cache.cached() |
| 193 |
def view_cake(flavor): |
| 194 |
# What's the cache key for apple cake? thanks for making me hungry |
| 195 |
view_cake.cake_cache_key = view_cake.make_cache_key("apple") |
| 196 |
return str(time.time()) |
| 197 |
|
| 198 |
view_cake.cake_cache_key = "" |
| 199 |
|
| 200 |
@app.route("/pie/<flavor>") |
| 201 |
@cache.cached() |
| 202 |
def view_pie(flavor): |
| 203 |
# What's the cache key for apple cake? |
| 204 |
view_pie.cake_cache_key = view_cake.make_cache_key("apple") |
| 205 |
return str(time.time()) |
| 206 |
|
| 207 |
view_pie.cake_cache_key = "" |
| 208 |
|
| 209 |
tc = app.test_client() |
| 210 |
tc.get("/cake/chocolate") |
| 211 |
tc.get("/pie/chocolate") |
| 212 |
|
| 213 |
assert view_cake.cake_cache_key == view_pie.cake_cache_key |
| 214 |
|
| 215 |
|
| 216 |
# rename/move to seperate module? |
| 217 |
def test_cache_key_property(app, cache): |
| 218 |
@app.route("/") |
| 219 |
@cache.cached(5) |
| 220 |
def cached_view(): |
| 221 |
return str(time.time()) |
| 222 |
|
| 223 |
assert hasattr(cached_view, "make_cache_key") |
| 224 |
assert callable(cached_view.make_cache_key) |
| 225 |
|
| 226 |
tc = app.test_client() |
| 227 |
|
| 228 |
rv = tc.get("/") |
| 229 |
the_time = rv.data.decode("utf-8") |
| 230 |
|
| 231 |
with app.test_request_context(): |
| 232 |
cache_data = cache.get(cached_view.make_cache_key()) |
| 233 |
assert the_time == cache_data |
| 234 |
|
| 235 |
|
| 236 |
def test_set_make_cache_key_property(app, cache): |
| 237 |
@app.route("/") |
| 238 |
@cache.cached(5) |
| 239 |
def cached_view(): |
| 240 |
return str(time.time()) |
| 241 |
|
| 242 |
cached_view.make_cache_key = lambda *args, **kwargs: request.args["foo"] |
| 243 |
|
| 244 |
tc = app.test_client() |
| 245 |
|
| 246 |
rv = tc.get("/?foo=a") |
| 247 |
a = rv.data.decode("utf-8") |
| 248 |
|
| 249 |
rv = tc.get("/?foo=b") |
| 250 |
b = rv.data.decode("utf-8") |
| 251 |
assert a != b |
| 252 |
|
| 253 |
tc = app.test_client() |
| 254 |
rv = tc.get("/?foo=a") |
| 255 |
a_2 = rv.data.decode("utf-8") |
| 256 |
assert a == a_2 |
| 257 |
|
| 258 |
rv = tc.get("/?foo=b") |
| 259 |
b_2 = rv.data.decode("utf-8") |
| 260 |
assert b == b_2 |
| 261 |
|
| 262 |
|
| 263 |
def test_make_cache_key_function_property(app, cache): |
| 264 |
@app.route("/<foo>/<bar>") |
| 265 |
@cache.memoize(5) |
| 266 |
def cached_view(foo, bar): |
| 267 |
return str(time.time()) |
| 268 |
|
| 269 |
assert hasattr(cached_view, "make_cache_key") |
| 270 |
assert callable(cached_view.make_cache_key) |
| 271 |
|
| 272 |
tc = app.test_client() |
| 273 |
|
| 274 |
rv = tc.get("/a/b") |
| 275 |
the_time = rv.data.decode("utf-8") |
| 276 |
|
| 277 |
cache_key = cached_view.make_cache_key(cached_view.uncached, foo="a", bar="b") |
| 278 |
cache_data = cache.get(cache_key) |
| 279 |
assert the_time == cache_data |
| 280 |
|
| 281 |
different_key = cached_view.make_cache_key(cached_view.uncached, foo="b", bar="a") |
| 282 |
different_data = cache.get(different_key) |
| 283 |
assert the_time != different_data |
| 284 |
|
| 285 |
|
| 286 |
def test_cache_timeout_property(app, cache, clock): |
| 287 |
counter = itertools.count() |
| 288 |
|
| 289 |
@app.route("/") |
| 290 |
@cache.memoize(2) |
| 291 |
def cached_view1(): |
| 292 |
return str(next(counter)) |
| 293 |
|
| 294 |
@app.route("/<foo>/<bar>") |
| 295 |
@cache.memoize(4) |
| 296 |
def cached_view2(foo, bar): |
| 297 |
return str(next(counter)) |
| 298 |
|
| 299 |
assert hasattr(cached_view1, "cache_timeout") |
| 300 |
assert hasattr(cached_view2, "cache_timeout") |
| 301 |
assert cached_view1.cache_timeout == 2 |
| 302 |
assert cached_view2.cache_timeout == 4 |
| 303 |
|
| 304 |
# test that this is a read-write property |
| 305 |
cached_view1.cache_timeout = 5 |
| 306 |
cached_view2.cache_timeout = 7 |
| 307 |
|
| 308 |
assert cached_view1.cache_timeout == 5 |
| 309 |
assert cached_view2.cache_timeout == 7 |
| 310 |
tc = app.test_client() |
| 311 |
|
| 312 |
rv1 = tc.get("/") |
| 313 |
body1 = rv1.data.decode("utf-8") |
| 314 |
clock.advance(1) |
| 315 |
rv2 = tc.get("/a/b") |
| 316 |
body2 = rv2.data.decode("utf-8") |
| 317 |
|
| 318 |
# VIEW1 |
| 319 |
# it's been 1 second, cache is still active |
| 320 |
assert body1 == tc.get("/").data.decode("utf-8") |
| 321 |
clock.advance(5) |
| 322 |
# it's been >5 seconds, cache is not still active |
| 323 |
assert body1 != tc.get("/").data.decode("utf-8") |
| 324 |
|
| 325 |
# VIEW2 |
| 326 |
# it's been 6 seconds, cache is still active |
| 327 |
assert body2 == tc.get("/a/b").data.decode("utf-8") |
| 328 |
clock.advance(3) |
| 329 |
# it's been >7 seconds, cache is not still active |
| 330 |
assert body2 != tc.get("/a/b").data.decode("utf-8") |
| 331 |
|
| 332 |
|
| 333 |
def test_cache_timeout_dynamic_via_cached_reponse(app, cache, clock): |
| 334 |
counter = itertools.count() |
| 335 |
|
| 336 |
@app.route("/") |
| 337 |
@cache.cached(timeout=1) |
| 338 |
def cached_view(): |
| 339 |
# This should override the timeout to be 2 seconds |
| 340 |
return CachedResponse(response=make_response(str(next(counter))), timeout=2) |
| 341 |
|
| 342 |
tc = app.test_client() |
| 343 |
|
| 344 |
rv1 = tc.get("/") |
| 345 |
body = rv1.data.decode("utf-8") |
| 346 |
clock.advance(1) |
| 347 |
|
| 348 |
# it's been 1 second, cache is still active |
| 349 |
assert body == tc.get("/").data.decode("utf-8") |
| 350 |
clock.advance(1) |
| 351 |
# it's been >2 seconds, cache is not still active |
| 352 |
assert body != tc.get("/").data.decode("utf-8") |
| 353 |
|
| 354 |
|
| 355 |
def test_generate_cache_key_from_query_string(app, cache): |
| 356 |
"""Test the _make_cache_key_query_string() cache key maker. |
| 357 |
|
| 358 |
Create three requests to verify that the same query string |
| 359 |
parameters (key/value) always reference the same cache, |
| 360 |
regardless of the order of parameters. |
| 361 |
|
| 362 |
Also test to make sure that the same cache isn't being used for |
| 363 |
any/all query string parameters. |
| 364 |
|
| 365 |
For example, these two requests should yield the same |
| 366 |
cache/cache key: |
| 367 |
|
| 368 |
* GET /v1/works?mock=true&offset=20&limit=15 |
| 369 |
* GET /v1/works?limit=15&mock=true&offset=20 |
| 370 |
|
| 371 |
Caching functionality is verified by a `@cached` route `/works` which |
| 372 |
produces a time in its response. The time in the response can verify that |
| 373 |
two requests with the same query string parameters/values, though |
| 374 |
differently ordered, produce responses with the same time. |
| 375 |
""" |
| 376 |
|
| 377 |
@app.route("/works") |
| 378 |
@cache.cached(query_string=True) |
| 379 |
def view_works(): |
| 380 |
return str(time.time()) |
| 381 |
|
| 382 |
tc = app.test_client() |
| 383 |
|
| 384 |
# Make our first query... |
| 385 |
first_response = tc.get("/works?mock=true&offset=20&limit=15") |
| 386 |
first_time = first_response.get_data(as_text=True) |
| 387 |
|
| 388 |
# Make the second query... |
| 389 |
second_response = tc.get("/works?limit=15&mock=true&offset=20") |
| 390 |
second_time = second_response.get_data(as_text=True) |
| 391 |
|
| 392 |
# Now make sure the time for the first and second |
| 393 |
# query are the same! |
| 394 |
assert second_time == first_time |
| 395 |
|
| 396 |
# Last/third query with different parameters/values should |
| 397 |
# produce a different time. |
| 398 |
third_response = tc.get("/v1/works?limit=20&mock=true&offset=60") |
| 399 |
third_time = third_response.get_data(as_text=True) |
| 400 |
|
| 401 |
# ... making sure that different query parameter values |
| 402 |
# don't yield the same cache! |
| 403 |
assert not third_time == second_time |
| 404 |
|
| 405 |
|
| 406 |
def test_generate_cache_key_from_query_string_repeated_paramaters(app, cache): |
| 407 |
"""Test the _make_cache_key_query_string() cache key maker's support for |
| 408 |
repeated query paramaters |
| 409 |
|
| 410 |
URL params can be repeated with different values. Flask's MultiDict |
| 411 |
supports them |
| 412 |
""" |
| 413 |
|
| 414 |
@app.route("/works") |
| 415 |
@cache.cached(query_string=True) |
| 416 |
def view_works(): |
| 417 |
flatted_values = sum(request.args.listvalues(), []) |
| 418 |
return str(sorted(flatted_values)) + str(time.time()) |
| 419 |
|
| 420 |
tc = app.test_client() |
| 421 |
|
| 422 |
# Make our first query... |
| 423 |
first_response = tc.get("/works?mock=true&offset=20&limit=15&user[]=123&user[]=124") |
| 424 |
first_time = first_response.get_data(as_text=True) |
| 425 |
|
| 426 |
# Make the second query... |
| 427 |
second_response = tc.get( |
| 428 |
"/works?mock=true&offset=20&limit=15&user[]=124&user[]=123" |
| 429 |
) |
| 430 |
second_time = second_response.get_data(as_text=True) |
| 431 |
|
| 432 |
# Now make sure the time for the first and second |
| 433 |
# query are the same! |
| 434 |
assert second_time == first_time |
| 435 |
|
| 436 |
# Last/third query with different parameters/values should |
| 437 |
# produce a different time. |
| 438 |
third_response = tc.get("/works?mock=true&offset=20&limit=15&user[]=125&user[]=124") |
| 439 |
third_time = third_response.get_data(as_text=True) |
| 440 |
|
| 441 |
# ... making sure that different query parameter values |
| 442 |
# don't yield the same cache! |
| 443 |
assert not third_time == second_time |
| 444 |
|
| 445 |
|
| 446 |
def test_generate_cache_key_from_request_body(app, cache): |
| 447 |
"""Test a user supplied cache key maker. |
| 448 |
Create three requests to verify that the same request body |
| 449 |
always reference the same cache |
| 450 |
Also test to make sure that the same cache isn't being used for |
| 451 |
any/all query string parameters. |
| 452 |
Caching functionality is verified by a `@cached` route `/works` which |
| 453 |
produces a time in its response. The time in the response can verify that |
| 454 |
two requests with the same request body produce responses with the same |
| 455 |
time. |
| 456 |
""" |
| 457 |
|
| 458 |
def _make_cache_key_request_body(argument): |
| 459 |
"""Create keys based on request body.""" |
| 460 |
# now hash the request body so it can be |
| 461 |
# used as a key for cache. |
| 462 |
request_body = request.get_data(as_text=False) |
| 463 |
hashed_body = str(hashlib.md5(request_body).hexdigest()) |
| 464 |
cache_key = request.path + hashed_body |
| 465 |
return cache_key |
| 466 |
|
| 467 |
@app.route("/works/<argument>", methods=["POST"]) |
| 468 |
@cache.cached(make_cache_key=_make_cache_key_request_body) |
| 469 |
def view_works(argument): |
| 470 |
return str(time.time()) + request.get_data().decode() |
| 471 |
|
| 472 |
tc = app.test_client() |
| 473 |
|
| 474 |
# Make our request... |
| 475 |
first_response = tc.post("/works/arg", data=dict(mock=True, value=1, test=2)) |
| 476 |
first_time = first_response.get_data(as_text=True) |
| 477 |
|
| 478 |
# Make the request... |
| 479 |
second_response = tc.post("/works/arg", data=dict(mock=True, value=1, test=2)) |
| 480 |
second_time = second_response.get_data(as_text=True) |
| 481 |
|
| 482 |
# Now make sure the time for the first and second |
| 483 |
# requests are the same! |
| 484 |
assert second_time == first_time |
| 485 |
|
| 486 |
# Last/third request with different body should |
| 487 |
# produce a different time. |
| 488 |
third_response = tc.post("/works/arg", data=dict(mock=True, value=2, test=3)) |
| 489 |
third_time = third_response.get_data(as_text=True) |
| 490 |
|
| 491 |
# ... making sure that different request bodies |
| 492 |
# don't yield the same cache! |
| 493 |
assert not third_time == second_time |
| 494 |
|
| 495 |
|
| 496 |
def test_cache_with_query_string_and_source_check_enabled(app, cache): |
| 497 |
"""Test the _make_cache_key_query_string() cache key maker with |
| 498 |
source_check set to True to include the view's function's source code as |
| 499 |
part of the cache hash key. |
| 500 |
""" |
| 501 |
|
| 502 |
@cache.cached(query_string=True, source_check=True) |
| 503 |
def view_works(): |
| 504 |
return str(time.time()) |
| 505 |
|
| 506 |
app.add_url_rule("/works", "works", view_works) |
| 507 |
|
| 508 |
tc = app.test_client() |
| 509 |
|
| 510 |
# Make our first query... |
| 511 |
first_response = tc.get("/works?mock=true&offset=20&limit=15") |
| 512 |
first_time = first_response.get_data(as_text=True) |
| 513 |
|
| 514 |
# Make our second query... |
| 515 |
second_response = tc.get("/works?mock=true&offset=20&limit=15") |
| 516 |
second_time = second_response.get_data(as_text=True) |
| 517 |
|
| 518 |
# The cache should yield the same data first and second time |
| 519 |
assert first_time == second_time |
| 520 |
|
| 521 |
# Change the source of the function attached to the view |
| 522 |
@cache.cached(query_string=True, source_check=True) |
| 523 |
def view_works(): |
| 524 |
return str(time.time()) |
| 525 |
|
| 526 |
# ... and we override the function attached to the view |
| 527 |
app.view_functions["works"] = view_works |
| 528 |
|
| 529 |
tc = app.test_client() |
| 530 |
|
| 531 |
# Make the second query... |
| 532 |
third_response = tc.get("/works?mock=true&offset=20&limit=15") |
| 533 |
third_time = third_response.get_data(as_text=True) |
| 534 |
|
| 535 |
# Now make sure the time for the first and third |
| 536 |
# responses are not the same i.e. cached is not used! |
| 537 |
assert third_time[0] != first_time |
| 538 |
|
| 539 |
# Change the source of the function to what it was originally |
| 540 |
@cache.cached(query_string=True, source_check=True) |
| 541 |
def view_works(): |
| 542 |
return str(time.time()) |
| 543 |
|
| 544 |
app.view_functions["works"] = view_works |
| 545 |
|
| 546 |
tc = app.test_client() |
| 547 |
|
| 548 |
# Last/third query with different parameters/values should |
| 549 |
# produce a different time. |
| 550 |
forth_response = tc.get("/works?mock=true&offset=20&limit=15") |
| 551 |
forth_time = forth_response.get_data(as_text=True) |
| 552 |
|
| 553 |
# ... making sure that the first value and the forth value are the same |
| 554 |
# since the source is the same |
| 555 |
assert forth_time == first_time |
| 556 |
|
| 557 |
|
| 558 |
def test_cache_with_query_string_and_source_check_disabled(app, cache): |
| 559 |
"""Test the _make_cache_key_query_string() cache key maker with |
| 560 |
source_check set to False to exclude the view's function's source code as |
| 561 |
part of the cache hash key and to see if changing the source changes the |
| 562 |
data. |
| 563 |
""" |
| 564 |
|
| 565 |
@cache.cached(query_string=True, source_check=False) |
| 566 |
def view_works(): |
| 567 |
return str(time.time()) |
| 568 |
|
| 569 |
app.add_url_rule("/works", "works", view_works) |
| 570 |
|
| 571 |
tc = app.test_client() |
| 572 |
|
| 573 |
# Make our first query... |
| 574 |
first_response = tc.get("/works?mock=true&offset=20&limit=15") |
| 575 |
first_time = first_response.get_data(as_text=True) |
| 576 |
|
| 577 |
# Make our second query... |
| 578 |
second_response = tc.get("/works?mock=true&offset=20&limit=15") |
| 579 |
second_time = second_response.get_data(as_text=True) |
| 580 |
|
| 581 |
# The cache should yield the same data first and second time |
| 582 |
assert first_time == second_time |
| 583 |
|
| 584 |
# Change the source of the function attached to the view |
| 585 |
@cache.cached(query_string=True, source_check=False) |
| 586 |
def view_works(): |
| 587 |
return str(time.time()) |
| 588 |
|
| 589 |
# ... and we override the function attached to the view |
| 590 |
app.view_functions["works"] = view_works |
| 591 |
|
| 592 |
tc = app.test_client() |
| 593 |
|
| 594 |
# Make the second query... |
| 595 |
third_response = tc.get("/works?mock=true&offset=20&limit=15") |
| 596 |
third_time = third_response.get_data(as_text=True) |
| 597 |
|
| 598 |
# Now make sure the time for the first and third responses are the same |
| 599 |
# i.e. cached is used since cache will not check for source changes! |
| 600 |
assert third_time == first_time |
| 601 |
|
| 602 |
|
| 603 |
def test_hit_cache(app, cache, clock): |
| 604 |
@app.route("/") |
| 605 |
@cache.cached(2, response_hit_indication=True) |
| 606 |
def cached_view(): |
| 607 |
# This should override the timeout to be 2 seconds |
| 608 |
return {"data": "data"} |
| 609 |
|
| 610 |
@app.route("/indication-false") |
| 611 |
@cache.cached(2, response_hit_indication=False) |
| 612 |
def indication_false_cached_view(): |
| 613 |
# This should override the timeout to be 2 seconds |
| 614 |
return {"data": "data"} |
| 615 |
|
| 616 |
tc = app.test_client() |
| 617 |
|
| 618 |
assert tc.get("/").headers.get("hit_cache") is None |
| 619 |
assert tc.get("/").headers.get("hit_cache") == "True" |
| 620 |
assert tc.get("/").headers.get("hit_cache") == "True" |
| 621 |
|
| 622 |
clock.advance(2) |
| 623 |
assert tc.get("/").headers.get("hit_cache") is None |
| 624 |
|
| 625 |
# indication-false |
| 626 |
assert tc.get("/indication-false").headers.get("hit_cache") is None |
| 627 |
assert tc.get("/indication-false").headers.get("hit_cache") is None |
| 628 |
assert tc.get("/indication-false").headers.get("hit_cache") is None |
| 629 |
clock.advance(2) |
| 630 |
assert tc.get("/indication-false").headers.get("hit_cache") is None |
| 631 |
|
| 632 |
|
| 633 |
def test_delete_cached_query_string(app, cache): |
| 634 |
counter = itertools.count() |
| 635 |
|
| 636 |
@app.route("/works") |
| 637 |
@cache.cached(query_string=True) |
| 638 |
def view_works(): |
| 639 |
return str(next(counter)) |
| 640 |
|
| 641 |
tc = app.test_client() |
| 642 |
|
| 643 |
first = tc.get("/works?mock=true&offset=20&limit=15").get_data(as_text=True) |
| 644 |
assert tc.get("/works?mock=true&offset=20&limit=15").get_data(as_text=True) == first |
| 645 |
|
| 646 |
with app.app_context(): |
| 647 |
assert cache.delete_cached(view_works, "/works", "limit=15&mock=true&offset=20") |
| 648 |
|
| 649 |
second = tc.get("/works?mock=true&offset=20&limit=15").get_data(as_text=True) |
| 650 |
assert second != first |
| 651 |
|
| 652 |
with app.app_context(): |
| 653 |
assert cache.delete_cached( |
| 654 |
view_works, "/works", {"offset": 20, "limit": 15, "mock": "true"} |
| 655 |
) |
| 656 |
|
| 657 |
third = tc.get("/works?mock=true&offset=20&limit=15").get_data(as_text=True) |
| 658 |
assert third != second |
| 659 |
|
| 660 |
with app.app_context(): |
| 661 |
assert cache.delete_cached( |
| 662 |
view_works, |
| 663 |
"/works", |
| 664 |
[("offset", "20"), ("mock", "true"), ("limit", "15")], |
| 665 |
) |
| 666 |
|
| 667 |
assert tc.get("/works?mock=true&offset=20&limit=15").get_data(as_text=True) != third |
| 668 |
|
| 669 |
|
| 670 |
def test_delete_cached_query_string_only_deletes_matching_arguments(app, cache): |
| 671 |
counter = itertools.count() |
| 672 |
|
| 673 |
@app.route("/works") |
| 674 |
@cache.cached(query_string=True) |
| 675 |
def view_works(): |
| 676 |
return str(next(counter)) |
| 677 |
|
| 678 |
tc = app.test_client() |
| 679 |
|
| 680 |
kept = tc.get("/works?limit=15").get_data(as_text=True) |
| 681 |
deleted = tc.get("/works?limit=20").get_data(as_text=True) |
| 682 |
|
| 683 |
with app.app_context(): |
| 684 |
assert cache.delete_cached(view_works, "/works", "limit=20") |
| 685 |
|
| 686 |
assert tc.get("/works?limit=15").get_data(as_text=True) == kept |
| 687 |
assert tc.get("/works?limit=20").get_data(as_text=True) != deleted |
| 688 |
|
| 689 |
|
| 690 |
def test_delete_cached_query_string_repeated_parameters(app, cache): |
| 691 |
counter = itertools.count() |
| 692 |
|
| 693 |
@app.route("/works") |
| 694 |
@cache.cached(query_string=True) |
| 695 |
def view_works(): |
| 696 |
return str(next(counter)) |
| 697 |
|
| 698 |
tc = app.test_client() |
| 699 |
|
| 700 |
first = tc.get("/works?user[]=123&user[]=124").get_data(as_text=True) |
| 701 |
|
| 702 |
with app.app_context(): |
| 703 |
assert cache.delete_cached(view_works, "/works", "user[]=124&user[]=123") |
| 704 |
|
| 705 |
assert tc.get("/works?user[]=123&user[]=124").get_data(as_text=True) != first |
| 706 |
|
| 707 |
|
| 708 |
def test_delete_cached_view(app, cache): |
| 709 |
counter = itertools.count() |
| 710 |
|
| 711 |
@app.route("/user/<name>") |
| 712 |
@cache.cached() |
| 713 |
def view_user(name): |
| 714 |
return f"{name}{next(counter)}" |
| 715 |
|
| 716 |
tc = app.test_client() |
| 717 |
|
| 718 |
first = tc.get("/user/bob").get_data(as_text=True) |
| 719 |
assert tc.get("/user/bob").get_data(as_text=True) == first |
| 720 |
|
| 721 |
with app.app_context(): |
| 722 |
assert cache.delete_cached(view_user, "/user/bob") |
| 723 |
|
| 724 |
assert tc.get("/user/bob").get_data(as_text=True) != first |
| 725 |
|
| 726 |
|
| 727 |
def test_delete_cached_view_builds_the_path_with_url_for(app, cache): |
| 728 |
app.config["SERVER_NAME"] = "localhost" |
| 729 |
counter = itertools.count() |
| 730 |
|
| 731 |
@app.route("/user/<name>") |
| 732 |
@cache.cached() |
| 733 |
def view_user(name): |
| 734 |
return f"{name}{next(counter)}" |
| 735 |
|
| 736 |
tc = app.test_client() |
| 737 |
|
| 738 |
first = tc.get("/user/bob").get_data(as_text=True) |
| 739 |
|
| 740 |
with app.app_context(): |
| 741 |
assert cache.delete_cached(view_user, name="bob") |
| 742 |
|
| 743 |
assert tc.get("/user/bob").get_data(as_text=True) != first |
| 744 |
|
| 745 |
|
| 746 |
def test_make_cache_key_outside_request_context(app, cache): |
| 747 |
app.config["SERVER_NAME"] = "localhost" |
| 748 |
|
| 749 |
@app.route("/works") |
| 750 |
@cache.cached(query_string=True) |
| 751 |
def view_works(): |
| 752 |
return "works" |
| 753 |
|
| 754 |
@app.route("/user/<name>") |
| 755 |
@cache.cached() |
| 756 |
def view_user(name): |
| 757 |
return name |
| 758 |
|
| 759 |
with app.test_request_context("/works?mock=true&limit=15"): |
| 760 |
in_request = view_works.make_cache_key() |
| 761 |
|
| 762 |
with app.test_request_context("/user/bob"): |
| 763 |
in_request_user = view_user.make_cache_key(name="bob") |
| 764 |
|
| 765 |
with app.app_context(): |
| 766 |
assert ( |
| 767 |
view_works.make_cache_key(path="/works", query_args="limit=15&mock=true") |
| 768 |
== in_request |
| 769 |
) |
| 770 |
assert view_user.make_cache_key(name="bob") == in_request_user |
| 771 |
|
| 772 |
|
| 773 |
def test_cached_view_http_exception(app, cache): |
| 774 |
calls = [] |
| 775 |
|
| 776 |
@app.route("/missing") |
| 777 |
@cache.cached(2) |
| 778 |
def cached_view(): |
| 779 |
calls.append(1) |
| 780 |
abort(404, "no such thing") |
| 781 |
|
| 782 |
tc = app.test_client() |
| 783 |
|
| 784 |
first = tc.get("/missing") |
| 785 |
second = tc.get("/missing") |
| 786 |
|
| 787 |
assert first.status_code == second.status_code == 404 |
| 788 |
assert len(calls) == 1 |
| 789 |
|
| 790 |
|
| 791 |
def test_cached_view_http_exception_runs_error_handler(app, cache): |
| 792 |
calls = [] |
| 793 |
|
| 794 |
@app.errorhandler(404) |
| 795 |
def handle_404(error): |
| 796 |
return f"handled {error.description}", 404 |
| 797 |
|
| 798 |
@app.route("/missing") |
| 799 |
@cache.cached(2) |
| 800 |
def cached_view(): |
| 801 |
calls.append(1) |
| 802 |
abort(404, "no such thing") |
| 803 |
|
| 804 |
tc = app.test_client() |
| 805 |
|
| 806 |
tc.get("/missing") |
| 807 |
cached = tc.get("/missing") |
| 808 |
|
| 809 |
assert cached.get_data(as_text=True) == "handled no such thing" |
| 810 |
assert len(calls) == 1 |
| 811 |
|
| 812 |
|
| 813 |
def test_cached_view_http_exception_expires(app, cache, clock): |
| 814 |
calls = [] |
| 815 |
|
| 816 |
@app.route("/missing") |
| 817 |
@cache.cached(2) |
| 818 |
def cached_view(): |
| 819 |
calls.append(1) |
| 820 |
abort(404) |
| 821 |
|
| 822 |
tc = app.test_client() |
| 823 |
|
| 824 |
tc.get("/missing") |
| 825 |
clock.advance(1) |
| 826 |
tc.get("/missing") |
| 827 |
|
| 828 |
assert len(calls) == 1 |
| 829 |
|
| 830 |
clock.advance(2) |
| 831 |
assert tc.get("/missing").status_code == 404 |
| 832 |
assert len(calls) == 2 |
| 833 |
|
| 834 |
|
| 835 |
def test_cached_view_http_exception_response_filter_gets_response(app, cache): |
| 836 |
calls = [] |
| 837 |
seen = [] |
| 838 |
|
| 839 |
def only_success(response): |
| 840 |
seen.append(response) |
| 841 |
return response.status_code == 200 |
| 842 |
|
| 843 |
@app.route("/down") |
| 844 |
@cache.cached(2, response_filter=only_success) |
| 845 |
def cached_view(): |
| 846 |
calls.append(1) |
| 847 |
abort(503) |
| 848 |
|
| 849 |
tc = app.test_client() |
| 850 |
|
| 851 |
assert tc.get("/down").status_code == 503 |
| 852 |
assert tc.get("/down").status_code == 503 |
| 853 |
|
| 854 |
assert [response.status_code for response in seen] == [503, 503] |
| 855 |
assert len(calls) == 2 |
| 856 |
|
| 857 |
|
| 858 |
def test_view_argument_named_path_does_not_change_the_key(app, cache): |
| 859 |
counter = itertools.count() |
| 860 |
|
| 861 |
@app.route("/database/<path:path>/summary") |
| 862 |
@cache.cached() |
| 863 |
def view_summary(path=""): |
| 864 |
return f"summary{next(counter)}" |
| 865 |
|
| 866 |
@app.route("/database/<path:path>/detail") |
| 867 |
@cache.cached() |
| 868 |
def view_detail(path=""): |
| 869 |
return f"detail{next(counter)}" |
| 870 |
|
| 871 |
tc = app.test_client() |
| 872 |
|
| 873 |
summary = tc.get("/database/db/summary").get_data(as_text=True) |
| 874 |
detail = tc.get("/database/db/detail").get_data(as_text=True) |
| 875 |
|
| 876 |
assert summary.startswith("summary") |
| 877 |
assert detail.startswith("detail") |
| 878 |
assert tc.get("/database/db/summary").get_data(as_text=True) == summary |
| 879 |
assert tc.get("/database/db/detail").get_data(as_text=True) == detail |
| 880 |
|
| 881 |
|
| 882 |
def test_view_argument_named_query_args_does_not_change_the_key(app, cache): |
| 883 |
counter = itertools.count() |
| 884 |
|
| 885 |
@app.route("/works/<query_args>") |
| 886 |
@cache.cached(query_string=True) |
| 887 |
def view_works(query_args): |
| 888 |
return f"{query_args}{next(counter)}" |
| 889 |
|
| 890 |
tc = app.test_client() |
| 891 |
|
| 892 |
first = tc.get("/works/a?limit=15").get_data(as_text=True) |
| 893 |
second = tc.get("/works/a?limit=20").get_data(as_text=True) |
| 894 |
|
| 895 |
assert first != second |
| 896 |
assert tc.get("/works/a?limit=15").get_data(as_text=True) == first |
| 897 |
assert tc.get("/works/a?limit=20").get_data(as_text=True) == second |