2013-07-01 12:22:27 +00:00
|
|
|
import unittest
|
|
|
|
|
2013-02-23 22:20:00 +00:00
|
|
|
from django.utils.functional import lazy, lazy_property, cached_property
|
2010-01-05 12:06:45 +00:00
|
|
|
|
|
|
|
|
2010-09-28 08:18:12 +00:00
|
|
|
class FunctionalTestCase(unittest.TestCase):
|
2010-01-05 12:06:45 +00:00
|
|
|
def test_lazy(self):
|
|
|
|
t = lazy(lambda: tuple(range(3)), list, tuple)
|
|
|
|
for a, b in zip(t(), range(3)):
|
|
|
|
self.assertEqual(a, b)
|
2011-05-05 00:09:51 +00:00
|
|
|
|
|
|
|
def test_lazy_base_class(self):
|
|
|
|
"""Test that lazy also finds base class methods in the proxy object"""
|
|
|
|
|
|
|
|
class Base(object):
|
|
|
|
def base_method(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Klazz(Base):
|
|
|
|
pass
|
|
|
|
|
|
|
|
t = lazy(lambda: Klazz(), Klazz)()
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertIn('base_method', dir(t))
|
2011-06-01 13:47:00 +00:00
|
|
|
|
2014-08-22 16:31:26 +00:00
|
|
|
def test_lazy_base_class_override(self):
|
|
|
|
"""Test that lazy finds the correct (overridden) method implementation"""
|
|
|
|
|
|
|
|
class Base(object):
|
|
|
|
def method(self):
|
|
|
|
return 'Base'
|
|
|
|
|
|
|
|
class Klazz(Base):
|
|
|
|
def method(self):
|
|
|
|
return 'Klazz'
|
|
|
|
|
|
|
|
t = lazy(lambda: Klazz(), Base)()
|
|
|
|
self.assertEqual(t.method(), 'Klazz')
|
|
|
|
|
2011-06-01 13:47:00 +00:00
|
|
|
def test_lazy_property(self):
|
|
|
|
|
|
|
|
class A(object):
|
|
|
|
|
|
|
|
def _get_do(self):
|
|
|
|
raise NotImplementedError
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2011-06-01 13:47:00 +00:00
|
|
|
def _set_do(self, value):
|
|
|
|
raise NotImplementedError
|
|
|
|
do = lazy_property(_get_do, _set_do)
|
|
|
|
|
|
|
|
class B(A):
|
|
|
|
def _get_do(self):
|
|
|
|
return "DO IT"
|
|
|
|
|
|
|
|
self.assertRaises(NotImplementedError, lambda: A().do)
|
|
|
|
self.assertEqual(B().do, 'DO IT')
|
2013-02-23 22:20:00 +00:00
|
|
|
|
|
|
|
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):
|
2014-10-19 22:34:35 +00:00
|
|
|
"""Here is the docstring..."""
|
2013-02-23 22:20:00 +00:00
|
|
|
return 1, object()
|
|
|
|
|
2014-05-24 10:29:31 +00:00
|
|
|
def other_value(self):
|
|
|
|
return 1
|
|
|
|
|
|
|
|
other = cached_property(other_value, name='other')
|
|
|
|
|
2014-10-19 22:34:35 +00:00
|
|
|
# docstring should be preserved
|
|
|
|
self.assertEqual(A.value.__doc__, "Here is the docstring...")
|
|
|
|
|
2013-02-23 22:20:00 +00:00
|
|
|
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)
|
2013-05-29 16:52:17 +00:00
|
|
|
|
2014-05-24 10:29:31 +00:00
|
|
|
# check that overriding name works
|
|
|
|
self.assertEqual(a.other, 1)
|
|
|
|
self.assertTrue(callable(a.other_value))
|
|
|
|
|
2013-05-29 16:52:17 +00:00
|
|
|
def test_lazy_equality(self):
|
|
|
|
"""
|
|
|
|
Tests that == and != work correctly for Promises.
|
|
|
|
"""
|
|
|
|
|
|
|
|
lazy_a = lazy(lambda: 4, int)
|
|
|
|
lazy_b = lazy(lambda: 4, int)
|
|
|
|
lazy_c = lazy(lambda: 5, int)
|
|
|
|
|
|
|
|
self.assertEqual(lazy_a(), lazy_b())
|
|
|
|
self.assertNotEqual(lazy_b(), lazy_c())
|