diff options
Diffstat (limited to 'tests/utils_tests/test_functional.py')
| -rw-r--r-- | tests/utils_tests/test_functional.py | 57 |
1 files changed, 33 insertions, 24 deletions
diff --git a/tests/utils_tests/test_functional.py b/tests/utils_tests/test_functional.py index f720ffac3d..1e403f351b 100644 --- a/tests/utils_tests/test_functional.py +++ b/tests/utils_tests/test_functional.py @@ -14,6 +14,7 @@ class FunctionalTests(SimpleTestCase): def test_lazy_base_class(self): """lazy also finds base class methods in the proxy object""" + class Base: def base_method(self): pass @@ -22,23 +23,23 @@ class FunctionalTests(SimpleTestCase): pass t = lazy(lambda: Klazz(), Klazz)() - self.assertIn('base_method', dir(t)) + 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' + return "Base" class Klazz(Base): def method(self): - return 'Klazz' + return "Klazz" t = lazy(lambda: Klazz(), Base)() - self.assertEqual(t.method(), 'Klazz') + self.assertEqual(t.method(), "Klazz") def test_lazy_object_to_string(self): - class Klazz: def __str__(self): return "Î am ā Ǩlâzz." @@ -52,6 +53,7 @@ class FunctionalTests(SimpleTestCase): def assertCachedPropertyWorks(self, attr, Class): with self.subTest(attr=attr): + def get(source): return getattr(source, attr) @@ -62,8 +64,8 @@ class FunctionalTests(SimpleTestCase): subobj = SubClass() # Docstring is preserved. - self.assertEqual(get(Class).__doc__, 'Here is the docstring...') - self.assertEqual(get(SubClass).__doc__, 'Here is the docstring...') + 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)) @@ -84,6 +86,7 @@ class FunctionalTests(SimpleTestCase): def test_cached_property(self): """cached_property caches its value and behaves like a property.""" + class Class: @cached_property def value(self): @@ -101,7 +104,7 @@ class FunctionalTests(SimpleTestCase): other = cached_property(other_value) - attrs = ['value', 'other', '__foo__'] + attrs = ["value", "other", "__foo__"] for attr in attrs: self.assertCachedPropertyWorks(attr, Class) @@ -112,14 +115,14 @@ class FunctionalTests(SimpleTestCase): """Here is the docstring...""" return 1, object() - other = cached_property(other_value, name='other') - other2 = cached_property(other_value, name='different_name') + other = cached_property(other_value, name="other") + other2 = cached_property(other_value, name="different_name") - self.assertCachedPropertyWorks('other', Class) + self.assertCachedPropertyWorks("other", Class) # An explicit name is ignored. obj = Class() obj.other2 - self.assertFalse(hasattr(obj, 'different_name')) + self.assertFalse(hasattr(obj, "different_name")) def test_cached_property_name_deprecation_warning(self): def value(self): @@ -127,13 +130,14 @@ class FunctionalTests(SimpleTestCase): msg = "The name argument is deprecated as it's unnecessary as of Python 3.6." with self.assertWarnsMessage(RemovedInDjango50Warning, msg): - cached_property(value, name='other_name') + cached_property(value, name="other_name") 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): @@ -146,13 +150,14 @@ class FunctionalTests(SimpleTestCase): other = cached_property(other_value) - attrs = ['_Class__value', 'other'] + attrs = ["_Class__value", "other"] for attr in attrs: self.assertCachedPropertyWorks(attr, Class) 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): @@ -162,10 +167,12 @@ class FunctionalTests(SimpleTestCase): self.assertEqual( str(ctx.exception.__context__), - str(TypeError( - "Cannot assign the same cached_property to two different " - "names ('a' and 'b')." - )) + str( + TypeError( + "Cannot assign the same cached_property to two different " + "names ('a' and 'b')." + ) + ), ) def test_cached_property_reuse_same_name(self): @@ -200,7 +207,9 @@ class FunctionalTests(SimpleTestCase): pass Foo.cp = cp - msg = 'Cannot use cached_property instance without calling __set_name__() on it.' + msg = ( + "Cannot use cached_property instance without calling __set_name__() on it." + ) with self.assertRaisesMessage(TypeError, msg): Foo().cp @@ -221,7 +230,7 @@ class FunctionalTests(SimpleTestCase): self.assertNotEqual(lazy_b(), lazy_c()) def test_lazy_repr_text(self): - original_object = 'Lazy translation text' + original_object = "Lazy translation text" lazy_obj = lazy(lambda: original_object, str) self.assertEqual(repr(original_object), repr(lazy_obj())) @@ -231,7 +240,7 @@ class FunctionalTests(SimpleTestCase): self.assertEqual(repr(original_object), repr(lazy_obj())) def test_lazy_repr_bytes(self): - original_object = b'J\xc3\xbcst a str\xc3\xadng' + 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())) @@ -240,13 +249,13 @@ class FunctionalTests(SimpleTestCase): # it's used. lazified = lazy(lambda: 0, int) __proxy__ = lazified().__class__ - with mock.patch.object(__proxy__, '__prepare_class__') as mocked: + with mock.patch.object(__proxy__, "__prepare_class__") as mocked: lazified() mocked.assert_not_called() def test_lazy_bytes_and_str_result_classes(self): - lazy_obj = lazy(lambda: 'test', str, bytes) - msg = 'Cannot call lazy() with both bytes and text return types.' + lazy_obj = lazy(lambda: "test", str, bytes) + msg = "Cannot call lazy() with both bytes and text return types." with self.assertRaisesMessage(ValueError, msg): lazy_obj() |
