| 1 |
from graphql.pyutils import cached_property |
| 2 |
|
| 3 |
|
| 4 |
def describe_cached_property(): |
| 5 |
def works_like_a_normal_property(): |
| 6 |
class TestClass: |
| 7 |
@cached_property |
| 8 |
def value(self): |
| 9 |
return 42 |
| 10 |
|
| 11 |
assert TestClass().value == 42 |
| 12 |
|
| 13 |
def caches_the_value(): |
| 14 |
class TestClass: |
| 15 |
evaluations = 0 |
| 16 |
|
| 17 |
@cached_property |
| 18 |
def value(self): |
| 19 |
self.__class__.evaluations += 1 |
| 20 |
return 42 |
| 21 |
|
| 22 |
obj = TestClass() |
| 23 |
assert TestClass.evaluations == 0 |
| 24 |
assert obj.value == 42 |
| 25 |
assert TestClass.evaluations == 1 |
| 26 |
assert obj.value == 42 |
| 27 |
assert TestClass.evaluations == 1 |