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 self.assertEqual(Todo.by_date(self.db_conn, 'foo'), [])
68 def test_Todo_on_conditions(self) -> None:
69 """Test effect of Todos on Conditions."""
70 assert isinstance(self.cond1.id_, int)
71 assert isinstance(self.cond2.id_, int)
72 todo = Todo(None, self.proc, False, self.date1)
73 todo.save(self.db_conn)
74 todo.set_enables(self.db_conn, [self.cond1.id_])
75 todo.set_disables(self.db_conn, [self.cond2.id_])
77 self.assertEqual(self.cond1.is_active, True)
78 self.assertEqual(self.cond2.is_active, False)
80 self.assertEqual(self.cond1.is_active, True)
81 self.assertEqual(self.cond2.is_active, False)
83 def test_Todo_children(self) -> None:
84 """Test Todo.children relations."""
85 todo_1 = Todo(None, self.proc, False, self.date1)
86 todo_2 = Todo(None, self.proc, False, self.date1)
87 todo_2.save(self.db_conn)
88 with self.assertRaises(HandledException):
89 todo_1.add_child(todo_2)
90 todo_1.save(self.db_conn)
91 todo_3 = Todo(None, self.proc, False, self.date1)
92 with self.assertRaises(HandledException):
93 todo_1.add_child(todo_3)
94 todo_3.save(self.db_conn)
95 todo_1.add_child(todo_3)
96 todo_1.save(self.db_conn)
97 assert isinstance(todo_1.id_, int)
98 todo_retrieved = Todo.by_id(self.db_conn, todo_1.id_)
99 self.assertEqual(todo_retrieved.children, [todo_3])
100 with self.assertRaises(BadFormatException):
101 todo_3.add_child(todo_1)
103 def test_Todo_conditioning(self) -> None:
104 """Test Todo.doability conditions."""
105 assert isinstance(self.cond1.id_, int)
106 todo_1 = Todo(None, self.proc, False, self.date1)
107 todo_1.save(self.db_conn)
108 todo_2 = Todo(None, self.proc, False, self.date1)
109 todo_2.save(self.db_conn)
110 todo_2.add_child(todo_1)
111 with self.assertRaises(BadFormatException):
112 todo_2.is_done = True
113 todo_1.is_done = True
114 todo_2.is_done = True
115 todo_2.is_done = False
116 todo_2.set_conditions(self.db_conn, [self.cond1.id_])
117 with self.assertRaises(BadFormatException):
118 todo_2.is_done = True
119 self.cond1.is_active = True
120 todo_2.is_done = True
122 def test_Todo_step_tree(self) -> None:
123 """Test self-configuration of TodoStepsNode tree for Day view."""
124 todo_1 = Todo(None, self.proc, False, self.date1)
125 todo_1.save(self.db_conn)
126 assert isinstance(todo_1.id_, int)
128 node_0 = TodoNode(todo_1, False, [])
129 self.assertEqual(todo_1.get_step_tree(set()), node_0)
130 # test non_emtpy seen_todo does something
132 self.assertEqual(todo_1.get_step_tree({todo_1.id_}), node_0)
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()), node_0)
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()), node_0)
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()), node_0)
156 def test_Todo_unsatisfied_steps(self) -> None:
157 """Test options of satisfying unfulfilled Process.explicit_steps."""
158 assert isinstance(self.proc.id_, int)
159 todo_1 = Todo(None, self.proc, False, self.date1)
160 todo_1.save(self.db_conn)
161 proc2 = Process(None)
162 proc2.save(self.db_conn)
163 assert isinstance(proc2.id_, int)
164 proc3 = Process(None)
165 proc3.save(self.db_conn)
166 assert isinstance(proc3.id_, int)
167 proc4 = Process(None)
168 proc4.save(self.db_conn)
169 assert isinstance(proc4.id_, int)
170 proc3.set_steps(self.db_conn, [(None, proc4.id_, None)])
171 proc2.set_steps(self.db_conn, [(None, self.proc.id_, None),
172 (None, self.proc.id_, None),
173 (None, proc3.id_, None)])
174 todo_2 = Todo(None, proc2, False, self.date1)
175 todo_2.save(self.db_conn)
176 # test empty adoption does nothing
177 todo_2.adopt_from([])
178 self.assertEqual(todo_2.children, [])
179 # test basic adoption
180 todo_2.adopt_from([todo_1])
181 self.assertEqual(todo_2.children, [todo_1])
182 self.assertEqual(todo_1.parents, [todo_2])
183 # test making missing children
184 todo_2.make_missing_children(self.db_conn)
185 todo_3 = Todo.by_id(self.db_conn, 3)
186 todo_4 = Todo.by_id(self.db_conn, 4)
187 self.assertEqual(todo_2.children, [todo_1, todo_3, todo_4])
188 self.assertEqual(todo_3.process, self.proc)
189 self.assertEqual(todo_3.parents, [todo_2])
190 self.assertEqual(todo_3.children, [])
191 self.assertEqual(todo_4.process, proc3)
192 self.assertEqual(todo_4.parents, [todo_2])
193 # test .make_missing_children lower down the tree
194 todo_4.make_missing_children(self.db_conn)
195 todo_5 = Todo.by_id(self.db_conn, 5)
196 self.assertEqual(todo_5.process, proc4)
197 self.assertEqual(todo_4.children, [todo_5])
198 self.assertEqual(todo_5.parents, [todo_4])
200 def test_Todo_singularity(self) -> None:
201 """Test pointers made for single object keep pointing to it."""
202 self.check_singularity('is_done', True, self.proc, False, self.date1)
204 def test_Todo_remove(self) -> None:
206 todo_1 = Todo(None, self.proc, False, self.date1)
207 todo_1.save(self.db_conn)
208 todo_0 = Todo(None, self.proc, False, self.date1)
209 todo_0.save(self.db_conn)
210 todo_0.add_child(todo_1)
211 todo_2 = Todo(None, self.proc, False, self.date1)
212 todo_2.save(self.db_conn)
213 todo_1.add_child(todo_2)
214 todo_1.remove(self.db_conn)
215 with self.assertRaises(NotFoundException):
216 Todo.by_id(self.db_conn, todo_1.id_)
217 self.assertEqual(todo_0.children, [])
218 self.assertEqual(todo_2.parents, [])
219 todo_2.comment = 'foo'
220 with self.assertRaises(HandledException):
221 todo_2.remove(self.db_conn)
224 with self.assertRaises(HandledException):
225 todo_2.remove(self.db_conn)
227 def test_Todo_autoremoval(self) -> None:
228 """"Test automatic removal for Todo.effort < 0."""
229 todo_1 = Todo(None, self.proc, False, self.date1)
230 todo_1.save(self.db_conn)
231 todo_1.comment = 'foo'
233 todo_1.save(self.db_conn)
234 Todo.by_id(self.db_conn, todo_1.id_)
236 todo_1.save(self.db_conn)
237 with self.assertRaises(NotFoundException):
238 Todo.by_id(self.db_conn, todo_1.id_)
241 class TestsWithServer(TestCaseWithServer):
242 """Tests against our HTTP server/handler (and database)."""
244 def test_do_POST_day(self) -> None:
245 """Test Todo posting of POST /day."""
248 proc = Process.by_id(self.db_conn, 1)
249 proc2 = Process.by_id(self.db_conn, 2)
250 form_data = {'day_comment': ''}
251 self.check_post(form_data, '/day?date=2024-01-01', 302)
252 self.assertEqual(Todo.by_date(self.db_conn, '2024-01-01'), [])
253 form_data['new_todo'] = str(proc.id_)
254 self.check_post(form_data, '/day?date=2024-01-01', 302)
255 todos = Todo.by_date(self.db_conn, '2024-01-01')
256 self.assertEqual(1, len(todos))
258 self.assertEqual(todo1.id_, 1)
259 self.assertEqual(todo1.process.id_, proc.id_)
260 self.assertEqual(todo1.is_done, False)
261 form_data['new_todo'] = str(proc2.id_)
262 self.check_post(form_data, '/day?date=2024-01-01', 302)
263 todos = Todo.by_date(self.db_conn, '2024-01-01')
265 self.assertEqual(todo1.id_, 2)
266 self.assertEqual(todo1.process.id_, proc2.id_)
267 self.assertEqual(todo1.is_done, False)
269 def test_do_POST_todo(self) -> None:
270 """Test POST /todo."""
271 def post_and_reload(form_data: dict[str, object], status: int = 302,
272 redir_url: str = '/todo?id=1') -> Todo:
273 self.check_post(form_data, '/todo?id=1', status, redir_url)
274 return Todo.by_date(self.db_conn, '2024-01-01')[0]
277 self.check_post({'day_comment': '', 'new_todo': 1},
278 '/day?date=2024-01-01', 302)
279 # test posting to bad URLs
280 self.check_post({}, '/todo=', 404)
281 self.check_post({}, '/todo?id=', 400)
282 self.check_post({}, '/todo?id=FOO', 400)
283 self.check_post({}, '/todo?id=0', 404)
284 # test posting naked entity
285 todo1 = post_and_reload({})
286 self.assertEqual(todo1.children, [])
287 self.assertEqual(todo1.parents, [])
288 self.assertEqual(todo1.is_done, False)
289 # test posting doneness
290 todo1 = post_and_reload({'done': ''})
291 self.assertEqual(todo1.is_done, True)
292 # test implicitly posting non-doneness
293 todo1 = post_and_reload({})
294 self.assertEqual(todo1.is_done, False)
295 # test malformed adoptions
296 self.check_post({'adopt': 'foo'}, '/todo?id=1', 400)
297 self.check_post({'adopt': 1}, '/todo?id=1', 400)
298 self.check_post({'adopt': 2}, '/todo?id=1', 404)
299 # test posting second todo of same process
300 self.check_post({'day_comment': '', 'new_todo': 1},
301 '/day?date=2024-01-01', 302)
302 # test todo 1 adopting todo 2
303 todo1 = post_and_reload({'adopt': 2})
304 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
305 self.assertEqual(todo1.children, [todo2])
306 self.assertEqual(todo1.parents, [])
307 self.assertEqual(todo2.children, [])
308 self.assertEqual(todo2.parents, [todo1])
309 # test todo1 cannot be set done with todo2 not done yet
310 todo1 = post_and_reload({'done': '', 'adopt': 2}, 400)
311 self.assertEqual(todo1.is_done, False)
312 # test todo1 un-adopting todo 2 by just not sending an adopt
313 todo1 = post_and_reload({}, 302)
314 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
315 self.assertEqual(todo1.children, [])
316 self.assertEqual(todo1.parents, [])
317 self.assertEqual(todo2.children, [])
318 self.assertEqual(todo2.parents, [])
319 # test todo1 deletion
320 todo1 = post_and_reload({'delete': ''}, 302, '/')
322 def test_do_POST_day_todo_adoption(self) -> None:
323 """Test Todos posted to Day view may adopt existing Todos."""
324 form_data = self.post_process()
325 form_data = self.post_process(2, form_data | {'new_top_step': 1})
326 form_data = {'day_comment': '', 'new_todo': 1}
327 self.check_post(form_data, '/day?date=2024-01-01', 302)
328 form_data['new_todo'] = 2
329 self.check_post(form_data, '/day?date=2024-01-01', 302)
330 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
331 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
332 self.assertEqual(todo1.children, [])
333 self.assertEqual(todo1.parents, [todo2])
334 self.assertEqual(todo2.children, [todo1])
335 self.assertEqual(todo2.parents, [])
337 def test_do_POST_day_todo_multiple(self) -> None:
338 """Test multiple Todos can be posted to Day view."""
339 form_data = self.post_process()
340 form_data = self.post_process(2)
341 form_data = {'day_comment': '', 'new_todo': [1, 2]}
342 self.check_post(form_data, '/day?date=2024-01-01', 302)
343 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
344 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
345 self.assertEqual(todo1.process.id_, 1)
346 self.assertEqual(todo2.process.id_, 2)
348 def test_do_POST_day_todo_multiple_inner_adoption(self) -> None:
349 """Test multiple Todos can be posted to Day view w. inner adoption."""
351 def key_order_func(t: Todo) -> int:
352 assert isinstance(t.process.id_, int)
355 def check_adoption(date: str, new_todo: list[int]) -> None:
356 form_data = {'day_comment': '', 'new_todo': new_todo}
357 self.check_post(form_data, f'/day?date={date}', 302)
358 day_todos = Todo.by_date(self.db_conn, date)
359 day_todos.sort(key=key_order_func)
362 self.assertEqual(todo1.children, [])
363 self.assertEqual(todo1.parents, [todo2])
364 self.assertEqual(todo2.children, [todo1])
365 self.assertEqual(todo2.parents, [])
367 def check_nesting_adoption(process_id: int, date: str,
368 new_top_steps: list[int]) -> None:
369 result_reversed = new_top_steps[1] < new_top_steps[0]
370 form_data = self.post_process()
371 form_data = self.post_process(process_id,
373 {'new_top_step': new_top_steps})
374 form_data = {'day_comment': '', 'new_todo': [process_id]}
375 self.check_post(form_data, f'/day?date={date}', 302)
376 day_todos = Todo.by_date(self.db_conn, date)
377 day_todos.sort(key=key_order_func, reverse=True)
378 self.assertEqual(len(day_todos), 3)
379 todo1 = day_todos[0] # process of process_id
380 todo2 = day_todos[1] # process 2
381 todo3 = day_todos[2] # process 1
383 self.assertEqual(todo1.children, [todo2, todo3])
385 self.assertEqual(todo1.children, [todo3, todo2])
386 self.assertEqual(todo1.parents, [])
387 self.assertEqual(todo2.children, [todo3])
388 self.assertEqual(todo2.parents, [todo1])
389 self.assertEqual(todo3.children, [])
390 self.assertEqual(todo3.parents, [todo2, todo1])
392 form_data = self.post_process()
393 form_data = self.post_process(2, form_data | {'new_top_step': 1})
394 check_adoption('2024-01-01', [1, 2])
395 check_adoption('2024-01-02', [2, 1])
396 check_nesting_adoption(3, '2024-01-03', [1, 2])
397 check_nesting_adoption(4, '2024-01-04', [2, 1])
399 def test_do_POST_day_todo_doneness(self) -> None:
400 """Test Todo doneness can be posted to Day view."""
401 form_data = self.post_process()
402 form_data = {'day_comment': '', 'new_todo': [1]}
403 self.check_post(form_data, '/day?date=2024-01-01', 302)
404 todo = Todo.by_date(self.db_conn, '2024-01-01')[0]
405 form_data = {'day_comment': '', 'todo_id': [1]}
406 self.check_post(form_data, '/day?date=2024-01-01', 302)
407 self.assertEqual(todo.is_done, False)
408 form_data = {'day_comment': '', 'todo_id': [1], 'done': [1]}
409 self.check_post(form_data, '/day?date=2024-01-01', 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}
416 self.check_post(form_data, '/day?date=2024-01-01', 302)
417 self.check_get('/todo', 400)
418 self.check_get('/todo?id=', 400)
419 self.check_get('/todo?id=foo', 400)
420 self.check_get('/todo?id=0', 404)
421 self.check_get('/todo?id=1', 200)