home · contact · privacy
Refactor saving and caching tests, treatment of None IDs.
[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         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))
66
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         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             r = Process.from_table_row(self.db_conn, row)
79             self.assertEqual(sorted(r.conditions), sorted(set1))
80             self.assertEqual(sorted(r.enables), sorted(set2))
81             self.assertEqual(sorted(r.disables), sorted(set3))
82
83     def test_Process_steps(self) -> None:
84         """Test addition, nesting, and non-recursion of ProcessSteps"""
85         # pylint: disable=too-many-locals
86         # pylint: disable=too-many-statements
87         p1, p2, p3 = self.three_processes()
88         assert isinstance(p1.id_, int)
89         assert isinstance(p2.id_, int)
90         assert isinstance(p3.id_, int)
91         steps_p1: list[ProcessStep] = []
92         # add step of process p2 as first (top-level) step to p1
93         s_p2_to_p1 = ProcessStep(None, p1.id_, p2.id_, None)
94         steps_p1 += [s_p2_to_p1]
95         p1.set_steps(self.db_conn, steps_p1)
96         p1_dict: dict[int, ProcessStepsNode] = {}
97         p1_dict[1] = ProcessStepsNode(p2, None, True, {})
98         self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
99         # add step of process p3 as second (top-level) step to p1
100         s_p3_to_p1 = ProcessStep(None, p1.id_, p3.id_, None)
101         steps_p1 += [s_p3_to_p1]
102         p1.set_steps(self.db_conn, steps_p1)
103         p1_dict[2] = ProcessStepsNode(p3, None, True, {})
104         self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
105         # add step of process p3 as first (top-level) step to p2,
106         steps_p2: list[ProcessStep] = []
107         s_p3_to_p2 = ProcessStep(None, p2.id_, p3.id_, None)
108         steps_p2 += [s_p3_to_p2]
109         p2.set_steps(self.db_conn, steps_p2)
110         # expect it as implicit sub-step of p1's second (p3) step
111         p2_dict = {3: ProcessStepsNode(p3, None, False, {})}
112         p1_dict[1].steps[3] = p2_dict[3]
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 second sub-step
115         s_p2_to_p1_first = ProcessStep(None, p1.id_, p2.id_, s_p3_to_p1.id_)
116         steps_p1 += [s_p2_to_p1_first]
117         p1.set_steps(self.db_conn, steps_p1)
118         seen_3 = ProcessStepsNode(p3, None, False, {}, False)
119         p1_dict[1].steps[3].seen = True
120         p1_dict[2].steps[4] = ProcessStepsNode(p2, s_p3_to_p1.id_, True,
121                                                {3: seen_3})
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_,
143         #                                         s_p2_to_p1.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, {}, True)
154         p1_dict[2].steps[4].steps[3].steps[7] = ProcessStepsNode(p3, 3, False,
155                                                                  {}, 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)
165
166     def test_Process_conditions(self) -> None:
167         """Test setting Process.conditions/enables/disables."""
168         p = Process(None)
169         p.save(self.db_conn)
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])
185
186     def test_Process_versioned_attributes_singularity(self) -> None:
187         """Test behavior of VersionedAttributes on saving (with .title)."""
188         self.check_versioned_singularity()
189
190     def test_Process_removal(self) -> None:
191         """Test removal of Processes and ProcessSteps."""
192         self.check_remove()
193         p1, p2, p3 = self.three_processes()
194         assert isinstance(p1.id_, int)
195         assert isinstance(p2.id_, int)
196         assert isinstance(p3.id_, int)
197         step = ProcessStep(None, p2.id_, p1.id_, None)
198         p2.set_steps(self.db_conn, [step])
199         step_id = step.id_
200         with self.assertRaises(HandledException):
201             p1.remove(self.db_conn)
202         p2.set_steps(self.db_conn, [])
203         with self.assertRaises(NotFoundException):
204             assert step_id is not None
205             ProcessStep.by_id(self.db_conn, step_id)
206         p1.remove(self.db_conn)
207         step = ProcessStep(None, p2.id_, p3.id_, None)
208         p2.set_steps(self.db_conn, [step])
209         step_id = step.id_
210         p2.remove(self.db_conn)
211         with self.assertRaises(NotFoundException):
212             assert step_id is not None
213             ProcessStep.by_id(self.db_conn, step_id)
214         todo = Todo(None, p3, False, '2024-01-01')
215         todo.save(self.db_conn)
216         with self.assertRaises(HandledException):
217             p3.remove(self.db_conn)
218         todo.remove(self.db_conn)
219         p3.remove(self.db_conn)
220
221
222 class TestsWithDBForProcessStep(TestCaseWithDB):
223     """Module tests requiring DB setup."""
224     checked_class = ProcessStep
225     default_init_kwargs = {'owner_id': 2, 'step_process_id': 3,
226                            'parent_step_id': 4}
227
228     def setUp(self) -> None:
229         super().setUp()
230         p = Process(1)
231         p.save(self.db_conn)
232         p = Process(2)
233         p.save(self.db_conn)
234
235     def test_ProcessStep_remove(self) -> None:
236         """Test .remove and unsetting of owner's .explicit_steps entry."""
237         p1 = Process(None)
238         p2 = Process(None)
239         p1.save(self.db_conn)
240         p2.save(self.db_conn)
241         assert isinstance(p1.id_, int)
242         assert isinstance(p2.id_, int)
243         step = ProcessStep(None, p1.id_, p2.id_, None)
244         p1.set_steps(self.db_conn, [step])
245         step.remove(self.db_conn)
246         self.assertEqual(p1.explicit_steps, [])
247         self.check_identity_with_cache_and_db([])
248
249
250 class TestsWithServer(TestCaseWithServer):
251     """Module tests against our HTTP server/handler (and database)."""
252
253     def test_do_POST_process(self) -> None:
254         """Test POST /process and its effect on the database."""
255         self.assertEqual(0, len(Process.all(self.db_conn)))
256         form_data = self.post_process()
257         self.assertEqual(1, len(Process.all(self.db_conn)))
258         self.check_post(form_data, '/process?id=FOO', 400)
259         self.check_post(form_data | {'effort': 'foo'}, '/process?id=', 400)
260         self.check_post({}, '/process?id=', 400)
261         self.check_post({'title': '', 'description': ''}, '/process?id=', 400)
262         self.check_post({'title': '', 'effort': 1.1}, '/process?id=', 400)
263         self.check_post({'description': '', 'effort': 1.0},
264                         '/process?id=', 400)
265         self.assertEqual(1, len(Process.all(self.db_conn)))
266         form_data = {'title': 'foo', 'description': 'foo', 'effort': 1.0}
267         self.post_process(2, form_data | {'condition': []})
268         self.check_post(form_data | {'condition': [1]}, '/process?id=', 404)
269         self.check_post({'title': 'foo', 'description': 'foo',
270                          'is_active': False},
271                         '/condition', 302, '/condition?id=1')
272         self.post_process(3, form_data | {'condition': [1]})
273         self.post_process(4, form_data | {'disables': [1]})
274         self.post_process(5, form_data | {'enables': [1]})
275         form_data['delete'] = ''
276         self.check_post(form_data, '/process?id=', 404)
277         self.check_post(form_data, '/process?id=6', 404)
278         self.check_post(form_data, '/process?id=5', 302, '/processes')
279
280     def test_do_POST_process_steps(self) -> None:
281         """Test behavior of ProcessStep posting."""
282         # pylint: disable=too-many-statements
283         form_data_1 = self.post_process(1)
284         self.post_process(2)
285         self.post_process(3)
286         # post first (top-level) step of process 2 to process 1
287         form_data_1['new_top_step'] = [2]
288         self.post_process(1, form_data_1)
289         retrieved_process = Process.by_id(self.db_conn, 1)
290         self.assertEqual(len(retrieved_process.explicit_steps), 1)
291         retrieved_step = retrieved_process.explicit_steps[0]
292         retrieved_step_id = retrieved_step.id_
293         self.assertEqual(retrieved_step.step_process_id, 2)
294         self.assertEqual(retrieved_step.owner_id, 1)
295         self.assertEqual(retrieved_step.parent_step_id, None)
296         # post empty steps list to process, expect clean slate, and old step to
297         # completely disappear
298         form_data_1['new_top_step'] = []
299         self.post_process(1, form_data_1)
300         retrieved_process = Process.by_id(self.db_conn, 1)
301         self.assertEqual(retrieved_process.explicit_steps, [])
302         assert retrieved_step_id is not None
303         with self.assertRaises(NotFoundException):
304             ProcessStep.by_id(self.db_conn, retrieved_step_id)
305         # post new first (top_level) step of process 3 to process 1
306         form_data_1['new_top_step'] = [3]
307         self.post_process(1, form_data_1)
308         retrieved_process = Process.by_id(self.db_conn, 1)
309         retrieved_step = retrieved_process.explicit_steps[0]
310         self.assertEqual(retrieved_step.step_process_id, 3)
311         self.assertEqual(retrieved_step.owner_id, 1)
312         self.assertEqual(retrieved_step.parent_step_id, None)
313         # post to process steps list without keeps, expect clean slate
314         form_data_1['new_top_step'] = []
315         form_data_1['steps'] = [retrieved_step.id_]
316         self.post_process(1, form_data_1)
317         retrieved_process = Process.by_id(self.db_conn, 1)
318         self.assertEqual(retrieved_process.explicit_steps, [])
319         # post to process empty steps list but keep, expect 400
320         form_data_1['steps'] = []
321         form_data_1['keep_step'] = [retrieved_step_id]
322         self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1')
323         # post to process steps list with keep on non-created step, expect 400
324         form_data_1['steps'] = [retrieved_step_id]
325         form_data_1['keep_step'] = [retrieved_step_id]
326         self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1')
327         # post to process steps list with keep and process ID, expect 200
328         form_data_1[f'step_{retrieved_step_id}_process_id'] = [2]
329         self.post_process(1, form_data_1)
330         retrieved_process = Process.by_id(self.db_conn, 1)
331         self.assertEqual(len(retrieved_process.explicit_steps), 1)
332         retrieved_step = retrieved_process.explicit_steps[0]
333         self.assertEqual(retrieved_step.step_process_id, 2)
334         self.assertEqual(retrieved_step.owner_id, 1)
335         self.assertEqual(retrieved_step.parent_step_id, None)
336         # post nonsense, expect 400 and preservation of previous state
337         form_data_1['steps'] = ['foo']
338         form_data_1['keep_step'] = []
339         self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1')
340         retrieved_process = Process.by_id(self.db_conn, 1)
341         self.assertEqual(len(retrieved_process.explicit_steps), 1)
342         retrieved_step = retrieved_process.explicit_steps[0]
343         self.assertEqual(retrieved_step.step_process_id, 2)
344         self.assertEqual(retrieved_step.owner_id, 1)
345         self.assertEqual(retrieved_step.parent_step_id, None)
346         # post to process steps list with keep and process ID, expect 200
347         form_data_1['new_top_step'] = [3]
348         form_data_1['steps'] = [retrieved_step.id_]
349         form_data_1['keep_step'] = [retrieved_step.id_]
350         self.post_process(1, form_data_1)
351         retrieved_process = Process.by_id(self.db_conn, 1)
352         self.assertEqual(len(retrieved_process.explicit_steps), 2)
353         retrieved_step_0 = retrieved_process.explicit_steps[0]
354         self.assertEqual(retrieved_step_0.step_process_id, 3)
355         self.assertEqual(retrieved_step_0.owner_id, 1)
356         self.assertEqual(retrieved_step_0.parent_step_id, None)
357         retrieved_step_1 = retrieved_process.explicit_steps[1]
358         self.assertEqual(retrieved_step_1.step_process_id, 2)
359         self.assertEqual(retrieved_step_1.owner_id, 1)
360         self.assertEqual(retrieved_step_1.parent_step_id, None)
361         # post to process steps list with keeps etc., but trigger recursion
362         form_data_1['new_top_step'] = []
363         form_data_1['steps'] = [retrieved_step_0.id_, retrieved_step_1.id_]
364         form_data_1['keep_step'] = [retrieved_step_0.id_, retrieved_step_1.id_]
365         form_data_1[f'step_{retrieved_step_0.id_}_process_id'] = [2]
366         form_data_1[f'step_{retrieved_step_1.id_}_process_id'] = [1]
367         self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1')
368         # check previous status preserved despite failed steps setting
369         retrieved_process = Process.by_id(self.db_conn, 1)
370         self.assertEqual(len(retrieved_process.explicit_steps), 2)
371         retrieved_step_0 = retrieved_process.explicit_steps[0]
372         self.assertEqual(retrieved_step_0.step_process_id, 2)
373         self.assertEqual(retrieved_step_0.owner_id, 1)
374         self.assertEqual(retrieved_step_0.parent_step_id, None)
375         retrieved_step_1 = retrieved_process.explicit_steps[1]
376         self.assertEqual(retrieved_step_1.step_process_id, 3)
377         self.assertEqual(retrieved_step_1.owner_id, 1)
378         self.assertEqual(retrieved_step_1.parent_step_id, None)
379         # post sub-step to step
380         form_data_1[f'step_{retrieved_step_0.id_}_process_id'] = [3]
381         form_data_1[f'new_step_to_{retrieved_step_0.id_}'] = [3]
382         self.post_process(1, form_data_1)
383         retrieved_process = Process.by_id(self.db_conn, 1)
384         self.assertEqual(len(retrieved_process.explicit_steps), 3)
385         retrieved_step_0 = retrieved_process.explicit_steps[0]
386         self.assertEqual(retrieved_step_0.step_process_id, 2)
387         self.assertEqual(retrieved_step_0.owner_id, 1)
388         self.assertEqual(retrieved_step_0.parent_step_id, None)
389         retrieved_step_1 = retrieved_process.explicit_steps[1]
390         self.assertEqual(retrieved_step_1.step_process_id, 3)
391         self.assertEqual(retrieved_step_1.owner_id, 1)
392         self.assertEqual(retrieved_step_1.parent_step_id, None)
393         retrieved_step_2 = retrieved_process.explicit_steps[2]
394         self.assertEqual(retrieved_step_2.step_process_id, 3)
395         self.assertEqual(retrieved_step_2.owner_id, 1)
396         self.assertEqual(retrieved_step_2.parent_step_id, retrieved_step_1.id_)
397
398     def test_do_GET(self) -> None:
399         """Test /process and /processes response codes."""
400         self.check_get_defaults('/process')
401         self.check_get('/processes', 200)