Conditions | 1 |
Total Lines | 103 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
98 | def test_run_workflow(self): |
||
99 | wf_meta = base.get_wf_fixture_meta_data(TEST_PACK_PATH, 'sequential.yaml') |
||
100 | wf_input = {'who': 'Thanos'} |
||
101 | lv_ac_db = lv_db_models.LiveActionDB(action=wf_meta['name'], parameters=wf_input) |
||
102 | lv_ac_db, ac_ex_db = ac_svc.request(lv_ac_db) |
||
103 | |||
104 | # Assert action execution is running. |
||
105 | lv_ac_db = lv_db_access.LiveAction.get_by_id(str(lv_ac_db.id)) |
||
106 | |||
107 | self.assertTrue(lv_ac_db.action_is_workflow) |
||
108 | self.assertEqual(lv_ac_db.status, ac_const.LIVEACTION_STATUS_RUNNING, lv_ac_db.result) |
||
109 | |||
110 | wf_ex_dbs = wf_db_access.WorkflowExecution.query(action_execution=str(ac_ex_db.id)) |
||
111 | wf_ex_db = wf_ex_dbs[0] |
||
112 | |||
113 | # Check required attributes. |
||
114 | self.assertEqual(len(wf_ex_dbs), 1) |
||
115 | self.assertIsNotNone(wf_ex_db.id) |
||
116 | self.assertGreater(wf_ex_db.rev, 0) |
||
117 | self.assertEqual(wf_ex_db.action_execution, str(ac_ex_db.id)) |
||
118 | self.assertEqual(wf_ex_db.status, ac_const.LIVEACTION_STATUS_RUNNING) |
||
119 | |||
120 | # Check context. |
||
121 | self.assertIn('workflow_execution', lv_ac_db.context) |
||
122 | self.assertEqual(lv_ac_db.context['workflow_execution'], str(wf_ex_db.id)) |
||
123 | |||
124 | # Check graph. |
||
125 | self.assertIsNotNone(wf_ex_db.graph) |
||
126 | self.assertTrue(isinstance(wf_ex_db.graph, dict)) |
||
127 | self.assertIn('nodes', wf_ex_db.graph) |
||
128 | self.assertIn('adjacency', wf_ex_db.graph) |
||
129 | |||
130 | # Check task flow. |
||
131 | self.assertIsNotNone(wf_ex_db.flow) |
||
132 | self.assertTrue(isinstance(wf_ex_db.flow, dict)) |
||
133 | self.assertIn('tasks', wf_ex_db.flow) |
||
134 | self.assertIn('sequence', wf_ex_db.flow) |
||
135 | |||
136 | # Check input. |
||
137 | self.assertDictEqual(wf_ex_db.input, wf_input) |
||
138 | |||
139 | # Assert task1 is already completed. |
||
140 | query_filters = {'workflow_execution': str(wf_ex_db.id), 'task_id': 'task1'} |
||
141 | tk1_ex_db = wf_db_access.TaskExecution.query(**query_filters)[0] |
||
142 | tk1_ac_ex_db = ex_db_access.ActionExecution.query(task_execution=str(tk1_ex_db.id))[0] |
||
143 | tk1_lv_ac_db = lv_db_access.LiveAction.get_by_id(tk1_ac_ex_db.liveaction['id']) |
||
144 | self.assertEqual(tk1_lv_ac_db.status, ac_const.LIVEACTION_STATUS_SUCCEEDED) |
||
145 | |||
146 | # Manually handle action execution completion. |
||
147 | wf_svc.handle_action_execution_completion(tk1_ac_ex_db) |
||
148 | |||
149 | # Assert task1 succeeded and workflow is still running. |
||
150 | tk1_ex_db = wf_db_access.TaskExecution.get_by_id(tk1_ex_db.id) |
||
151 | self.assertEqual(tk1_ex_db.status, wf_states.SUCCEEDED) |
||
152 | wf_ex_db = wf_db_access.WorkflowExecution.get_by_id(wf_ex_db.id) |
||
153 | self.assertEqual(wf_ex_db.status, wf_states.RUNNING) |
||
154 | |||
155 | # Assert task2 is already completed. |
||
156 | query_filters = {'workflow_execution': str(wf_ex_db.id), 'task_id': 'task2'} |
||
157 | tk2_ex_db = wf_db_access.TaskExecution.query(**query_filters)[0] |
||
158 | tk2_ac_ex_db = ex_db_access.ActionExecution.query(task_execution=str(tk2_ex_db.id))[0] |
||
159 | tk2_lv_ac_db = lv_db_access.LiveAction.get_by_id(tk2_ac_ex_db.liveaction['id']) |
||
160 | self.assertEqual(tk2_lv_ac_db.status, ac_const.LIVEACTION_STATUS_SUCCEEDED) |
||
161 | |||
162 | # Manually handle action execution completion. |
||
163 | wf_svc.handle_action_execution_completion(tk2_ac_ex_db) |
||
164 | |||
165 | # Assert task2 succeeded and workflow is still running. |
||
166 | tk2_ex_db = wf_db_access.TaskExecution.get_by_id(tk2_ex_db.id) |
||
167 | self.assertEqual(tk2_ex_db.status, wf_states.SUCCEEDED) |
||
168 | wf_ex_db = wf_db_access.WorkflowExecution.get_by_id(wf_ex_db.id) |
||
169 | self.assertEqual(wf_ex_db.status, wf_states.RUNNING) |
||
170 | |||
171 | # Assert task3 is already completed. |
||
172 | query_filters = {'workflow_execution': str(wf_ex_db.id), 'task_id': 'task3'} |
||
173 | tk3_ex_db = wf_db_access.TaskExecution.query(**query_filters)[0] |
||
174 | tk3_ac_ex_db = ex_db_access.ActionExecution.query(task_execution=str(tk3_ex_db.id))[0] |
||
175 | tk3_lv_ac_db = lv_db_access.LiveAction.get_by_id(tk3_ac_ex_db.liveaction['id']) |
||
176 | self.assertEqual(tk3_lv_ac_db.status, ac_const.LIVEACTION_STATUS_SUCCEEDED) |
||
177 | |||
178 | # Manually handle action execution completion. |
||
179 | wf_svc.handle_action_execution_completion(tk3_ac_ex_db) |
||
180 | |||
181 | # Assert task3 succeeded and workflow is completed. |
||
182 | tk3_ex_db = wf_db_access.TaskExecution.get_by_id(tk3_ex_db.id) |
||
183 | self.assertEqual(tk3_ex_db.status, wf_states.SUCCEEDED) |
||
184 | wf_ex_db = wf_db_access.WorkflowExecution.get_by_id(wf_ex_db.id) |
||
185 | self.assertEqual(wf_ex_db.status, wf_states.SUCCEEDED) |
||
186 | lv_ac_db = lv_db_access.LiveAction.get_by_id(str(lv_ac_db.id)) |
||
187 | self.assertEqual(lv_ac_db.status, ac_const.LIVEACTION_STATUS_SUCCEEDED) |
||
188 | ac_ex_db = ex_db_access.ActionExecution.get_by_id(str(ac_ex_db.id)) |
||
189 | self.assertEqual(ac_ex_db.status, ac_const.LIVEACTION_STATUS_SUCCEEDED) |
||
190 | |||
191 | # Check workflow output. |
||
192 | expected_output = {'msg': '%s, All your base are belong to us!' % wf_input['who']} |
||
193 | |||
194 | self.assertDictEqual(wf_ex_db.output, expected_output) |
||
195 | |||
196 | # Check liveaction and action execution result. |
||
197 | expected_result = {'output': expected_output} |
||
198 | |||
199 | self.assertDictEqual(lv_ac_db.result, expected_result) |
||
200 | self.assertDictEqual(ac_ex_db.result, expected_result) |
||
201 |