|
9 | 9 |
|
10 | 10 | from datetime import datetime
|
11 | 11 |
|
| 12 | +GLOBAL_COUNTER = 200 |
| 13 | + |
12 | 14 |
|
13 | 15 | class Incrementer:
|
14 | 16 | class_counter = 500
|
@@ -39,6 +41,25 @@ def func_with_multiple_args_and_kwargs(
|
39 | 41 | ):
|
40 | 42 | return datetime.utcnow()
|
41 | 43 |
|
| 44 | + @cached_instance_method(60 * 60) |
| 45 | + def instance_increment_by_none_return(self, num): |
| 46 | + self.instance_counter += num |
| 47 | + return None |
| 48 | + |
| 49 | + @classmethod |
| 50 | + @cached_class_method(60 * 60) |
| 51 | + def class_increment_by_none_return(cls, num): |
| 52 | + cls.class_counter += num |
| 53 | + return None |
| 54 | + |
| 55 | + @staticmethod |
| 56 | + @cached(60 * 60) |
| 57 | + def static_increment_by_none_return(num): |
| 58 | + global GLOBAL_COUNTER |
| 59 | + GLOBAL_COUNTER += num |
| 60 | + |
| 61 | + return None |
| 62 | + |
42 | 63 |
|
43 | 64 | class SubclassIncrementer(Incrementer):
|
44 | 65 | class_counter = 500
|
@@ -268,6 +289,26 @@ def test_invalidate_instance_method(self):
|
268 | 289 | # but 2 is still stale
|
269 | 290 | self.assertEqual(incrementer.instance_increment_by(2), 103)
|
270 | 291 |
|
| 292 | + def test_cached_instance_method_none_return(self): |
| 293 | + """ |
| 294 | + Tests that calling a cached instance method returning None still uses the cache as expected. |
| 295 | + """ |
| 296 | + incrementer = Incrementer(100) |
| 297 | + |
| 298 | + # Hasn't been computed before, so the function actually gets called |
| 299 | + incrementer.instance_increment_by_none_return(1) |
| 300 | + self.assertEqual(incrementer.instance_counter, 101) |
| 301 | + |
| 302 | + incrementer.instance_increment_by_none_return(2) |
| 303 | + self.assertEqual(incrementer.instance_counter, 103) |
| 304 | + |
| 305 | + # We expect the instance counter to stay the same as the last time it was updated and not increment again |
| 306 | + incrementer.instance_increment_by_none_return(1) |
| 307 | + self.assertEqual(incrementer.instance_counter, 103) |
| 308 | + |
| 309 | + incrementer.instance_increment_by_none_return(2) |
| 310 | + self.assertEqual(incrementer.instance_counter, 103) |
| 311 | + |
271 | 312 |
|
272 | 313 | class CachedClassMethodTests(TestCase):
|
273 | 314 | def tearDown(self):
|
@@ -410,10 +451,31 @@ def test_invalidate_class_method(self):
|
410 | 451 | # but 2 is still stale
|
411 | 452 | self.assertEqual(Incrementer.class_increment_by(2), 503)
|
412 | 453 |
|
| 454 | + def test_cached_class_method_none_return(self): |
| 455 | + """ |
| 456 | + Tests that calling a cached class method returning None still uses the cache as expected. |
| 457 | + """ |
| 458 | + # Hasn't been computed before, so the function actually gets called |
| 459 | + Incrementer.class_increment_by_none_return(1) |
| 460 | + self.assertEqual(Incrementer.class_counter, 501) |
| 461 | + |
| 462 | + Incrementer.class_increment_by_none_return(2) |
| 463 | + self.assertEqual(Incrementer.class_counter, 503) |
| 464 | + |
| 465 | + # We expect the class counter to stay the same as the last time it was updated and not increment again |
| 466 | + Incrementer.class_increment_by_none_return(1) |
| 467 | + self.assertEqual(Incrementer.class_counter, 503) |
| 468 | + |
| 469 | + Incrementer.class_increment_by_none_return(2) |
| 470 | + self.assertEqual(Incrementer.class_counter, 503) |
| 471 | + |
413 | 472 |
|
414 | 473 | class CachedStaticMethodTests(TestCase):
|
415 | 474 | def tearDown(self):
|
416 | 475 | super().tearDown()
|
| 476 | + |
| 477 | + global GLOBAL_COUNTER |
| 478 | + GLOBAL_COUNTER = 200 |
417 | 479 | cache.clear()
|
418 | 480 |
|
419 | 481 | def test_exception_during_cache_retrieval(self):
|
@@ -549,6 +611,24 @@ def test_invalidate_static_method(self):
|
549 | 611 | # but 2 is still stale
|
550 | 612 | self.assertEqual(Incrementer.get_datetime(2), initial_datetime_2)
|
551 | 613 |
|
| 614 | + def test_cached_class_method_none_return(self): |
| 615 | + """ |
| 616 | + Tests that calling a cached static method returning None still uses the cache as expected. |
| 617 | + """ |
| 618 | + # Hasn't been computed before, so the function actually gets called |
| 619 | + Incrementer.static_increment_by_none_return(1) |
| 620 | + self.assertEqual(GLOBAL_COUNTER, 201) |
| 621 | + |
| 622 | + Incrementer.static_increment_by_none_return(2) |
| 623 | + self.assertEqual(GLOBAL_COUNTER, 203) |
| 624 | + |
| 625 | + # We expect the global counter to stay the same as the last time it was updated and not increment again |
| 626 | + Incrementer.static_increment_by_none_return(1) |
| 627 | + self.assertEqual(GLOBAL_COUNTER, 203) |
| 628 | + |
| 629 | + Incrementer.static_increment_by_none_return(2) |
| 630 | + self.assertEqual(GLOBAL_COUNTER, 203) |
| 631 | + |
552 | 632 |
|
553 | 633 | class CacheHelperCacheableTests(TestCase):
|
554 | 634 | def tearDown(self):
|
|
0 commit comments