Ticket #19872: ticket19872.diff

File ticket19872.diff, 1.8 KB (added by Tomek Paczkowski, 11 years ago)

simonpercival's patches combined

  • django/utils/functional.py

    diff --git a/django/utils/functional.py b/django/utils/functional.py
    index 1b5200c..51001ea 100644
    a b class cached_property(object):  
    3939    def __init__(self, func):
    4040        self.func = func
    4141
    42     def __get__(self, instance, type):
     42    def __get__(self, instance, type=None):
     43        if instance is None:
     44            return self
    4345        res = instance.__dict__[self.func.__name__] = self.func(instance)
    4446        return res
    4547
  • tests/regressiontests/utils/functional.py

    diff --git a/tests/regressiontests/utils/functional.py b/tests/regressiontests/utils/functional.py
    index 90a6f08..3bb5000 100644
    a b  
    11from django.utils import unittest
    2 from django.utils.functional import lazy, lazy_property
     2from django.utils.functional import lazy, lazy_property, cached_property
    33
    44
    55class FunctionalTestCase(unittest.TestCase):
    class FunctionalTestCase(unittest.TestCase):  
    3737
    3838        self.assertRaises(NotImplementedError, lambda: A().do)
    3939        self.assertEqual(B().do, 'DO IT')
     40
     41    def test_cached_property(self):
     42        """
     43        Test that cached_property caches its value,
     44        and that it behaves like a property
     45        """
     46
     47        class A(object):
     48
     49            @cached_property
     50            def value(self):
     51                return 1, object()
     52
     53        a = A()
     54
     55        # check that it is cached
     56        self.assertEqual(a.value, a.value)
     57
     58        # check that it returns the right thing
     59        self.assertEqual(a.value[0], 1)
     60
     61        # check that state isn't shared between instances
     62        a2 = A()
     63        self.assertNotEqual(a.value, a2.value)
     64
     65        # check that it behaves like a property when there's no instance
     66        self.assertIsInstance(A.value, cached_property)
Back to Top