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 proc = Process.by_id(self.db_conn, 1)
249 form_data['new_todo'] = str(proc.id_)
250 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
251 todos = Todo.by_date(self.db_conn, '2024-01-01')
252 self.assertEqual(1, len(todos))
254 self.assertEqual(todo1.id_, 1)
255 proc = Process.by_id(self.db_conn, 1)
256 self.assertEqual(todo1.process.id_, proc.id_)
257 self.assertEqual(todo1.is_done, False)
258 proc2 = Process.by_id(self.db_conn, 2)
259 form_data['new_todo'] = str(proc2.id_)
260 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
261 todos = Todo.by_date(self.db_conn, '2024-01-01')
263 self.assertEqual(todo1.id_, 2)
264 proc2 = Process.by_id(self.db_conn, 1)
265 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
266 self.assertEqual(todo1.id_, 1)
267 self.assertEqual(todo1.process.id_, proc2.id_)
268 self.assertEqual(todo1.is_done, False)
270 def test_do_POST_todo(self) -> None:
271 """Test POST /todo."""
272 def post_and_reload(form_data: dict[str, object], status: int = 302,
273 redir_url: str = '/todo?id=1') -> Todo:
274 self.check_post(form_data, '/todo?id=1', status, redir_url)
275 return Todo.by_date(self.db_conn, '2024-01-01')[0]
278 self.check_post({'day_comment': '', 'new_todo': 1,
279 'make_type': 'full'},
280 '/day?date=2024-01-01&make_type=full', 302)
281 # test posting to bad URLs
282 self.check_post({}, '/todo=', 404)
283 self.check_post({}, '/todo?id=', 404)
284 self.check_post({}, '/todo?id=FOO', 400)
285 self.check_post({}, '/todo?id=0', 404)
286 # test posting naked entity
287 todo1 = post_and_reload({})
288 self.assertEqual(todo1.children, [])
289 self.assertEqual(todo1.parents, [])
290 self.assertEqual(todo1.is_done, False)
291 # test posting doneness
292 todo1 = post_and_reload({'done': ''})
293 self.assertEqual(todo1.is_done, True)
294 # test implicitly posting non-doneness
295 todo1 = post_and_reload({})
296 self.assertEqual(todo1.is_done, False)
297 # test malformed adoptions
298 self.check_post({'adopt': 'foo'}, '/todo?id=1', 400)
299 self.check_post({'adopt': 1}, '/todo?id=1', 400)
300 self.check_post({'adopt': 2}, '/todo?id=1', 404)
301 # test posting second todo of same process
302 self.check_post({'day_comment': '', 'new_todo': 1,
303 'make_type': 'full'},
304 '/day?date=2024-01-01&make_type=full', 302)
305 # test todo 1 adopting todo 2
306 todo1 = post_and_reload({'adopt': 2})
307 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
308 self.assertEqual(todo1.children, [todo2])
309 self.assertEqual(todo1.parents, [])
310 self.assertEqual(todo2.children, [])
311 self.assertEqual(todo2.parents, [todo1])
312 # test todo1 cannot be set done with todo2 not done yet
313 todo1 = post_and_reload({'done': '', 'adopt': 2}, 400)
314 self.assertEqual(todo1.is_done, False)
315 # test todo1 un-adopting todo 2 by just not sending an adopt
316 todo1 = post_and_reload({}, 302)
317 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
318 self.assertEqual(todo1.children, [])
319 self.assertEqual(todo1.parents, [])
320 self.assertEqual(todo2.children, [])
321 self.assertEqual(todo2.parents, [])
322 # test todo1 deletion
323 todo1 = post_and_reload({'delete': ''}, 302, '/')
325 def test_do_POST_day_todo_adoption(self) -> None:
326 """Test Todos posted to Day view may adopt existing Todos."""
327 form_data = self.post_process()
328 form_data = self.post_process(2, form_data | {'new_top_step': 1})
329 form_data = {'day_comment': '', 'new_todo': 1, 'make_type': 'full'}
330 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
331 form_data['new_todo'] = 2
332 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
333 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
334 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
335 self.assertEqual(todo1.children, [])
336 self.assertEqual(todo1.parents, [todo2])
337 self.assertEqual(todo2.children, [todo1])
338 self.assertEqual(todo2.parents, [])
340 def test_do_POST_day_todo_multiple(self) -> None:
341 """Test multiple Todos can be posted to Day view."""
342 form_data = self.post_process()
343 form_data = self.post_process(2)
344 form_data = {'day_comment': '', 'new_todo': [1, 2],
346 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
347 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
348 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
349 self.assertEqual(todo1.process.id_, 1)
350 self.assertEqual(todo2.process.id_, 2)
352 def test_do_POST_day_todo_multiple_inner_adoption(self) -> None:
353 """Test multiple Todos can be posted to Day view w. inner adoption."""
355 def key_order_func(t: Todo) -> int:
356 assert isinstance(t.process.id_, int)
359 def check_adoption(date: str, new_todos: list[int]) -> None:
360 form_data = {'day_comment': '', 'new_todo': new_todos,
362 self.check_post(form_data, f'/day?date={date}&make_type=full', 302)
363 day_todos = Todo.by_date(self.db_conn, date)
364 day_todos.sort(key=key_order_func)
367 self.assertEqual(todo1.children, [])
368 self.assertEqual(todo1.parents, [todo2])
369 self.assertEqual(todo2.children, [todo1])
370 self.assertEqual(todo2.parents, [])
372 def check_nesting_adoption(process_id: int, date: str,
373 new_top_steps: list[int]) -> None:
374 form_data = {'title': '', 'description': '', 'effort': 1,
376 form_data = self.post_process(1, form_data)
377 form_data['new_top_step'] = new_top_steps
378 form_data['step_of'] = []
379 form_data = self.post_process(process_id, form_data)
380 form_data = {'day_comment': '', 'new_todo': [process_id],
382 self.check_post(form_data, f'/day?date={date}&make_type=full', 302)
383 day_todos = Todo.by_date(self.db_conn, date)
384 day_todos.sort(key=key_order_func, reverse=True)
385 self.assertEqual(len(day_todos), 3)
386 todo1 = day_todos[0] # process of process_id
387 todo2 = day_todos[1] # process 2
388 todo3 = day_todos[2] # process 1
389 self.assertEqual(sorted(todo1.children), sorted([todo2, todo3]))
390 self.assertEqual(todo1.parents, [])
391 self.assertEqual(todo2.children, [todo3])
392 self.assertEqual(todo2.parents, [todo1])
393 self.assertEqual(todo3.children, [])
394 self.assertEqual(sorted(todo3.parents), sorted([todo2, todo1]))
396 form_data = self.post_process()
397 form_data = self.post_process(2, form_data | {'new_top_step': 1})
398 check_adoption('2024-01-01', [1, 2])
399 check_adoption('2024-01-02', [2, 1])
400 check_nesting_adoption(3, '2024-01-03', [1, 2])
401 check_nesting_adoption(4, '2024-01-04', [2, 1])
403 def test_do_POST_day_todo_doneness(self) -> None:
404 """Test Todo doneness can be posted to Day view."""
406 form_data = {'day_comment': '', 'new_todo': [1], 'make_type': 'full'}
407 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
408 todo = Todo.by_date(self.db_conn, '2024-01-01')[0]
409 form_data = {'day_comment': '', 'todo_id': [1], 'make_type': 'full',
410 'comment': [''], 'done': [], 'effort': ['']}
411 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
412 todo = Todo.by_date(self.db_conn, '2024-01-01')[0]
413 self.assertEqual(todo.is_done, False)
414 form_data = {'day_comment': '', 'todo_id': [1], 'done': [1],
415 'make_type': 'full', 'comment': [''], 'effort': ['']}
416 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
417 todo = Todo.by_date(self.db_conn, '2024-01-01')[0]
418 self.assertEqual(todo.is_done, True)
420 def test_do_GET_todo(self) -> None:
421 """Test GET /todo response codes."""
423 form_data = {'day_comment': '', 'new_todo': 1, 'make_type': 'full'}
424 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
425 self.check_get('/todo', 404)
426 self.check_get('/todo?id=', 404)
427 self.check_get('/todo?id=foo', 400)
428 self.check_get('/todo?id=0', 404)
429 self.check_get('/todo?id=1', 200)