public
Created

django.utils.functional.lazy issues

  • Download Gist
gistfile1.diff
Diff
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
diff --git a/tests/regressiontests/utils/functional.py b/tests/regressiontests/utils/functional.py
index 90a6f08..07d26af 100644
--- a/tests/regressiontests/utils/functional.py
+++ b/tests/regressiontests/utils/functional.py
@@ -1,5 +1,6 @@
from django.utils import unittest
from django.utils.functional import lazy, lazy_property
+from django.utils import six
class FunctionalTestCase(unittest.TestCase):
@@ -37,3 +38,46 @@ class FunctionalTestCase(unittest.TestCase):
self.assertRaises(NotImplementedError, lambda: A().do)
self.assertEqual(B().do, 'DO IT')
+
+ def test_lazy_magicmethods_mod(self):
+ class A(six.text_type):
+ def __mod__(self, rhs):
+ return 'Result: ' + super(A, self).__mod__(rhs)
+
+ t = lazy(lambda: A('Hello %(word)s'), A)()
+ self.assertEqual(t % {'word': 'world'}, 'Result: Hello world')
+
+ def test_lazy_magicmethods_hash(self):
+ class NotAHashable(object):
+ __hash__ = None
+
+ notahash = NotAHashable()
+ notahash_lazy = lazy(lambda: NotAHashable(), NotAHashable)()
+ self.assertRaises(TypeError, lambda: {notahash: 1})
+ self.assertRaises(TypeError, lambda: {notahash_lazy: 2})
+
+ def test_lazy_losing_docstring(self):
+ class A(object):
+ def hello(self):
+ '''Docstring'''
+ return 'Hello world'
+ lazy_a = lazy(lambda: A(), A)()
+ self.assertEqual(lazy_a.hello.__doc__, 'Docstring')
+
+ def test_lazy_attributes(self):
+ class A(object):
+ counter = 15
+
+ lazy_a = lazy(lambda: A(), A)()
+ self.assertEqual(lazy_a.counter, 15)
+ lazy_a.counter = 16
+ self.assertEqual(lazy_a.counter, 16)
+
+ class B(object):
+ @property
+ def counter(self):
+ return 15
+
+ lazy_b = lazy(lambda: B(), B)()
+ self.assertEqual(lazy_b.counter, 15)
+

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.