home · contact · privacy
Harmonize treatment of GET /[item]?id=.
[plomtask] / tests / processes.py
1 """Test Processes module."""
2 from typing import Any
3 from tests.utils import (TestCaseSansDB, TestCaseWithDB, TestCaseWithServer,
4                          Expected)
5 from plomtask.processes import Process, ProcessStep
6 from plomtask.conditions import Condition
7 from plomtask.exceptions import NotFoundException
8
9
10 class TestsSansDB(TestCaseSansDB):
11     """Module tests not requiring DB setup."""
12     checked_class = Process
13
14
15 class TestsSansDBProcessStep(TestCaseSansDB):
16     """Module tests not requiring DB setup."""
17     checked_class = ProcessStep
18     default_init_kwargs = {'owner_id': 2, 'step_process_id': 3,
19                            'parent_step_id': 4}
20
21
22 class TestsWithDB(TestCaseWithDB):
23     """Module tests requiring DB setup."""
24     checked_class = Process
25
26     def three_processes(self) -> tuple[Process, Process, Process]:
27         """Return three saved processes."""
28         p1, p2, p3 = Process(None), Process(None), Process(None)
29         for p in [p1, p2, p3]:
30             p.save(self.db_conn)
31         return p1, p2, p3
32
33     def p_of_conditions(self) -> tuple[Process, list[Condition],
34                                        list[Condition], list[Condition]]:
35         """Return Process and its three Condition sets."""
36         p = Process(None)
37         c1, c2, c3 = Condition(None), Condition(None), Condition(None)
38         for c in [c1, c2, c3]:
39             c.save(self.db_conn)
40         assert isinstance(c1.id_, int)
41         assert isinstance(c2.id_, int)
42         assert isinstance(c3.id_, int)
43         set_1 = [c1, c2]
44         set_2 = [c2, c3]
45         set_3 = [c1, c3]
46         conds = [c.id_ for c in set_1 if isinstance(c.id_, int)]
47         enables = [c.id_ for c in set_2 if isinstance(c.id_, int)]
48         disables = [c.id_ for c in set_3 if isinstance(c.id_, int)]
49         p.set_condition_relations(self.db_conn, conds, [], enables, disables)
50         p.save(self.db_conn)
51         return p, set_1, set_2, set_3
52
53     def test_Process_conditions_saving(self) -> None:
54         """Test .save/.save_core."""
55         p, set1, set2, set3 = self.p_of_conditions()
56         assert p.id_ is not None
57         r = Process.by_id(self.db_conn, p.id_)
58         self.assertEqual(sorted(r.conditions), sorted(set1))
59         self.assertEqual(sorted(r.enables), sorted(set2))
60         self.assertEqual(sorted(r.disables), sorted(set3))
61
62     def test_from_table_row(self) -> None:
63         """Test .from_table_row() properly reads in class from DB."""
64         super().test_from_table_row()
65         p, set1, set2, set3 = self.p_of_conditions()
66         p.save(self.db_conn)
67         assert isinstance(p.id_, int)
68         for row in self.db_conn.row_where(self.checked_class.table_name,
69                                           'id', p.id_):
70             r = Process.from_table_row(self.db_conn, row)
71             self.assertEqual(sorted(r.conditions), sorted(set1))
72             self.assertEqual(sorted(r.enables), sorted(set2))
73             self.assertEqual(sorted(r.disables), sorted(set3))
74
75     # def test_Process_steps(self) -> None:
76     #     """Test addition, nesting, and non-recursion of ProcessSteps"""
77     #     # pylint: disable=too-many-locals
78     #     # pylint: disable=too-many-statements
79     #     p1, p2, p3 = self.three_processes()
80     #     assert isinstance(p1.id_, int)
81     #     assert isinstance(p2.id_, int)
82     #     assert isinstance(p3.id_, int)
83     #     steps_p1: list[ProcessStep] = []
84     #     # add step of process p2 as first (top-level) step to p1
85     #     s_p2_to_p1 = ProcessStep(None, p1.id_, p2.id_, None)
86     #     steps_p1 += [s_p2_to_p1]
87     #     p1.set_steps(self.db_conn, steps_p1)
88     #     p1_dict: dict[int, ProcessStepsNode] = {}
89     #     p1_dict[1] = ProcessStepsNode(p2, None, True, {})
90     #     self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
91     #     # add step of process p3 as second (top-level) step to p1
92     #     s_p3_to_p1 = ProcessStep(None, p1.id_, p3.id_, None)
93     #     steps_p1 += [s_p3_to_p1]
94     #     p1.set_steps(self.db_conn, steps_p1)
95     #     p1_dict[2] = ProcessStepsNode(p3, None, True, {})
96     #     self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
97     #     # add step of process p3 as first (top-level) step to p2,
98     #     steps_p2: list[ProcessStep] = []
99     #     s_p3_to_p2 = ProcessStep(None, p2.id_, p3.id_, None)
100     #     steps_p2 += [s_p3_to_p2]
101     #     p2.set_steps(self.db_conn, steps_p2)
102     #     # expect it as implicit sub-step of p1's second (p3) step
103     #     p2_dict = {3: ProcessStepsNode(p3, None, False, {})}
104     #     p1_dict[1].steps[3] = p2_dict[3]
105     #     self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
106     #     # add step of process p2 as explicit sub-step to p1's second sub-step
107     #     s_p2_to_p1_first = ProcessStep(None, p1.id_, p2.id_, s_p3_to_p1.id_)
108     #     steps_p1 += [s_p2_to_p1_first]
109     #     p1.set_steps(self.db_conn, steps_p1)
110     #     seen_3 = ProcessStepsNode(p3, None, False, {}, False)
111     #     p1_dict[1].steps[3].seen = True
112     #     p1_dict[2].steps[4] = ProcessStepsNode(p2, s_p3_to_p1.id_, True,
113     #                                            {3: seen_3})
114     #     self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
115     #     # add step of process p3 as explicit sub-step to non-existing p1
116     #     # sub-step (of id=999), expect it to become another p1 top-level step
117     #     s_p3_to_p1_999 = ProcessStep(None, p1.id_, p3.id_, 999)
118     #     steps_p1 += [s_p3_to_p1_999]
119     #     p1.set_steps(self.db_conn, steps_p1)
120     #     p1_dict[5] = ProcessStepsNode(p3, None, True, {})
121     #     self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
122     #     # add step of process p3 as explicit sub-step to p1's implicit p3
123     #     # sub-step, expect it to become another p1 top-level step
124     #     s_p3_to_p1_impl_p3 = ProcessStep(None, p1.id_, p3.id_,
125     #                                      s_p3_to_p2.id_)
126     #     steps_p1 += [s_p3_to_p1_impl_p3]
127     #     p1.set_steps(self.db_conn, steps_p1)
128     #     p1_dict[6] = ProcessStepsNode(p3, None, True, {})
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 p3 as explicit sub-step to p1's first
134     #     # # sub-step, expect it to eliminate implicit p3 sub-step
135     #     # s_p3_to_p1_first_explicit = ProcessStep(None, p1.id_, p3.id_,
136     #     #                                         s_p2_to_p1.id_)
137     #     # p1_dict[1].steps = {7: ProcessStepsNode(p3, 1, True, {})}
138     #     # p1_dict[2].steps[4].steps[3].seen = False
139     #     # steps_p1 += [s_p3_to_p1_first_explicit]
140     #     # p1.set_steps(self.db_conn, steps_p1)
141     #     # self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
142     #     # ensure implicit steps non-top explicit steps are shown
143     #     s_p3_to_p2_first = ProcessStep(None, p2.id_, p3.id_, s_p3_to_p2.id_)
144     #     steps_p2 += [s_p3_to_p2_first]
145     #     p2.set_steps(self.db_conn, steps_p2)
146     #     p1_dict[1].steps[3].steps[7] = ProcessStepsNode(p3, 3, False, {},
147     #                                                     True)
148     #     p1_dict[2].steps[4].steps[3].steps[7] = ProcessStepsNode(
149     #             p3, 3, False, {}, False)
150     #     self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
151     #     # ensure suppressed step nodes are hidden
152     #     assert isinstance(s_p3_to_p2.id_, int)
153     #     p1.set_step_suppressions(self.db_conn, [s_p3_to_p2.id_])
154     #     p1_dict[1].steps[3].steps = {}
155     #     p1_dict[1].steps[3].is_suppressed = True
156     #     p1_dict[2].steps[4].steps[3].steps = {}
157     #     p1_dict[2].steps[4].steps[3].is_suppressed = True
158     #     self.assertEqual(p1.get_steps(self.db_conn), p1_dict)
159
160     def test_Process_conditions(self) -> None:
161         """Test setting Process.conditions/enables/disables."""
162         p = Process(None)
163         p.save(self.db_conn)
164         targets = ['conditions', 'blockers', 'enables', 'disables']
165         for i, target in enumerate(targets):
166             c1, c2 = Condition(None), Condition(None)
167             c1.save(self.db_conn)
168             c2.save(self.db_conn)
169             assert isinstance(c1.id_, int)
170             assert isinstance(c2.id_, int)
171             args: list[list[int]] = [[], [], [], []]
172             args[i] = []
173             p.set_condition_relations(self.db_conn, *args)
174             self.assertEqual(getattr(p, target), [])
175             args[i] = [c1.id_]
176             p.set_condition_relations(self.db_conn, *args)
177             self.assertEqual(getattr(p, target), [c1])
178             args[i] = [c2.id_]
179             p.set_condition_relations(self.db_conn, *args)
180             self.assertEqual(getattr(p, target), [c2])
181             args[i] = [c1.id_, c2.id_]
182             p.set_condition_relations(self.db_conn, *args)
183             self.assertEqual(getattr(p, target), [c1, c2])
184
185     def test_remove(self) -> None:
186         """Test removal of Processes and ProcessSteps."""
187         super().test_remove()
188         p1, p2, p3 = self.three_processes()
189         assert isinstance(p1.id_, int)
190         assert isinstance(p2.id_, int)
191         assert isinstance(p3.id_, int)
192         step = ProcessStep(None, p2.id_, p1.id_, None)
193         p2.set_steps(self.db_conn, [step])
194         step_id = step.id_
195         p2.set_steps(self.db_conn, [])
196         with self.assertRaises(NotFoundException):
197             # check unset ProcessSteps actually cannot be found anymore
198             assert step_id is not None
199             ProcessStep.by_id(self.db_conn, step_id)
200         p1.remove(self.db_conn)
201         step = ProcessStep(None, p2.id_, p3.id_, None)
202         p2.set_steps(self.db_conn, [step])
203         step_id = step.id_
204         # check _can_ remove Process pointed to by ProcessStep.owner_id, and …
205         p2.remove(self.db_conn)
206         with self.assertRaises(NotFoundException):
207             # … being dis-owned eliminates ProcessStep
208             assert step_id is not None
209             ProcessStep.by_id(self.db_conn, step_id)
210
211
212 class TestsWithDBForProcessStep(TestCaseWithDB):
213     """Module tests requiring DB setup."""
214     checked_class = ProcessStep
215     default_init_kwargs = {'owner_id': 1, 'step_process_id': 2,
216                            'parent_step_id': 3}
217
218     def setUp(self) -> None:
219         super().setUp()
220         self.p1 = Process(1)
221         self.p1.save(self.db_conn)
222
223     def test_remove(self) -> None:
224         """Test .remove and unsetting of owner's .explicit_steps entry."""
225         p2 = Process(2)
226         p2.save(self.db_conn)
227         assert isinstance(self.p1.id_, int)
228         assert isinstance(p2.id_, int)
229         step = ProcessStep(None, self.p1.id_, p2.id_, None)
230         self.p1.set_steps(self.db_conn, [step])
231         step.remove(self.db_conn)
232         self.assertEqual(self.p1.explicit_steps, [])
233         self.check_identity_with_cache_and_db([])
234
235
236 class ExpectedGetProcess(Expected):
237     """Builder of expectations for GET /processes."""
238     _default_dict = {'is_new': False, 'preset_top_step': None, 'n_todos': 0}
239     _on_empty_make_temp = ('Process', 'proc_as_dict')
240
241     def __init__(self,
242                  proc_id: int,
243                  *args: Any, **kwargs: Any) -> None:
244         self._fields = {'process': proc_id, 'steps': []}
245         super().__init__(*args, **kwargs)
246
247     @staticmethod
248     def stepnode_as_dict(step_id: int,
249                          proc_id: int,
250                          seen: bool = False,
251                          steps: None | list[dict[str, object]] = None,
252                          is_explicit: bool = True,
253                          is_suppressed: bool = False) -> dict[str, object]:
254         # pylint: disable=too-many-arguments
255         """Return JSON of ProcessStepNode to expect."""
256         return {'step': step_id,
257                 'process': proc_id,
258                 'seen': seen,
259                 'steps': steps if steps else [],
260                 'is_explicit': is_explicit,
261                 'is_suppressed': is_suppressed}
262
263     def recalc(self) -> None:
264         """Update internal dictionary by subclass-specific rules."""
265         super().recalc()
266         self._fields['process_candidates'] = self.as_ids(
267                 self.lib_all('Process'))
268         self._fields['condition_candidates'] = self.as_ids(
269                 self.lib_all('Condition'))
270         self._fields['owners'] = [
271                 s['owner_id'] for s in self.lib_all('ProcessStep')
272                 if s['step_process_id'] == self._fields['process']]
273
274
275 class ExpectedGetProcesses(Expected):
276     """Builder of expectations for GET /processes."""
277     _default_dict = {'sort_by': 'title', 'pattern': ''}
278
279     def recalc(self) -> None:
280         """Update internal dictionary by subclass-specific rules."""
281         super().recalc()
282         self._fields['processes'] = self.as_ids(self.lib_all('Process'))
283
284
285 class TestsWithServer(TestCaseWithServer):
286     """Module tests against our HTTP server/handler (and database)."""
287     checked_class = Process
288
289     def _post_process(self, id_: int = 1,
290                       form_data: dict[str, Any] | None = None
291                       ) -> dict[str, Any]:
292         """POST basic Process."""
293         if not form_data:
294             form_data = {'title': 'foo', 'description': 'foo', 'effort': 1.1}
295         self.check_post(form_data, f'/process?id={id_}',
296                         redir=f'/process?id={id_}')
297         return form_data
298
299     def test_fail_POST_process(self) -> None:
300         """Test POST /process and its effect on the database."""
301         valid_post = {'title': '', 'description': '', 'effort': 1.0}
302         # check payloads lacking minimum expecteds
303         self.check_post({}, '/process', 400)
304         self.check_post({'title': '', 'description': ''}, '/process', 400)
305         self.check_post({'title': '', 'effort': 1}, '/process', 400)
306         self.check_post({'description': '', 'effort': 1}, '/process', 400)
307         # check payloads of bad data types
308         self.check_post(valid_post | {'effort': ''}, '/process', 400)
309         # check references to non-existant items
310         self.check_post(valid_post | {'conditions': [1]}, '/process', 404)
311         self.check_post(valid_post | {'disables': [1]}, '/process', 404)
312         self.check_post(valid_post | {'blockers': [1]}, '/process', 404)
313         self.check_post(valid_post | {'enables': [1]}, '/process', 404)
314         self.check_post(valid_post | {'new_top_step': 2}, '/process', 404)
315         # check deletion of non-existant
316         self.check_post({'delete': ''}, '/process?id=1', 404)
317
318     def test_basic_POST_process(self) -> None:
319         """Test basic GET/POST /process operations."""
320         # check on un-saved
321         exp = ExpectedGetProcess(1)
322         exp.force('process_candidates', [])
323         exp.set('is_new', True)
324         self.check_json_get('/process?id=1', exp)
325         # check on minimal payload post
326         exp = ExpectedGetProcess(1)
327         valid_post = {'title': 'foo', 'description': 'oof', 'effort': 2.3}
328         self.post_exp_process([exp], valid_post, 1)
329         self.check_json_get('/process?id=1', exp)
330         # check n_todos field
331         self.post_exp_day([], {'new_todo': ['1']}, '2024-01-01')
332         self.post_exp_day([], {'new_todo': ['1']}, '2024-01-02')
333         exp.set('n_todos', 2)
334         self.check_json_get('/process?id=1', exp)
335         # check cannot delete if Todos to Process
336         self.check_post({'delete': ''}, '/process?id=1', 500)
337         # check cannot delete if some ProcessStep's .step_process_id
338         self.post_exp_process([exp], valid_post, 2)
339         self.post_exp_process([exp], valid_post | {'new_top_step': 2}, 3)
340         self.check_post({'delete': ''}, '/process?id=2', 500)
341         # check successful deletion
342         self.post_exp_process([exp], valid_post, 4)
343         self.check_post({'delete': ''}, '/process?id=4', 302, '/processes')
344         exp = ExpectedGetProcess(4)
345         exp.set_proc_from_post(1, valid_post)
346         exp.set_proc_from_post(2, valid_post)
347         exp.set_proc_from_post(3, valid_post)
348         exp.lib_set('ProcessStep', [exp.procstep_as_dict(1, 3, 2)])
349         exp.force('process_candidates', [1, 2, 3])
350         exp.set('is_new', True)
351         self.check_json_get('/process?id=4', exp)
352
353     def test_POST_process_steps(self) -> None:
354         """Test behavior of ProcessStep posting."""
355         # pylint: disable=too-many-statements
356         url = '/process?id=1'
357         exp = ExpectedGetProcess(1)
358         self.post_exp_process([exp], {}, 1)
359         # post first (top-level) step of proc 2 to proc 1 by 'step_of' in 2
360         self.post_exp_process([exp], {'step_of': 1}, 2)
361         exp.lib_set('ProcessStep', [exp.procstep_as_dict(1, 1, 2)])
362         exp.set('steps', [exp.stepnode_as_dict(1, 2)])
363         self.check_json_get(url, exp)
364         # post empty/absent steps list to process, expect clean slate, and old
365         # step to completely disappear
366         self.post_exp_process([exp], {}, 1)
367         exp.lib_wipe('ProcessStep')
368         exp.set('steps', [])
369         self.check_json_get(url, exp)
370         # post new step of proc2 to proc1 by 'new_top_step'
371         self.post_exp_process([exp], {'new_top_step': 2}, 1)
372         exp.lib_set('ProcessStep', [exp.procstep_as_dict(1, 1, 2)])
373         self.post_exp_process([exp], {'kept_steps': [1]}, 1)
374         exp.set('steps', [exp.stepnode_as_dict(1, 2)])
375         self.check_json_get(url, exp)
376         # fail on single- and multi-step recursion
377         p_min = {'title': '', 'description': '', 'effort': 0}
378         self.check_post(p_min | {'new_top_step': 1}, url, 400)
379         self.check_post(p_min | {'step_of': 1}, url, 400)
380         self.post_exp_process([exp], {'new_top_step': 1}, 2)
381         self.check_post(p_min | {'step_of': 2, 'new_top_step': 2}, url, 400)
382         self.post_exp_process([exp], {}, 3)
383         self.post_exp_process([exp], {'step_of': 3}, 4)
384         self.check_post(p_min | {'new_top_step': 3, 'step_of': 4}, url, 400)
385         # post sibling steps
386         self.post_exp_process([exp], {}, 4)
387         self.post_exp_process([exp], {'new_top_step': 4}, 1)
388         self.post_exp_process([exp], {'kept_steps': [1], 'new_top_step': 4}, 1)
389         exp.lib_set('ProcessStep', [exp.procstep_as_dict(1, 1, 4),
390                                     exp.procstep_as_dict(2, 1, 4)])
391         exp.set('steps', [exp.stepnode_as_dict(1, 4),
392                           exp.stepnode_as_dict(2, 4)])
393         self.check_json_get(url, exp)
394         # post sub-step chain
395         p = {'kept_steps': [1, 2], 'new_step_to_2': 4}
396         self.post_exp_process([exp], p, 1)
397         exp.lib_set('ProcessStep', [exp.procstep_as_dict(3, 1, 4, 2)])
398         exp.set('steps', [exp.stepnode_as_dict(1, 4),
399                           exp.stepnode_as_dict(2, 4, steps=[
400                               exp.stepnode_as_dict(3, 4)])])
401         self.check_json_get(url, exp)
402         # fail posting sub-step that would cause recursion
403         self.post_exp_process([exp], {}, 6)
404         self.post_exp_process([exp], {'new_top_step': 6}, 5)
405         p = p_min | {'kept_steps': [1, 2, 3], 'new_step_to_2': 5, 'step_of': 6}
406         self.check_post(p, url, 400)
407
408     def test_fail_GET_process(self) -> None:
409         """Test invalid GET /process params."""
410         # check for invalid IDs
411         self.check_get_defaults('/process')
412         # check we catch invalid base64
413         self.check_get('/process?title_b64=foo', 400)
414         # check failure on references to unknown processes; we create Process
415         # of ID=1 here so we know the 404 comes from step_to=2 etc. (that tie
416         # the Process displayed by /process to others), not from not finding
417         # the main Process itself
418         self.post_exp_process([], {}, 1)
419         self.check_get('/process?id=1&step_to=2', 404)
420         self.check_get('/process?id=1&has_step=2', 404)
421
422     def test_GET_processes(self) -> None:
423         """Test GET /processes."""
424         # pylint: disable=too-many-statements
425         # test empty result on empty DB, default-settings on empty params
426         exp = ExpectedGetProcesses()
427         self.check_json_get('/processes', exp)
428         # test on meaningless non-empty params (incl. entirely un-used key),
429         # that 'sort_by' default to 'title' (even if set to something else, as
430         # long as without handler) and 'pattern' get preserved
431         exp.set('pattern', 'bar')
432         url = '/processes?sort_by=foo&pattern=bar&foo=x'
433         self.check_json_get(url, exp)
434         # test non-empty result, automatic (positive) sorting by title
435         proc1_post = {'title': 'foo', 'description': 'oof', 'effort': 1.0}
436         self.post_exp_process([exp], proc1_post, 1)
437         proc2_post = {'title': 'bar', 'description': 'rab', 'effort': 1.1}
438         self.post_exp_process([exp], proc2_post | {'new_top_step': [1]}, 2)
439         proc3_post = {'title': 'baz', 'description': 'zab', 'effort': 0.9}
440         self.post_exp_process([exp], proc3_post | {'new_top_step': [1]}, 3)
441         proc3_post = proc3_post | {'new_top_step': [2], 'kept_steps': [2]}
442         self.post_exp_process([exp], proc3_post, 3)
443         exp.lib_set('ProcessStep', [exp.procstep_as_dict(1, 2, 1),
444                                     exp.procstep_as_dict(2, 3, 1),
445                                     exp.procstep_as_dict(3, 3, 2)])
446         exp.set('pattern', '')
447         self.check_filter(exp, 'processes', 'sort_by', 'title', [2, 3, 1])
448         # test other sortings
449         self.check_filter(exp, 'processes', 'sort_by', '-title', [1, 3, 2])
450         self.check_filter(exp, 'processes', 'sort_by', 'effort', [3, 1, 2])
451         self.check_filter(exp, 'processes', 'sort_by', '-effort', [2, 1, 3])
452         self.check_filter(exp, 'processes', 'sort_by', 'steps', [1, 2, 3])
453         self.check_filter(exp, 'processes', 'sort_by', '-steps', [3, 2, 1])
454         self.check_filter(exp, 'processes', 'sort_by', 'owners', [3, 2, 1])
455         self.check_filter(exp, 'processes', 'sort_by', '-owners', [1, 2, 3])
456         # test pattern matching on title
457         exp.set('sort_by', 'title')
458         exp.lib_del('Process', '1')
459         self.check_filter(exp, 'processes', 'pattern', 'ba', [2, 3])
460         # test pattern matching on description
461         exp.lib_wipe('Process')
462         exp.lib_wipe('ProcessStep')
463         self.post_exp_process([exp], {'description': 'oof', 'effort': 1.0}, 1)
464         self.check_filter(exp, 'processes', 'pattern', 'of', [1])