home · contact · privacy
Minor tests refactoring.
[plomtask] / tests / todos.py
1 """Test Todos module."""
2 from typing import Any
3 from tests.utils import (TestCaseSansDB, TestCaseWithDB, TestCaseWithServer,
4                          Expected)
5 from plomtask.todos import Todo, TodoNode
6 from plomtask.processes import Process, ProcessStep
7 from plomtask.conditions import Condition
8 from plomtask.exceptions import (NotFoundException, BadFormatException,
9                                  HandledException)
10
11
12 class TestsWithDB(TestCaseWithDB, TestCaseSansDB):
13     """Tests requiring DB, but not server setup.
14
15     NB: We subclass TestCaseSansDB too, to run any tests there that due to any
16     Todo requiring a _saved_ Process wouldn't run without a DB.
17     """
18     checked_class = Todo
19     default_init_kwargs = {'process': None, 'is_done': False,
20                            'date': '2024-01-01'}
21
22     def setUp(self) -> None:
23         super().setUp()
24         self.date1 = '2024-01-01'
25         self.date2 = '2024-01-02'
26         self.proc = Process(None)
27         self.proc.save(self.db_conn)
28         self.cond1 = Condition(None)
29         self.cond1.save(self.db_conn)
30         self.cond2 = Condition(None)
31         self.cond2.save(self.db_conn)
32         self.default_init_kwargs['process'] = self.proc
33
34     def test_Todo_init(self) -> None:
35         """Test creation of Todo and what they default to."""
36         process = Process(None)
37         with self.assertRaises(NotFoundException):
38             Todo(None, process, False, self.date1)
39         process.save(self.db_conn)
40         assert isinstance(self.cond1.id_, int)
41         assert isinstance(self.cond2.id_, int)
42         process.set_condition_relations(self.db_conn,
43                                         [self.cond1.id_, self.cond2.id_], [],
44                                         [self.cond1.id_], [self.cond2.id_])
45         todo_no_id = Todo(None, process, False, self.date1)
46         self.assertEqual(todo_no_id.conditions, [self.cond1, self.cond2])
47         self.assertEqual(todo_no_id.enables, [self.cond1])
48         self.assertEqual(todo_no_id.disables, [self.cond2])
49         todo_yes_id = Todo(5, process, False, self.date1)
50         self.assertEqual(todo_yes_id.conditions, [])
51         self.assertEqual(todo_yes_id.enables, [])
52         self.assertEqual(todo_yes_id.disables, [])
53
54     def test_Todo_by_date(self) -> None:
55         """Test findability of Todos by date."""
56         t1 = Todo(None, self.proc, False, self.date1)
57         t1.save(self.db_conn)
58         t2 = Todo(None, self.proc, False, self.date1)
59         t2.save(self.db_conn)
60         self.assertEqual(Todo.by_date(self.db_conn, self.date1), [t1, t2])
61         self.assertEqual(Todo.by_date(self.db_conn, self.date2), [])
62         with self.assertRaises(BadFormatException):
63             self.assertEqual(Todo.by_date(self.db_conn, 'foo'), [])
64
65     def test_Todo_by_date_range_with_limits(self) -> None:
66         """Test .by_date_range_with_limits."""
67         self.check_by_date_range_with_limits('day')
68
69     def test_Todo_on_conditions(self) -> None:
70         """Test effect of Todos on Conditions."""
71         assert isinstance(self.cond1.id_, int)
72         assert isinstance(self.cond2.id_, int)
73         todo = Todo(None, self.proc, False, self.date1)
74         todo.save(self.db_conn)
75         todo.set_condition_relations(self.db_conn, [], [],
76                                      [self.cond1.id_], [self.cond2.id_])
77         todo.is_done = True
78         self.assertEqual(self.cond1.is_active, True)
79         self.assertEqual(self.cond2.is_active, False)
80         todo.is_done = False
81         self.assertEqual(self.cond1.is_active, True)
82         self.assertEqual(self.cond2.is_active, False)
83
84     def test_Todo_children(self) -> None:
85         """Test Todo.children relations."""
86         todo_1 = Todo(None, self.proc, False, self.date1)
87         todo_2 = Todo(None, self.proc, False, self.date1)
88         todo_2.save(self.db_conn)
89         with self.assertRaises(HandledException):
90             todo_1.add_child(todo_2)
91         todo_1.save(self.db_conn)
92         todo_3 = Todo(None, self.proc, False, self.date1)
93         with self.assertRaises(HandledException):
94             todo_1.add_child(todo_3)
95         todo_3.save(self.db_conn)
96         todo_1.add_child(todo_3)
97         todo_1.save(self.db_conn)
98         assert isinstance(todo_1.id_, int)
99         todo_retrieved = Todo.by_id(self.db_conn, todo_1.id_)
100         self.assertEqual(todo_retrieved.children, [todo_3])
101         with self.assertRaises(BadFormatException):
102             todo_3.add_child(todo_1)
103
104     def test_Todo_conditioning(self) -> None:
105         """Test Todo.doability conditions."""
106         assert isinstance(self.cond1.id_, int)
107         todo_1 = Todo(None, self.proc, False, self.date1)
108         todo_1.save(self.db_conn)
109         todo_2 = Todo(None, self.proc, False, self.date1)
110         todo_2.save(self.db_conn)
111         todo_2.add_child(todo_1)
112         with self.assertRaises(BadFormatException):
113             todo_2.is_done = True
114         todo_1.is_done = True
115         todo_2.is_done = True
116         todo_2.is_done = False
117         todo_2.set_condition_relations(
118                 self.db_conn, [self.cond1.id_], [], [], [])
119         with self.assertRaises(BadFormatException):
120             todo_2.is_done = True
121         self.cond1.is_active = True
122         todo_2.is_done = True
123
124     def test_Todo_step_tree(self) -> None:
125         """Test self-configuration of TodoStepsNode tree for Day view."""
126
127         def todo_node_as_dict(node: TodoNode) -> dict[str, object]:
128             return {'todo': node.todo.id_, 'seen': node.seen,
129                     'children': [todo_node_as_dict(c) for c in node.children]}
130
131         todo_1 = Todo(None, self.proc, False, self.date1)
132         todo_1.save(self.db_conn)
133         assert isinstance(todo_1.id_, int)
134         # test minimum
135         node_0 = TodoNode(todo_1, False, [])
136         cmp_0_dict = todo_node_as_dict(todo_1.get_step_tree(set()))
137         cmp_1_dict = todo_node_as_dict(node_0)
138         self.assertEqual(cmp_0_dict, cmp_1_dict)
139         # test non_emtpy seen_todo does something
140         node_0.seen = True
141         cmp_0_dict = todo_node_as_dict(todo_1.get_step_tree({todo_1.id_}))
142         cmp_1_dict = todo_node_as_dict(node_0)
143         self.assertEqual(cmp_0_dict, cmp_1_dict)
144         # test child shows up
145         todo_2 = Todo(None, self.proc, False, self.date1)
146         todo_2.save(self.db_conn)
147         assert isinstance(todo_2.id_, int)
148         todo_1.add_child(todo_2)
149         node_2 = TodoNode(todo_2, False, [])
150         node_0.children = [node_2]
151         node_0.seen = False
152         cmp_0_dict = todo_node_as_dict(todo_1.get_step_tree(set()))
153         cmp_1_dict = todo_node_as_dict(node_0)
154         self.assertEqual(cmp_0_dict, cmp_1_dict)
155         # test child shows up with child
156         todo_3 = Todo(None, self.proc, False, self.date1)
157         todo_3.save(self.db_conn)
158         assert isinstance(todo_3.id_, int)
159         todo_2.add_child(todo_3)
160         node_3 = TodoNode(todo_3, False, [])
161         node_2.children = [node_3]
162         cmp_0_dict = todo_node_as_dict(todo_1.get_step_tree(set()))
163         cmp_1_dict = todo_node_as_dict(node_0)
164         self.assertEqual(cmp_0_dict, cmp_1_dict)
165         # test same todo can be child-ed multiple times at different locations
166         todo_1.add_child(todo_3)
167         node_4 = TodoNode(todo_3, True, [])
168         node_0.children += [node_4]
169         cmp_0_dict = todo_node_as_dict(todo_1.get_step_tree(set()))
170         cmp_1_dict = todo_node_as_dict(node_0)
171         self.assertEqual(cmp_0_dict, cmp_1_dict)
172
173     def test_Todo_ensure_children(self) -> None:
174         """Test parenthood guarantees of Todo.ensure_children."""
175         assert isinstance(self.proc.id_, int)
176         proc2 = Process(None)
177         proc2.save(self.db_conn)
178         assert isinstance(proc2.id_, int)
179         proc3 = Process(None)
180         proc3.save(self.db_conn)
181         assert isinstance(proc3.id_, int)
182         proc4 = Process(None)
183         proc4.save(self.db_conn)
184         assert isinstance(proc4.id_, int)
185         # make proc4 step of proc3
186         step = ProcessStep(None, proc3.id_, proc4.id_, None)
187         proc3.set_steps(self.db_conn, [step])
188         # give proc2 three steps; 2× proc1, 1× proc3
189         step1 = ProcessStep(None, proc2.id_, self.proc.id_, None)
190         step2 = ProcessStep(None, proc2.id_, self.proc.id_, None)
191         step3 = ProcessStep(None, proc2.id_, proc3.id_, None)
192         proc2.set_steps(self.db_conn, [step1, step2, step3])
193         # test mere creation does nothing
194         todo_ignore = Todo(None, proc2, False, self.date1)
195         todo_ignore.save(self.db_conn)
196         self.assertEqual(todo_ignore.children, [])
197         # test create_with_children on step-less does nothing
198         todo_1 = Todo(None, self.proc, False, self.date1)
199         todo_1.save(self.db_conn)
200         todo_1.ensure_children(self.db_conn)
201         self.assertEqual(todo_1.children, [])
202         self.assertEqual(len(Todo.all(self.db_conn)), 2)
203         # test create_with_children adopts and creates, and down tree too
204         todo_2 = Todo(None, proc2, False, self.date1)
205         todo_2.save(self.db_conn)
206         todo_2.ensure_children(self.db_conn)
207         self.assertEqual(3, len(todo_2.children))
208         self.assertEqual(todo_1, todo_2.children[0])
209         self.assertEqual(self.proc, todo_2.children[2].process)
210         self.assertEqual(proc3, todo_2.children[1].process)
211         todo_3 = todo_2.children[1]
212         self.assertEqual(len(todo_3.children), 1)
213         self.assertEqual(todo_3.children[0].process, proc4)
214
215
216 class ExpectedGetTodo(Expected):
217     """Builder of expectations for GET /todo."""
218
219     def __init__(self,
220                  todo_id: int,
221                  *args: Any, **kwargs: Any) -> None:
222         self._fields = {'todo': todo_id,
223                         'steps_todo_to_process': []}
224         super().__init__(*args, **kwargs)
225
226     def recalc(self) -> None:
227         """Update internal dictionary by subclass-specific rules."""
228
229         def walk_steps(step: dict[str, Any]) -> None:
230             if not step['todo']:
231                 proc_id = step['process']
232                 cands = self.as_ids(
233                         [t for t in todos if proc_id == t['process_id']
234                          and t['id'] in self._fields['todo_candidates']])
235                 self._fields['adoption_candidates_for'][str(proc_id)] = cands
236             for child in step['children']:
237                 walk_steps(child)
238
239         super().recalc()
240         self.lib_wipe('Day')
241         todos = self.lib_all('Todo')
242         procs = self.lib_all('Process')
243         conds = self.lib_all('Condition')
244         self._fields['todo_candidates'] = self.as_ids(
245                 [t for t in todos if t['id'] != self._fields['todo']])
246         self._fields['process_candidates'] = self.as_ids(procs)
247         self._fields['condition_candidates'] = self.as_ids(conds)
248         self._fields['adoption_candidates_for'] = {}
249         for step in self._fields['steps_todo_to_process']:
250             walk_steps(step)
251
252     @staticmethod
253     def step_as_dict(node_id: int,
254                      children: list[dict[str, object]],
255                      process: int | None = None,
256                      todo: int | None = None,
257                      fillable: bool = False,
258                      ) -> dict[str, object]:
259         """Return JSON of TodoOrProcStepsNode to expect."""
260         return {'node_id': node_id,
261                 'children': children,
262                 'process': process,
263                 'fillable': fillable,
264                 'todo': todo}
265
266
267 class TestsWithServer(TestCaseWithServer):
268     """Tests against our HTTP server/handler (and database)."""
269
270     def _post_exp_todo(
271             self, id_: int, payload: dict[str, Any], exp: Expected) -> None:
272         self.check_post(payload, f'/todo?id={id_}')
273         exp.set_todo_from_post(id_, payload)
274
275     def test_basic_fail_POST_todo(self) -> None:
276         """Test basic malformed/illegal POST /todo requests."""
277         self.post_exp_process([], {}, 1)
278         # test we cannot just POST into non-existing Todo
279         self.check_post({}, '/todo', 404)
280         self.check_post({}, '/todo?id=FOO', 400)
281         self.check_post({}, '/todo?id=0', 404)
282         self.check_post({}, '/todo?id=1', 404)
283         # test malformed values on existing Todo
284         self.post_exp_day([], {'new_todo': [1]})
285         for name in [
286                 'adopt', 'effort', 'make_full', 'make_empty', 'step_filler',
287                 'conditions', 'disables', 'blockers', 'enables']:
288             self.check_post({name: 'x'}, '/todo?id=1', 400, '/todo')
289         for prefix in ['make_empty_', 'make_full_']:
290             for suffix in ['', 'x', '1.1']:
291                 self.check_post({'step_filler': f'{prefix}{suffix}'},
292                                 '/todo?id=1', 400, '/todo')
293
294     def test_basic_POST_todo(self) -> None:
295         """Test basic POST /todo manipulations."""
296         exp = ExpectedGetTodo(1)
297         self.post_exp_process([exp], {'calendarize': 0}, 1)
298         self.post_exp_day([exp], {'new_todo': [1]})
299         # test posting naked entity at first changes nothing
300         self.check_json_get('/todo?id=1', exp)
301         self.check_post({}, '/todo?id=1')
302         self.check_json_get('/todo?id=1', exp)
303         # test posting doneness, comment, calendarization, effort
304         todo_post = {'is_done': 1, 'calendarize': 1,
305                      'comment': 'foo', 'effort': 2.3}
306         self._post_exp_todo(1, todo_post, exp)
307         self.check_json_get('/todo?id=1', exp)
308         # test implicitly un-setting (only) comment by empty post
309         self.check_post({}, '/todo?id=1')
310         exp.lib_get('Todo', 1)['comment'] = ''
311         self.check_json_get('/todo?id=1', exp)
312         # test effort post can be explicitly unset by "effort":"" post
313         self.check_post({'effort': ''}, '/todo?id=1')
314         exp.lib_get('Todo', 1)['effort'] = None
315         self.check_json_get('/todo?id=1', exp)
316         # test Condition posts
317         c1_post = {'title': 'foo', 'description': 'oof', 'is_active': 0}
318         c2_post = {'title': 'bar', 'description': 'rab', 'is_active': 1}
319         self.post_exp_cond([exp], 1, c1_post, '?id=1', '?id=1')
320         self.post_exp_cond([exp], 2, c2_post, '?id=2', '?id=2')
321         self.check_json_get('/todo?id=1', exp)
322         todo_post = {'conditions': [1], 'disables': [1],
323                      'blockers': [2], 'enables': [2]}
324         self._post_exp_todo(1, todo_post, exp)
325         self.check_json_get('/todo?id=1', exp)
326
327     def test_POST_todo_deletion(self) -> None:
328         """Test deletions via POST /todo."""
329         exp = ExpectedGetTodo(1)
330         self.post_exp_process([exp], {}, 1)
331         # test failure of deletion on non-existing Todo
332         self.check_post({'delete': ''}, '/todo?id=2', 404, '/')
333         # test deletion of existing Todo
334         self.post_exp_day([exp], {'new_todo': [1]})
335         self.check_post({'delete': ''}, '/todo?id=1', 302, '/')
336         self.check_get('/todo?id=1', 404)
337         exp.lib_del('Todo', 1)
338         # test deletion of adopted Todo
339         self.post_exp_day([exp], {'new_todo': [1]})
340         self.post_exp_day([exp], {'new_todo': [1]})
341         self.check_post({'adopt': 2}, '/todo?id=1')
342         self.check_post({'delete': ''}, '/todo?id=2', 302, '/')
343         exp.lib_del('Todo', 2)
344         self.check_get('/todo?id=2', 404)
345         self.check_json_get('/todo?id=1', exp)
346         # test deletion of adopting Todo
347         self.post_exp_day([exp], {'new_todo': [1]})
348         self.check_post({'adopt': 2}, '/todo?id=1')
349         self.check_post({'delete': ''}, '/todo?id=1', 302, '/')
350         exp.set('todo', 2)
351         exp.lib_del('Todo', 1)
352         self.check_json_get('/todo?id=2', exp)
353         # test cannot delete Todo with comment or effort
354         self.check_post({'comment': 'foo'}, '/todo?id=2')
355         self.check_post({'delete': ''}, '/todo?id=2', 500, '/')
356         self.check_post({'effort': 5}, '/todo?id=2')
357         self.check_post({'delete': ''}, '/todo?id=2', 500, '/')
358         # test deletion via effort < 0, but only if deletable
359         self.check_post({'effort': -1, 'comment': 'foo'}, '/todo?id=2')
360         self.check_post({}, '/todo?id=2')
361         self.check_get('/todo?id=2', 404)
362
363     def test_POST_todo_adoption(self) -> None:
364         """Test adoption via POST /todo with "adopt"."""
365         # post two Todos to Day, have first adopt second
366         exp = ExpectedGetTodo(1)
367         self.post_exp_process([exp], {}, 1)
368         self.post_exp_day([exp], {'new_todo': [1]})
369         self.post_exp_day([exp], {'new_todo': [1]})
370         self._post_exp_todo(1, {'adopt': 2}, exp)
371         exp.set('steps_todo_to_process', [exp.step_as_dict(1, [], todo=2)])
372         self.check_json_get('/todo?id=1', exp)
373         # test Todo un-adopting by just not sending an adopt
374         self._post_exp_todo(1, {}, exp)
375         exp.set('steps_todo_to_process', [])
376         self.check_json_get('/todo?id=1', exp)
377         # test fail on trying to adopt non-existing Todo
378         self.check_post({'adopt': 3}, '/todo?id=1', 404)
379         # test cannot self-adopt
380         self.check_post({'adopt': 1}, '/todo?id=1', 400)
381         # test cannot do 1-step circular adoption
382         self._post_exp_todo(2, {'adopt': 1}, exp)
383         self.check_post({'adopt': 2}, '/todo?id=1', 400)
384         # test cannot do 2-step circular adoption
385         self.post_exp_day([exp], {'new_todo': [1]})
386         self._post_exp_todo(3, {'adopt': 2}, exp)
387         self.check_post({'adopt': 3}, '/todo?id=1', 400)
388         # test can adopt Todo into ProcessStep chain via its Process (with key
389         # 'step_filler' equivalent to single-element 'adopt' if intable)
390         self.post_exp_process([exp], {}, 2)
391         self.post_exp_process([exp], {}, 3)
392         self.post_exp_process([exp], {'new_top_step': [2, 3]}, 1)
393         exp.lib_set('ProcessStep', [exp.procstep_as_dict(1, 1, 2),
394                                     exp.procstep_as_dict(2, 1, 3)])
395         step1_proc2 = exp.step_as_dict(1, [], 2, None, True)
396         step2_proc3 = exp.step_as_dict(2, [], 3, None, True)
397         exp.set('steps_todo_to_process', [step1_proc2, step2_proc3])
398         self.post_exp_day([exp], {'new_todo': [2]})
399         self.post_exp_day([exp], {'new_todo': [3]})
400         self.check_json_get('/todo?id=1', exp)
401         self._post_exp_todo(1, {'step_filler': 5, 'adopt': [4]}, exp)
402         step1_proc2 = exp.step_as_dict(1, [], 2, 4, True)
403         step2_proc3 = exp.step_as_dict(2, [], 3, 5, True)
404         exp.set('steps_todo_to_process', [step1_proc2, step2_proc3])
405         self.check_json_get('/todo?id=1', exp)
406         # test 'ignore' values for 'step_filler' are ignored, and intable
407         # 'step_filler' values are interchangeable with those of 'adopt'
408         todo_post = {'adopt': 5, 'step_filler': ['ignore', 4]}
409         self.check_post(todo_post, '/todo?id=1')
410         self.check_json_get('/todo?id=1', exp)
411         # test cannot adopt into non-top-level elements of chain, instead
412         # creating new top-level steps when adopting of respective Process
413         self.post_exp_process([exp], {}, 4)
414         self.post_exp_process([exp], {'new_top_step': 4, 'step_of': [1]}, 3)
415         exp.lib_set('ProcessStep', [exp.procstep_as_dict(3, 3, 4)])
416         step3_proc4 = exp.step_as_dict(3, [], 4, None, True)
417         step2_proc3 = exp.step_as_dict(2, [step3_proc4], 3, 5, True)
418         exp.set('steps_todo_to_process', [step1_proc2, step2_proc3])
419         self.post_exp_day([exp], {'new_todo': [4]})
420         self._post_exp_todo(1, {'adopt': [4, 5, 6]}, exp)
421         step4_todo6 = exp.step_as_dict(4, [], None, 6, False)
422         exp.set('steps_todo_to_process', [step1_proc2, step2_proc3,
423                                           step4_todo6])
424         self.check_json_get('/todo?id=1', exp)
425
426     def test_POST_todo_make_full(self) -> None:
427         """Test creation and adoption via POST /todo with "make_full"."""
428         # create chain of Processes
429         exp = ExpectedGetTodo(1)
430         self.post_exp_process([exp], {}, 1)
431         for i in range(1, 4):
432             self.post_exp_process([exp], {'new_top_step': i}, i+1)
433         exp.lib_set('ProcessStep', [exp.procstep_as_dict(1, 2, 1),
434                                     exp.procstep_as_dict(2, 3, 2),
435                                     exp.procstep_as_dict(3, 4, 3)])
436         step3_proc1 = exp.step_as_dict(3, [], 1, None, False)
437         step2_proc2 = exp.step_as_dict(2, [step3_proc1], 2, None, False)
438         step1_proc3 = exp.step_as_dict(1, [step2_proc2], 3, None, True)
439         exp.set('steps_todo_to_process', [step1_proc3])
440         # post (childless) Todo of chain end, then make_full on next in line
441         self.post_exp_day([exp], {'new_todo': [4]})
442         self.check_post({'step_filler': 'make_full_3'}, '/todo?id=1')
443         exp.set_todo_from_post(4, {'process_id': 1})
444         exp.set_todo_from_post(3, {'process_id': 2, 'children': [4]})
445         exp.set_todo_from_post(2, {'process_id': 3, 'children': [3]})
446         exp.set_todo_from_post(1, {'process_id': 4, 'children': [2]})
447         step3_proc1 = exp.step_as_dict(3, [], 1, 4, True)
448         step2_proc2 = exp.step_as_dict(2, [step3_proc1], 2, 3, True)
449         step1_proc3 = exp.step_as_dict(1, [step2_proc2], 3, 2, True)
450         exp.set('steps_todo_to_process', [step1_proc3])
451         self.check_json_get('/todo?id=1', exp)
452         # make new chain next to expected, find steps_todo_to_process extended,
453         # expect existing Todo demanded by new chain be adopted into new chain
454         self.check_post({'make_full': 2, 'adopt': [2]}, '/todo?id=1')
455         exp.set_todo_from_post(5, {'process_id': 2, 'children': [4]})
456         exp.set_todo_from_post(1, {'process_id': 4, 'children': [2, 5]})
457         step5_todo4 = exp.step_as_dict(5, [], None, 4)
458         step4_todo5 = exp.step_as_dict(4, [step5_todo4], None, 5)
459         exp.set('steps_todo_to_process', [step1_proc3, step4_todo5])
460         self.check_json_get('/todo?id=1', exp)
461         # fail on trying to call make_full on non-existing Process
462         self.check_post({'make_full': 5}, '/todo?id=1', 404)
463
464     def test_POST_todo_make_empty(self) -> None:
465         """Test creation and adoption via POST /todo with "make_empty"."""
466         # create chain of Processes
467         exp = ExpectedGetTodo(1)
468         self.post_exp_process([exp], {}, 1)
469         for i in range(1, 4):
470             self.post_exp_process([exp], {'new_top_step': i}, i+1)
471         exp.lib_set('ProcessStep', [exp.procstep_as_dict(1, 2, 1),
472                                     exp.procstep_as_dict(2, 3, 2),
473                                     exp.procstep_as_dict(3, 4, 3)])
474         # post (childless) Todo of chain end, then make empty on next in line
475         self.post_exp_day([exp], {'new_todo': [4]})
476         step3_proc1 = exp.step_as_dict(3, [], 1)
477         step2_proc2 = exp.step_as_dict(2, [step3_proc1], 2)
478         step1_proc3 = exp.step_as_dict(1, [step2_proc2], 3, None, True)
479         exp.set('steps_todo_to_process', [step1_proc3])
480         self.check_json_get('/todo?id=1', exp)
481         self.check_post({'step_filler': 'make_empty_3'}, '/todo?id=1')
482         exp.set_todo_from_post(2, {'process_id': 3})
483         exp.set_todo_from_post(1, {'process_id': 4, 'children': [2]})
484         step2_proc2 = exp.step_as_dict(2, [step3_proc1], 2, None, True)
485         step1_proc3 = exp.step_as_dict(1, [step2_proc2], 3, 2, True)
486         exp.set('steps_todo_to_process', [step1_proc3])
487         self.check_json_get('/todo?id=1', exp)
488         # make new top-level Todo without chain implied by its Process
489         self.check_post({'make_empty': 2, 'adopt': [2]}, '/todo?id=1')
490         exp.set_todo_from_post(3, {'process_id': 2})
491         exp.set_todo_from_post(1, {'process_id': 4, 'children': [2, 3]})
492         step4_todo3 = exp.step_as_dict(4, [], None, 3)
493         exp.set('steps_todo_to_process', [step1_proc3, step4_todo3])
494         self.check_json_get('/todo?id=1', exp)
495         # fail on trying to call make_empty on non-existing Process
496         self.check_post({'make_full': 5}, '/todo?id=1', 404)
497
498     def test_GET_todo(self) -> None:
499         """Test GET /todo response codes."""
500         # test malformed or illegal parameter values
501         self.check_get('/todo', 404)
502         self.check_get('/todo?id=', 404)
503         self.check_get('/todo?id=foo', 400)
504         self.check_get('/todo?id=0', 404)
505         self.check_get('/todo?id=2', 404)
506         # test all existing Processes are shown as available
507         exp = ExpectedGetTodo(1)
508         self.post_exp_process([exp], {}, 1)
509         self.post_exp_day([exp], {'new_todo': [1]})
510         self.post_exp_process([exp], {}, 2)
511         self.check_json_get('/todo?id=1', exp)
512         # test chain of Processes shown as potential step nodes
513         self.post_exp_process([exp], {}, 3)
514         self.post_exp_process([exp], {}, 4)
515         self.post_exp_process([exp], {'new_top_step': 2}, 1)
516         self.post_exp_process([exp], {'new_top_step': 3, 'step_of': [1]}, 2)
517         self.post_exp_process([exp], {'new_top_step': 4, 'step_of': [2]}, 3)
518         exp.lib_set('ProcessStep', [exp.procstep_as_dict(1, 1, 2, None),
519                                     exp.procstep_as_dict(2, 2, 3, None),
520                                     exp.procstep_as_dict(3, 3, 4, None)])
521         step3_proc4 = exp.step_as_dict(3, [], 4)
522         step2_proc3 = exp.step_as_dict(2, [step3_proc4], 3)
523         step1_proc2 = exp.step_as_dict(1, [step2_proc3], 2, fillable=True)
524         exp.set('steps_todo_to_process', [step1_proc2])
525         self.check_json_get('/todo?id=1', exp)
526         # test display of parallel chains
527         proc_steps_post = {'new_top_step': 4, 'kept_steps': [1, 3]}
528         self.post_exp_process([], proc_steps_post, 1)
529         step4_proc4 = exp.step_as_dict(4, [], 4, fillable=True)
530         exp.lib_set('ProcessStep', [exp.procstep_as_dict(4, 1, 4, None)])
531         exp.set('steps_todo_to_process', [step1_proc2, step4_proc4])
532         self.check_json_get('/todo?id=1', exp)
533
534     def test_POST_todo_doneness_relations(self) -> None:
535         """Test Todo.is_done Condition, adoption relations for /todo POSTs."""
536         self.post_exp_process([], {}, 1)
537         # test Todo with adoptee can only be set done if adoptee is done too
538         self.post_exp_day([], {'new_todo': [1]})
539         self.post_exp_day([], {'new_todo': [1]})
540         self.check_post({'adopt': 2, 'is_done': 1}, '/todo?id=1', 400)
541         self.check_post({'is_done': 1}, '/todo?id=2')
542         self.check_post({'adopt': 2, 'is_done': 1}, '/todo?id=1', 302)
543         # test Todo cannot be set undone with adopted Todo not done yet
544         self.check_post({'is_done': 0}, '/todo?id=2')
545         self.check_post({'adopt': 2, 'is_done': 0}, '/todo?id=1', 400)
546         # test unadoption relieves block
547         self.check_post({'is_done': 0}, '/todo?id=1', 302)
548         # test Condition being set or unset can block doneness setting
549         c1_post = {'title': '', 'description': '', 'is_active': 0}
550         c2_post = {'title': '', 'description': '', 'is_active': 1}
551         self.check_post(c1_post, '/condition', redir='/condition?id=1')
552         self.check_post(c2_post, '/condition', redir='/condition?id=2')
553         self.check_post({'conditions': [1], 'is_done': 1}, '/todo?id=1', 400)
554         self.check_post({'is_done': 1}, '/todo?id=1', 302)
555         self.check_post({'is_done': 0}, '/todo?id=1', 302)
556         self.check_post({'blockers': [2], 'is_done': 1}, '/todo?id=1', 400)
557         self.check_post({'is_done': 1}, '/todo?id=1', 302)
558         # test setting Todo doneness can set/un-set Conditions, but only on
559         # doneness change, not by mere passive state
560         self.check_post({'is_done': 0}, '/todo?id=2', 302)
561         self.check_post({'enables': [1], 'is_done': 1}, '/todo?id=1')
562         self.check_post({'conditions': [1], 'is_done': 1}, '/todo?id=2', 400)
563         self.check_post({'enables': [1], 'is_done': 0}, '/todo?id=1')
564         self.check_post({'enables': [1], 'is_done': 1}, '/todo?id=1')
565         self.check_post({'conditions': [1], 'is_done': 1}, '/todo?id=2')
566         self.check_post({'blockers': [1], 'is_done': 0}, '/todo?id=2', 400)
567         self.check_post({'disables': [1], 'is_done': 1}, '/todo?id=1')
568         self.check_post({'blockers': [1], 'is_done': 0}, '/todo?id=2', 400)
569         self.check_post({'disables': [1]}, '/todo?id=1')
570         self.check_post({'disables': [1], 'is_done': 1}, '/todo?id=1')
571         self.check_post({'blockers': [1]}, '/todo?id=2')