home · contact · privacy
Fix bug of /day POSTS breaking on empty new_todo fields.
[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     checked_class = Todo
270
271     def _post_exp_todo(
272             self, id_: int, payload: dict[str, Any], exp: Expected) -> None:
273         self.check_post(payload, f'/todo?id={id_}')
274         exp.set_todo_from_post(id_, payload)
275
276     def test_basic_fail_POST_todo(self) -> None:
277         """Test basic malformed/illegal POST /todo requests."""
278         self.post_exp_process([], {}, 1)
279         # test we cannot just POST into non-existing Todo
280         self.check_post({}, '/todo', 404)
281         self.check_post({}, '/todo?id=FOO', 400)
282         self.check_post({}, '/todo?id=0', 400)
283         self.check_post({}, '/todo?id=1', 404)
284         # test malformed values on existing Todo
285         self.post_exp_day([], {'new_todo': [1]})
286         for name in ['adopt', 'effort', 'make_full', 'make_empty',
287                      'conditions', 'disables', 'blockers', 'enables']:
288             self.check_post({name: 'x'}, '/todo?id=1', 400, '/todo')
289         for prefix in ['make_', '']:
290             for suffix in ['', 'x', '1.1']:
291                 self.check_post({'step_filler_to_1': [f'{prefix}{suffix}']},
292                                 '/todo?id=1', 400, '/todo')
293         for suffix in ['', 'x', '1.1']:
294             self.check_post({'step_filler_to_{suffix}': ['1']},
295                             '/todo?id=1', 400, '/todo')
296
297     def test_basic_POST_todo(self) -> None:
298         """Test basic POST /todo manipulations."""
299         exp = ExpectedGetTodo(1)
300         self.post_exp_process([exp], {'calendarize': 0}, 1)
301         self.post_exp_day([exp], {'new_todo': [1]})
302         # test posting naked entity at first changes nothing
303         self.check_json_get('/todo?id=1', exp)
304         self.check_post({}, '/todo?id=1')
305         self.check_json_get('/todo?id=1', exp)
306         # test posting doneness, comment, calendarization, effort
307         todo_post = {'is_done': 1, 'calendarize': 1,
308                      'comment': 'foo', 'effort': 2.3}
309         self._post_exp_todo(1, todo_post, exp)
310         self.check_json_get('/todo?id=1', exp)
311         # test implicitly un-setting (only) comment by empty post
312         self.check_post({}, '/todo?id=1')
313         exp.lib_get('Todo', 1)['comment'] = ''
314         self.check_json_get('/todo?id=1', exp)
315         # test effort post can be explicitly unset by "effort":"" post
316         self.check_post({'effort': ''}, '/todo?id=1')
317         exp.lib_get('Todo', 1)['effort'] = None
318         self.check_json_get('/todo?id=1', exp)
319         # test Condition posts
320         c1_post = {'title': 'foo', 'description': 'oof', 'is_active': 0}
321         c2_post = {'title': 'bar', 'description': 'rab', 'is_active': 1}
322         self.post_exp_cond([exp], c1_post, 1)
323         self.post_exp_cond([exp], c2_post, 2)
324         self.check_json_get('/todo?id=1', exp)
325         todo_post = {'conditions': [1], 'disables': [1],
326                      'blockers': [2], 'enables': [2]}
327         self._post_exp_todo(1, todo_post, exp)
328         self.check_json_get('/todo?id=1', exp)
329
330     def test_POST_todo_deletion(self) -> None:
331         """Test deletions via POST /todo."""
332         exp = ExpectedGetTodo(1)
333         self.post_exp_process([exp], {}, 1)
334         # test failure of deletion on non-existing Todo
335         self.check_post({'delete': ''}, '/todo?id=2', 404, '/')
336         # test deletion of existing Todo
337         self.post_exp_day([exp], {'new_todo': [1]})
338         self.check_post({'delete': ''}, '/todo?id=1', 302, '/')
339         self.check_get('/todo?id=1', 404)
340         exp.lib_del('Todo', 1)
341         # test deletion of adopted Todo
342         self.post_exp_day([exp], {'new_todo': [1]})
343         self.post_exp_day([exp], {'new_todo': [1]})
344         self.check_post({'adopt': 2}, '/todo?id=1')
345         self.check_post({'delete': ''}, '/todo?id=2', 302, '/')
346         exp.lib_del('Todo', 2)
347         self.check_get('/todo?id=2', 404)
348         self.check_json_get('/todo?id=1', exp)
349         # test deletion of adopting Todo
350         self.post_exp_day([exp], {'new_todo': [1]})
351         self.check_post({'adopt': 2}, '/todo?id=1')
352         self.check_post({'delete': ''}, '/todo?id=1', 302, '/')
353         exp.set('todo', 2)
354         exp.lib_del('Todo', 1)
355         self.check_json_get('/todo?id=2', exp)
356         # test cannot delete Todo with comment or effort
357         self.check_post({'comment': 'foo'}, '/todo?id=2')
358         self.check_post({'delete': ''}, '/todo?id=2', 500, '/')
359         self.check_post({'effort': 5}, '/todo?id=2')
360         self.check_post({'delete': ''}, '/todo?id=2', 500, '/')
361         # test deletion via effort < 0, but only if deletable
362         self.check_post({'effort': -1, 'comment': 'foo'}, '/todo?id=2')
363         self.check_post({}, '/todo?id=2')
364         self.check_get('/todo?id=2', 404)
365
366     def test_POST_todo_adoption(self) -> None:
367         """Test adoption via POST /todo with "adopt"."""
368         # post two Todos to Day, have first adopt second
369         exp = ExpectedGetTodo(1)
370         self.post_exp_process([exp], {}, 1)
371         self.post_exp_day([exp], {'new_todo': [1]})
372         self.post_exp_day([exp], {'new_todo': [1]})
373         self._post_exp_todo(1, {'adopt': 2}, exp)
374         exp.set('steps_todo_to_process', [exp.step_as_dict(1, [], todo=2)])
375         self.check_json_get('/todo?id=1', exp)
376         # test Todo un-adopting by just not sending an adopt
377         self._post_exp_todo(1, {}, exp)
378         exp.set('steps_todo_to_process', [])
379         self.check_json_get('/todo?id=1', exp)
380         # test fail on trying to adopt non-existing Todo
381         self.check_post({'adopt': 3}, '/todo?id=1', 404)
382         # test cannot self-adopt
383         self.check_post({'adopt': 1}, '/todo?id=1', 400)
384         # test cannot do 1-step circular adoption
385         self._post_exp_todo(2, {'adopt': 1}, exp)
386         self.check_post({'adopt': 2}, '/todo?id=1', 400)
387         # test cannot do 2-step circular adoption
388         self.post_exp_day([exp], {'new_todo': [1]})
389         self._post_exp_todo(3, {'adopt': 2}, exp)
390         self.check_post({'adopt': 3}, '/todo?id=1', 400)
391         # test can adopt Todo into ProcessStep chain via its Process (with key
392         # 'step_filler' equivalent to single-element 'adopt' if intable)
393         self.post_exp_process([exp], {}, 2)
394         self.post_exp_process([exp], {}, 3)
395         self.post_exp_process([exp], {'new_top_step': [2, 3]}, 1)
396         exp.lib_set('ProcessStep', [exp.procstep_as_dict(1, 1, 2),
397                                     exp.procstep_as_dict(2, 1, 3)])
398         step1_proc2 = exp.step_as_dict(1, [], 2, None, True)
399         step2_proc3 = exp.step_as_dict(2, [], 3, None, True)
400         exp.set('steps_todo_to_process', [step1_proc2, step2_proc3])
401         self.post_exp_day([exp], {'new_todo': [2]})
402         self.post_exp_day([exp], {'new_todo': [3]})
403         self.check_json_get('/todo?id=1', exp)
404         self._post_exp_todo(1, {'step_filler_to_1': 5, 'adopt': [4]}, exp)
405         exp.lib_get('Todo', 1)['children'] += [5]
406         step1_proc2 = exp.step_as_dict(1, [], 2, 4, True)
407         step2_proc3 = exp.step_as_dict(2, [], 3, 5, True)
408         exp.set('steps_todo_to_process', [step1_proc2, step2_proc3])
409         self.check_json_get('/todo?id=1', exp)
410         # test 'ignore' values for 'step_filler' are ignored, and intable
411         # 'step_filler' values are interchangeable with those of 'adopt'
412         todo_post = {'adopt': 5, 'step_filler_to_1': ['ignore', 4]}
413         self.check_post(todo_post, '/todo?id=1')
414         self.check_json_get('/todo?id=1', exp)
415         # test cannot adopt into non-top-level elements of chain, instead
416         # creating new top-level steps when adopting of respective Process
417         self.post_exp_process([exp], {}, 4)
418         self.post_exp_process([exp], {'new_top_step': 4, 'step_of': [1]}, 3)
419         exp.lib_set('ProcessStep', [exp.procstep_as_dict(3, 3, 4)])
420         step3_proc4 = exp.step_as_dict(3, [], 4, None, True)
421         step2_proc3 = exp.step_as_dict(2, [step3_proc4], 3, 5, True)
422         exp.set('steps_todo_to_process', [step1_proc2, step2_proc3])
423         self.post_exp_day([exp], {'new_todo': [4]})
424         self._post_exp_todo(1, {'adopt': [4, 5, 6]}, exp)
425         step4_todo6 = exp.step_as_dict(4, [], None, 6, False)
426         exp.set('steps_todo_to_process', [step1_proc2, step2_proc3,
427                                           step4_todo6])
428         self.check_json_get('/todo?id=1', exp)
429
430     def test_POST_todo_make_empty(self) -> None:
431         """Test creation via POST /todo "step_filler_to"/"make"."""
432         # create chain of Processes
433         exp = ExpectedGetTodo(1)
434         self.post_exp_process([exp], {}, 1)
435         for i in range(1, 4):
436             self.post_exp_process([exp], {'new_top_step': i}, i+1)
437         exp.lib_set('ProcessStep', [exp.procstep_as_dict(1, 2, 1),
438                                     exp.procstep_as_dict(2, 3, 2),
439                                     exp.procstep_as_dict(3, 4, 3)])
440         # post (childless) Todo of chain end, then make empty on next in line
441         self.post_exp_day([exp], {'new_todo': [4]})
442         step3_proc1 = exp.step_as_dict(3, [], 1)
443         step2_proc2 = exp.step_as_dict(2, [step3_proc1], 2)
444         step1_proc3 = exp.step_as_dict(1, [step2_proc2], 3, None, True)
445         exp.set('steps_todo_to_process', [step1_proc3])
446         self.check_json_get('/todo?id=1', exp)
447         self.check_post({'step_filler_to_1': 'make_3'}, '/todo?id=1')
448         exp.set_todo_from_post(2, {'process_id': 3})
449         exp.set_todo_from_post(1, {'process_id': 4, 'children': [2]})
450         step2_proc2 = exp.step_as_dict(2, [step3_proc1], 2, None, True)
451         step1_proc3 = exp.step_as_dict(1, [step2_proc2], 3, 2, True)
452         exp.set('steps_todo_to_process', [step1_proc3])
453         self.check_json_get('/todo?id=1', exp)
454         # make new top-level Todo without chain implied by its Process
455         self.check_post({'make_empty': 2, 'adopt': [2]}, '/todo?id=1')
456         exp.set_todo_from_post(3, {'process_id': 2})
457         exp.set_todo_from_post(1, {'process_id': 4, 'children': [2, 3]})
458         step4_todo3 = exp.step_as_dict(4, [], None, 3)
459         exp.set('steps_todo_to_process', [step1_proc3, step4_todo3])
460         self.check_json_get('/todo?id=1', exp)
461         # fail on trying to call make_empty on non-existing Process
462         self.check_post({'make_full': 5}, '/todo?id=1', 404)
463
464     def test_GET_todo(self) -> None:
465         """Test GET /todo response codes."""
466         # test malformed or illegal parameter values
467         self.check_get_defaults('/todo')
468         # test all existing Processes are shown as available
469         exp = ExpectedGetTodo(1)
470         self.post_exp_process([exp], {}, 1)
471         self.post_exp_day([exp], {'new_todo': [1]})
472         self.post_exp_process([exp], {}, 2)
473         self.check_json_get('/todo?id=1', exp)
474         # test chain of Processes shown as potential step nodes
475         self.post_exp_process([exp], {}, 3)
476         self.post_exp_process([exp], {}, 4)
477         self.post_exp_process([exp], {'new_top_step': 2}, 1)
478         self.post_exp_process([exp], {'new_top_step': 3, 'step_of': [1]}, 2)
479         self.post_exp_process([exp], {'new_top_step': 4, 'step_of': [2]}, 3)
480         exp.lib_set('ProcessStep', [exp.procstep_as_dict(1, 1, 2, None),
481                                     exp.procstep_as_dict(2, 2, 3, None),
482                                     exp.procstep_as_dict(3, 3, 4, None)])
483         step3_proc4 = exp.step_as_dict(3, [], 4)
484         step2_proc3 = exp.step_as_dict(2, [step3_proc4], 3)
485         step1_proc2 = exp.step_as_dict(1, [step2_proc3], 2, fillable=True)
486         exp.set('steps_todo_to_process', [step1_proc2])
487         self.check_json_get('/todo?id=1', exp)
488         # test display of parallel chains
489         proc_steps_post = {'new_top_step': 4, 'kept_steps': [1, 3]}
490         self.post_exp_process([], proc_steps_post, 1)
491         step4_proc4 = exp.step_as_dict(4, [], 4, fillable=True)
492         exp.lib_set('ProcessStep', [exp.procstep_as_dict(4, 1, 4, None)])
493         exp.set('steps_todo_to_process', [step1_proc2, step4_proc4])
494         self.check_json_get('/todo?id=1', exp)
495
496     def test_POST_todo_doneness_relations(self) -> None:
497         """Test Todo.is_done Condition, adoption relations for /todo POSTs."""
498         self.post_exp_process([], {}, 1)
499         # test Todo with adoptee can only be set done if adoptee is done too
500         self.post_exp_day([], {'new_todo': [1]})
501         self.post_exp_day([], {'new_todo': [1]})
502         self.check_post({'adopt': 2, 'is_done': 1}, '/todo?id=1', 400)
503         self.check_post({'is_done': 1}, '/todo?id=2')
504         self.check_post({'adopt': 2, 'is_done': 1}, '/todo?id=1', 302)
505         # test Todo cannot be set undone with adopted Todo not done yet
506         self.check_post({'is_done': 0}, '/todo?id=2')
507         self.check_post({'adopt': 2, 'is_done': 0}, '/todo?id=1', 400)
508         # test unadoption relieves block
509         self.check_post({'is_done': 0}, '/todo?id=1', 302)
510         # test Condition being set or unset can block doneness setting
511         c1_post = {'title': '', 'description': '', 'is_active': 0}
512         c2_post = {'title': '', 'description': '', 'is_active': 1}
513         self.check_post(c1_post, '/condition', redir='/condition?id=1')
514         self.check_post(c2_post, '/condition', redir='/condition?id=2')
515         self.check_post({'conditions': [1], 'is_done': 1}, '/todo?id=1', 400)
516         self.check_post({'is_done': 1}, '/todo?id=1', 302)
517         self.check_post({'is_done': 0}, '/todo?id=1', 302)
518         self.check_post({'blockers': [2], 'is_done': 1}, '/todo?id=1', 400)
519         self.check_post({'is_done': 1}, '/todo?id=1', 302)
520         # test setting Todo doneness can set/un-set Conditions, but only on
521         # doneness change, not by mere passive state
522         self.check_post({'is_done': 0}, '/todo?id=2', 302)
523         self.check_post({'enables': [1], 'is_done': 1}, '/todo?id=1')
524         self.check_post({'conditions': [1], 'is_done': 1}, '/todo?id=2', 400)
525         self.check_post({'enables': [1], 'is_done': 0}, '/todo?id=1')
526         self.check_post({'enables': [1], 'is_done': 1}, '/todo?id=1')
527         self.check_post({'conditions': [1], 'is_done': 1}, '/todo?id=2')
528         self.check_post({'blockers': [1], 'is_done': 0}, '/todo?id=2', 400)
529         self.check_post({'disables': [1], 'is_done': 1}, '/todo?id=1')
530         self.check_post({'blockers': [1], 'is_done': 0}, '/todo?id=2', 400)
531         self.check_post({'disables': [1]}, '/todo?id=1')
532         self.check_post({'disables': [1], 'is_done': 1}, '/todo?id=1')
533         self.check_post({'blockers': [1]}, '/todo?id=2')