home · contact · privacy
Extend and refactor tests.
[plomtask] / tests / todos.py
1 """Test Todos module."""
2 from tests.utils import TestCaseSansDB, TestCaseWithDB, TestCaseWithServer
3 from plomtask.todos import Todo, TodoNode
4 from plomtask.processes import Process, ProcessStep
5 from plomtask.conditions import Condition
6 from plomtask.exceptions import (NotFoundException, BadFormatException,
7                                  HandledException)
8
9
10 class TestsWithDB(TestCaseWithDB, TestCaseSansDB):
11     """Tests requiring DB, but not server setup.
12
13     NB: We subclass TestCaseSansDB too, to run any tests there that due to any
14     Todo requiring a _saved_ Process wouldn't run without a DB.
15     """
16     checked_class = Todo
17     default_init_kwargs = {'process': None, 'is_done': False,
18                            'date': '2024-01-01'}
19
20     def setUp(self) -> None:
21         super().setUp()
22         self.date1 = '2024-01-01'
23         self.date2 = '2024-01-02'
24         self.proc = Process(None)
25         self.proc.save(self.db_conn)
26         self.cond1 = Condition(None)
27         self.cond1.save(self.db_conn)
28         self.cond2 = Condition(None)
29         self.cond2.save(self.db_conn)
30         self.default_init_kwargs['process'] = self.proc
31
32     def test_Todo_init(self) -> None:
33         """Test creation of Todo and what they default to."""
34         process = Process(None)
35         with self.assertRaises(NotFoundException):
36             Todo(None, process, False, self.date1)
37         process.save(self.db_conn)
38         assert isinstance(self.cond1.id_, int)
39         assert isinstance(self.cond2.id_, int)
40         process.set_conditions(self.db_conn, [self.cond1.id_, self.cond2.id_])
41         process.set_enables(self.db_conn, [self.cond1.id_])
42         process.set_disables(self.db_conn, [self.cond2.id_])
43         todo_no_id = Todo(None, process, False, self.date1)
44         self.assertEqual(todo_no_id.conditions, [self.cond1, self.cond2])
45         self.assertEqual(todo_no_id.enables, [self.cond1])
46         self.assertEqual(todo_no_id.disables, [self.cond2])
47         todo_yes_id = Todo(5, process, False, self.date1)
48         self.assertEqual(todo_yes_id.conditions, [])
49         self.assertEqual(todo_yes_id.enables, [])
50         self.assertEqual(todo_yes_id.disables, [])
51
52     def test_Todo_by_date(self) -> None:
53         """Test findability of Todos by date."""
54         t1 = Todo(None, self.proc, False, self.date1)
55         t1.save(self.db_conn)
56         t2 = Todo(None, self.proc, False, self.date1)
57         t2.save(self.db_conn)
58         self.assertEqual(Todo.by_date(self.db_conn, self.date1), [t1, t2])
59         self.assertEqual(Todo.by_date(self.db_conn, self.date2), [])
60         with self.assertRaises(BadFormatException):
61             self.assertEqual(Todo.by_date(self.db_conn, 'foo'), [])
62
63     def test_Todo_by_date_range_with_limits(self) -> None:
64         """Test .by_date_range_with_limits."""
65         self.check_by_date_range_with_limits('day')
66
67     def test_Todo_on_conditions(self) -> None:
68         """Test effect of Todos on Conditions."""
69         assert isinstance(self.cond1.id_, int)
70         assert isinstance(self.cond2.id_, int)
71         todo = Todo(None, self.proc, False, self.date1)
72         todo.save(self.db_conn)
73         todo.set_enables(self.db_conn, [self.cond1.id_])
74         todo.set_disables(self.db_conn, [self.cond2.id_])
75         todo.is_done = True
76         self.assertEqual(self.cond1.is_active, True)
77         self.assertEqual(self.cond2.is_active, False)
78         todo.is_done = False
79         self.assertEqual(self.cond1.is_active, True)
80         self.assertEqual(self.cond2.is_active, False)
81
82     def test_Todo_children(self) -> None:
83         """Test Todo.children relations."""
84         todo_1 = Todo(None, self.proc, False, self.date1)
85         todo_2 = Todo(None, self.proc, False, self.date1)
86         todo_2.save(self.db_conn)
87         with self.assertRaises(HandledException):
88             todo_1.add_child(todo_2)
89         todo_1.save(self.db_conn)
90         todo_3 = Todo(None, self.proc, False, self.date1)
91         with self.assertRaises(HandledException):
92             todo_1.add_child(todo_3)
93         todo_3.save(self.db_conn)
94         todo_1.add_child(todo_3)
95         todo_1.save(self.db_conn)
96         assert isinstance(todo_1.id_, int)
97         todo_retrieved = Todo.by_id(self.db_conn, todo_1.id_)
98         self.assertEqual(todo_retrieved.children, [todo_3])
99         with self.assertRaises(BadFormatException):
100             todo_3.add_child(todo_1)
101
102     def test_Todo_conditioning(self) -> None:
103         """Test Todo.doability conditions."""
104         assert isinstance(self.cond1.id_, int)
105         todo_1 = Todo(None, self.proc, False, self.date1)
106         todo_1.save(self.db_conn)
107         todo_2 = Todo(None, self.proc, False, self.date1)
108         todo_2.save(self.db_conn)
109         todo_2.add_child(todo_1)
110         with self.assertRaises(BadFormatException):
111             todo_2.is_done = True
112         todo_1.is_done = True
113         todo_2.is_done = True
114         todo_2.is_done = False
115         todo_2.set_conditions(self.db_conn, [self.cond1.id_])
116         with self.assertRaises(BadFormatException):
117             todo_2.is_done = True
118         self.cond1.is_active = True
119         todo_2.is_done = True
120
121     def test_Todo_step_tree(self) -> None:
122         """Test self-configuration of TodoStepsNode tree for Day view."""
123         todo_1 = Todo(None, self.proc, False, self.date1)
124         todo_1.save(self.db_conn)
125         assert isinstance(todo_1.id_, int)
126         # test minimum
127         node_0 = TodoNode(todo_1, False, [])
128         self.assertEqual(todo_1.get_step_tree(set()).as_dict, node_0.as_dict)
129         # test non_emtpy seen_todo does something
130         node_0.seen = True
131         self.assertEqual(todo_1.get_step_tree({todo_1.id_}).as_dict,
132                          node_0.as_dict)
133         # test child shows up
134         todo_2 = Todo(None, self.proc, False, self.date1)
135         todo_2.save(self.db_conn)
136         assert isinstance(todo_2.id_, int)
137         todo_1.add_child(todo_2)
138         node_2 = TodoNode(todo_2, False, [])
139         node_0.children = [node_2]
140         node_0.seen = False
141         self.assertEqual(todo_1.get_step_tree(set()).as_dict, node_0.as_dict)
142         # test child shows up with child
143         todo_3 = Todo(None, self.proc, False, self.date1)
144         todo_3.save(self.db_conn)
145         assert isinstance(todo_3.id_, int)
146         todo_2.add_child(todo_3)
147         node_3 = TodoNode(todo_3, False, [])
148         node_2.children = [node_3]
149         self.assertEqual(todo_1.get_step_tree(set()).as_dict, node_0.as_dict)
150         # test same todo can be child-ed multiple times at different locations
151         todo_1.add_child(todo_3)
152         node_4 = TodoNode(todo_3, True, [])
153         node_0.children += [node_4]
154         self.assertEqual(todo_1.get_step_tree(set()).as_dict, node_0.as_dict)
155
156     def test_Todo_create_with_children(self) -> None:
157         """Test parenthood guaranteeds of Todo.create_with_children."""
158         assert isinstance(self.proc.id_, int)
159         proc2 = Process(None)
160         proc2.save(self.db_conn)
161         assert isinstance(proc2.id_, int)
162         proc3 = Process(None)
163         proc3.save(self.db_conn)
164         assert isinstance(proc3.id_, int)
165         proc4 = Process(None)
166         proc4.save(self.db_conn)
167         assert isinstance(proc4.id_, int)
168         # make proc4 step of proc3
169         step = ProcessStep(None, proc3.id_, proc4.id_, None)
170         proc3.set_steps(self.db_conn, [step])
171         # give proc2 three steps; 2× proc1, 1× proc3
172         step1 = ProcessStep(None, proc2.id_, self.proc.id_, None)
173         step2 = ProcessStep(None, proc2.id_, self.proc.id_, None)
174         step3 = ProcessStep(None, proc2.id_, proc3.id_, None)
175         proc2.set_steps(self.db_conn, [step1, step2, step3])
176         # test mere creation does nothing
177         todo_ignore = Todo(None, proc2, False, self.date1)
178         todo_ignore.save(self.db_conn)
179         self.assertEqual(todo_ignore.children, [])
180         # test create_with_children on step-less does nothing
181         todo_1 = Todo.create_with_children(self.db_conn, self.proc.id_,
182                                            self.date1)
183         self.assertEqual(todo_1.children, [])
184         self.assertEqual(len(Todo.all(self.db_conn)), 2)
185         # test create_with_children adopts and creates, and down tree too
186         todo_2 = Todo.create_with_children(self.db_conn, proc2.id_, self.date1)
187         self.assertEqual(3, len(todo_2.children))
188         self.assertEqual(todo_1, todo_2.children[0])
189         self.assertEqual(self.proc, todo_2.children[2].process)
190         self.assertEqual(proc3, todo_2.children[1].process)
191         todo_3 = todo_2.children[1]
192         self.assertEqual(len(todo_3.children), 1)
193         self.assertEqual(todo_3.children[0].process, proc4)
194
195     def test_Todo_remove(self) -> None:
196         """Test removal."""
197         todo_1 = Todo(None, self.proc, False, self.date1)
198         todo_1.save(self.db_conn)
199         assert todo_1.id_ is not None
200         todo_0 = Todo(None, self.proc, False, self.date1)
201         todo_0.save(self.db_conn)
202         todo_0.add_child(todo_1)
203         todo_2 = Todo(None, self.proc, False, self.date1)
204         todo_2.save(self.db_conn)
205         todo_1.add_child(todo_2)
206         todo_1_id = todo_1.id_
207         todo_1.remove(self.db_conn)
208         with self.assertRaises(NotFoundException):
209             Todo.by_id(self.db_conn, todo_1_id)
210         self.assertEqual(todo_0.children, [])
211         self.assertEqual(todo_2.parents, [])
212         todo_2.comment = 'foo'
213         with self.assertRaises(HandledException):
214             todo_2.remove(self.db_conn)
215         todo_2.comment = ''
216         todo_2.effort = 5
217         with self.assertRaises(HandledException):
218             todo_2.remove(self.db_conn)
219
220     def test_Todo_autoremoval(self) -> None:
221         """"Test automatic removal for Todo.effort < 0."""
222         todo_1 = Todo(None, self.proc, False, self.date1)
223         todo_1.save(self.db_conn)
224         todo_1.comment = 'foo'
225         todo_1.effort = -0.1
226         todo_1.save(self.db_conn)
227         assert todo_1.id_ is not None
228         Todo.by_id(self.db_conn, todo_1.id_)
229         todo_1.comment = ''
230         todo_1_id = todo_1.id_
231         todo_1.save(self.db_conn)
232         with self.assertRaises(NotFoundException):
233             Todo.by_id(self.db_conn, todo_1_id)
234
235
236 class TestsWithServer(TestCaseWithServer):
237     """Tests against our HTTP server/handler (and database)."""
238
239     def setUp(self) -> None:
240         super().setUp()
241         self._proc1_form_data = self.post_process(1)
242
243     def test_basic_fail_POST_todo(self) -> None:
244         """Test basic malformed/illegal POST /todo requests."""
245         # test we cannot just POST into non-existing Todo
246         self.check_post({}, '/todo', 404)
247         self.check_post({}, '/todo?id=FOO', 400)
248         self.check_post({}, '/todo?id=0', 404)
249         self.check_post({}, '/todo?id=1', 404)
250         # test malformed values on existing Todo
251         day_post = {'day_comment': '', 'new_todo': 1, 'make_type': 'full'}
252         self.check_post(day_post, '/day?date=2024-01-01&make_type=full')
253         for name in ['adopt', 'effort', 'make_full', 'make_empty',
254                      'conditions', 'disables', 'blockers', 'enables']:
255             self.check_post({name: 'x'}, '/todo?id=1', 400, '/todo')
256         for prefix in ['make_empty_', 'make_full_']:
257             for suffix in ['', 'x', '1.1']:
258                 self.check_post({'fill_for_1': f'{prefix}{suffix}'},
259                                 '/todo?id=1', 400, '/todo')
260         # test we cannot POST adoption of self or non-existing Todo
261         self.check_post({'adopt': 1}, '/todo?id=1', 400)
262         self.check_post({'adopt': 2}, '/todo?id=1', 404)
263
264     def test_do_POST_day(self) -> None:
265         """Test Todo posting of POST /day."""
266         self.post_process(2)
267         proc = Process.by_id(self.db_conn, 1)
268         proc2 = Process.by_id(self.db_conn, 2)
269         form_data = {'day_comment': '', 'make_type': 'full'}
270         self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
271         self.assertEqual(Todo.by_date(self.db_conn, '2024-01-01'), [])
272         proc = Process.by_id(self.db_conn, 1)
273         form_data['new_todo'] = str(proc.id_)
274         self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
275         todos = Todo.by_date(self.db_conn, '2024-01-01')
276         self.assertEqual(1, len(todos))
277         todo1 = todos[0]
278         self.assertEqual(todo1.id_, 1)
279         proc = Process.by_id(self.db_conn, 1)
280         self.assertEqual(todo1.process.id_, proc.id_)
281         self.assertEqual(todo1.is_done, False)
282         proc2 = Process.by_id(self.db_conn, 2)
283         form_data['new_todo'] = str(proc2.id_)
284         self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
285         todos = Todo.by_date(self.db_conn, '2024-01-01')
286         todo1 = todos[1]
287         self.assertEqual(todo1.id_, 2)
288         proc2 = Process.by_id(self.db_conn, 1)
289         todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
290         self.assertEqual(todo1.id_, 1)
291         self.assertEqual(todo1.process.id_, proc2.id_)
292         self.assertEqual(todo1.is_done, False)
293
294     def test_do_POST_todo(self) -> None:
295         """Test POST /todo."""
296         def post_and_reload(form_data: dict[str, object], status: int = 302,
297                             redir_url: str = '/todo?id=1') -> Todo:
298             self.check_post(form_data, '/todo?id=1', status, redir_url)
299             return Todo.by_date(self.db_conn, '2024-01-01')[0]
300         self.check_post({'day_comment': '', 'new_todo': 1,
301                          'make_type': 'full'},
302                         '/day?date=2024-01-01&make_type=full', 302)
303         # test posting naked entity
304         todo1 = post_and_reload({})
305         self.assertEqual(todo1.children, [])
306         self.assertEqual(todo1.parents, [])
307         self.assertEqual(todo1.is_done, False)
308         # test posting doneness
309         todo1 = post_and_reload({'done': ''})
310         self.assertEqual(todo1.is_done, True)
311         # test implicitly posting non-doneness
312         todo1 = post_and_reload({})
313         self.assertEqual(todo1.is_done, False)
314         # test posting second todo of same process
315         self.check_post({'day_comment': '', 'new_todo': 1,
316                          'make_type': 'full'},
317                         '/day?date=2024-01-01&make_type=full', 302)
318         # test todo 1 adopting todo 2
319         todo1 = post_and_reload({'adopt': 2})
320         todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
321         self.assertEqual(todo1.children, [todo2])
322         self.assertEqual(todo1.parents, [])
323         self.assertEqual(todo2.children, [])
324         self.assertEqual(todo2.parents, [todo1])
325         # test todo1 cannot be set done with todo2 not done yet
326         todo1 = post_and_reload({'done': '', 'adopt': 2}, 400)
327         self.assertEqual(todo1.is_done, False)
328         # test todo1 un-adopting todo 2 by just not sending an adopt
329         todo1 = post_and_reload({}, 302)
330         todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
331         self.assertEqual(todo1.children, [])
332         self.assertEqual(todo1.parents, [])
333         self.assertEqual(todo2.children, [])
334         self.assertEqual(todo2.parents, [])
335         # test todo1 deletion
336         todo1 = post_and_reload({'delete': ''}, 302, '/')
337
338     def test_do_POST_day_todo_adoption(self) -> None:
339         """Test Todos posted to Day view may adopt existing Todos."""
340         form_data = self.post_process(
341                 2, self._proc1_form_data | {'new_top_step': 1})
342         form_data = {'day_comment': '', 'new_todo': 1, 'make_type': 'full'}
343         self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
344         form_data['new_todo'] = 2
345         self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
346         todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
347         todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
348         self.assertEqual(todo1.children, [])
349         self.assertEqual(todo1.parents, [todo2])
350         self.assertEqual(todo2.children, [todo1])
351         self.assertEqual(todo2.parents, [])
352
353     def test_do_POST_day_todo_multiple(self) -> None:
354         """Test multiple Todos can be posted to Day view."""
355         # form_data = self.post_process()
356         form_data = self.post_process(2)
357         form_data = {'day_comment': '', 'new_todo': [1, 2],
358                      'make_type': 'full'}
359         self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
360         todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
361         todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
362         self.assertEqual(todo1.process.id_, 1)
363         self.assertEqual(todo2.process.id_, 2)
364
365     def test_do_POST_day_todo_multiple_inner_adoption(self) -> None:
366         """Test multiple Todos can be posted to Day view w. inner adoption."""
367
368         def key_order_func(t: Todo) -> int:
369             assert isinstance(t.process.id_, int)
370             return t.process.id_
371
372         def check_adoption(date: str, new_todos: list[int]) -> None:
373             form_data = {'day_comment': '', 'new_todo': new_todos,
374                          'make_type': 'full'}
375             self.check_post(form_data, f'/day?date={date}&make_type=full', 302)
376             day_todos = Todo.by_date(self.db_conn, date)
377             day_todos.sort(key=key_order_func)
378             todo1 = day_todos[0]
379             todo2 = day_todos[1]
380             self.assertEqual(todo1.children, [])
381             self.assertEqual(todo1.parents, [todo2])
382             self.assertEqual(todo2.children, [todo1])
383             self.assertEqual(todo2.parents, [])
384
385         def check_nesting_adoption(process_id: int, date: str,
386                                    new_top_steps: list[int]) -> None:
387             form_data = {'title': '', 'description': '', 'effort': 1,
388                          'step_of': [2]}
389             form_data = self.post_process(1, form_data)
390             form_data['new_top_step'] = new_top_steps
391             form_data['step_of'] = []
392             form_data = self.post_process(process_id, form_data)
393             form_data = {'day_comment': '', 'new_todo': [process_id],
394                          'make_type': 'full'}
395             self.check_post(form_data, f'/day?date={date}&make_type=full', 302)
396             day_todos = Todo.by_date(self.db_conn, date)
397             day_todos.sort(key=key_order_func, reverse=True)
398             self.assertEqual(len(day_todos), 3)
399             todo1 = day_todos[0]  # process of process_id
400             todo2 = day_todos[1]  # process 2
401             todo3 = day_todos[2]  # process 1
402             self.assertEqual(sorted(todo1.children), sorted([todo2, todo3]))
403             self.assertEqual(todo1.parents, [])
404             self.assertEqual(todo2.children, [todo3])
405             self.assertEqual(todo2.parents, [todo1])
406             self.assertEqual(todo3.children, [])
407             self.assertEqual(sorted(todo3.parents), sorted([todo2, todo1]))
408
409         self.post_process(2, self._proc1_form_data | {'new_top_step': 1})
410         check_adoption('2024-01-01', [1, 2])
411         check_adoption('2024-01-02', [2, 1])
412         check_nesting_adoption(3, '2024-01-03', [1, 2])
413         check_nesting_adoption(4, '2024-01-04', [2, 1])
414
415     def test_do_POST_day_todo_doneness(self) -> None:
416         """Test Todo doneness can be posted to Day view."""
417         form_data = {'day_comment': '', 'new_todo': [1], 'make_type': 'full'}
418         self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
419         todo = Todo.by_date(self.db_conn, '2024-01-01')[0]
420         form_data = {'day_comment': '', 'todo_id': [1], 'make_type': 'full',
421                      'comment': [''], 'done': [], 'effort': ['']}
422         self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
423         todo = Todo.by_date(self.db_conn, '2024-01-01')[0]
424         self.assertEqual(todo.is_done, False)
425         form_data = {'day_comment': '', 'todo_id': [1], 'done': [1],
426                      'make_type': 'full', 'comment': [''], 'effort': ['']}
427         self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
428         todo = Todo.by_date(self.db_conn, '2024-01-01')[0]
429         self.assertEqual(todo.is_done, True)
430
431     def test_do_GET_todo(self) -> None:
432         """Test GET /todo response codes."""
433         form_data = {'day_comment': '', 'new_todo': 1, 'make_type': 'full'}
434         self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
435         self.check_get('/todo', 404)
436         self.check_get('/todo?id=', 404)
437         self.check_get('/todo?id=foo', 400)
438         self.check_get('/todo?id=0', 404)
439         self.check_get('/todo?id=1', 200)
440         self.check_get('/todo?id=2', 404)