1 """Test Processes module."""
2 from tests.utils import TestCaseWithDB, TestCaseWithServer, TestCaseSansDB
3 from plomtask.processes import Process, ProcessStep, ProcessStepsNode
4 from plomtask.conditions import Condition
5 from plomtask.exceptions import HandledException, NotFoundException
6 from plomtask.todos import Todo
9 class TestsSansDB(TestCaseSansDB):
10 """Module tests not requiring DB setup."""
11 checked_class = Process
13 versioned_defaults_to_test = {'title': 'UNNAMED', 'description': '',
17 class TestsSansDBProcessStep(TestCaseSansDB):
18 """Module tests not requiring DB setup."""
19 checked_class = ProcessStep
21 default_init_args = [2, 3, 4]
24 class TestsWithDB(TestCaseWithDB):
25 """Module tests requiring DB setup."""
26 checked_class = Process
27 test_versioneds = {'title': str, 'description': str, 'effort': float}
29 def three_processes(self) -> tuple[Process, Process, Process]:
30 """Return three saved processes."""
31 p1, p2, p3 = Process(None), Process(None), Process(None)
32 for p in [p1, p2, p3]:
36 def p_of_conditions(self) -> tuple[Process, list[Condition],
37 list[Condition], list[Condition]]:
38 """Return Process and its three Condition sets."""
40 c1, c2, c3 = Condition(None), Condition(None), Condition(None)
41 for c in [c1, c2, c3]:
43 assert isinstance(c1.id_, int)
44 assert isinstance(c2.id_, int)
45 assert isinstance(c3.id_, int)
49 p.set_conditions(self.db_conn, [c.id_ for c in set_1
50 if isinstance(c.id_, int)])
51 p.set_enables(self.db_conn, [c.id_ for c in set_2
52 if isinstance(c.id_, int)])
53 p.set_disables(self.db_conn, [c.id_ for c in set_3
54 if isinstance(c.id_, int)])
56 return p, set_1, set_2, set_3
58 def test_Process_conditions_saving(self) -> None:
59 """Test .save/.save_core."""
60 p, set1, set2, set3 = self.p_of_conditions()
61 assert p.id_ is not None
62 r = Process.by_id(self.db_conn, p.id_)
63 self.assertEqual(sorted(r.conditions), sorted(set1))
64 self.assertEqual(sorted(r.enables), sorted(set2))
65 self.assertEqual(sorted(r.disables), sorted(set3))
67 def test_from_table_row(self) -> None:
68 """Test .from_table_row() properly reads in class from DB."""
69 super().test_from_table_row()
70 p, set1, set2, set3 = self.p_of_conditions()
72 assert isinstance(p.id_, int)
73 for row in self.db_conn.row_where(self.checked_class.table_name,
75 r = Process.from_table_row(self.db_conn, row)
76 self.assertEqual(sorted(r.conditions), sorted(set1))
77 self.assertEqual(sorted(r.enables), sorted(set2))
78 self.assertEqual(sorted(r.disables), sorted(set3))
80 def test_Process_steps(self) -> None:
81 """Test addition, nesting, and non-recursion of ProcessSteps"""
82 # pylint: disable=too-many-locals
83 # pylint: disable=too-many-statements
84 p1, p2, p3 = self.three_processes()
85 assert isinstance(p1.id_, int)
86 assert isinstance(p2.id_, int)
87 assert isinstance(p3.id_, int)
88 steps_p1: list[ProcessStep] = []
89 # add step of process p2 as first (top-level) step to p1
90 s_p2_to_p1 = ProcessStep(None, p1.id_, p2.id_, None)
91 steps_p1 += [s_p2_to_p1]
92 p1.set_steps(self.db_conn, steps_p1)
93 p1_dict: dict[int, ProcessStepsNode] = {}
94 p1_dict[1] = ProcessStepsNode(p2, None, True, {})
95 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
96 # add step of process p3 as second (top-level) step to p1
97 s_p3_to_p1 = ProcessStep(None, p1.id_, p3.id_, None)
98 steps_p1 += [s_p3_to_p1]
99 p1.set_steps(self.db_conn, steps_p1)
100 p1_dict[2] = ProcessStepsNode(p3, None, True, {})
101 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
102 # add step of process p3 as first (top-level) step to p2,
103 steps_p2: list[ProcessStep] = []
104 s_p3_to_p2 = ProcessStep(None, p2.id_, p3.id_, None)
105 steps_p2 += [s_p3_to_p2]
106 p2.set_steps(self.db_conn, steps_p2)
107 # expect it as implicit sub-step of p1's second (p3) step
108 p2_dict = {3: ProcessStepsNode(p3, None, False, {})}
109 p1_dict[1].steps[3] = p2_dict[3]
110 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
111 # add step of process p2 as explicit sub-step to p1's second sub-step
112 s_p2_to_p1_first = ProcessStep(None, p1.id_, p2.id_, s_p3_to_p1.id_)
113 steps_p1 += [s_p2_to_p1_first]
114 p1.set_steps(self.db_conn, steps_p1)
115 seen_3 = ProcessStepsNode(p3, None, False, {}, False)
116 p1_dict[1].steps[3].seen = True
117 p1_dict[2].steps[4] = ProcessStepsNode(p2, s_p3_to_p1.id_, True,
119 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
120 # add step of process p3 as explicit sub-step to non-existing p1
121 # sub-step (of id=999), expect it to become another p1 top-level step
122 s_p3_to_p1_999 = ProcessStep(None, p1.id_, p3.id_, 999)
123 steps_p1 += [s_p3_to_p1_999]
124 p1.set_steps(self.db_conn, steps_p1)
125 p1_dict[5] = ProcessStepsNode(p3, None, True, {})
126 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
127 # add step of process p3 as explicit sub-step to p1's implicit p3
128 # sub-step, expect it to become another p1 top-level step
129 s_p3_to_p1_impl_p3 = ProcessStep(None, p1.id_, p3.id_, s_p3_to_p2.id_)
130 steps_p1 += [s_p3_to_p1_impl_p3]
131 p1.set_steps(self.db_conn, steps_p1)
132 p1_dict[6] = ProcessStepsNode(p3, None, True, {})
133 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
134 self.assertEqual(p1.used_as_step_by(self.db_conn), [])
135 self.assertEqual(p2.used_as_step_by(self.db_conn), [p1])
136 self.assertEqual(p3.used_as_step_by(self.db_conn), [p1, p2])
137 # # add step of process p3 as explicit sub-step to p1's first sub-step,
138 # # expect it to eliminate implicit p3 sub-step
139 # s_p3_to_p1_first_explicit = ProcessStep(None, p1.id_, p3.id_,
141 # p1_dict[1].steps = {7: ProcessStepsNode(p3, 1, True, {})}
142 # p1_dict[2].steps[4].steps[3].seen = False
143 # steps_p1 += [s_p3_to_p1_first_explicit]
144 # p1.set_steps(self.db_conn, steps_p1)
145 # self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
146 # ensure implicit steps non-top explicit steps are shown
147 s_p3_to_p2_first = ProcessStep(None, p2.id_, p3.id_, s_p3_to_p2.id_)
148 steps_p2 += [s_p3_to_p2_first]
149 p2.set_steps(self.db_conn, steps_p2)
150 p1_dict[1].steps[3].steps[7] = ProcessStepsNode(p3, 3, False, {}, True)
151 p1_dict[2].steps[4].steps[3].steps[7] = ProcessStepsNode(p3, 3, False,
153 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
154 # ensure suppressed step nodes are hidden
155 assert isinstance(s_p3_to_p2.id_, int)
156 p1.set_step_suppressions(self.db_conn, [s_p3_to_p2.id_])
157 p1_dict[1].steps[3].steps = {}
158 p1_dict[1].steps[3].is_suppressed = True
159 p1_dict[2].steps[4].steps[3].steps = {}
160 p1_dict[2].steps[4].steps[3].is_suppressed = True
161 self.assertEqual(p1.get_steps(self.db_conn), p1_dict)
163 def test_Process_conditions(self) -> None:
164 """Test setting Process.conditions/enables/disables."""
167 for target in ('conditions', 'enables', 'disables'):
168 method = getattr(p, f'set_{target}')
169 c1, c2 = Condition(None), Condition(None)
170 c1.save(self.db_conn)
171 c2.save(self.db_conn)
172 assert isinstance(c1.id_, int)
173 assert isinstance(c2.id_, int)
174 method(self.db_conn, [])
175 self.assertEqual(getattr(p, target), [])
176 method(self.db_conn, [c1.id_])
177 self.assertEqual(getattr(p, target), [c1])
178 method(self.db_conn, [c2.id_])
179 self.assertEqual(getattr(p, target), [c2])
180 method(self.db_conn, [c1.id_, c2.id_])
181 self.assertEqual(getattr(p, target), [c1, c2])
183 def test_remove(self) -> None:
184 """Test removal of Processes and ProcessSteps."""
185 super().test_remove()
186 p1, p2, p3 = self.three_processes()
187 assert isinstance(p1.id_, int)
188 assert isinstance(p2.id_, int)
189 assert isinstance(p3.id_, int)
190 step = ProcessStep(None, p2.id_, p1.id_, None)
191 p2.set_steps(self.db_conn, [step])
193 with self.assertRaises(HandledException):
194 p1.remove(self.db_conn)
195 p2.set_steps(self.db_conn, [])
196 with self.assertRaises(NotFoundException):
197 assert step_id is not None
198 ProcessStep.by_id(self.db_conn, step_id)
199 p1.remove(self.db_conn)
200 step = ProcessStep(None, p2.id_, p3.id_, None)
201 p2.set_steps(self.db_conn, [step])
203 p2.remove(self.db_conn)
204 with self.assertRaises(NotFoundException):
205 assert step_id is not None
206 ProcessStep.by_id(self.db_conn, step_id)
207 todo = Todo(None, p3, False, '2024-01-01')
208 todo.save(self.db_conn)
209 with self.assertRaises(HandledException):
210 p3.remove(self.db_conn)
211 todo.remove(self.db_conn)
212 p3.remove(self.db_conn)
215 class TestsWithDBForProcessStep(TestCaseWithDB):
216 """Module tests requiring DB setup."""
217 checked_class = ProcessStep
218 default_init_kwargs = {'owner_id': 1, 'step_process_id': 2,
221 def setUp(self) -> None:
224 self.p1.save(self.db_conn)
226 def test_remove(self) -> None:
227 """Test .remove and unsetting of owner's .explicit_steps entry."""
229 p2.save(self.db_conn)
230 assert isinstance(self.p1.id_, int)
231 assert isinstance(p2.id_, int)
232 step = ProcessStep(None, self.p1.id_, p2.id_, None)
233 self.p1.set_steps(self.db_conn, [step])
234 step.remove(self.db_conn)
235 self.assertEqual(self.p1.explicit_steps, [])
236 self.check_identity_with_cache_and_db([])
239 class TestsWithServer(TestCaseWithServer):
240 """Module tests against our HTTP server/handler (and database)."""
242 def test_do_POST_process(self) -> None:
243 """Test POST /process and its effect on the database."""
244 self.assertEqual(0, len(Process.all(self.db_conn)))
245 form_data = self.post_process()
246 self.assertEqual(1, len(Process.all(self.db_conn)))
247 self.check_post(form_data, '/process?id=FOO', 400)
248 self.check_post(form_data | {'effort': 'foo'}, '/process?id=', 400)
249 self.check_post({}, '/process?id=', 400)
250 self.check_post({'title': '', 'description': ''}, '/process?id=', 400)
251 self.check_post({'title': '', 'effort': 1.1}, '/process?id=', 400)
252 self.check_post({'description': '', 'effort': 1.0},
254 self.assertEqual(1, len(Process.all(self.db_conn)))
255 form_data = {'title': 'foo', 'description': 'foo', 'effort': 1.0}
256 self.post_process(2, form_data | {'condition': []})
257 self.check_post(form_data | {'condition': [1]}, '/process?id=', 404)
258 self.check_post({'title': 'foo', 'description': 'foo',
260 '/condition', 302, '/condition?id=1')
261 self.post_process(3, form_data | {'condition': [1]})
262 self.post_process(4, form_data | {'disables': [1]})
263 self.post_process(5, form_data | {'enables': [1]})
264 form_data['delete'] = ''
265 self.check_post(form_data, '/process?id=', 404)
266 self.check_post(form_data, '/process?id=6', 404)
267 self.check_post(form_data, '/process?id=5', 302, '/processes')
269 def test_do_POST_process_steps(self) -> None:
270 """Test behavior of ProcessStep posting."""
271 # pylint: disable=too-many-statements
272 form_data_1 = self.post_process(1)
275 # post first (top-level) step of process 2 to process 1
276 form_data_1['new_top_step'] = [2]
277 self.post_process(1, form_data_1)
278 retrieved_process = Process.by_id(self.db_conn, 1)
279 self.assertEqual(len(retrieved_process.explicit_steps), 1)
280 retrieved_step = retrieved_process.explicit_steps[0]
281 retrieved_step_id = retrieved_step.id_
282 self.assertEqual(retrieved_step.step_process_id, 2)
283 self.assertEqual(retrieved_step.owner_id, 1)
284 self.assertEqual(retrieved_step.parent_step_id, None)
285 # post empty steps list to process, expect clean slate, and old step to
286 # completely disappear
287 form_data_1['new_top_step'] = []
288 self.post_process(1, form_data_1)
289 retrieved_process = Process.by_id(self.db_conn, 1)
290 self.assertEqual(retrieved_process.explicit_steps, [])
291 assert retrieved_step_id is not None
292 with self.assertRaises(NotFoundException):
293 ProcessStep.by_id(self.db_conn, retrieved_step_id)
294 # post new first (top_level) step of process 3 to process 1
295 form_data_1['new_top_step'] = [3]
296 self.post_process(1, form_data_1)
297 retrieved_process = Process.by_id(self.db_conn, 1)
298 retrieved_step = retrieved_process.explicit_steps[0]
299 self.assertEqual(retrieved_step.step_process_id, 3)
300 self.assertEqual(retrieved_step.owner_id, 1)
301 self.assertEqual(retrieved_step.parent_step_id, None)
302 # post to process steps list without keeps, expect clean slate
303 form_data_1['new_top_step'] = []
304 form_data_1['steps'] = [retrieved_step.id_]
305 self.post_process(1, form_data_1)
306 retrieved_process = Process.by_id(self.db_conn, 1)
307 self.assertEqual(retrieved_process.explicit_steps, [])
308 # post to process empty steps list but keep, expect 400
309 form_data_1['steps'] = []
310 form_data_1['keep_step'] = [retrieved_step_id]
311 self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1')
312 # post to process steps list with keep on non-created step, expect 400
313 form_data_1['steps'] = [retrieved_step_id]
314 form_data_1['keep_step'] = [retrieved_step_id]
315 self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1')
316 # post to process steps list with keep and process ID, expect 200
317 form_data_1[f'step_{retrieved_step_id}_process_id'] = [2]
318 self.post_process(1, form_data_1)
319 retrieved_process = Process.by_id(self.db_conn, 1)
320 self.assertEqual(len(retrieved_process.explicit_steps), 1)
321 retrieved_step = retrieved_process.explicit_steps[0]
322 self.assertEqual(retrieved_step.step_process_id, 2)
323 self.assertEqual(retrieved_step.owner_id, 1)
324 self.assertEqual(retrieved_step.parent_step_id, None)
325 # post nonsense, expect 400 and preservation of previous state
326 form_data_1['steps'] = ['foo']
327 form_data_1['keep_step'] = []
328 self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1')
329 retrieved_process = Process.by_id(self.db_conn, 1)
330 self.assertEqual(len(retrieved_process.explicit_steps), 1)
331 retrieved_step = retrieved_process.explicit_steps[0]
332 self.assertEqual(retrieved_step.step_process_id, 2)
333 self.assertEqual(retrieved_step.owner_id, 1)
334 self.assertEqual(retrieved_step.parent_step_id, None)
335 # post to process steps list with keep and process ID, expect 200
336 form_data_1['new_top_step'] = [3]
337 form_data_1['steps'] = [retrieved_step.id_]
338 form_data_1['keep_step'] = [retrieved_step.id_]
339 self.post_process(1, form_data_1)
340 retrieved_process = Process.by_id(self.db_conn, 1)
341 self.assertEqual(len(retrieved_process.explicit_steps), 2)
342 retrieved_step_0 = retrieved_process.explicit_steps[0]
343 self.assertEqual(retrieved_step_0.step_process_id, 3)
344 self.assertEqual(retrieved_step_0.owner_id, 1)
345 self.assertEqual(retrieved_step_0.parent_step_id, None)
346 retrieved_step_1 = retrieved_process.explicit_steps[1]
347 self.assertEqual(retrieved_step_1.step_process_id, 2)
348 self.assertEqual(retrieved_step_1.owner_id, 1)
349 self.assertEqual(retrieved_step_1.parent_step_id, None)
350 # post to process steps list with keeps etc., but trigger recursion
351 form_data_1['new_top_step'] = []
352 form_data_1['steps'] = [retrieved_step_0.id_, retrieved_step_1.id_]
353 form_data_1['keep_step'] = [retrieved_step_0.id_, retrieved_step_1.id_]
354 form_data_1[f'step_{retrieved_step_0.id_}_process_id'] = [2]
355 form_data_1[f'step_{retrieved_step_1.id_}_process_id'] = [1]
356 self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1')
357 # check previous status preserved despite failed steps setting
358 retrieved_process = Process.by_id(self.db_conn, 1)
359 self.assertEqual(len(retrieved_process.explicit_steps), 2)
360 retrieved_step_0 = retrieved_process.explicit_steps[0]
361 self.assertEqual(retrieved_step_0.step_process_id, 2)
362 self.assertEqual(retrieved_step_0.owner_id, 1)
363 self.assertEqual(retrieved_step_0.parent_step_id, None)
364 retrieved_step_1 = retrieved_process.explicit_steps[1]
365 self.assertEqual(retrieved_step_1.step_process_id, 3)
366 self.assertEqual(retrieved_step_1.owner_id, 1)
367 self.assertEqual(retrieved_step_1.parent_step_id, None)
368 # post sub-step to step
369 form_data_1[f'step_{retrieved_step_0.id_}_process_id'] = [3]
370 form_data_1[f'new_step_to_{retrieved_step_0.id_}'] = [3]
371 self.post_process(1, form_data_1)
372 retrieved_process = Process.by_id(self.db_conn, 1)
373 self.assertEqual(len(retrieved_process.explicit_steps), 3)
374 retrieved_step_0 = retrieved_process.explicit_steps[0]
375 self.assertEqual(retrieved_step_0.step_process_id, 2)
376 self.assertEqual(retrieved_step_0.owner_id, 1)
377 self.assertEqual(retrieved_step_0.parent_step_id, None)
378 retrieved_step_1 = retrieved_process.explicit_steps[1]
379 self.assertEqual(retrieved_step_1.step_process_id, 3)
380 self.assertEqual(retrieved_step_1.owner_id, 1)
381 self.assertEqual(retrieved_step_1.parent_step_id, None)
382 retrieved_step_2 = retrieved_process.explicit_steps[2]
383 self.assertEqual(retrieved_step_2.step_process_id, 3)
384 self.assertEqual(retrieved_step_2.owner_id, 1)
385 self.assertEqual(retrieved_step_2.parent_step_id, retrieved_step_1.id_)
387 def test_do_GET(self) -> None:
388 """Test /process and /processes response codes."""
389 self.check_get_defaults('/process')
390 self.check_get('/processes', 200)