| 1 |
import random |
| 2 |
import time |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from flask_caching import Cache |
| 7 |
from flask_caching import function_namespace |
| 8 |
|
| 9 |
|
| 10 |
def test_memoize(app, cache): |
| 11 |
with app.test_request_context(): |
| 12 |
|
| 13 |
@cache.memoize(3) |
| 14 |
def big_foo(a, b): |
| 15 |
return a + b + random.randrange(0, 100000) |
| 16 |
|
| 17 |
result = big_foo(5, 2) |
| 18 |
|
| 19 |
time.sleep(1) |
| 20 |
|
| 21 |
assert big_foo(5, 2) == result |
| 22 |
|
| 23 |
result2 = big_foo(5, 3) |
| 24 |
assert result2 != result |
| 25 |
|
| 26 |
time.sleep(3) |
| 27 |
|
| 28 |
assert big_foo(5, 2) != result |
| 29 |
|
| 30 |
time.sleep(1) |
| 31 |
|
| 32 |
assert big_foo(5, 3) != result2 |
| 33 |
|
| 34 |
|
| 35 |
def test_memoize_hashes(app, cache, hash_method): |
| 36 |
with app.test_request_context(): |
| 37 |
|
| 38 |
@cache.memoize(3, hash_method=hash_method) |
| 39 |
def big_foo(a, b): |
| 40 |
return a + b + random.randrange(0, 100000) |
| 41 |
|
| 42 |
result = big_foo(5, 2) |
| 43 |
|
| 44 |
time.sleep(1) |
| 45 |
|
| 46 |
assert big_foo(5, 2) == result |
| 47 |
|
| 48 |
result2 = big_foo(5, 3) |
| 49 |
assert result2 != result |
| 50 |
|
| 51 |
time.sleep(3) |
| 52 |
|
| 53 |
assert big_foo(5, 2) != result |
| 54 |
|
| 55 |
time.sleep(1) |
| 56 |
|
| 57 |
assert big_foo(5, 3) != result2 |
| 58 |
|
| 59 |
|
| 60 |
def test_memoize_timeout(app): |
| 61 |
app.config["CACHE_DEFAULT_TIMEOUT"] = 1 |
| 62 |
cache = Cache(app) |
| 63 |
|
| 64 |
with app.test_request_context(): |
| 65 |
|
| 66 |
@cache.memoize() |
| 67 |
def big_foo(a, b): |
| 68 |
return a + b + random.randrange(0, 100000) |
| 69 |
|
| 70 |
result = big_foo(5, 2) |
| 71 |
assert big_foo(5, 2) == result |
| 72 |
time.sleep(2) |
| 73 |
assert big_foo(5, 2) != result |
| 74 |
|
| 75 |
|
| 76 |
def test_memoize_annotated(app, cache): |
| 77 |
with app.test_request_context(): |
| 78 |
|
| 79 |
@cache.memoize(50) |
| 80 |
def big_foo_annotated(a, b): |
| 81 |
return a + b + random.randrange(0, 100000) |
| 82 |
|
| 83 |
big_foo_annotated.__annotations__ = { |
| 84 |
"a": int, |
| 85 |
"b": int, |
| 86 |
"return": int, |
| 87 |
} |
| 88 |
|
| 89 |
result = big_foo_annotated(5, 2) |
| 90 |
|
| 91 |
time.sleep(1) |
| 92 |
|
| 93 |
assert big_foo_annotated(5, 2) == result |
| 94 |
|
| 95 |
|
| 96 |
def test_memoize_utf8_arguments(app, cache): |
| 97 |
with app.test_request_context(): |
| 98 |
|
| 99 |
@cache.memoize() |
| 100 |
def big_foo(a, b): |
| 101 |
return f"{a}-{b}" |
| 102 |
|
| 103 |
big_foo("æøå", "chars") |
| 104 |
|
| 105 |
|
| 106 |
def test_memoize_unicode_arguments(app, cache): |
| 107 |
with app.test_request_context(): |
| 108 |
|
| 109 |
@cache.memoize() |
| 110 |
def big_foo(a, b): |
| 111 |
return f"{a}-{b}" |
| 112 |
|
| 113 |
big_foo("æøå", "chars") |
| 114 |
|
| 115 |
|
| 116 |
def test_memoize_delete(app, cache): |
| 117 |
with app.test_request_context(): |
| 118 |
|
| 119 |
@cache.memoize(5) |
| 120 |
def big_foo(a, b): |
| 121 |
return a + b + random.randrange(0, 100000) |
| 122 |
|
| 123 |
result = big_foo(5, 2) |
| 124 |
result2 = big_foo(5, 3) |
| 125 |
|
| 126 |
time.sleep(1) |
| 127 |
|
| 128 |
assert big_foo(5, 2) == result |
| 129 |
assert big_foo(5, 2) == result |
| 130 |
assert big_foo(5, 3) != result |
| 131 |
assert big_foo(5, 3) == result2 |
| 132 |
|
| 133 |
cache.delete_memoized(big_foo) |
| 134 |
|
| 135 |
assert big_foo(5, 2) != result |
| 136 |
assert big_foo(5, 3) != result2 |
| 137 |
|
| 138 |
|
| 139 |
def test_memoize_no_timeout_delete(app, cache): |
| 140 |
with app.test_request_context(): |
| 141 |
|
| 142 |
@cache.memoize() |
| 143 |
def big_foo(a, b): |
| 144 |
return a + b + random.randrange(0, 100000) |
| 145 |
|
| 146 |
result_a = big_foo(5, 1) |
| 147 |
result_b = big_foo(5, 2) |
| 148 |
|
| 149 |
assert big_foo(5, 1) == result_a |
| 150 |
assert big_foo(5, 2) == result_b |
| 151 |
cache.delete_memoized(big_foo, 5, 2) |
| 152 |
|
| 153 |
assert big_foo(5, 1) == result_a |
| 154 |
assert big_foo(5, 2) != result_b |
| 155 |
|
| 156 |
# Cleanup bigfoo 5,1 5,2 or it might conflict with |
| 157 |
# following run if it also uses memecache |
| 158 |
cache.delete_memoized(big_foo, 5, 2) |
| 159 |
cache.delete_memoized(big_foo, 5, 1) |
| 160 |
|
| 161 |
|
| 162 |
def test_memoize_verhash_delete(app, cache): |
| 163 |
with app.test_request_context(): |
| 164 |
|
| 165 |
@cache.memoize(5) |
| 166 |
def big_foo(a, b): |
| 167 |
return a + b + random.randrange(0, 100000) |
| 168 |
|
| 169 |
result = big_foo(5, 2) |
| 170 |
result2 = big_foo(5, 3) |
| 171 |
|
| 172 |
time.sleep(1) |
| 173 |
|
| 174 |
assert big_foo(5, 2) == result |
| 175 |
assert big_foo(5, 2) == result |
| 176 |
assert big_foo(5, 3) != result |
| 177 |
assert big_foo(5, 3) == result2 |
| 178 |
|
| 179 |
cache.delete_memoized_verhash(big_foo) |
| 180 |
|
| 181 |
_fname, _fname_instance = function_namespace(big_foo) |
| 182 |
version_key = cache._memvname(_fname) |
| 183 |
assert cache.get(version_key) is None |
| 184 |
|
| 185 |
assert big_foo(5, 2) != result |
| 186 |
assert big_foo(5, 3) != result2 |
| 187 |
|
| 188 |
assert cache.get(version_key) is not None |
| 189 |
|
| 190 |
|
| 191 |
def test_memoize_annotated_delete(app, cache): |
| 192 |
with app.test_request_context(): |
| 193 |
|
| 194 |
@cache.memoize(5) |
| 195 |
def big_foo_annotated(a, b): |
| 196 |
return a + b + random.randrange(0, 100000) |
| 197 |
|
| 198 |
big_foo_annotated.__annotations__ = {"a": int, "b": int, "return": int} |
| 199 |
|
| 200 |
result = big_foo_annotated(5, 2) |
| 201 |
result2 = big_foo_annotated(5, 3) |
| 202 |
|
| 203 |
time.sleep(1) |
| 204 |
|
| 205 |
assert big_foo_annotated(5, 2) == result |
| 206 |
assert big_foo_annotated(5, 2) == result |
| 207 |
assert big_foo_annotated(5, 3) != result |
| 208 |
assert big_foo_annotated(5, 3) == result2 |
| 209 |
|
| 210 |
cache.delete_memoized_verhash(big_foo_annotated) |
| 211 |
|
| 212 |
_fname, _fname_instance = function_namespace(big_foo_annotated) |
| 213 |
version_key = cache._memvname(_fname) |
| 214 |
assert cache.get(version_key) is None |
| 215 |
|
| 216 |
assert big_foo_annotated(5, 2) != result |
| 217 |
assert big_foo_annotated(5, 3) != result2 |
| 218 |
|
| 219 |
assert cache.get(version_key) is not None |
| 220 |
|
| 221 |
|
| 222 |
def test_memoize_args(app, cache): |
| 223 |
with app.test_request_context(): |
| 224 |
|
| 225 |
@cache.memoize() |
| 226 |
def big_foo(a, b): |
| 227 |
return sum(a) + sum(b) + random.randrange(0, 100000) |
| 228 |
|
| 229 |
result_a = big_foo([5, 3, 2], [1]) |
| 230 |
result_b = big_foo([3, 3], [3, 1]) |
| 231 |
|
| 232 |
assert big_foo([5, 3, 2], [1]) == result_a |
| 233 |
assert big_foo([3, 3], [3, 1]) == result_b |
| 234 |
|
| 235 |
cache.delete_memoized(big_foo, [5, 3, 2], [1]) |
| 236 |
|
| 237 |
assert big_foo([5, 3, 2], [1]) != result_a |
| 238 |
assert big_foo([3, 3], [3, 1]) == result_b |
| 239 |
|
| 240 |
# Cleanup bigfoo 5,1 5,2 or it might conflict with |
| 241 |
# following run if it also uses memecache |
| 242 |
cache.delete_memoized(big_foo, [5, 3, 2], [1]) |
| 243 |
cache.delete_memoized(big_foo, [3, 3], [1]) |
| 244 |
|
| 245 |
|
| 246 |
def test_memoize_kwargs(app, cache): |
| 247 |
with app.test_request_context(): |
| 248 |
|
| 249 |
@cache.memoize() |
| 250 |
def big_foo(a, b=None): |
| 251 |
return a + sum(b.values()) + random.randrange(0, 100000) |
| 252 |
|
| 253 |
result_a = big_foo(1, dict(one=1, two=2)) |
| 254 |
result_b = big_foo(5, dict(three=3, four=4)) |
| 255 |
|
| 256 |
assert big_foo(1, dict(one=1, two=2)) == result_a |
| 257 |
assert big_foo(5, dict(three=3, four=4)) == result_b |
| 258 |
|
| 259 |
cache.delete_memoized(big_foo, 1, dict(one=1, two=2)) |
| 260 |
|
| 261 |
assert big_foo(1, dict(one=1, two=2)) != result_a |
| 262 |
assert big_foo(5, dict(three=3, four=4)) == result_b |
| 263 |
|
| 264 |
|
| 265 |
def test_memoize_kwargonly(app, cache): |
| 266 |
with app.test_request_context(): |
| 267 |
|
| 268 |
@cache.memoize() |
| 269 |
def big_foo(a=None): |
| 270 |
if a is None: |
| 271 |
a = 0 |
| 272 |
return a + random.random() |
| 273 |
|
| 274 |
result_a = big_foo() |
| 275 |
result_b = big_foo(5) |
| 276 |
|
| 277 |
assert big_foo() == result_a |
| 278 |
assert big_foo() < 1 |
| 279 |
assert big_foo(5) == result_b |
| 280 |
assert big_foo(5) >= 5 and big_foo(5) < 6 |
| 281 |
|
| 282 |
|
| 283 |
def test_memoize_arg_kwarg(app, cache): |
| 284 |
with app.test_request_context(): |
| 285 |
|
| 286 |
@cache.memoize() |
| 287 |
def f(a, b, c=1): |
| 288 |
return a + b + c + random.randrange(0, 100000) |
| 289 |
|
| 290 |
assert f(1, 2) == f(1, 2, c=1) |
| 291 |
assert f(1, 2) == f(1, 2, 1) |
| 292 |
assert f(1, 2) == f(1, 2) |
| 293 |
assert f(1, 2, 3) != f(1, 2) |
| 294 |
|
| 295 |
with pytest.raises(TypeError): |
| 296 |
f(1) |
| 297 |
|
| 298 |
|
| 299 |
def test_memoize_arg_kwarg_var_keyword(app, cache): |
| 300 |
with app.test_request_context(): |
| 301 |
|
| 302 |
@cache.memoize() |
| 303 |
def f(a, b, c=1, **kwargs): |
| 304 |
return a + b + c + random.randrange(0, 100000) + sum(list(kwargs.values())) |
| 305 |
|
| 306 |
assert f(1, 2) == f(1, 2, c=1) |
| 307 |
assert f(1, 2) == f(1, 2, 1) |
| 308 |
assert f(1, 2) == f(1, 2) |
| 309 |
assert f(1, 2, d=5, e=8) == f(1, 2, e=8, d=5) |
| 310 |
assert f(1, b=2, c=3, d=5, e=8) == f(1, 2, e=8, d=5, b=2, c=3) |
| 311 |
assert f(1, 2, 3) != f(1, 2) |
| 312 |
assert f(1, 2, 3) != f(1, 2) |
| 313 |
|
| 314 |
with pytest.raises(TypeError): |
| 315 |
f(1) |
| 316 |
|
| 317 |
|
| 318 |
def test_memoize_classarg(app, cache): |
| 319 |
@cache.memoize() |
| 320 |
def bar(a): |
| 321 |
return a.value + random.random() |
| 322 |
|
| 323 |
class Adder: # noqa: B903 |
| 324 |
def __init__(self, value): |
| 325 |
self.value = value |
| 326 |
|
| 327 |
adder = Adder(15) |
| 328 |
adder2 = Adder(20) |
| 329 |
|
| 330 |
y = bar(adder) |
| 331 |
z = bar(adder2) |
| 332 |
|
| 333 |
assert y != z |
| 334 |
assert bar(adder) == y |
| 335 |
assert bar(adder) != z |
| 336 |
adder.value = 14 |
| 337 |
assert bar(adder) == y |
| 338 |
assert bar(adder) != z |
| 339 |
|
| 340 |
assert bar(adder) != bar(adder2) |
| 341 |
assert bar(adder2) == z |
| 342 |
|
| 343 |
|
| 344 |
def test_memoize_classfunc(app, cache): |
| 345 |
class Adder: |
| 346 |
def __init__(self, initial): |
| 347 |
self.initial = initial |
| 348 |
|
| 349 |
@cache.memoize() |
| 350 |
def add(self, b): |
| 351 |
return self.initial + b |
| 352 |
|
| 353 |
adder1 = Adder(1) |
| 354 |
adder2 = Adder(2) |
| 355 |
|
| 356 |
x = adder1.add(3) |
| 357 |
assert adder1.add(3) == x |
| 358 |
assert adder1.add(4) != x |
| 359 |
assert adder1.add(3) != adder2.add(3) |
| 360 |
|
| 361 |
|
| 362 |
def test_memoize_classfunc_repr(app, cache): |
| 363 |
class Adder: |
| 364 |
def __init__(self, initial): |
| 365 |
self.initial = initial |
| 366 |
|
| 367 |
@cache.memoize() |
| 368 |
def add(self, b): |
| 369 |
return self.initial + b |
| 370 |
|
| 371 |
def __repr__(self): |
| 372 |
return "42" |
| 373 |
|
| 374 |
def __caching_id__(self): |
| 375 |
return self.initial |
| 376 |
|
| 377 |
adder1 = Adder(1) |
| 378 |
adder2 = Adder(2) |
| 379 |
|
| 380 |
x = adder1.add(3) |
| 381 |
assert adder1.add(3) == x |
| 382 |
assert adder1.add(4) != x |
| 383 |
assert adder1.add(3) != adder2.add(3) |
| 384 |
|
| 385 |
|
| 386 |
def test_memoize_classfunc_delete(app, cache): |
| 387 |
with app.test_request_context(): |
| 388 |
|
| 389 |
class Adder: |
| 390 |
def __init__(self, initial): |
| 391 |
self.initial = initial |
| 392 |
|
| 393 |
@cache.memoize() |
| 394 |
def add(self, b): |
| 395 |
return self.initial + b + random.random() |
| 396 |
|
| 397 |
adder1 = Adder(1) |
| 398 |
adder2 = Adder(2) |
| 399 |
|
| 400 |
a1 = adder1.add(3) |
| 401 |
a2 = adder2.add(3) |
| 402 |
|
| 403 |
assert a1 != a2 |
| 404 |
assert adder1.add(3) == a1 |
| 405 |
assert adder2.add(3) == a2 |
| 406 |
|
| 407 |
cache.delete_memoized(adder1.add) |
| 408 |
|
| 409 |
a3 = adder1.add(3) |
| 410 |
a4 = adder2.add(3) |
| 411 |
|
| 412 |
assert not a1 == a3 |
| 413 |
# self.assertNotEqual(a1, a3) |
| 414 |
|
| 415 |
assert a1 != a3 |
| 416 |
|
| 417 |
assert a2 == a4 |
| 418 |
# self.assertEqual(a2, a4) |
| 419 |
|
| 420 |
cache.delete_memoized(Adder.add) |
| 421 |
|
| 422 |
a5 = adder1.add(3) |
| 423 |
a6 = adder2.add(3) |
| 424 |
|
| 425 |
assert not a5 == a6 |
| 426 |
# self.assertNotEqual(a5, a6) |
| 427 |
assert not a3 == a5 |
| 428 |
# self.assertNotEqual(a3, a5) |
| 429 |
assert not a4 == a6 |
| 430 |
# self.assertNotEqual(a4, a6) |
| 431 |
|
| 432 |
|
| 433 |
def test_memoize_classmethod_delete(app, cache): |
| 434 |
with app.test_request_context(): |
| 435 |
|
| 436 |
class Mock: |
| 437 |
@classmethod |
| 438 |
@cache.memoize(5) |
| 439 |
def big_foo(cls, a, b): |
| 440 |
return a + b + random.randrange(0, 100000) |
| 441 |
|
| 442 |
result = Mock.big_foo(5, 2) |
| 443 |
result2 = Mock.big_foo(5, 3) |
| 444 |
|
| 445 |
time.sleep(1) |
| 446 |
|
| 447 |
assert Mock.big_foo(5, 2) == result |
| 448 |
assert Mock.big_foo(5, 2) == result |
| 449 |
assert Mock.big_foo(5, 3) != result |
| 450 |
assert Mock.big_foo(5, 3) == result2 |
| 451 |
|
| 452 |
cache.delete_memoized(Mock.big_foo) |
| 453 |
|
| 454 |
assert Mock.big_foo(5, 2) != result |
| 455 |
assert Mock.big_foo(5, 3) != result2 |
| 456 |
|
| 457 |
|
| 458 |
def test_memoize_classmethod_delete_with_args(app, cache): |
| 459 |
with app.test_request_context(): |
| 460 |
|
| 461 |
class Mock: |
| 462 |
@classmethod |
| 463 |
@cache.memoize(5) |
| 464 |
def big_foo(cls, a, b): |
| 465 |
return a + b + random.randrange(0, 100000) |
| 466 |
|
| 467 |
result = Mock.big_foo(5, 2) |
| 468 |
result2 = Mock.big_foo(5, 3) |
| 469 |
|
| 470 |
time.sleep(1) |
| 471 |
|
| 472 |
assert Mock.big_foo(5, 2) == result |
| 473 |
assert Mock.big_foo(5, 2) == result |
| 474 |
assert Mock.big_foo(5, 3) != result |
| 475 |
assert Mock.big_foo(5, 3) == result2 |
| 476 |
|
| 477 |
with pytest.raises(ValueError): |
| 478 |
cache.delete_memoized(Mock.big_foo, 5, 2) |
| 479 |
|
| 480 |
assert Mock.big_foo(5, 2) == result |
| 481 |
assert Mock.big_foo(5, 3) == result2 |
| 482 |
|
| 483 |
cache.delete_memoized(Mock.big_foo, Mock, 5, 2) |
| 484 |
|
| 485 |
assert Mock.big_foo(5, 2) != result |
| 486 |
assert Mock.big_foo(5, 3) == result2 |
| 487 |
|
| 488 |
|
| 489 |
def test_memoize_forced_update(app, cache): |
| 490 |
with app.test_request_context(): |
| 491 |
forced_update = False |
| 492 |
|
| 493 |
@cache.memoize(5, forced_update=lambda: forced_update) |
| 494 |
def big_foo(a, b): |
| 495 |
return a + b + random.randrange(0, 100000) |
| 496 |
|
| 497 |
result = big_foo(5, 2) |
| 498 |
time.sleep(1) |
| 499 |
assert big_foo(5, 2) == result |
| 500 |
|
| 501 |
forced_update = True |
| 502 |
new_result = big_foo(5, 2) |
| 503 |
assert new_result != result |
| 504 |
|
| 505 |
forced_update = False |
| 506 |
time.sleep(1) |
| 507 |
assert big_foo(5, 2) == new_result |
| 508 |
|
| 509 |
|
| 510 |
def test_memoize_forced_update_parameters(app, cache): |
| 511 |
from collections import Counter |
| 512 |
|
| 513 |
with app.test_request_context(): |
| 514 |
call_counter = Counter() |
| 515 |
call_params = {} |
| 516 |
forced_update = False |
| 517 |
|
| 518 |
def forced_update_func(a, b): |
| 519 |
call_counter[1] += 1 |
| 520 |
call_params[call_counter[1] - 1] = (a, b) |
| 521 |
|
| 522 |
return forced_update |
| 523 |
|
| 524 |
@cache.memoize(5, forced_update=forced_update_func) |
| 525 |
def memoized_func(a, b): |
| 526 |
return a + b + random.randrange(0, 100000) |
| 527 |
|
| 528 |
# Save the value for later inspection |
| 529 |
result = memoized_func(5, 2) |
| 530 |
# forced_update_func should have been called twice; once by memoize |
| 531 |
# itself, once by _memoize_version… |
| 532 |
assert call_counter[1] == 2 |
| 533 |
# …with the values we called the function with |
| 534 |
assert call_params[0] == (5, 2) |
| 535 |
assert call_params[1] == (5, 2) |
| 536 |
time.sleep(1) |
| 537 |
|
| 538 |
# Calling the function again should return the cached value |
| 539 |
assert memoized_func(5, 2) == result |
| 540 |
# forced_update_func should have been called two more times… |
| 541 |
assert call_counter[1] == 4 |
| 542 |
# …with the values we called the function with |
| 543 |
assert call_params[2] == (5, 2) |
| 544 |
assert call_params[3] == (5, 2) |
| 545 |
|
| 546 |
# Tell forced_update_func to return True next time |
| 547 |
forced_update = True |
| 548 |
# Save the new result… |
| 549 |
new_result = memoized_func(5, 2) |
| 550 |
# …which, due to the random number in the function, should be different |
| 551 |
# from the old one |
| 552 |
assert new_result != result |
| 553 |
# forced_update_func should have been called two more times again… |
| 554 |
assert call_counter[1] == 6 |
| 555 |
# …with the values we called the function with |
| 556 |
assert call_params[4] == (5, 2) |
| 557 |
assert call_params[5] == (5, 2) |
| 558 |
|
| 559 |
# Now stop forced updating again |
| 560 |
forced_update = False |
| 561 |
time.sleep(1) |
| 562 |
# The function should return the same value as it did last time |
| 563 |
assert memoized_func(5, 2) == new_result |
| 564 |
# forced_update_func should have been called two more times again… |
| 565 |
assert call_counter[1] == 8 |
| 566 |
# …with the values we called the function with |
| 567 |
assert call_params[6] == (5, 2) |
| 568 |
assert call_params[7] == (5, 2) |
| 569 |
|
| 570 |
|
| 571 |
def test_memoize_multiple_arg_kwarg_calls(app, cache): |
| 572 |
with app.test_request_context(): |
| 573 |
|
| 574 |
@cache.memoize() |
| 575 |
def big_foo(a, b, c=None, d=None): |
| 576 |
if c is None: |
| 577 |
c = [1, 1] |
| 578 |
if d is None: |
| 579 |
d = [1, 1] |
| 580 |
return sum(a) + sum(b) + sum(c) + sum(d) + random.randrange(0, 100000) # noqa |
| 581 |
|
| 582 |
result_a = big_foo([5, 3, 2], [1], c=[3, 3], d=[3, 3]) |
| 583 |
|
| 584 |
assert big_foo([5, 3, 2], [1], d=[3, 3], c=[3, 3]) == result_a |
| 585 |
assert big_foo(b=[1], a=[5, 3, 2], c=[3, 3], d=[3, 3]) == result_a |
| 586 |
assert big_foo([5, 3, 2], [1], [3, 3], [3, 3]) == result_a |
| 587 |
|
| 588 |
|
| 589 |
def test_memoize_multiple_arg_kwarg_delete(app, cache): |
| 590 |
with app.test_request_context(): |
| 591 |
|
| 592 |
@cache.memoize() |
| 593 |
def big_foo(a, b, c=None, d=None): |
| 594 |
if c is None: |
| 595 |
c = [1, 1] |
| 596 |
if d is None: |
| 597 |
d = [1, 1] |
| 598 |
return sum(a) + sum(b) + sum(c) + sum(d) + random.randrange(0, 100000) # noqa |
| 599 |
|
| 600 |
result_a = big_foo([5, 3, 2], [1], c=[3, 3], d=[3, 3]) |
| 601 |
cache.delete_memoized(big_foo, [5, 3, 2], [1], [3, 3], [3, 3]) |
| 602 |
result_b = big_foo([5, 3, 2], [1], c=[3, 3], d=[3, 3]) |
| 603 |
assert result_a != result_b |
| 604 |
|
| 605 |
cache.delete_memoized(big_foo, [5, 3, 2], b=[1], c=[3, 3], d=[3, 3]) |
| 606 |
result_b = big_foo([5, 3, 2], [1], c=[3, 3], d=[3, 3]) |
| 607 |
assert result_a != result_b |
| 608 |
|
| 609 |
cache.delete_memoized(big_foo, [5, 3, 2], [1], c=[3, 3], d=[3, 3]) |
| 610 |
result_a = big_foo([5, 3, 2], [1], c=[3, 3], d=[3, 3]) |
| 611 |
assert result_a != result_b |
| 612 |
|
| 613 |
cache.delete_memoized(big_foo, [5, 3, 2], b=[1], c=[3, 3], d=[3, 3]) |
| 614 |
result_a = big_foo([5, 3, 2], [1], c=[3, 3], d=[3, 3]) |
| 615 |
assert result_a != result_b |
| 616 |
|
| 617 |
cache.delete_memoized(big_foo, [5, 3, 2], [1], c=[3, 3], d=[3, 3]) |
| 618 |
result_b = big_foo([5, 3, 2], [1], c=[3, 3], d=[3, 3]) |
| 619 |
assert result_a != result_b |
| 620 |
|
| 621 |
cache.delete_memoized(big_foo, [5, 3, 2], [1], [3, 3], [3, 3]) |
| 622 |
result_a = big_foo([5, 3, 2], [1], c=[3, 3], d=[3, 3]) |
| 623 |
assert result_a != result_b |
| 624 |
|
| 625 |
|
| 626 |
def test_memoize_kwargs_to_args(app, cache): |
| 627 |
with app.test_request_context(): |
| 628 |
|
| 629 |
def big_foo(a, b, c=None, d=None): |
| 630 |
return sum(a) + sum(b) + random.randrange(0, 100000) |
| 631 |
|
| 632 |
expected = (1, 2, "foo", "bar") |
| 633 |
|
| 634 |
args, kwargs = cache._memoize_kwargs_to_args(big_foo, 1, 2, "foo", "bar") |
| 635 |
assert args == expected |
| 636 |
args, kwargs = cache._memoize_kwargs_to_args(big_foo, 2, "foo", "bar", a=1) |
| 637 |
assert args == expected |
| 638 |
args, kwargs = cache._memoize_kwargs_to_args( |
| 639 |
big_foo, a=1, b=2, c="foo", d="bar" |
| 640 |
) |
| 641 |
assert args == expected |
| 642 |
args, kwargs = cache._memoize_kwargs_to_args( |
| 643 |
big_foo, d="bar", b=2, a=1, c="foo" |
| 644 |
) |
| 645 |
assert args == expected |
| 646 |
args, kwargs = cache._memoize_kwargs_to_args(big_foo, 1, 2, d="bar", c="foo") |
| 647 |
assert args == expected |
| 648 |
|
| 649 |
|
| 650 |
def test_memoize_when_using_args_unpacking(app, cache): |
| 651 |
with app.test_request_context(): |
| 652 |
|
| 653 |
@cache.memoize() |
| 654 |
def big_foo(*args): |
| 655 |
return sum(args) + random.randrange(0, 100000) |
| 656 |
|
| 657 |
result_a = big_foo(1, 2) |
| 658 |
result_b = big_foo(1, 3) |
| 659 |
|
| 660 |
assert big_foo(1, 2) == result_a |
| 661 |
assert big_foo(1, 3) == result_b |
| 662 |
assert big_foo(1, 2) != result_b |
| 663 |
assert big_foo(1, 3) != result_a |
| 664 |
|
| 665 |
cache.delete_memoized(big_foo) |
| 666 |
|
| 667 |
assert big_foo(1, 2) != result_a |
| 668 |
assert big_foo(1, 3) != result_b |
| 669 |
|
| 670 |
|
| 671 |
def test_memoize_when_using_variable_mix_args_unpacking(app, cache): |
| 672 |
with app.test_request_context(): |
| 673 |
|
| 674 |
@cache.memoize() |
| 675 |
def big_foo(a, b, *args, **kwargs): |
| 676 |
return ( |
| 677 |
sum([a, b]) |
| 678 |
+ sum(args) |
| 679 |
+ sum(kwargs.values()) |
| 680 |
+ random.randrange(0, 100000) |
| 681 |
) |
| 682 |
|
| 683 |
result_a = big_foo(1, 2, 3, 4, x=2, y=5) |
| 684 |
result_b = big_foo(4, 7, 7, 2, x=1, y=4) |
| 685 |
|
| 686 |
assert big_foo(1, 2, 3, 4, x=2, y=5) == result_a |
| 687 |
assert big_foo(4, 7, 7, 2, x=1, y=4) == result_b |
| 688 |
assert big_foo(1, 2, 3, 4, x=2, y=5) != result_b |
| 689 |
assert big_foo(4, 7, 7, 2, x=1, y=4) != result_a |
| 690 |
|
| 691 |
cache.delete_memoized(big_foo) |
| 692 |
|
| 693 |
assert big_foo(1, 2, 3, 4, x=2, y=5) != result_a |
| 694 |
assert big_foo(4, 7, 7, 2, x=1, y=4) != result_b |
| 695 |
|
| 696 |
|
| 697 |
def test_memoize_none(app, cache): |
| 698 |
with app.test_request_context(): |
| 699 |
from collections import Counter |
| 700 |
|
| 701 |
call_counter = Counter() |
| 702 |
|
| 703 |
@cache.memoize(cache_none=True) |
| 704 |
def memoize_none(param): |
| 705 |
call_counter[param] += 1 |
| 706 |
|
| 707 |
return None |
| 708 |
|
| 709 |
memoize_none(1) |
| 710 |
|
| 711 |
# The memoized function should have been called |
| 712 |
assert call_counter[1] == 1 |
| 713 |
|
| 714 |
# Next time we call the function, the value should be coming from the |
| 715 |
# cache... |
| 716 |
assert memoize_none(1) is None |
| 717 |
|
| 718 |
# …thus, the call counter should remain 1 |
| 719 |
assert call_counter[1] == 1 |
| 720 |
|
| 721 |
cache.clear() |
| 722 |
|
| 723 |
memoize_none(1) |
| 724 |
assert call_counter[1] == 2 |
| 725 |
|
| 726 |
|
| 727 |
def test_memoize_never_accept_none(app, cache): |
| 728 |
"""Asserting that when cache_none is False, we always |
| 729 |
assume a None value returned from .get() means the key is not found |
| 730 |
""" |
| 731 |
with app.test_request_context(): |
| 732 |
from collections import Counter |
| 733 |
|
| 734 |
call_counter = Counter() |
| 735 |
|
| 736 |
@cache.memoize() |
| 737 |
def memoize_none(param): |
| 738 |
call_counter[param] += 1 |
| 739 |
|
| 740 |
return None |
| 741 |
|
| 742 |
memoize_none(1) |
| 743 |
|
| 744 |
# The memoized function should have been called |
| 745 |
assert call_counter[1] == 1 |
| 746 |
|
| 747 |
# Next time we call the function, the value should be coming from the |
| 748 |
# cache… |
| 749 |
# But the value is None and so we treat it as uncached. |
| 750 |
assert memoize_none(1) is None |
| 751 |
|
| 752 |
# …thus, the call counter should increment to 2 |
| 753 |
assert call_counter[1] == 2 |
| 754 |
|
| 755 |
cache.clear() |
| 756 |
|
| 757 |
memoize_none(1) |
| 758 |
assert call_counter[1] == 3 |
| 759 |
|
| 760 |
|
| 761 |
def test_memoize_with_source_check_enabled(app, cache): |
| 762 |
with app.test_request_context(): |
| 763 |
|
| 764 |
@cache.memoize(source_check=True) |
| 765 |
def big_foo(a, b): |
| 766 |
return str(time.time()) |
| 767 |
|
| 768 |
first_try = big_foo(5, 2) |
| 769 |
|
| 770 |
second_try = big_foo(5, 2) |
| 771 |
|
| 772 |
assert second_try == first_try |
| 773 |
|
| 774 |
@cache.memoize(source_check=True) |
| 775 |
def big_foo(a, b): |
| 776 |
return str(time.time()) |
| 777 |
|
| 778 |
third_try = big_foo(5, 2) |
| 779 |
|
| 780 |
assert third_try[0] != first_try |
| 781 |
|
| 782 |
@cache.memoize(source_check=True) |
| 783 |
def big_foo(a, b): |
| 784 |
return str(time.time()) |
| 785 |
|
| 786 |
forth_try = big_foo(5, 2) |
| 787 |
|
| 788 |
assert forth_try == first_try |
| 789 |
|
| 790 |
|
| 791 |
def test_memoize_with_source_check_disabled(app, cache): |
| 792 |
with app.test_request_context(): |
| 793 |
|
| 794 |
@cache.memoize(source_check=False) |
| 795 |
def big_foo(a, b): |
| 796 |
return str(time.time()) |
| 797 |
|
| 798 |
first_try = big_foo(5, 2) |
| 799 |
|
| 800 |
second_try = big_foo(5, 2) |
| 801 |
|
| 802 |
assert second_try == first_try |
| 803 |
|
| 804 |
@cache.memoize(source_check=False) |
| 805 |
def big_foo(a, b): |
| 806 |
return time.time() |
| 807 |
|
| 808 |
third_try = big_foo(5, 2) |
| 809 |
|
| 810 |
assert third_try == first_try |
| 811 |
|
| 812 |
|
| 813 |
def test_memoize_ignore_args(app, cache): |
| 814 |
with app.test_request_context(): |
| 815 |
|
| 816 |
@cache.memoize(50, args_to_ignore=["b"]) |
| 817 |
def big_foo(a, b): |
| 818 |
return a + b + random.randrange(0, 100000) |
| 819 |
|
| 820 |
result = big_foo(5, 2) |
| 821 |
assert big_foo(5, 3) == result |
| 822 |
|
| 823 |
|
| 824 |
def test_memoize_method_ignore_self_arg(app, cache): |
| 825 |
with app.test_request_context(): |
| 826 |
|
| 827 |
class Foo: |
| 828 |
@cache.memoize(50, args_to_ignore=["self"]) |
| 829 |
def big_foo(self, a, b): |
| 830 |
return a + b + random.randrange(0, 100000) |
| 831 |
|
| 832 |
assert Foo().big_foo(5, 2) == Foo().big_foo(5, 2) |
| 833 |
|
| 834 |
|
| 835 |
def test_memoize_function_ignore_kwarg(app, cache): |
| 836 |
with app.test_request_context(): |
| 837 |
|
| 838 |
@cache.memoize(50, args_to_ignore=["b"]) |
| 839 |
def big_foo(a, b): |
| 840 |
return a + b + random.randrange(0, 100000) |
| 841 |
|
| 842 |
assert big_foo(5, 2) == big_foo(5, b=3) |