summaryrefslogtreecommitdiff
path: root/tests/template_tests
diff options
context:
space:
mode:
authorLightDiscord <root@arnaud.sh>2022-10-04 20:11:28 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-11-04 11:08:58 +0100
commite20c9eb60ab9d1c84b19672def918097c943edd8 (patch)
treeca449f88bc092614667a0bb30469799064397c9b /tests/template_tests
parent5a7f3213ae4a2dc5e962ae1fcdedc6e6ab68dfcd (diff)
downloaddjango-e20c9eb60ab9d1c84b19672def918097c943edd8.tar.gz
Fixed #27654 -- Propagated alters_data attribute to callables overridden in subclasses.
Thanks Shai Berger and Adam Johnson for reviews and the implementation idea.
Diffstat (limited to 'tests/template_tests')
-rw-r--r--tests/template_tests/test_callables.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/tests/template_tests/test_callables.py b/tests/template_tests/test_callables.py
index acd8fb9d2c..bd53de5ca5 100644
--- a/tests/template_tests/test_callables.py
+++ b/tests/template_tests/test_callables.py
@@ -1,5 +1,6 @@
from unittest import TestCase
+from django.db.models.utils import AltersData
from django.template import Context, Engine
@@ -63,6 +64,68 @@ class CallableVariablesTests(TestCase):
# template rendering.
self.assertEqual(my_doodad.num_calls, 0)
+ def test_alters_data_propagation(self):
+ class GrandParentLeft(AltersData):
+ def my_method(self):
+ return 42
+
+ my_method.alters_data = True
+
+ class ParentLeft(GrandParentLeft):
+ def change_alters_data_method(self):
+ return 63
+
+ change_alters_data_method.alters_data = True
+
+ def sub_non_callable_method(self):
+ return 64
+
+ sub_non_callable_method.alters_data = True
+
+ class ParentRight(AltersData):
+ def other_method(self):
+ return 52
+
+ other_method.alters_data = True
+
+ class Child(ParentLeft, ParentRight):
+ def my_method(self):
+ return 101
+
+ def other_method(self):
+ return 102
+
+ def change_alters_data_method(self):
+ return 103
+
+ change_alters_data_method.alters_data = False
+
+ sub_non_callable_method = 104
+
+ class GrandChild(Child):
+ pass
+
+ child = Child()
+ self.assertIs(child.my_method.alters_data, True)
+ self.assertIs(child.other_method.alters_data, True)
+ self.assertIs(child.change_alters_data_method.alters_data, False)
+
+ grand_child = GrandChild()
+ self.assertIs(grand_child.my_method.alters_data, True)
+ self.assertIs(grand_child.other_method.alters_data, True)
+ self.assertIs(grand_child.change_alters_data_method.alters_data, False)
+
+ c = Context({"element": grand_child})
+
+ t = self.engine.from_string("{{ element.my_method }}")
+ self.assertEqual(t.render(c), "")
+ t = self.engine.from_string("{{ element.other_method }}")
+ self.assertEqual(t.render(c), "")
+ t = self.engine.from_string("{{ element.change_alters_data_method }}")
+ self.assertEqual(t.render(c), "103")
+ t = self.engine.from_string("{{ element.sub_non_callable_method }}")
+ self.assertEqual(t.render(c), "104")
+
def test_do_not_call(self):
class Doodad:
do_not_call_in_templates = True