1 """Test Todos module."""
2 from tests.utils import TestCaseWithDB, TestCaseWithServer
3 from plomtask.todos import Todo, TodoNode
4 from plomtask.processes import Process
5 from plomtask.conditions import Condition
6 from plomtask.exceptions import (NotFoundException, BadFormatException,
10 class TestsWithDB(TestCaseWithDB):
11 """Tests requiring DB, but not server setup."""
13 default_init_kwargs = {'process': None, 'is_done': False,
16 def setUp(self) -> None:
18 self.date1 = '2024-01-01'
19 self.date2 = '2024-01-02'
20 self.proc = Process(None)
21 self.proc.save(self.db_conn)
22 self.cond1 = Condition(None)
23 self.cond1.save(self.db_conn)
24 self.cond2 = Condition(None)
25 self.cond2.save(self.db_conn)
26 self.default_init_kwargs['process'] = self.proc
28 def test_Todo_init(self) -> None:
29 """Test creation of Todo and what they default to."""
30 process = Process(None)
31 with self.assertRaises(NotFoundException):
32 Todo(None, process, False, self.date1)
33 process.save(self.db_conn)
34 assert isinstance(self.cond1.id_, int)
35 assert isinstance(self.cond2.id_, int)
36 process.set_conditions(self.db_conn, [self.cond1.id_, self.cond2.id_])
37 process.set_enables(self.db_conn, [self.cond1.id_])
38 process.set_disables(self.db_conn, [self.cond2.id_])
39 todo_no_id = Todo(None, process, False, self.date1)
40 self.assertEqual(todo_no_id.conditions, [self.cond1, self.cond2])
41 self.assertEqual(todo_no_id.enables, [self.cond1])
42 self.assertEqual(todo_no_id.disables, [self.cond2])
43 todo_yes_id = Todo(5, process, False, self.date1)
44 self.assertEqual(todo_yes_id.conditions, [])
45 self.assertEqual(todo_yes_id.enables, [])
46 self.assertEqual(todo_yes_id.disables, [])
48 def test_Todo_by_id(self) -> None:
49 """Test findability of Todos."""
50 todo = Todo(1, self.proc, False, self.date1)
51 todo.save(self.db_conn)
52 self.assertEqual(Todo.by_id(self.db_conn, 1), todo)
53 with self.assertRaises(NotFoundException):
54 Todo.by_id(self.db_conn, 0)
55 with self.assertRaises(NotFoundException):
56 Todo.by_id(self.db_conn, 2)
58 def test_Todo_by_date(self) -> None:
59 """Test findability of Todos by date."""
60 t1 = Todo(None, self.proc, False, self.date1)
62 t2 = Todo(None, self.proc, False, self.date1)
64 self.assertEqual(Todo.by_date(self.db_conn, self.date1), [t1, t2])
65 self.assertEqual(Todo.by_date(self.db_conn, self.date2), [])
66 with self.assertRaises(BadFormatException):
67 self.assertEqual(Todo.by_date(self.db_conn, 'foo'), [])
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_enables(self.db_conn, [self.cond1.id_])
76 todo.set_disables(self.db_conn, [self.cond2.id_])
78 self.assertEqual(self.cond1.is_active, True)
79 self.assertEqual(self.cond2.is_active, False)
81 self.assertEqual(self.cond1.is_active, True)
82 self.assertEqual(self.cond2.is_active, False)
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)
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_conditions(self.db_conn, [self.cond1.id_])
118 with self.assertRaises(BadFormatException):
119 todo_2.is_done = True
120 self.cond1.is_active = True
121 todo_2.is_done = True
123 def test_Todo_step_tree(self) -> None:
124 """Test self-configuration of TodoStepsNode tree for Day view."""
125 todo_1 = Todo(None, self.proc, False, self.date1)
126 todo_1.save(self.db_conn)
127 assert isinstance(todo_1.id_, int)
129 node_0 = TodoNode(todo_1, False, [])
130 self.assertEqual(todo_1.get_step_tree(set()), node_0)
131 # test non_emtpy seen_todo does something
133 self.assertEqual(todo_1.get_step_tree({todo_1.id_}), node_0)
134 # test child shows up
135 todo_2 = Todo(None, self.proc, False, self.date1)
136 todo_2.save(self.db_conn)
137 assert isinstance(todo_2.id_, int)
138 todo_1.add_child(todo_2)
139 node_2 = TodoNode(todo_2, False, [])
140 node_0.children = [node_2]
142 self.assertEqual(todo_1.get_step_tree(set()), node_0)
143 # test child shows up with child
144 todo_3 = Todo(None, self.proc, False, self.date1)
145 todo_3.save(self.db_conn)
146 assert isinstance(todo_3.id_, int)
147 todo_2.add_child(todo_3)
148 node_3 = TodoNode(todo_3, False, [])
149 node_2.children = [node_3]
150 self.assertEqual(todo_1.get_step_tree(set()), node_0)
151 # test same todo can be child-ed multiple times at different locations
152 todo_1.add_child(todo_3)
153 node_4 = TodoNode(todo_3, True, [])
154 node_0.children += [node_4]
155 self.assertEqual(todo_1.get_step_tree(set()), node_0)
157 def test_Todo_unsatisfied_steps(self) -> None:
158 """Test options of satisfying unfulfilled Process.explicit_steps."""
159 assert isinstance(self.proc.id_, int)
160 todo_1 = Todo(None, self.proc, False, self.date1)
161 todo_1.save(self.db_conn)
162 proc2 = Process(None)
163 proc2.save(self.db_conn)
164 assert isinstance(proc2.id_, int)
165 proc3 = Process(None)
166 proc3.save(self.db_conn)
167 assert isinstance(proc3.id_, int)
168 proc4 = Process(None)
169 proc4.save(self.db_conn)
170 assert isinstance(proc4.id_, int)
171 proc3.set_steps(self.db_conn, [(None, proc4.id_, None)])
172 proc2.set_steps(self.db_conn, [(None, self.proc.id_, None),
173 (None, self.proc.id_, None),
174 (None, proc3.id_, None)])
175 todo_2 = Todo(None, proc2, False, self.date1)
176 todo_2.save(self.db_conn)
177 # test empty adoption does nothing
178 todo_2.adopt_from([])
179 self.assertEqual(todo_2.children, [])
180 # test basic adoption
181 todo_2.adopt_from([todo_1])
182 self.assertEqual(todo_2.children, [todo_1])
183 self.assertEqual(todo_1.parents, [todo_2])
184 # test making missing children
185 todo_2.make_missing_children(self.db_conn)
186 todo_3 = Todo.by_id(self.db_conn, 3)
187 todo_4 = Todo.by_id(self.db_conn, 4)
188 self.assertEqual(todo_2.children, [todo_1, todo_3, todo_4])
189 self.assertEqual(todo_3.process, self.proc)
190 self.assertEqual(todo_3.parents, [todo_2])
191 self.assertEqual(todo_3.children, [])
192 self.assertEqual(todo_4.process, proc3)
193 self.assertEqual(todo_4.parents, [todo_2])
194 # test .make_missing_children lower down the tree
195 todo_4.make_missing_children(self.db_conn)
196 todo_5 = Todo.by_id(self.db_conn, 5)
197 self.assertEqual(todo_5.process, proc4)
198 self.assertEqual(todo_4.children, [todo_5])
199 self.assertEqual(todo_5.parents, [todo_4])
201 def test_Todo_singularity(self) -> None:
202 """Test pointers made for single object keep pointing to it."""
203 self.check_singularity('is_done', True, self.proc, False, self.date1)
205 def test_Todo_remove(self) -> None:
207 todo_1 = Todo(None, self.proc, False, self.date1)
208 todo_1.save(self.db_conn)
209 todo_0 = Todo(None, self.proc, False, self.date1)
210 todo_0.save(self.db_conn)
211 todo_0.add_child(todo_1)
212 todo_2 = Todo(None, self.proc, False, self.date1)
213 todo_2.save(self.db_conn)
214 todo_1.add_child(todo_2)
215 todo_1.remove(self.db_conn)
216 with self.assertRaises(NotFoundException):
217 Todo.by_id(self.db_conn, todo_1.id_)
218 self.assertEqual(todo_0.children, [])
219 self.assertEqual(todo_2.parents, [])
220 todo_2.comment = 'foo'
221 with self.assertRaises(HandledException):
222 todo_2.remove(self.db_conn)
225 with self.assertRaises(HandledException):
226 todo_2.remove(self.db_conn)
228 def test_Todo_autoremoval(self) -> None:
229 """"Test automatic removal for Todo.effort < 0."""
230 todo_1 = Todo(None, self.proc, False, self.date1)
231 todo_1.save(self.db_conn)
232 todo_1.comment = 'foo'
234 todo_1.save(self.db_conn)
235 Todo.by_id(self.db_conn, todo_1.id_)
237 todo_1.save(self.db_conn)
238 with self.assertRaises(NotFoundException):
239 Todo.by_id(self.db_conn, todo_1.id_)
242 class TestsWithServer(TestCaseWithServer):
243 """Tests against our HTTP server/handler (and database)."""
245 def test_do_POST_day(self) -> None:
246 """Test Todo posting of POST /day."""
249 proc = Process.by_id(self.db_conn, 1)
250 proc2 = Process.by_id(self.db_conn, 2)
251 form_data = {'day_comment': ''}
252 self.check_post(form_data, '/day?date=2024-01-01', 302)
253 self.assertEqual(Todo.by_date(self.db_conn, '2024-01-01'), [])
254 form_data['new_todo'] = str(proc.id_)
255 self.check_post(form_data, '/day?date=2024-01-01', 302)
256 todos = Todo.by_date(self.db_conn, '2024-01-01')
257 self.assertEqual(1, len(todos))
259 self.assertEqual(todo1.id_, 1)
260 self.assertEqual(todo1.process.id_, proc.id_)
261 self.assertEqual(todo1.is_done, False)
262 form_data['new_todo'] = str(proc2.id_)
263 self.check_post(form_data, '/day?date=2024-01-01', 302)
264 todos = Todo.by_date(self.db_conn, '2024-01-01')
266 self.assertEqual(todo1.id_, 2)
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 '/day?date=2024-01-01', 302)
280 # test posting to bad URLs
281 self.check_post({}, '/todo=', 404)
282 self.check_post({}, '/todo?id=', 400)
283 self.check_post({}, '/todo?id=FOO', 400)
284 self.check_post({}, '/todo?id=0', 404)
285 # test posting naked entity
286 todo1 = post_and_reload({})
287 self.assertEqual(todo1.children, [])
288 self.assertEqual(todo1.parents, [])
289 self.assertEqual(todo1.is_done, False)
290 # test posting doneness
291 todo1 = post_and_reload({'done': ''})
292 self.assertEqual(todo1.is_done, True)
293 # test implicitly posting non-doneness
294 todo1 = post_and_reload({})
295 self.assertEqual(todo1.is_done, False)
296 # test malformed adoptions
297 self.check_post({'adopt': 'foo'}, '/todo?id=1', 400)
298 self.check_post({'adopt': 1}, '/todo?id=1', 400)
299 self.check_post({'adopt': 2}, '/todo?id=1', 404)
300 # test posting second todo of same process
301 self.check_post({'day_comment': '', 'new_todo': 1},
302 '/day?date=2024-01-01', 302)
303 # test todo 1 adopting todo 2
304 todo1 = post_and_reload({'adopt': 2})
305 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
306 self.assertEqual(todo1.children, [todo2])
307 self.assertEqual(todo1.parents, [])
308 self.assertEqual(todo2.children, [])
309 self.assertEqual(todo2.parents, [todo1])
310 # test todo1 cannot be set done with todo2 not done yet
311 todo1 = post_and_reload({'done': '', 'adopt': 2}, 400)
312 self.assertEqual(todo1.is_done, False)
313 # test todo1 un-adopting todo 2 by just not sending an adopt
314 todo1 = post_and_reload({}, 302)
315 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
316 self.assertEqual(todo1.children, [])
317 self.assertEqual(todo1.parents, [])
318 self.assertEqual(todo2.children, [])
319 self.assertEqual(todo2.parents, [])
320 # test todo1 deletion
321 todo1 = post_and_reload({'delete': ''}, 302, '/')
323 def test_do_POST_day_todo_adoption(self) -> None:
324 """Test Todos posted to Day view may adopt existing Todos."""
325 form_data = self.post_process()
326 form_data = self.post_process(2, form_data | {'new_top_step': 1})
327 form_data = {'day_comment': '', 'new_todo': 1}
328 self.check_post(form_data, '/day?date=2024-01-01', 302)
329 form_data['new_todo'] = 2
330 self.check_post(form_data, '/day?date=2024-01-01', 302)
331 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
332 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
333 self.assertEqual(todo1.children, [])
334 self.assertEqual(todo1.parents, [todo2])
335 self.assertEqual(todo2.children, [todo1])
336 self.assertEqual(todo2.parents, [])
338 def test_do_POST_day_todo_multiple(self) -> None:
339 """Test multiple Todos can be posted to Day view."""
340 form_data = self.post_process()
341 form_data = self.post_process(2)
342 form_data = {'day_comment': '', 'new_todo': [1, 2]}
343 self.check_post(form_data, '/day?date=2024-01-01', 302)
344 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
345 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
346 self.assertEqual(todo1.process.id_, 1)
347 self.assertEqual(todo2.process.id_, 2)
349 def test_do_POST_day_todo_multiple_inner_adoption(self) -> None:
350 """Test multiple Todos can be posted to Day view w. inner adoption."""
352 def key_order_func(t: Todo) -> int:
353 assert isinstance(t.process.id_, int)
356 def check_adoption(date: str, new_todo: list[int]) -> None:
357 form_data = {'day_comment': '', 'new_todo': new_todo}
358 self.check_post(form_data, f'/day?date={date}', 302)
359 day_todos = Todo.by_date(self.db_conn, date)
360 day_todos.sort(key=key_order_func)
363 self.assertEqual(todo1.children, [])
364 self.assertEqual(todo1.parents, [todo2])
365 self.assertEqual(todo2.children, [todo1])
366 self.assertEqual(todo2.parents, [])
368 def check_nesting_adoption(process_id: int, date: str,
369 new_top_steps: list[int]) -> None:
370 result_reversed = new_top_steps[1] < new_top_steps[0]
371 form_data = self.post_process()
372 form_data = self.post_process(process_id,
374 {'new_top_step': new_top_steps})
375 form_data = {'day_comment': '', 'new_todo': [process_id]}
376 self.check_post(form_data, f'/day?date={date}', 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
384 self.assertEqual(todo1.children, [todo2, todo3])
386 self.assertEqual(todo1.children, [todo3, todo2])
387 self.assertEqual(todo1.parents, [])
388 self.assertEqual(todo2.children, [todo3])
389 self.assertEqual(todo2.parents, [todo1])
390 self.assertEqual(todo3.children, [])
391 self.assertEqual(todo3.parents, [todo2, todo1])
393 form_data = self.post_process()
394 form_data = self.post_process(2, form_data | {'new_top_step': 1})
395 check_adoption('2024-01-01', [1, 2])
396 check_adoption('2024-01-02', [2, 1])
397 check_nesting_adoption(3, '2024-01-03', [1, 2])
398 check_nesting_adoption(4, '2024-01-04', [2, 1])
400 def test_do_POST_day_todo_doneness(self) -> None:
401 """Test Todo doneness can be posted to Day view."""
402 form_data = self.post_process()
403 form_data = {'day_comment': '', 'new_todo': [1]}
404 self.check_post(form_data, '/day?date=2024-01-01', 302)
405 todo = Todo.by_date(self.db_conn, '2024-01-01')[0]
406 form_data = {'day_comment': '', 'todo_id': [1]}
407 self.check_post(form_data, '/day?date=2024-01-01', 302)
408 self.assertEqual(todo.is_done, False)
409 form_data = {'day_comment': '', 'todo_id': [1], 'done': [1]}
410 self.check_post(form_data, '/day?date=2024-01-01', 302)
411 self.assertEqual(todo.is_done, True)
413 def test_do_GET_todo(self) -> None:
414 """Test GET /todo response codes."""
416 form_data = {'day_comment': '', 'new_todo': 1}
417 self.check_post(form_data, '/day?date=2024-01-01', 302)
418 self.check_get('/todo', 400)
419 self.check_get('/todo?id=', 400)
420 self.check_get('/todo?id=foo', 400)
421 self.check_get('/todo?id=0', 404)
422 self.check_get('/todo?id=1', 200)