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,
10 class TestsWithDB(TestCaseWithDB, TestCaseSansDB):
11 """Tests requiring DB, but not server setup.
13 NB: We subclass TestCaseSansDB too, to pull in its .test_id_validation,
14 which for Todo wouldn't run without a DB being set up due to the need for
15 Processes with set IDs.
18 default_init_kwargs = {'process': None, 'is_done': False,
20 # solely used for TestCaseSansDB.test_id_setting
21 default_init_args = [None, False, '2024-01-01']
23 def setUp(self) -> None:
25 self.date1 = '2024-01-01'
26 self.date2 = '2024-01-02'
27 self.proc = Process(None)
28 self.proc.save(self.db_conn)
29 self.cond1 = Condition(None)
30 self.cond1.save(self.db_conn)
31 self.cond2 = Condition(None)
32 self.cond2.save(self.db_conn)
33 self.default_init_kwargs['process'] = self.proc
34 self.default_init_args[0] = self.proc
36 def test_Todo_init(self) -> None:
37 """Test creation of Todo and what they default to."""
38 process = Process(None)
39 with self.assertRaises(NotFoundException):
40 Todo(None, process, False, self.date1)
41 process.save(self.db_conn)
42 assert isinstance(self.cond1.id_, int)
43 assert isinstance(self.cond2.id_, int)
44 process.set_conditions(self.db_conn, [self.cond1.id_, self.cond2.id_])
45 process.set_enables(self.db_conn, [self.cond1.id_])
46 process.set_disables(self.db_conn, [self.cond2.id_])
47 todo_no_id = Todo(None, process, False, self.date1)
48 self.assertEqual(todo_no_id.conditions, [self.cond1, self.cond2])
49 self.assertEqual(todo_no_id.enables, [self.cond1])
50 self.assertEqual(todo_no_id.disables, [self.cond2])
51 todo_yes_id = Todo(5, process, False, self.date1)
52 self.assertEqual(todo_yes_id.conditions, [])
53 self.assertEqual(todo_yes_id.enables, [])
54 self.assertEqual(todo_yes_id.disables, [])
56 def test_Todo_by_date(self) -> None:
57 """Test findability of Todos by date."""
58 t1 = Todo(None, self.proc, False, self.date1)
60 t2 = Todo(None, self.proc, False, self.date1)
62 self.assertEqual(Todo.by_date(self.db_conn, self.date1), [t1, t2])
63 self.assertEqual(Todo.by_date(self.db_conn, self.date2), [])
64 with self.assertRaises(BadFormatException):
65 self.assertEqual(Todo.by_date(self.db_conn, 'foo'), [])
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_])
76 self.assertEqual(self.cond1.is_active, True)
77 self.assertEqual(self.cond2.is_active, False)
79 self.assertEqual(self.cond1.is_active, True)
80 self.assertEqual(self.cond2.is_active, False)
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)
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
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)
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
131 self.assertEqual(todo_1.get_step_tree({todo_1.id_}).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]
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)
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_,
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)
195 def test_Todo_remove(self) -> None:
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)
217 with self.assertRaises(HandledException):
218 todo_2.remove(self.db_conn)
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'
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_)
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)
236 class TestsWithServer(TestCaseWithServer):
237 """Tests against our HTTP server/handler (and database)."""
239 def test_do_POST_day(self) -> None:
240 """Test Todo posting of POST /day."""
243 proc = Process.by_id(self.db_conn, 1)
244 proc2 = Process.by_id(self.db_conn, 2)
245 form_data = {'day_comment': '', 'make_type': 'full'}
246 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
247 self.assertEqual(Todo.by_date(self.db_conn, '2024-01-01'), [])
248 form_data['new_todo'] = str(proc.id_)
249 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
250 todos = Todo.by_date(self.db_conn, '2024-01-01')
251 self.assertEqual(1, len(todos))
253 self.assertEqual(todo1.id_, 1)
254 self.assertEqual(todo1.process.id_, proc.id_)
255 self.assertEqual(todo1.is_done, False)
256 form_data['new_todo'] = str(proc2.id_)
257 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
258 todos = Todo.by_date(self.db_conn, '2024-01-01')
260 self.assertEqual(todo1.id_, 2)
261 self.assertEqual(todo1.process.id_, proc2.id_)
262 self.assertEqual(todo1.is_done, False)
264 def test_do_POST_todo(self) -> None:
265 """Test POST /todo."""
266 def post_and_reload(form_data: dict[str, object], status: int = 302,
267 redir_url: str = '/todo?id=1') -> Todo:
268 self.check_post(form_data, '/todo?id=1', status, redir_url)
269 return Todo.by_date(self.db_conn, '2024-01-01')[0]
272 self.check_post({'day_comment': '', 'new_todo': 1,
273 'make_type': 'full'},
274 '/day?date=2024-01-01&make_type=full', 302)
275 # test posting to bad URLs
276 self.check_post({}, '/todo=', 404)
277 self.check_post({}, '/todo?id=', 404)
278 self.check_post({}, '/todo?id=FOO', 400)
279 self.check_post({}, '/todo?id=0', 404)
280 # test posting naked entity
281 todo1 = post_and_reload({})
282 self.assertEqual(todo1.children, [])
283 self.assertEqual(todo1.parents, [])
284 self.assertEqual(todo1.is_done, False)
285 # test posting doneness
286 todo1 = post_and_reload({'done': ''})
287 self.assertEqual(todo1.is_done, True)
288 # test implicitly posting non-doneness
289 todo1 = post_and_reload({})
290 self.assertEqual(todo1.is_done, False)
291 # test malformed adoptions
292 self.check_post({'adopt': 'foo'}, '/todo?id=1', 400)
293 self.check_post({'adopt': 1}, '/todo?id=1', 400)
294 self.check_post({'adopt': 2}, '/todo?id=1', 404)
295 # test posting second todo of same process
296 self.check_post({'day_comment': '', 'new_todo': 1,
297 'make_type': 'full'},
298 '/day?date=2024-01-01&make_type=full', 302)
299 # test todo 1 adopting todo 2
300 todo1 = post_and_reload({'adopt': 2})
301 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
302 self.assertEqual(todo1.children, [todo2])
303 self.assertEqual(todo1.parents, [])
304 self.assertEqual(todo2.children, [])
305 self.assertEqual(todo2.parents, [todo1])
306 # test todo1 cannot be set done with todo2 not done yet
307 todo1 = post_and_reload({'done': '', 'adopt': 2}, 400)
308 self.assertEqual(todo1.is_done, False)
309 # test todo1 un-adopting todo 2 by just not sending an adopt
310 todo1 = post_and_reload({}, 302)
311 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
312 self.assertEqual(todo1.children, [])
313 self.assertEqual(todo1.parents, [])
314 self.assertEqual(todo2.children, [])
315 self.assertEqual(todo2.parents, [])
316 # test todo1 deletion
317 todo1 = post_and_reload({'delete': ''}, 302, '/')
319 def test_do_POST_day_todo_adoption(self) -> None:
320 """Test Todos posted to Day view may adopt existing Todos."""
321 form_data = self.post_process()
322 form_data = self.post_process(2, form_data | {'new_top_step': 1})
323 form_data = {'day_comment': '', 'new_todo': 1, 'make_type': 'full'}
324 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
325 form_data['new_todo'] = 2
326 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
327 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
328 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
329 self.assertEqual(todo1.children, [])
330 self.assertEqual(todo1.parents, [todo2])
331 self.assertEqual(todo2.children, [todo1])
332 self.assertEqual(todo2.parents, [])
334 def test_do_POST_day_todo_multiple(self) -> None:
335 """Test multiple Todos can be posted to Day view."""
336 form_data = self.post_process()
337 form_data = self.post_process(2)
338 form_data = {'day_comment': '', 'new_todo': [1, 2],
340 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
341 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
342 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
343 self.assertEqual(todo1.process.id_, 1)
344 self.assertEqual(todo2.process.id_, 2)
346 def test_do_POST_day_todo_multiple_inner_adoption(self) -> None:
347 """Test multiple Todos can be posted to Day view w. inner adoption."""
349 def key_order_func(t: Todo) -> int:
350 assert isinstance(t.process.id_, int)
353 def check_adoption(date: str, new_todos: list[int]) -> None:
354 form_data = {'day_comment': '', 'new_todo': new_todos,
356 self.check_post(form_data, f'/day?date={date}&make_type=full', 302)
357 day_todos = Todo.by_date(self.db_conn, date)
358 day_todos.sort(key=key_order_func)
361 self.assertEqual(todo1.children, [])
362 self.assertEqual(todo1.parents, [todo2])
363 self.assertEqual(todo2.children, [todo1])
364 self.assertEqual(todo2.parents, [])
366 def check_nesting_adoption(process_id: int, date: str,
367 new_top_steps: list[int]) -> None:
368 form_data = {'title': '', 'description': '', 'effort': 1,
370 form_data = self.post_process(1, form_data)
371 form_data['new_top_step'] = new_top_steps
372 form_data['step_of'] = []
373 form_data = self.post_process(process_id, form_data)
374 form_data = {'day_comment': '', 'new_todo': [process_id],
376 self.check_post(form_data, f'/day?date={date}&make_type=full', 302)
377 day_todos = Todo.by_date(self.db_conn, date)
378 day_todos.sort(key=key_order_func, reverse=True)
379 self.assertEqual(len(day_todos), 3)
380 todo1 = day_todos[0] # process of process_id
381 todo2 = day_todos[1] # process 2
382 todo3 = day_todos[2] # process 1
383 self.assertEqual(sorted(todo1.children), sorted([todo2, todo3]))
384 self.assertEqual(todo1.parents, [])
385 self.assertEqual(todo2.children, [todo3])
386 self.assertEqual(todo2.parents, [todo1])
387 self.assertEqual(todo3.children, [])
388 self.assertEqual(sorted(todo3.parents), sorted([todo2, todo1]))
390 form_data = self.post_process()
391 form_data = self.post_process(2, form_data | {'new_top_step': 1})
392 check_adoption('2024-01-01', [1, 2])
393 check_adoption('2024-01-02', [2, 1])
394 check_nesting_adoption(3, '2024-01-03', [1, 2])
395 check_nesting_adoption(4, '2024-01-04', [2, 1])
397 def test_do_POST_day_todo_doneness(self) -> None:
398 """Test Todo doneness can be posted to Day view."""
400 form_data = {'day_comment': '', 'new_todo': [1], 'make_type': 'full'}
401 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
402 todo = Todo.by_date(self.db_conn, '2024-01-01')[0]
403 form_data = {'day_comment': '', 'todo_id': [1], 'make_type': 'full',
404 'comment': [''], 'done': [], 'effort': ['']}
405 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
406 self.assertEqual(todo.is_done, False)
407 form_data = {'day_comment': '', 'todo_id': [1], 'done': [1],
408 'make_type': 'full', 'comment': [''], 'effort': ['']}
409 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
410 self.assertEqual(todo.is_done, True)
412 def test_do_GET_todo(self) -> None:
413 """Test GET /todo response codes."""
415 form_data = {'day_comment': '', 'new_todo': 1, 'make_type': 'full'}
416 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
417 self.check_get('/todo', 404)
418 self.check_get('/todo?id=', 404)
419 self.check_get('/todo?id=foo', 400)
420 self.check_get('/todo?id=0', 404)
421 self.check_get('/todo?id=1', 200)