MCPcopy
hub / github.com/django/django / test_execute_tree

Method test_execute_tree

tests/test_utils/tests.py:2151–2201  ·  view source on GitHub ↗

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

(self)

Source from the content-addressed store, hash-verified

2149 self.assertIs(self.callback_called, True)
2150
2151 def test_execute_tree(self):
2152 """
2153 A visualisation of the callback tree tested. Each node is expected to
2154 be visited only once:
2155
2156 └─branch_1
2157 ├─branch_2
2158 │ ├─leaf_1
2159 │ └─leaf_2
2160 └─leaf_3
2161 """
2162 branch_1_call_counter = 0
2163 branch_2_call_counter = 0
2164 leaf_1_call_counter = 0
2165 leaf_2_call_counter = 0
2166 leaf_3_call_counter = 0
2167
2168 def leaf_1():
2169 nonlocal leaf_1_call_counter
2170 leaf_1_call_counter += 1
2171
2172 def leaf_2():
2173 nonlocal leaf_2_call_counter
2174 leaf_2_call_counter += 1
2175
2176 def leaf_3():
2177 nonlocal leaf_3_call_counter
2178 leaf_3_call_counter += 1
2179
2180 def branch_1():
2181 nonlocal branch_1_call_counter
2182 branch_1_call_counter += 1
2183 transaction.on_commit(branch_2)
2184 transaction.on_commit(leaf_3)
2185
2186 def branch_2():
2187 nonlocal branch_2_call_counter
2188 branch_2_call_counter += 1
2189 transaction.on_commit(leaf_1)
2190 transaction.on_commit(leaf_2)
2191
2192 with self.captureOnCommitCallbacks(execute=True) as callbacks:
2193 transaction.on_commit(branch_1)
2194
2195 self.assertEqual(branch_1_call_counter, 1)
2196 self.assertEqual(branch_2_call_counter, 1)
2197 self.assertEqual(leaf_1_call_counter, 1)
2198 self.assertEqual(leaf_2_call_counter, 1)
2199 self.assertEqual(leaf_3_call_counter, 1)
2200
2201 self.assertEqual(callbacks, [branch_1, branch_2, leaf_3, leaf_1, leaf_2])
2202
2203 def test_execute_robust(self):
2204 class MyException(Exception):

Callers

nothing calls this directly

Calls 2

on_commitMethod · 0.45

Tested by

no test coverage detected