Ticket #18447: lazy_with_tests.patch

File lazy_with_tests.patch, 1.6 KB (added by d1ffuz0r, 3 years ago)

joined in one

  • django/utils/functional.py

    diff --git a/django/utils/functional.py b/django/utils/functional.py
    index 1b5200c..bf850db 100644
    a b class LazyObject(object): 
    230230            self._setup()
    231231        delattr(self._wrapped, name)
    232232
     233    def __getitem__(self, name):
     234        if self._wrapped is empty:
     235            self._setup()
     236        return self._wrapped[name]
     237
     238    def __setitem__(self, name, val):
     239        if self._wrapped is empty:
     240            self._setup()
     241        self._wrapped[name] = val
     242
    233243    def _setup(self):
    234244        """
    235245        Must be implemented by subclasses to initialise the wrapped object.
  • tests/regressiontests/utils/simplelazyobject.py

    diff --git a/tests/regressiontests/utils/simplelazyobject.py b/tests/regressiontests/utils/simplelazyobject.py
    index 3f81e8f..0c15cae 100644
    a b from django.utils.functional import SimpleLazyObject, empty 
    1010
    1111
    1212class _ComplexObject(object):
     13
    1314    def __init__(self, name):
    1415        self.name = name
    1516
    class TestUtilsSimpleLazyObject(TestCase): 
    121122        self.assertEqual(unpickled, x)
    122123        self.assertEqual(six.text_type(unpickled), six.text_type(x))
    123124        self.assertEqual(unpickled.name, x.name)
     125
     126    def test_lazy_dict(self):
     127        lazydict = SimpleLazyObject(lambda: {'one': 1, 'two': 2,
     128                                             'three': 3, 'four': 4})
     129        self.assertTrue(lazydict['one'])
     130        lazydict['one'] = 0
     131        self.assertFalse(lazydict['one'])
Back to Top