home · contact · privacy
Re-organize testing.
[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], {}, 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 = {'done': '', 'calendarize': '',
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 all of those except effort by empty post
309         self.check_post({}, '/todo?id=1')
310         exp.lib_set('Todo', [exp.todo_as_dict(effort=2.3)])
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_set('Todo', [exp.todo_as_dict(effort=None)])
315         self.check_json_get('/todo?id=1', exp)
316         # test Condition posts
317         c1_post = {'title': 'foo', 'description': 'oof', 'is_active': False}
318         c2_post = {'title': 'bar', 'description': 'rab', 'is_active': True}
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         todo_post = {'conditions': [1], 'disables': [1],
322                      'blockers': [2], 'enables': [2]}
323         self._post_exp_todo(1, todo_post, exp)
324         self.check_json_get('/todo?id=1', exp)
325
326     def test_POST_todo_deletion(self) -> None:
327         """Test deletions via POST /todo."""
328         exp = ExpectedGetTodo(1)
329         self.post_exp_process([exp], {}, 1)
330         # test failure of deletion on non-existing Todo
331         self.check_post({'delete': ''}, '/todo?id=2', 404, '/')
332         # test deletion of existing Todo
333         self.post_exp_day([exp], {'new_todo': [1]})
334         self.check_post({'delete': ''}, '/todo?id=1', 302, '/')
335         self.check_get('/todo?id=1', 404)
336         exp.lib_del('Todo', 1)
337         # test deletion of adopted Todo
338         self.post_exp_day([exp], {'new_todo': [1]})
339         self.post_exp_day([exp], {'new_todo': [1]})
340         self.check_post({'adopt': 2}, '/todo?id=1')
341         self.check_post({'delete': ''}, '/todo?id=2', 302, '/')
342         exp.lib_del('Todo', 2)
343         self.check_get('/todo?id=2', 404)
344         self.check_json_get('/todo?id=1', exp)
345         # test deletion of adopting Todo
346         self.post_exp_day([exp], {'new_todo': [1]})
347         self.check_post({'adopt': 2}, '/todo?id=1')
348         self.check_post({'delete': ''}, '/todo?id=1', 302, '/')
349         exp.set('todo', 2)
350         exp.lib_del('Todo', 1)
351         self.check_json_get('/todo?id=2', exp)
352         # test cannot delete Todo with comment or effort
353         self.check_post({'comment': 'foo'}, '/todo?id=2')
354         self.check_post({'delete': ''}, '/todo?id=2', 500, '/')
355         self.check_post({'effort': 5}, '/todo?id=2')
356         self.check_post({'delete': ''}, '/todo?id=2', 500, '/')
357         # test deletion via effort < 0, but only if deletable
358         self.check_post({'effort': -1, 'comment': 'foo'}, '/todo?id=2')
359         self.check_post({}, '/todo?id=2')
360         self.check_get('/todo?id=2', 404)
361
362     def test_POST_todo_adoption(self) -> None:
363         """Test adoption via POST /todo with "adopt"."""
364         # post two Todos to Day, have first adopt second
365         exp = ExpectedGetTodo(1)
366         self.post_exp_process([exp], {}, 1)
367         self.post_exp_day([exp], {'new_todo': [1]})
368         self.post_exp_day([exp], {'new_todo': [1]})
369         self._post_exp_todo(1, {'adopt': 2}, exp)
370         exp.set('steps_todo_to_process', [exp.step_as_dict(1, [], todo=2)])
371         self.check_json_get('/todo?id=1', exp)
372         # test Todo un-adopting by just not sending an adopt
373         self._post_exp_todo(1, {}, exp)
374         exp.set('steps_todo_to_process', [])
375         self.check_json_get('/todo?id=1', exp)
376         # test fail on trying to adopt non-existing Todo
377         self.check_post({'adopt': 3}, '/todo?id=1', 404)
378         # test cannot self-adopt
379         self.check_post({'adopt': 1}, '/todo?id=1', 400)
380         # test cannot do 1-step circular adoption
381         self._post_exp_todo(2, {'adopt': 1}, exp)
382         self.check_post({'adopt': 2}, '/todo?id=1', 400)
383         # test cannot do 2-step circular adoption
384         self.post_exp_day([exp], {'new_todo': [1]})
385         self._post_exp_todo(3, {'adopt': 2}, exp)
386         self.check_post({'adopt': 3}, '/todo?id=1', 400)
387         # test can adopt Todo into ProcessStep chain via its Process (with key
388         # 'step_filler' equivalent to single-element 'adopt' if intable)
389         self.post_exp_process([exp], {}, 2)
390         self.post_exp_process([exp], {}, 3)
391         self.post_exp_process([exp], {'new_top_step': [2, 3]}, 1)
392         exp.lib_set('ProcessStep', [exp.procstep_as_dict(1, 1, 2),
393                                     exp.procstep_as_dict(2, 1, 3)])
394         step1_proc2 = exp.step_as_dict(1, [], 2, None, True)
395         step2_proc3 = exp.step_as_dict(2, [], 3, None, True)
396         exp.set('steps_todo_to_process', [step1_proc2, step2_proc3])
397         self.post_exp_day([exp], {'new_todo': [2]})
398         self.post_exp_day([exp], {'new_todo': [3]})
399         self.check_json_get('/todo?id=1', exp)
400         self._post_exp_todo(1, {'step_filler': 5, 'adopt': [4]}, exp)
401         step1_proc2 = exp.step_as_dict(1, [], 2, 4, True)
402         step2_proc3 = exp.step_as_dict(2, [], 3, 5, True)
403         exp.set('steps_todo_to_process', [step1_proc2, step2_proc3])
404         self.check_json_get('/todo?id=1', exp)
405         # test 'ignore' values for 'step_filler' are ignored, and intable
406         # 'step_filler' values are interchangeable with those of 'adopt'
407         todo_post = {'adopt': 5, 'step_filler': ['ignore', 4]}
408         self.check_post(todo_post, '/todo?id=1')
409         self.check_json_get('/todo?id=1', exp)
410         # test cannot adopt into non-top-level elements of chain, instead
411         # creating new top-level steps when adopting of respective Process
412         self.post_exp_process([exp], {}, 4)
413         self.post_exp_process([exp], {'new_top_step': 4, 'step_of': [1]}, 3)
414         exp.lib_set('ProcessStep', [exp.procstep_as_dict(3, 3, 4)])
415         step3_proc4 = exp.step_as_dict(3, [], 4, None, True)
416         step2_proc3 = exp.step_as_dict(2, [step3_proc4], 3, 5, True)
417         exp.set('steps_todo_to_process', [step1_proc2, step2_proc3])
418         self.post_exp_day([exp], {'new_todo': [4]})
419         self._post_exp_todo(1, {'adopt': [4, 5, 6]}, exp)
420         step4_todo6 = exp.step_as_dict(4, [], None, 6, False)
421         exp.set('steps_todo_to_process', [step1_proc2, step2_proc3,
422                                           step4_todo6])
423         self.check_json_get('/todo?id=1', exp)
424
425     def test_POST_todo_make_full(self) -> None:
426         """Test creation and adoption via POST /todo with "make_full"."""
427         # create chain of Processes
428         exp = ExpectedGetTodo(1)
429         self.post_exp_process([exp], {}, 1)
430         for i in range(1, 4):
431             self.post_exp_process([exp], {'new_top_step': i}, i+1)
432         exp.lib_set('ProcessStep', [exp.procstep_as_dict(1, 2, 1),
433                                     exp.procstep_as_dict(2, 3, 2),
434                                     exp.procstep_as_dict(3, 4, 3)])
435         step3_proc1 = exp.step_as_dict(3, [], 1, None, False)
436         step2_proc2 = exp.step_as_dict(2, [step3_proc1], 2, None, False)
437         step1_proc3 = exp.step_as_dict(1, [step2_proc2], 3, None, True)
438         exp.set('steps_todo_to_process', [step1_proc3])
439         # post (childless) Todo of chain end, then make_full on next in line
440         self.post_exp_day([exp], {'new_todo': [4]})
441         self.check_post({'step_filler': 'make_full_3'}, '/todo?id=1')
442         exp.set_todo_from_post(4, {'process_id': 1})
443         exp.set_todo_from_post(3, {'process_id': 2, 'children': [4]})
444         exp.set_todo_from_post(2, {'process_id': 3, 'children': [3]})
445         exp.set_todo_from_post(1, {'process_id': 4, 'children': [2]})
446         step3_proc1 = exp.step_as_dict(3, [], 1, 4, True)
447         step2_proc2 = exp.step_as_dict(2, [step3_proc1], 2, 3, True)
448         step1_proc3 = exp.step_as_dict(1, [step2_proc2], 3, 2, True)
449         exp.set('steps_todo_to_process', [step1_proc3])
450         self.check_json_get('/todo?id=1', exp)
451         # make new chain next to expected, find steps_todo_to_process extended,
452         # expect existing Todo demanded by new chain be adopted into new chain
453         self.check_post({'make_full': 2, 'adopt': [2]}, '/todo?id=1')
454         exp.set_todo_from_post(5, {'process_id': 2, 'children': [4]})
455         exp.set_todo_from_post(1, {'process_id': 4, 'children': [2, 5]})
456         step5_todo4 = exp.step_as_dict(5, [], None, 4)
457         step4_todo5 = exp.step_as_dict(4, [step5_todo4], None, 5)
458         exp.set('steps_todo_to_process', [step1_proc3, step4_todo5])
459         self.check_json_get('/todo?id=1', exp)
460         # fail on trying to call make_full on non-existing Process
461         self.check_post({'make_full': 5}, '/todo?id=1', 404)
462
463     def test_POST_todo_make_empty(self) -> None:
464         """Test creation and adoption via POST /todo with "make_empty"."""
465         # create chain of Processes
466         exp = ExpectedGetTodo(1)
467         self.post_exp_process([exp], {}, 1)
468         for i in range(1, 4):
469             self.post_exp_process([exp], {'new_top_step': i}, i+1)
470         exp.lib_set('ProcessStep', [exp.procstep_as_dict(1, 2, 1),
471                                     exp.procstep_as_dict(2, 3, 2),
472                                     exp.procstep_as_dict(3, 4, 3)])
473         # post (childless) Todo of chain end, then make empty on next in line
474         self.post_exp_day([exp], {'new_todo': [4]})
475         step3_proc1 = exp.step_as_dict(3, [], 1)
476         step2_proc2 = exp.step_as_dict(2, [step3_proc1], 2)
477         step1_proc3 = exp.step_as_dict(1, [step2_proc2], 3, None, True)
478         exp.set('steps_todo_to_process', [step1_proc3])
479         self.check_json_get('/todo?id=1', exp)
480         self.check_post({'step_filler': 'make_empty_3'}, '/todo?id=1')
481         exp.set_todo_from_post(2, {'process_id': 3})
482         exp.set_todo_from_post(1, {'process_id': 4, 'children': [2]})
483         step2_proc2 = exp.step_as_dict(2, [step3_proc1], 2, None, True)
484         step1_proc3 = exp.step_as_dict(1, [step2_proc2], 3, 2, True)
485         exp.set('steps_todo_to_process', [step1_proc3])
486         self.check_json_get('/todo?id=1', exp)
487         # make new top-level Todo without chain implied by its Process
488         self.check_post({'make_empty': 2, 'adopt': [2]}, '/todo?id=1')
489         exp.set_todo_from_post(3, {'process_id': 2})
490         exp.set_todo_from_post(1, {'process_id': 4, 'children': [2, 3]})
491         step4_todo3 = exp.step_as_dict(4, [], None, 3)
492         exp.set('steps_todo_to_process', [step1_proc3, step4_todo3])
493         self.check_json_get('/todo?id=1', exp)
494         # fail on trying to call make_empty on non-existing Process
495         self.check_post({'make_full': 5}, '/todo?id=1', 404)
496
497     def test_GET_todo(self) -> None:
498         """Test GET /todo response codes."""
499         # test malformed or illegal parameter values
500         self.check_get('/todo', 404)
501         self.check_get('/todo?id=', 404)
502         self.check_get('/todo?id=foo', 400)
503         self.check_get('/todo?id=0', 404)
504         self.check_get('/todo?id=2', 404)
505         # test all existing Processes are shown as available
506         exp = ExpectedGetTodo(1)
507         self.post_exp_process([exp], {}, 1)
508         self.post_exp_day([exp], {'new_todo': [1]})
509         self.post_exp_process([exp], {}, 2)
510         self.check_json_get('/todo?id=1', exp)
511         # test chain of Processes shown as potential step nodes
512         self.post_exp_process([exp], {}, 3)
513         self.post_exp_process([exp], {}, 4)
514         self.post_exp_process([exp], {'new_top_step': 2}, 1)
515         self.post_exp_process([exp], {'new_top_step': 3, 'step_of': [1]}, 2)
516         self.post_exp_process([exp], {'new_top_step': 4, 'step_of': [2]}, 3)
517         exp.lib_set('ProcessStep', [exp.procstep_as_dict(1, 1, 2, None),
518                                     exp.procstep_as_dict(2, 2, 3, None),
519                                     exp.procstep_as_dict(3, 3, 4, None)])
520         step3_proc4 = exp.step_as_dict(3, [], 4)
521         step2_proc3 = exp.step_as_dict(2, [step3_proc4], 3)
522         step1_proc2 = exp.step_as_dict(1, [step2_proc3], 2, fillable=True)
523         exp.set('steps_todo_to_process', [step1_proc2])
524         self.check_json_get('/todo?id=1', exp)
525         # test display of parallel chains
526         proc_steps_post = {'new_top_step': 4, 'keep_step': [1],
527                            'step_1_process_id': 2, 'steps': [1, 4]}
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, 'done': ''}, '/todo?id=1', 400)
541         self.check_post({'done': ''}, '/todo?id=2')
542         self.check_post({'adopt': 2, 'done': ''}, '/todo?id=1', 302)
543         # test Todo cannot be set undone with adopted Todo not done yet
544         self.check_post({}, '/todo?id=2')
545         self.check_post({'adopt': 2}, '/todo?id=1', 400)
546         # test unadoption relieves block
547         self.check_post({}, '/todo?id=1', 302)
548         # test Condition being set or unset can block doneness setting
549         c1_post = {'title': '', 'description': '', 'is_active': False}
550         c2_post = {'title': '', 'description': '', 'is_active': True}
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], 'done': ''}, '/todo?id=1', 400)
554         self.check_post({'done': ''}, '/todo?id=1', 302)
555         self.check_post({'blockers': [2]}, '/todo?id=1', 400)
556         self.check_post({'done': ''}, '/todo?id=1', 302)
557         # test setting Todo doneness can set/un-set Conditions, but only on
558         # doneness change, not by mere passive state
559         self.check_post({'enables': [1], 'done': ''}, '/todo?id=1')
560         self.check_post({'conditions': [1], 'done': ''}, '/todo?id=2', 400)
561         self.check_post({'enables': [1]}, '/todo?id=1')
562         self.check_post({'enables': [1], 'done': ''}, '/todo?id=1')
563         self.check_post({'conditions': [1], 'done': ''}, '/todo?id=2')
564         self.check_post({'blockers': [1]}, '/todo?id=2', 400)
565         self.check_post({'disables': [1], 'done': ''}, '/todo?id=1')
566         self.check_post({'blockers': [1]}, '/todo?id=2', 400)
567         self.check_post({'disables': [1]}, '/todo?id=1')
568         self.check_post({'disables': [1], 'done': ''}, '/todo?id=1')
569         self.check_post({'blockers': [1]}, '/todo?id=2')