home · contact · privacy
Fix bug of relationships writing for todo_children writing in wrong column.
[plomtask] / tests / processes.py
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
7
8
9 class TestsSansDB(TestCaseSansDB):
10     """Module tests not requiring DB setup."""
11     checked_class = Process
12     do_id_test = True
13     versioned_defaults_to_test = {'title': 'UNNAMED', 'description': '',
14                                   'effort': 1.0}
15
16
17 class TestsSansDBProcessStep(TestCaseSansDB):
18     """Module tests not requiring DB setup."""
19     checked_class = ProcessStep
20     do_id_test = True
21     default_init_args = [2, 3, 4]
22
23
24 class TestsWithDB(TestCaseWithDB):
25     """Module tests requiring DB setup."""
26     checked_class = Process
27     test_versioneds = {'title': str, 'description': str, 'effort': float}
28
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]:
33             p.save(self.db_conn)
34         return p1, p2, p3
35
36     def p_of_conditions(self) -> tuple[Process, list[Condition],
37                                        list[Condition], list[Condition]]:
38         """Return Process and its three Condition sets."""
39         p = Process(None)
40         c1, c2, c3 = Condition(None), Condition(None), Condition(None)
41         for c in [c1, c2, c3]:
42             c.save(self.db_conn)
43         assert isinstance(c1.id_, int)
44         assert isinstance(c2.id_, int)
45         assert isinstance(c3.id_, int)
46         set_1 = [c1, c2]
47         set_2 = [c2, c3]
48         set_3 = [c1, c3]
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)])
55         p.save(self.db_conn)
56         return p, set_1, set_2, set_3
57
58     def test_Process_conditions_saving(self) -> None:
59         """Test .save/.save_core."""
60         p, set1, set2, set3 = self.p_of_conditions()
61         p.uncache()
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))
66
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()
74         p.save(self.db_conn)
75         assert isinstance(p.id_, int)
76         for row in self.db_conn.row_where(self.checked_class.table_name,
77                                           'id', p.id_):
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))
83
84     def test_Process_steps(self) -> None:
85         """Test addition, nesting, and non-recursion of ProcessSteps"""
86         def add_step(proc: Process,
87                      steps_proc: list[tuple[int | None, int, int | None]],
88                      step_tuple: tuple[int | None, int, int | None],
89                      expected_id: int) -> None:
90             steps_proc += [step_tuple]
91             proc.set_steps(self.db_conn, steps_proc)
92             steps_proc[-1] = (expected_id, step_tuple[1], step_tuple[2])
93         p1, p2, p3 = self.three_processes()
94         assert isinstance(p1.id_, int)
95         assert isinstance(p2.id_, int)
96         assert isinstance(p3.id_, int)
97         steps_p1: list[tuple[int | None, int, int | None]] = []
98         # add step of process p2 as first (top-level) step to p1
99         add_step(p1, steps_p1, (None, p2.id_, None), 1)
100         p1_dict: dict[int, ProcessStepsNode] = {}
101         p1_dict[1] = ProcessStepsNode(p2, None, True, {}, False)
102         self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
103         # add step of process p3 as second (top-level) step to p1
104         add_step(p1, steps_p1, (None, p3.id_, None), 2)
105         step_2 = p1.explicit_steps[-1]
106         p1_dict[2] = ProcessStepsNode(p3, None, True, {}, False)
107         self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
108         # add step of process p3 as first (top-level) step to p2,
109         # expect it as implicit sub-step of p1's second (p3) step
110         steps_p2: list[tuple[int | None, int, int | None]] = []
111         add_step(p2, steps_p2, (None, p3.id_, None), 3)
112         p1_dict[1].steps[3] = ProcessStepsNode(p3, None, False, {}, False)
113         self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
114         # add step of process p2 as explicit sub-step to p1's first sub-step
115         add_step(p1, steps_p1, (None, p2.id_, step_2.id_), 4)
116         step_3 = ProcessStepsNode(p3, None, False, {}, True)
117         p1_dict[2].steps[4] = ProcessStepsNode(p2, step_2.id_, True,
118                                                {3: step_3}, False)
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         add_step(p1, steps_p1, (None, p3.id_, 999), 5)
123         p1_dict[5] = ProcessStepsNode(p3, None, True, {}, False)
124         self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
125         # add step of process p3 as explicit sub-step to p1's implicit p3
126         # sub-step, expect it to become another p1 top-level step
127         add_step(p1, steps_p1, (None, p3.id_, 3), 6)
128         p1_dict[6] = ProcessStepsNode(p3, None, True, {}, False)
129         self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
130         self.assertEqual(p1.used_as_step_by(self.db_conn), [])
131         self.assertEqual(p2.used_as_step_by(self.db_conn), [p1])
132         self.assertEqual(p3.used_as_step_by(self.db_conn), [p1, p2])
133         # add step of process p2 as explicit sub-step to p1's second (p3)
134         # top-level step
135         add_step(p1, steps_p1, (None, p3.id_, 2), 7)
136         p1_dict[2].steps[7] = ProcessStepsNode(p3, 2, True, {}, False)
137         # import pprint
138         # pprint.pp(p1.get_steps(self.db_conn, None))
139         # pprint.pp(p1_dict)
140         self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
141
142     def test_Process_conditions(self) -> None:
143         """Test setting Process.conditions/enables/disables."""
144         p = Process(None)
145         p.save(self.db_conn)
146         for target in ('conditions', 'enables', 'disables'):
147             method = getattr(p, f'set_{target}')
148             c1, c2 = Condition(None), Condition(None)
149             c1.save(self.db_conn)
150             c2.save(self.db_conn)
151             assert isinstance(c1.id_, int)
152             assert isinstance(c2.id_, int)
153             method(self.db_conn, [])
154             self.assertEqual(getattr(p, target), [])
155             method(self.db_conn, [c1.id_])
156             self.assertEqual(getattr(p, target), [c1])
157             method(self.db_conn, [c2.id_])
158             self.assertEqual(getattr(p, target), [c2])
159             method(self.db_conn, [c1.id_, c2.id_])
160             self.assertEqual(getattr(p, target), [c1, c2])
161
162     def test_Process_by_id(self) -> None:
163         """Test .by_id(), including creation"""
164         self.check_by_id()
165
166     def test_Process_all(self) -> None:
167         """Test .all()."""
168         self.check_all()
169
170     def test_Process_singularity(self) -> None:
171         """Test pointers made for single object keep pointing to it."""
172         self.check_singularity('conditions', [Condition(None)])
173
174     def test_Process_versioned_attributes_singularity(self) -> None:
175         """Test behavior of VersionedAttributes on saving (with .title)."""
176         self.check_versioned_singularity()
177
178     def test_Process_removal(self) -> None:
179         """Test removal of Processes and ProcessSteps."""
180         self.check_remove()
181         p1, p2, p3 = self.three_processes()
182         assert isinstance(p1.id_, int)
183         assert isinstance(p2.id_, int)
184         assert isinstance(p3.id_, int)
185         p2.set_steps(self.db_conn, [(None, p1.id_, None)])
186         with self.assertRaises(HandledException):
187             p1.remove(self.db_conn)
188         step = p2.explicit_steps[0]
189         p2.set_steps(self.db_conn, [])
190         with self.assertRaises(NotFoundException):
191             ProcessStep.by_id(self.db_conn, step.id_)
192         p1.remove(self.db_conn)
193         p2.set_steps(self.db_conn, [(None, p3.id_, None)])
194         step = p2.explicit_steps[0]
195         p2.remove(self.db_conn)
196         with self.assertRaises(NotFoundException):
197             ProcessStep.by_id(self.db_conn, step.id_)
198         todo = Todo(None, p3, False, '2024-01-01')
199         todo.save(self.db_conn)
200         with self.assertRaises(HandledException):
201             p3.remove(self.db_conn)
202         todo.remove(self.db_conn)
203         p3.remove(self.db_conn)
204
205
206 class TestsWithDBForProcessStep(TestCaseWithDB):
207     """Module tests requiring DB setup."""
208     checked_class = ProcessStep
209     default_init_kwargs = {'owner_id': 2, 'step_process_id': 3,
210                            'parent_step_id': 4}
211
212     def test_ProcessStep_from_table_row(self) -> None:
213         """Test .from_table_row() properly reads in class from DB"""
214         self.check_from_table_row(2, 3, None)
215
216     def test_ProcessStep_singularity(self) -> None:
217         """Test pointers made for single object keep pointing to it."""
218         self.check_singularity('parent_step_id', 1, 2, 3, None)
219
220     def test_ProcessStep_remove(self) -> None:
221         """Test .remove and unsetting of owner's .explicit_steps entry."""
222         p1 = Process(None)
223         p2 = Process(None)
224         p1.save(self.db_conn)
225         p2.save(self.db_conn)
226         assert isinstance(p2.id_, int)
227         p1.set_steps(self.db_conn, [(None, p2.id_, None)])
228         step = p1.explicit_steps[0]
229         step.remove(self.db_conn)
230         self.assertEqual(p1.explicit_steps, [])
231         self.check_storage([])
232
233
234 class TestsWithServer(TestCaseWithServer):
235     """Module tests against our HTTP server/handler (and database)."""
236
237     def test_do_POST_process(self) -> None:
238         """Test POST /process and its effect on the database."""
239         self.assertEqual(0, len(Process.all(self.db_conn)))
240         form_data = self.post_process()
241         self.assertEqual(1, len(Process.all(self.db_conn)))
242         self.check_post(form_data, '/process?id=FOO', 400)
243         self.check_post(form_data | {'effort': 'foo'}, '/process?id=', 400)
244         self.check_post({}, '/process?id=', 400)
245         self.check_post({'title': '', 'description': ''}, '/process?id=', 400)
246         self.check_post({'title': '', 'effort': 1.1}, '/process?id=', 400)
247         self.check_post({'description': '', 'effort': 1.0},
248                         '/process?id=', 400)
249         self.assertEqual(1, len(Process.all(self.db_conn)))
250         form_data = {'title': 'foo', 'description': 'foo', 'effort': 1.0}
251         self.post_process(2, form_data | {'condition': []})
252         self.check_post(form_data | {'condition': [1]}, '/process?id=', 404)
253         self.check_post({'title': 'foo', 'description': 'foo'},
254                         '/condition', 302, '/condition?id=1')
255         self.post_process(3, form_data | {'condition': [1]})
256         self.post_process(4, form_data | {'disables': [1]})
257         self.post_process(5, form_data | {'enables': [1]})
258         form_data['delete'] = ''
259         self.check_post(form_data, '/process?id=', 404)
260         self.check_post(form_data, '/process?id=6', 404)
261         self.check_post(form_data, '/process?id=5', 302, '/processes')
262
263     def test_do_POST_process_steps(self) -> None:
264         """Test behavior of ProcessStep posting."""
265         # pylint: disable=too-many-statements
266         form_data_1 = self.post_process(1)
267         self.post_process(2)
268         self.post_process(3)
269         # post first (top-level) step of process 2 to process 1
270         form_data_1['new_top_step'] = [2]
271         self.post_process(1, form_data_1)
272         retrieved_process = Process.by_id(self.db_conn, 1)
273         self.assertEqual(len(retrieved_process.explicit_steps), 1)
274         retrieved_step = retrieved_process.explicit_steps[0]
275         self.assertEqual(retrieved_step.step_process_id, 2)
276         self.assertEqual(retrieved_step.owner_id, 1)
277         self.assertEqual(retrieved_step.parent_step_id, None)
278         # post empty steps list to process, expect clean slate, and old step to
279         # completely disappear
280         form_data_1['new_top_step'] = []
281         self.post_process(1, form_data_1)
282         retrieved_process = Process.by_id(self.db_conn, 1)
283         self.assertEqual(retrieved_process.explicit_steps, [])
284         with self.assertRaises(NotFoundException):
285             ProcessStep.by_id(self.db_conn, retrieved_step.id_)
286         # post new first (top_level) step of process 3 to process 1
287         form_data_1['new_top_step'] = [3]
288         self.post_process(1, form_data_1)
289         retrieved_process = Process.by_id(self.db_conn, 1)
290         retrieved_step = retrieved_process.explicit_steps[0]
291         self.assertEqual(retrieved_step.step_process_id, 3)
292         self.assertEqual(retrieved_step.owner_id, 1)
293         self.assertEqual(retrieved_step.parent_step_id, None)
294         # post to process steps list without keeps, expect clean slate
295         form_data_1['new_top_step'] = []
296         form_data_1['steps'] = [retrieved_step.id_]
297         self.post_process(1, form_data_1)
298         retrieved_process = Process.by_id(self.db_conn, 1)
299         self.assertEqual(retrieved_process.explicit_steps, [])
300         # post to process empty steps list but keep, expect 400
301         form_data_1['steps'] = []
302         form_data_1['keep_step'] = [retrieved_step.id_]
303         self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1')
304         # post to process steps list with keep on non-created step, expect 400
305         form_data_1['steps'] = [retrieved_step.id_]
306         form_data_1['keep_step'] = [retrieved_step.id_]
307         self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1')
308         # post to process steps list with keep and process ID, expect 200
309         form_data_1[f'step_{retrieved_step.id_}_process_id'] = [2]
310         self.post_process(1, form_data_1)
311         retrieved_process = Process.by_id(self.db_conn, 1)
312         self.assertEqual(len(retrieved_process.explicit_steps), 1)
313         retrieved_step = retrieved_process.explicit_steps[0]
314         self.assertEqual(retrieved_step.step_process_id, 2)
315         self.assertEqual(retrieved_step.owner_id, 1)
316         self.assertEqual(retrieved_step.parent_step_id, None)
317         # post nonsensical new_top_step id and otherwise zero'd steps, expect
318         # 400 and preservation of previous state
319         form_data_1['new_top_step'] = ['foo']
320         form_data_1['steps'] = []
321         form_data_1['keep_step'] = []
322         self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1')
323         retrieved_process = Process.by_id(self.db_conn, 1)
324         self.assertEqual(len(retrieved_process.explicit_steps), 1)
325         retrieved_step = retrieved_process.explicit_steps[0]
326         self.assertEqual(retrieved_step.step_process_id, 2)
327         self.assertEqual(retrieved_step.owner_id, 1)
328         self.assertEqual(retrieved_step.parent_step_id, None)
329         # post to process steps list with keep and process ID, expect 200
330         form_data_1['new_top_step'] = [3]
331         form_data_1['steps'] = [retrieved_step.id_]
332         form_data_1['keep_step'] = [retrieved_step.id_]
333         self.post_process(1, form_data_1)
334         retrieved_process = Process.by_id(self.db_conn, 1)
335         self.assertEqual(len(retrieved_process.explicit_steps), 2)
336         retrieved_step_0 = retrieved_process.explicit_steps[0]
337         self.assertEqual(retrieved_step_0.step_process_id, 2)
338         self.assertEqual(retrieved_step_0.owner_id, 1)
339         self.assertEqual(retrieved_step_0.parent_step_id, None)
340         retrieved_step_1 = retrieved_process.explicit_steps[1]
341         self.assertEqual(retrieved_step_1.step_process_id, 3)
342         self.assertEqual(retrieved_step_1.owner_id, 1)
343         self.assertEqual(retrieved_step_1.parent_step_id, None)
344         # post to process steps list with keeps etc., but trigger recursion
345         form_data_1['new_top_step'] = []
346         form_data_1['steps'] = [retrieved_step_0.id_, retrieved_step_1.id_]
347         form_data_1['keep_step'] = [retrieved_step_0.id_, retrieved_step_1.id_]
348         form_data_1[f'step_{retrieved_step_0.id_}_process_id'] = [2]
349         form_data_1[f'step_{retrieved_step_1.id_}_process_id'] = [1]
350         self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1')
351         # check previous status preserved despite failed steps setting
352         retrieved_process = Process.by_id(self.db_conn, 1)
353         self.assertEqual(len(retrieved_process.explicit_steps), 2)
354         retrieved_step_0 = retrieved_process.explicit_steps[0]
355         self.assertEqual(retrieved_step_0.step_process_id, 2)
356         self.assertEqual(retrieved_step_0.owner_id, 1)
357         self.assertEqual(retrieved_step_0.parent_step_id, None)
358         retrieved_step_1 = retrieved_process.explicit_steps[1]
359         self.assertEqual(retrieved_step_1.step_process_id, 3)
360         self.assertEqual(retrieved_step_1.owner_id, 1)
361         self.assertEqual(retrieved_step_1.parent_step_id, None)
362         form_data_1[f'step_{retrieved_step_1.id_}_process_id'] = [3]
363         # post sub-step to step
364         form_data_1[f'new_step_to_{retrieved_step_1.id_}'] = [3]
365         self.post_process(1, form_data_1)
366         retrieved_process = Process.by_id(self.db_conn, 1)
367         self.assertEqual(len(retrieved_process.explicit_steps), 3)
368         retrieved_step_0 = retrieved_process.explicit_steps[0]
369         self.assertEqual(retrieved_step_0.step_process_id, 2)
370         self.assertEqual(retrieved_step_0.owner_id, 1)
371         self.assertEqual(retrieved_step_0.parent_step_id, None)
372         retrieved_step_1 = retrieved_process.explicit_steps[1]
373         self.assertEqual(retrieved_step_1.step_process_id, 3)
374         self.assertEqual(retrieved_step_1.owner_id, 1)
375         self.assertEqual(retrieved_step_1.parent_step_id, None)
376         retrieved_step_2 = retrieved_process.explicit_steps[2]
377         self.assertEqual(retrieved_step_2.step_process_id, 3)
378         self.assertEqual(retrieved_step_2.owner_id, 1)
379         self.assertEqual(retrieved_step_2.parent_step_id, retrieved_step_1.id_)
380
381     def test_do_GET(self) -> None:
382         """Test /process and /processes response codes."""
383         self.check_get_defaults('/process')
384         self.check_get('/processes', 200)