2015-06-20 20:00:55 +08:00
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
|
|
|
|
2015-06-20 20:00:55 +08:00
|
|
|
from django.utils import six
|
2016-11-30 08:01:12 +08:00
|
|
|
from django.utils.functional import cached_property, lazy
|
2010-01-05 20:06:45 +08:00
|
|
|
|
|
|
|
|
2010-09-28 16:18:12 +08:00
|
|
|
class FunctionalTestCase(unittest.TestCase):
|
2010-01-05 20:06:45 +08: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 08:09:51 +08:00
|
|
|
|
|
|
|
def test_lazy_base_class(self):
|
2016-10-27 15:53:39 +08:00
|
|
|
"""lazy also finds base class methods in the proxy object"""
|
2011-05-05 08:09:51 +08:00
|
|
|
class Base(object):
|
|
|
|
def base_method(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Klazz(Base):
|
|
|
|
pass
|
|
|
|
|
|
|
|
t = lazy(lambda: Klazz(), Klazz)()
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn('base_method', dir(t))
|
2011-06-01 21:47:00 +08:00
|
|
|
|
2014-08-23 00:31:26 +08:00
|
|
|
def test_lazy_base_class_override(self):
|
2016-10-27 15:53:39 +08:00
|
|
|
"""lazy finds the correct (overridden) method implementation"""
|
2014-08-23 00:31:26 +08:00
|
|
|
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')
|
|
|
|
|
2015-06-20 20:00:55 +08:00
|
|
|
def test_lazy_object_to_string(self):
|
|
|
|
|
|
|
|
class Klazz(object):
|
|
|
|
if six.PY3:
|
|
|
|
def __str__(self):
|
|
|
|
return "Î am ā Ǩlâzz."
|
|
|
|
|
|
|
|
def __bytes__(self):
|
|
|
|
return b"\xc3\x8e am \xc4\x81 binary \xc7\xa8l\xc3\xa2zz."
|
|
|
|
else:
|
|
|
|
def __unicode__(self):
|
|
|
|
return "Î am ā Ǩlâzz."
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return b"\xc3\x8e am \xc4\x81 binary \xc7\xa8l\xc3\xa2zz."
|
|
|
|
|
|
|
|
t = lazy(lambda: Klazz(), Klazz)()
|
|
|
|
self.assertEqual(six.text_type(t), "Î am ā Ǩlâzz.")
|
|
|
|
self.assertEqual(six.binary_type(t), b"\xc3\x8e am \xc4\x81 binary \xc7\xa8l\xc3\xa2zz.")
|
|
|
|
|
2013-02-24 06:20:00 +08:00
|
|
|
def test_cached_property(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
cached_property caches its value and that it behaves like a property
|
2013-02-24 06:20:00 +08:00
|
|
|
"""
|
|
|
|
class A(object):
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def value(self):
|
2014-10-20 06:34:35 +08:00
|
|
|
"""Here is the docstring..."""
|
2013-02-24 06:20:00 +08:00
|
|
|
return 1, object()
|
|
|
|
|
2014-05-24 18:29:31 +08:00
|
|
|
def other_value(self):
|
|
|
|
return 1
|
|
|
|
|
|
|
|
other = cached_property(other_value, name='other')
|
|
|
|
|
2014-10-20 06:34:35 +08:00
|
|
|
# docstring should be preserved
|
|
|
|
self.assertEqual(A.value.__doc__, "Here is the docstring...")
|
|
|
|
|
2013-02-24 06:20:00 +08: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-30 00:52:17 +08:00
|
|
|
|
2014-05-24 18:29:31 +08:00
|
|
|
# check that overriding name works
|
|
|
|
self.assertEqual(a.other, 1)
|
|
|
|
self.assertTrue(callable(a.other_value))
|
|
|
|
|
2013-05-30 00:52:17 +08:00
|
|
|
def test_lazy_equality(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
== and != work correctly for Promises.
|
2013-05-30 00:52:17 +08: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-05 07:13:00 +08:00
|
|
|
|
|
|
|
def test_lazy_repr_text(self):
|
|
|
|
original_object = 'Lazy translation text'
|
|
|
|
lazy_obj = lazy(lambda: original_object, six.text_type)
|
2016-06-14 21:03:12 +08:00
|
|
|
self.assertEqual(repr(original_object), repr(lazy_obj()))
|
2016-06-05 07:13:00 +08:00
|
|
|
|
|
|
|
def test_lazy_repr_int(self):
|
|
|
|
original_object = 15
|
|
|
|
lazy_obj = lazy(lambda: original_object, int)
|
2016-06-14 21:03:12 +08:00
|
|
|
self.assertEqual(repr(original_object), repr(lazy_obj()))
|
2016-06-05 07:13:00 +08: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 21:03:12 +08:00
|
|
|
self.assertEqual(repr(original_object), repr(lazy_obj()))
|