diff options
author | Petter Friberg <petter_friberg@hotmail.com> | 2022-01-04 23:06:46 +0100 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-01-06 06:38:17 +0100 |
commit | bc174e6ea0ce676c5a7f467bda9739e6ef6b6186 (patch) | |
tree | 68ae81b376e74e74ce3e8736592f02b3f3ada5ec /tests/test_utils | |
parent | 806efe912b846c1fde250c9321d8334b7517cd56 (diff) | |
download | django-bc174e6ea0ce676c5a7f467bda9739e6ef6b6186.tar.gz |
Fixed #33410 -- Fixed recursive capturing of callbacks by TestCase.captureOnCommitCallbacks().
Regression in d89f976bddb49fb168334960acc8979c3de991fa.
Diffstat (limited to 'tests/test_utils')
-rw-r--r-- | tests/test_utils/tests.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py index 2addc1fdde..3380a1c67e 100644 --- a/tests/test_utils/tests.py +++ b/tests/test_utils/tests.py @@ -1829,6 +1829,58 @@ class CaptureOnCommitCallbacksTests(TestCase): self.assertEqual(len(callbacks), 2) self.assertIs(self.callback_called, True) + def test_execute_tree(self): + """ + A visualisation of the callback tree tested. Each node is expected to + be visited only once: + + └─branch_1 + ├─branch_2 + │ ├─leaf_1 + │ └─leaf_2 + └─leaf_3 + """ + branch_1_call_counter = 0 + branch_2_call_counter = 0 + leaf_1_call_counter = 0 + leaf_2_call_counter = 0 + leaf_3_call_counter = 0 + + def leaf_1(): + nonlocal leaf_1_call_counter + leaf_1_call_counter += 1 + + def leaf_2(): + nonlocal leaf_2_call_counter + leaf_2_call_counter += 1 + + def leaf_3(): + nonlocal leaf_3_call_counter + leaf_3_call_counter += 1 + + def branch_1(): + nonlocal branch_1_call_counter + branch_1_call_counter += 1 + transaction.on_commit(branch_2) + transaction.on_commit(leaf_3) + + def branch_2(): + nonlocal branch_2_call_counter + branch_2_call_counter += 1 + transaction.on_commit(leaf_1) + transaction.on_commit(leaf_2) + + with self.captureOnCommitCallbacks(execute=True) as callbacks: + transaction.on_commit(branch_1) + + self.assertEqual(branch_1_call_counter, 1) + self.assertEqual(branch_2_call_counter, 1) + self.assertEqual(leaf_1_call_counter, 1) + self.assertEqual(leaf_2_call_counter, 1) + self.assertEqual(leaf_3_call_counter, 1) + + self.assertEqual(callbacks, [branch_1, branch_2, leaf_3, leaf_1, leaf_2]) + class DisallowedDatabaseQueriesTests(SimpleTestCase): def test_disallowed_database_connections(self): |