2018-06-07 13:03:45 +00:00
|
|
|
from django.test import SimpleTestCase
|
2016-11-30 00:01:12 +00:00
|
|
|
from django.utils.functional import cached_property, lazy
|
2010-01-05 12:06:45 +00:00
|
|
|
|
|
|
|
|
2018-06-07 13:03:45 +00:00
|
|
|
class FunctionalTests(SimpleTestCase):
|
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):
|
2016-10-27 07:53:39 +00:00
|
|
|
"""lazy also finds base class methods in the proxy object"""
|
2017-01-19 07:39:46 +00:00
|
|
|
class Base:
|
2011-05-05 00:09:51 +00:00
|
|
|
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):
|
2016-10-27 07:53:39 +00:00
|
|
|
"""lazy finds the correct (overridden) method implementation"""
|
2017-01-19 07:39:46 +00:00
|
|
|
class Base:
|
2014-08-22 16:31:26 +00:00
|
|
|
def method(self):
|
|
|
|
return 'Base'
|
|
|
|
|
|
|
|
class Klazz(Base):
|
|
|
|
def method(self):
|
|
|
|
return 'Klazz'
|
|
|
|
|
|
|
|
t = lazy(lambda: Klazz(), Base)()
|
|
|
|
self.assertEqual(t.method(), 'Klazz')
|
|
|
|
|
2015-06-20 12:00:55 +00:00
|
|
|
def test_lazy_object_to_string(self):
|
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class Klazz:
|
2016-12-01 10:38:01 +00:00
|
|
|
def __str__(self):
|
|
|
|
return "Î am ā Ǩlâzz."
|
|
|
|
|
|
|
|
def __bytes__(self):
|
|
|
|
return b"\xc3\x8e am \xc4\x81 binary \xc7\xa8l\xc3\xa2zz."
|
2015-06-20 12:00:55 +00:00
|
|
|
|
|
|
|
t = lazy(lambda: Klazz(), Klazz)()
|
2016-12-29 15:27:49 +00:00
|
|
|
self.assertEqual(str(t), "Î am ā Ǩlâzz.")
|
|
|
|
self.assertEqual(bytes(t), b"\xc3\x8e am \xc4\x81 binary \xc7\xa8l\xc3\xa2zz.")
|
2015-06-20 12:00:55 +00:00
|
|
|
|
2018-06-07 13:03:45 +00:00
|
|
|
def assertCachedPropertyWorks(self, attr, Class):
|
|
|
|
with self.subTest(attr=attr):
|
|
|
|
def get(source):
|
|
|
|
return getattr(source, attr)
|
|
|
|
|
|
|
|
obj = Class()
|
2013-02-23 22:20:00 +00:00
|
|
|
|
2018-06-07 13:03:45 +00:00
|
|
|
class SubClass(Class):
|
|
|
|
pass
|
|
|
|
|
|
|
|
subobj = SubClass()
|
|
|
|
# Docstring is preserved.
|
|
|
|
self.assertEqual(get(Class).__doc__, 'Here is the docstring...')
|
|
|
|
self.assertEqual(get(SubClass).__doc__, 'Here is the docstring...')
|
|
|
|
# It's cached.
|
|
|
|
self.assertEqual(get(obj), get(obj))
|
|
|
|
self.assertEqual(get(subobj), get(subobj))
|
|
|
|
# The correct value is returned.
|
|
|
|
self.assertEqual(get(obj)[0], 1)
|
|
|
|
self.assertEqual(get(subobj)[0], 1)
|
|
|
|
# State isn't shared between instances.
|
|
|
|
obj2 = Class()
|
|
|
|
subobj2 = SubClass()
|
|
|
|
self.assertNotEqual(get(obj), get(obj2))
|
|
|
|
self.assertNotEqual(get(subobj), get(subobj2))
|
|
|
|
# It behaves like a property when there's no instance.
|
|
|
|
self.assertIsInstance(get(Class), cached_property)
|
|
|
|
self.assertIsInstance(get(SubClass), cached_property)
|
|
|
|
# 'other_value' doesn't become a property.
|
|
|
|
self.assertTrue(callable(obj.other_value))
|
|
|
|
self.assertTrue(callable(subobj.other_value))
|
|
|
|
|
|
|
|
def test_cached_property(self):
|
|
|
|
"""cached_property caches its value and behaves like a property."""
|
|
|
|
class Class:
|
2013-02-23 22:20:00 +00:00
|
|
|
@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()
|
|
|
|
|
2018-06-07 13:03:45 +00:00
|
|
|
@cached_property
|
|
|
|
def __foo__(self):
|
|
|
|
"""Here is the docstring..."""
|
|
|
|
return 1, object()
|
|
|
|
|
2014-05-24 10:29:31 +00:00
|
|
|
def other_value(self):
|
2018-06-07 13:03:45 +00:00
|
|
|
"""Here is the docstring..."""
|
|
|
|
return 1, object()
|
2014-05-24 10:29:31 +00:00
|
|
|
|
|
|
|
other = cached_property(other_value, name='other')
|
|
|
|
|
2018-06-07 13:03:45 +00:00
|
|
|
attrs = ['value', 'other', '__foo__']
|
|
|
|
for attr in attrs:
|
|
|
|
self.assertCachedPropertyWorks(attr, Class)
|
2014-10-19 22:34:35 +00:00
|
|
|
|
2018-06-07 13:03:45 +00:00
|
|
|
def test_cached_property_auto_name(self):
|
|
|
|
"""
|
|
|
|
cached_property caches its value and behaves like a property
|
|
|
|
on mangled methods or when the name kwarg isn't set.
|
|
|
|
"""
|
|
|
|
class Class:
|
|
|
|
@cached_property
|
|
|
|
def __value(self):
|
|
|
|
"""Here is the docstring..."""
|
|
|
|
return 1, object()
|
|
|
|
|
|
|
|
def other_value(self):
|
|
|
|
"""Here is the docstring..."""
|
|
|
|
return 1, object()
|
|
|
|
|
|
|
|
other = cached_property(other_value)
|
|
|
|
other2 = cached_property(other_value, name='different_name')
|
|
|
|
|
|
|
|
attrs = ['_Class__value', 'other']
|
|
|
|
for attr in attrs:
|
|
|
|
self.assertCachedPropertyWorks(attr, Class)
|
|
|
|
|
|
|
|
# An explicit name is ignored.
|
|
|
|
obj = Class()
|
|
|
|
obj.other2
|
|
|
|
self.assertFalse(hasattr(obj, 'different_name'))
|
|
|
|
|
|
|
|
def test_cached_property_reuse_different_names(self):
|
|
|
|
"""Disallow this case because the decorated function wouldn't be cached."""
|
|
|
|
with self.assertRaises(RuntimeError) as ctx:
|
|
|
|
class ReusedCachedProperty:
|
|
|
|
@cached_property
|
|
|
|
def a(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
b = a
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
str(ctx.exception.__context__),
|
|
|
|
str(TypeError(
|
|
|
|
"Cannot assign the same cached_property to two different "
|
|
|
|
"names ('a' and 'b')."
|
|
|
|
))
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_cached_property_reuse_same_name(self):
|
|
|
|
"""
|
|
|
|
Reusing a cached_property on different classes under the same name is
|
|
|
|
allowed.
|
|
|
|
"""
|
|
|
|
counter = 0
|
2013-02-23 22:20:00 +00:00
|
|
|
|
2018-06-07 13:03:45 +00:00
|
|
|
@cached_property
|
|
|
|
def _cp(_self):
|
|
|
|
nonlocal counter
|
|
|
|
counter += 1
|
|
|
|
return counter
|
2013-02-23 22:20:00 +00:00
|
|
|
|
2018-06-07 13:03:45 +00:00
|
|
|
class A:
|
|
|
|
cp = _cp
|
2013-02-23 22:20:00 +00:00
|
|
|
|
2018-06-07 13:03:45 +00:00
|
|
|
class B:
|
|
|
|
cp = _cp
|
2013-02-23 22:20:00 +00:00
|
|
|
|
2018-06-07 13:03:45 +00:00
|
|
|
a = A()
|
|
|
|
b = B()
|
|
|
|
self.assertEqual(a.cp, 1)
|
|
|
|
self.assertEqual(b.cp, 2)
|
|
|
|
self.assertEqual(a.cp, 1)
|
2013-05-29 16:52:17 +00:00
|
|
|
|
2018-06-07 13:03:45 +00:00
|
|
|
def test_cached_property_set_name_not_called(self):
|
|
|
|
cp = cached_property(lambda s: None)
|
|
|
|
|
|
|
|
class Foo:
|
|
|
|
pass
|
|
|
|
|
|
|
|
Foo.cp = cp
|
|
|
|
msg = 'Cannot use cached_property instance without calling __set_name__() on it.'
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
|
|
|
Foo().cp
|
|
|
|
|
2013-05-29 16:52:17 +00:00
|
|
|
def test_lazy_equality(self):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
== and != work correctly for Promises.
|
2013-05-29 16:52:17 +00:00
|
|
|
"""
|
|
|
|
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())
|
2016-06-04 23:13:00 +00:00
|
|
|
|
|
|
|
def test_lazy_repr_text(self):
|
|
|
|
original_object = 'Lazy translation text'
|
2016-12-29 15:27:49 +00:00
|
|
|
lazy_obj = lazy(lambda: original_object, str)
|
2016-06-14 13:03:12 +00:00
|
|
|
self.assertEqual(repr(original_object), repr(lazy_obj()))
|
2016-06-04 23:13:00 +00:00
|
|
|
|
|
|
|
def test_lazy_repr_int(self):
|
|
|
|
original_object = 15
|
|
|
|
lazy_obj = lazy(lambda: original_object, int)
|
2016-06-14 13:03:12 +00:00
|
|
|
self.assertEqual(repr(original_object), repr(lazy_obj()))
|
2016-06-04 23:13:00 +00:00
|
|
|
|
|
|
|
def test_lazy_repr_bytes(self):
|
|
|
|
original_object = b'J\xc3\xbcst a str\xc3\xadng'
|
|
|
|
lazy_obj = lazy(lambda: original_object, bytes)
|
2016-06-14 13:03:12 +00:00
|
|
|
self.assertEqual(repr(original_object), repr(lazy_obj()))
|