mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			113 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			113 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| import unittest
 | |
| 
 | |
| from django.utils.functional import cached_property, lazy
 | |
| 
 | |
| 
 | |
| class FunctionalTestCase(unittest.TestCase):
 | |
|     def test_lazy(self):
 | |
|         t = lazy(lambda: tuple(range(3)), list, tuple)
 | |
|         for a, b in zip(t(), range(3)):
 | |
|             self.assertEqual(a, b)
 | |
| 
 | |
|     def test_lazy_base_class(self):
 | |
|         """lazy also finds base class methods in the proxy object"""
 | |
|         class Base:
 | |
|             def base_method(self):
 | |
|                 pass
 | |
| 
 | |
|         class Klazz(Base):
 | |
|             pass
 | |
| 
 | |
|         t = lazy(lambda: Klazz(), Klazz)()
 | |
|         self.assertIn('base_method', dir(t))
 | |
| 
 | |
|     def test_lazy_base_class_override(self):
 | |
|         """lazy finds the correct (overridden) method implementation"""
 | |
|         class Base:
 | |
|             def method(self):
 | |
|                 return 'Base'
 | |
| 
 | |
|         class Klazz(Base):
 | |
|             def method(self):
 | |
|                 return 'Klazz'
 | |
| 
 | |
|         t = lazy(lambda: Klazz(), Base)()
 | |
|         self.assertEqual(t.method(), 'Klazz')
 | |
| 
 | |
|     def test_lazy_object_to_string(self):
 | |
| 
 | |
|         class Klazz:
 | |
|             def __str__(self):
 | |
|                 return "Î am ā Ǩlâzz."
 | |
| 
 | |
|             def __bytes__(self):
 | |
|                 return b"\xc3\x8e am \xc4\x81 binary \xc7\xa8l\xc3\xa2zz."
 | |
| 
 | |
|         t = lazy(lambda: Klazz(), Klazz)()
 | |
|         self.assertEqual(str(t), "Î am ā Ǩlâzz.")
 | |
|         self.assertEqual(bytes(t), b"\xc3\x8e am \xc4\x81 binary \xc7\xa8l\xc3\xa2zz.")
 | |
| 
 | |
|     def test_cached_property(self):
 | |
|         """
 | |
|         cached_property caches its value and that it behaves like a property
 | |
|         """
 | |
|         class A:
 | |
| 
 | |
|             @cached_property
 | |
|             def value(self):
 | |
|                 """Here is the docstring..."""
 | |
|                 return 1, object()
 | |
| 
 | |
|             def other_value(self):
 | |
|                 return 1
 | |
| 
 | |
|             other = cached_property(other_value, name='other')
 | |
| 
 | |
|         # docstring should be preserved
 | |
|         self.assertEqual(A.value.__doc__, "Here is the docstring...")
 | |
| 
 | |
|         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)
 | |
| 
 | |
|         # check that overriding name works
 | |
|         self.assertEqual(a.other, 1)
 | |
|         self.assertTrue(callable(a.other_value))
 | |
| 
 | |
|     def test_lazy_equality(self):
 | |
|         """
 | |
|         == 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())
 | |
| 
 | |
|     def test_lazy_repr_text(self):
 | |
|         original_object = 'Lazy translation text'
 | |
|         lazy_obj = lazy(lambda: original_object, str)
 | |
|         self.assertEqual(repr(original_object), repr(lazy_obj()))
 | |
| 
 | |
|     def test_lazy_repr_int(self):
 | |
|         original_object = 15
 | |
|         lazy_obj = lazy(lambda: original_object, int)
 | |
|         self.assertEqual(repr(original_object), repr(lazy_obj()))
 | |
| 
 | |
|     def test_lazy_repr_bytes(self):
 | |
|         original_object = b'J\xc3\xbcst a str\xc3\xadng'
 | |
|         lazy_obj = lazy(lambda: original_object, bytes)
 | |
|         self.assertEqual(repr(original_object), repr(lazy_obj()))
 |