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()
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_Process_from_table_row(self) -> None:
68 """Test .from_table_row() properly reads in class from DB"""
69 self.check_from_table_row()
70 self.check_versioned_from_table_row('title', str)
71 self.check_versioned_from_table_row('description', str)
72 self.check_versioned_from_table_row('effort', float)
73 p, set1, set2, set3 = self.p_of_conditions()
75 assert isinstance(p.id_, int)
76 for row in self.db_conn.row_where(self.checked_class.table_name,
78 # pylint: disable=no-member
79 r = Process.from_table_row(self.db_conn, row)
80 self.assertEqual(sorted(r.conditions), sorted(set1))
81 self.assertEqual(sorted(r.enables), sorted(set2))
82 self.assertEqual(sorted(r.disables), sorted(set3))
84 def test_Process_steps(self) -> None:
85 """Test addition, nesting, and non-recursion of ProcessSteps"""
86 # pylint: disable=too-many-locals
87 # pylint: disable=too-many-statements
88 p1, p2, p3 = self.three_processes()
89 assert isinstance(p1.id_, int)
90 assert isinstance(p2.id_, int)
91 assert isinstance(p3.id_, int)
92 steps_p1: list[ProcessStep] = []
93 # add step of process p2 as first (top-level) step to p1
94 s_p2_to_p1 = ProcessStep(None, p1.id_, p2.id_, None)
95 steps_p1 += [s_p2_to_p1]
96 p1.set_steps(self.db_conn, steps_p1)
97 p1_dict: dict[int, ProcessStepsNode] = {}
98 p1_dict[1] = ProcessStepsNode(p2, None, True, {})
99 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
100 # add step of process p3 as second (top-level) step to p1
101 s_p3_to_p1 = ProcessStep(None, p1.id_, p3.id_, None)
102 steps_p1 += [s_p3_to_p1]
103 p1.set_steps(self.db_conn, steps_p1)
104 p1_dict[2] = ProcessStepsNode(p3, None, True, {})
105 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
106 # add step of process p3 as first (top-level) step to p2,
107 steps_p2: list[ProcessStep] = []
108 s_p3_to_p2 = ProcessStep(None, p2.id_, p3.id_, None)
109 steps_p2 += [s_p3_to_p2]
110 p2.set_steps(self.db_conn, steps_p2)
111 # expect it as implicit sub-step of p1's second (p3) step
112 p2_dict = {3: ProcessStepsNode(p3, None, False, {})}
113 p1_dict[1].steps[3] = p2_dict[3]
114 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
115 # add step of process p2 as explicit sub-step to p1's second sub-step
116 s_p2_to_p1_first = ProcessStep(None, p1.id_, p2.id_, s_p3_to_p1.id_)
117 steps_p1 += [s_p2_to_p1_first]
118 p1.set_steps(self.db_conn, steps_p1)
119 seen_3 = ProcessStepsNode(p3, None, False, {}, True)
120 p1_dict[2].steps[4] = ProcessStepsNode(p2, s_p3_to_p1.id_, True,
122 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
123 # add step of process p3 as explicit sub-step to non-existing p1
124 # sub-step (of id=999), expect it to become another p1 top-level step
125 s_p3_to_p1_999 = ProcessStep(None, p1.id_, p3.id_, 999)
126 steps_p1 += [s_p3_to_p1_999]
127 p1.set_steps(self.db_conn, steps_p1)
128 p1_dict[5] = ProcessStepsNode(p3, None, True, {})
129 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
130 # add step of process p3 as explicit sub-step to p1's implicit p3
131 # sub-step, expect it to become another p1 top-level step
132 s_p3_to_p1_impl_p3 = ProcessStep(None, p1.id_, p3.id_, s_p3_to_p2.id_)
133 steps_p1 += [s_p3_to_p1_impl_p3]
134 p1.set_steps(self.db_conn, steps_p1)
135 p1_dict[6] = ProcessStepsNode(p3, None, True, {})
136 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
137 self.assertEqual(p1.used_as_step_by(self.db_conn), [])
138 self.assertEqual(p2.used_as_step_by(self.db_conn), [p1])
139 self.assertEqual(p3.used_as_step_by(self.db_conn), [p1, p2])
140 # # add step of process p3 as explicit sub-step to p1's first sub-step,
141 # # expect it to eliminate implicit p3 sub-step
142 # s_p3_to_p1_first_explicit = ProcessStep(None, p1.id_, p3.id_,
144 # p1_dict[1].steps = {7: ProcessStepsNode(p3, 1, True, {})}
145 # p1_dict[2].steps[4].steps[3].seen = False
146 # steps_p1 += [s_p3_to_p1_first_explicit]
147 # p1.set_steps(self.db_conn, steps_p1)
148 # self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
149 # ensure implicit steps non-top explicit steps are shown
150 s_p3_to_p2_first = ProcessStep(None, p2.id_, p3.id_, s_p3_to_p2.id_)
151 steps_p2 += [s_p3_to_p2_first]
152 p2.set_steps(self.db_conn, steps_p2)
153 p1_dict[1].steps[3].steps[7] = ProcessStepsNode(p3, 3, False, {})
154 p1_dict[2].steps[4].steps[3].steps[7] = ProcessStepsNode(p3, 3, False,
156 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
157 # ensure suppressed step nodes are hidden
158 assert isinstance(s_p3_to_p2.id_, int)
159 p1.set_step_suppressions(self.db_conn, [s_p3_to_p2.id_])
160 p1_dict[1].steps[3].steps = {}
161 p1_dict[1].steps[3].is_suppressed = True
162 p1_dict[2].steps[4].steps[3].steps = {}
163 p1_dict[2].steps[4].steps[3].is_suppressed = True
164 self.assertEqual(p1.get_steps(self.db_conn), p1_dict)
166 def test_Process_conditions(self) -> None:
167 """Test setting Process.conditions/enables/disables."""
170 for target in ('conditions', 'enables', 'disables'):
171 method = getattr(p, f'set_{target}')
172 c1, c2 = Condition(None), Condition(None)
173 c1.save(self.db_conn)
174 c2.save(self.db_conn)
175 assert isinstance(c1.id_, int)
176 assert isinstance(c2.id_, int)
177 method(self.db_conn, [])
178 self.assertEqual(getattr(p, target), [])
179 method(self.db_conn, [c1.id_])
180 self.assertEqual(getattr(p, target), [c1])
181 method(self.db_conn, [c2.id_])
182 self.assertEqual(getattr(p, target), [c2])
183 method(self.db_conn, [c1.id_, c2.id_])
184 self.assertEqual(getattr(p, target), [c1, c2])
186 def test_Process_by_id(self) -> None:
187 """Test .by_id(), including creation"""
190 def test_Process_all(self) -> None:
194 def test_Process_singularity(self) -> None:
195 """Test pointers made for single object keep pointing to it."""
196 self.check_singularity('conditions', [Condition(None)])
198 def test_Process_versioned_attributes_singularity(self) -> None:
199 """Test behavior of VersionedAttributes on saving (with .title)."""
200 self.check_versioned_singularity()
202 def test_Process_removal(self) -> None:
203 """Test removal of Processes and ProcessSteps."""
205 p1, p2, p3 = self.three_processes()
206 assert isinstance(p1.id_, int)
207 assert isinstance(p2.id_, int)
208 assert isinstance(p3.id_, int)
209 step = ProcessStep(None, p2.id_, p1.id_, None)
210 p2.set_steps(self.db_conn, [step])
211 with self.assertRaises(HandledException):
212 p1.remove(self.db_conn)
213 p2.set_steps(self.db_conn, [])
214 with self.assertRaises(NotFoundException):
215 ProcessStep.by_id(self.db_conn, step.id_)
216 p1.remove(self.db_conn)
217 step = ProcessStep(None, p2.id_, p3.id_, None)
218 p2.set_steps(self.db_conn, [step])
219 p2.remove(self.db_conn)
220 with self.assertRaises(NotFoundException):
221 ProcessStep.by_id(self.db_conn, step.id_)
222 todo = Todo(None, p3, False, '2024-01-01')
223 todo.save(self.db_conn)
224 with self.assertRaises(HandledException):
225 p3.remove(self.db_conn)
226 todo.remove(self.db_conn)
227 p3.remove(self.db_conn)
230 class TestsWithDBForProcessStep(TestCaseWithDB):
231 """Module tests requiring DB setup."""
232 checked_class = ProcessStep
233 default_init_kwargs = {'owner_id': 2, 'step_process_id': 3,
236 def test_ProcessStep_from_table_row(self) -> None:
237 """Test .from_table_row() properly reads in class from DB"""
238 self.check_from_table_row(2, 3, None)
240 def test_ProcessStep_singularity(self) -> None:
241 """Test pointers made for single object keep pointing to it."""
242 self.check_singularity('parent_step_id', 1, 2, 3, None)
244 def test_ProcessStep_remove(self) -> None:
245 """Test .remove and unsetting of owner's .explicit_steps entry."""
248 p1.save(self.db_conn)
249 p2.save(self.db_conn)
250 assert isinstance(p1.id_, int)
251 assert isinstance(p2.id_, int)
252 step = ProcessStep(None, p1.id_, p2.id_, None)
253 p1.set_steps(self.db_conn, [step])
254 step.remove(self.db_conn)
255 self.assertEqual(p1.explicit_steps, [])
256 self.check_storage([])
259 class TestsWithServer(TestCaseWithServer):
260 """Module tests against our HTTP server/handler (and database)."""
262 def test_do_POST_process(self) -> None:
263 """Test POST /process and its effect on the database."""
264 self.assertEqual(0, len(Process.all(self.db_conn)))
265 form_data = self.post_process()
266 self.assertEqual(1, len(Process.all(self.db_conn)))
267 self.check_post(form_data, '/process?id=FOO', 400)
268 self.check_post(form_data | {'effort': 'foo'}, '/process?id=', 400)
269 self.check_post({}, '/process?id=', 400)
270 self.check_post({'title': '', 'description': ''}, '/process?id=', 400)
271 self.check_post({'title': '', 'effort': 1.1}, '/process?id=', 400)
272 self.check_post({'description': '', 'effort': 1.0},
274 self.assertEqual(1, len(Process.all(self.db_conn)))
275 form_data = {'title': 'foo', 'description': 'foo', 'effort': 1.0}
276 self.post_process(2, form_data | {'condition': []})
277 self.check_post(form_data | {'condition': [1]}, '/process?id=', 404)
278 self.check_post({'title': 'foo', 'description': 'foo'},
279 '/condition', 302, '/condition?id=1')
280 self.post_process(3, form_data | {'condition': [1]})
281 self.post_process(4, form_data | {'disables': [1]})
282 self.post_process(5, form_data | {'enables': [1]})
283 form_data['delete'] = ''
284 self.check_post(form_data, '/process?id=', 404)
285 self.check_post(form_data, '/process?id=6', 404)
286 self.check_post(form_data, '/process?id=5', 302, '/processes')
288 def test_do_POST_process_steps(self) -> None:
289 """Test behavior of ProcessStep posting."""
290 # pylint: disable=too-many-statements
291 form_data_1 = self.post_process(1)
294 # post first (top-level) step of process 2 to process 1
295 form_data_1['new_top_step'] = [2]
296 self.post_process(1, form_data_1)
297 retrieved_process = Process.by_id(self.db_conn, 1)
298 self.assertEqual(len(retrieved_process.explicit_steps), 1)
299 retrieved_step = retrieved_process.explicit_steps[0]
300 self.assertEqual(retrieved_step.step_process_id, 2)
301 self.assertEqual(retrieved_step.owner_id, 1)
302 self.assertEqual(retrieved_step.parent_step_id, None)
303 # post empty steps list to process, expect clean slate, and old step to
304 # completely disappear
305 form_data_1['new_top_step'] = []
306 self.post_process(1, form_data_1)
307 retrieved_process = Process.by_id(self.db_conn, 1)
308 self.assertEqual(retrieved_process.explicit_steps, [])
309 with self.assertRaises(NotFoundException):
310 ProcessStep.by_id(self.db_conn, retrieved_step.id_)
311 # post new first (top_level) step of process 3 to process 1
312 form_data_1['new_top_step'] = [3]
313 self.post_process(1, form_data_1)
314 retrieved_process = Process.by_id(self.db_conn, 1)
315 retrieved_step = retrieved_process.explicit_steps[0]
316 self.assertEqual(retrieved_step.step_process_id, 3)
317 self.assertEqual(retrieved_step.owner_id, 1)
318 self.assertEqual(retrieved_step.parent_step_id, None)
319 # post to process steps list without keeps, expect clean slate
320 form_data_1['new_top_step'] = []
321 form_data_1['steps'] = [retrieved_step.id_]
322 self.post_process(1, form_data_1)
323 retrieved_process = Process.by_id(self.db_conn, 1)
324 self.assertEqual(retrieved_process.explicit_steps, [])
325 # post to process empty steps list but keep, expect 400
326 form_data_1['steps'] = []
327 form_data_1['keep_step'] = [retrieved_step.id_]
328 self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1')
329 # post to process steps list with keep on non-created step, expect 400
330 form_data_1['steps'] = [retrieved_step.id_]
331 form_data_1['keep_step'] = [retrieved_step.id_]
332 self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1')
333 # post to process steps list with keep and process ID, expect 200
334 form_data_1[f'step_{retrieved_step.id_}_process_id'] = [2]
335 self.post_process(1, form_data_1)
336 retrieved_process = Process.by_id(self.db_conn, 1)
337 self.assertEqual(len(retrieved_process.explicit_steps), 1)
338 retrieved_step = retrieved_process.explicit_steps[0]
339 self.assertEqual(retrieved_step.step_process_id, 2)
340 self.assertEqual(retrieved_step.owner_id, 1)
341 self.assertEqual(retrieved_step.parent_step_id, None)
342 # post nonsensical new_top_step id and otherwise zero'd steps, expect
343 # 400 and preservation of previous state
344 form_data_1['new_top_step'] = ['foo']
345 form_data_1['steps'] = []
346 form_data_1['keep_step'] = []
347 self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1')
348 retrieved_process = Process.by_id(self.db_conn, 1)
349 self.assertEqual(len(retrieved_process.explicit_steps), 1)
350 retrieved_step = retrieved_process.explicit_steps[0]
351 self.assertEqual(retrieved_step.step_process_id, 2)
352 self.assertEqual(retrieved_step.owner_id, 1)
353 self.assertEqual(retrieved_step.parent_step_id, None)
354 # post to process steps list with keep and process ID, expect 200
355 form_data_1['new_top_step'] = [3]
356 form_data_1['steps'] = [retrieved_step.id_]
357 form_data_1['keep_step'] = [retrieved_step.id_]
358 self.post_process(1, form_data_1)
359 retrieved_process = Process.by_id(self.db_conn, 1)
360 self.assertEqual(len(retrieved_process.explicit_steps), 2)
361 retrieved_step_0 = retrieved_process.explicit_steps[0]
362 self.assertEqual(retrieved_step_0.step_process_id, 2)
363 self.assertEqual(retrieved_step_0.owner_id, 1)
364 self.assertEqual(retrieved_step_0.parent_step_id, None)
365 retrieved_step_1 = retrieved_process.explicit_steps[1]
366 self.assertEqual(retrieved_step_1.step_process_id, 3)
367 self.assertEqual(retrieved_step_1.owner_id, 1)
368 self.assertEqual(retrieved_step_1.parent_step_id, None)
369 # post to process steps list with keeps etc., but trigger recursion
370 form_data_1['new_top_step'] = []
371 form_data_1['steps'] = [retrieved_step_0.id_, retrieved_step_1.id_]
372 form_data_1['keep_step'] = [retrieved_step_0.id_, retrieved_step_1.id_]
373 form_data_1[f'step_{retrieved_step_0.id_}_process_id'] = [2]
374 form_data_1[f'step_{retrieved_step_1.id_}_process_id'] = [1]
375 self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1')
376 # check previous status preserved despite failed steps setting
377 retrieved_process = Process.by_id(self.db_conn, 1)
378 self.assertEqual(len(retrieved_process.explicit_steps), 2)
379 retrieved_step_0 = retrieved_process.explicit_steps[0]
380 self.assertEqual(retrieved_step_0.step_process_id, 2)
381 self.assertEqual(retrieved_step_0.owner_id, 1)
382 self.assertEqual(retrieved_step_0.parent_step_id, None)
383 retrieved_step_1 = retrieved_process.explicit_steps[1]
384 self.assertEqual(retrieved_step_1.step_process_id, 3)
385 self.assertEqual(retrieved_step_1.owner_id, 1)
386 self.assertEqual(retrieved_step_1.parent_step_id, None)
387 form_data_1[f'step_{retrieved_step_1.id_}_process_id'] = [3]
388 # post sub-step to step
389 form_data_1[f'new_step_to_{retrieved_step_1.id_}'] = [3]
390 self.post_process(1, form_data_1)
391 retrieved_process = Process.by_id(self.db_conn, 1)
392 self.assertEqual(len(retrieved_process.explicit_steps), 3)
393 retrieved_step_0 = retrieved_process.explicit_steps[0]
394 self.assertEqual(retrieved_step_0.step_process_id, 2)
395 self.assertEqual(retrieved_step_0.owner_id, 1)
396 self.assertEqual(retrieved_step_0.parent_step_id, None)
397 retrieved_step_1 = retrieved_process.explicit_steps[1]
398 self.assertEqual(retrieved_step_1.step_process_id, 3)
399 self.assertEqual(retrieved_step_1.owner_id, 1)
400 self.assertEqual(retrieved_step_1.parent_step_id, None)
401 retrieved_step_2 = retrieved_process.explicit_steps[2]
402 self.assertEqual(retrieved_step_2.step_process_id, 3)
403 self.assertEqual(retrieved_step_2.owner_id, 1)
404 self.assertEqual(retrieved_step_2.parent_step_id, retrieved_step_1.id_)
406 def test_do_GET(self) -> None:
407 """Test /process and /processes response codes."""
408 self.check_get_defaults('/process')
409 self.check_get('/processes', 200)