This commit is contained in:
Honza Kral 2013-02-23 23:32:09 +01:00
commit ef1e6ef1eb
2 changed files with 31 additions and 2 deletions

View File

@ -39,7 +39,9 @@ class cached_property(object):
def __init__(self, func): def __init__(self, func):
self.func = func self.func = func
def __get__(self, instance, type): def __get__(self, instance, type=None):
if instance is None:
return self
res = instance.__dict__[self.func.__name__] = self.func(instance) res = instance.__dict__[self.func.__name__] = self.func(instance)
return res return res

View File

@ -1,5 +1,5 @@
from django.utils import unittest from django.utils import unittest
from django.utils.functional import lazy, lazy_property from django.utils.functional import lazy, lazy_property, cached_property
class FunctionalTestCase(unittest.TestCase): class FunctionalTestCase(unittest.TestCase):
@ -37,3 +37,30 @@ class FunctionalTestCase(unittest.TestCase):
self.assertRaises(NotImplementedError, lambda: A().do) self.assertRaises(NotImplementedError, lambda: A().do)
self.assertEqual(B().do, 'DO IT') self.assertEqual(B().do, 'DO IT')
def test_cached_property(self):
"""
Test that cached_property caches its value,
and that it behaves like a property
"""
class A(object):
@cached_property
def value(self):
return 1, object()
a = A()
# check that it is cached
self.assertEqual(a.value, a.value)
# check that it returns the right thing
self.assertEqual(a.value[0], 1)
# check that state isn't shared between instances
a2 = A()
self.assertNotEqual(a.value, a2.value)
# check that it behaves like a property when there's no instance
self.assertIsInstance(A.value, cached_property)