1 """Test Todos module."""
2 from tests.utils import 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):
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_create_with_children(self) -> None:
158 """Test parenthood guaranteeds of Todo.create_with_children."""
159 assert isinstance(self.proc.id_, int)
160 proc2 = Process(None)
161 proc2.save(self.db_conn)
162 assert isinstance(proc2.id_, int)
163 proc3 = Process(None)
164 proc3.save(self.db_conn)
165 assert isinstance(proc3.id_, int)
166 proc4 = Process(None)
167 proc4.save(self.db_conn)
168 assert isinstance(proc4.id_, int)
169 # make proc4 step of proc3
170 step = ProcessStep(None, proc3.id_, proc4.id_, None)
171 proc3.set_steps(self.db_conn, [step])
172 # give proc2 three steps; 2× proc1, 1× proc3
173 step1 = ProcessStep(None, proc2.id_, self.proc.id_, None)
174 step2 = ProcessStep(None, proc2.id_, self.proc.id_, None)
175 step3 = ProcessStep(None, proc2.id_, proc3.id_, None)
176 proc2.set_steps(self.db_conn, [step1, step2, step3])
177 # test mere creation does nothing
178 todo_ignore = Todo(None, proc2, False, self.date1)
179 todo_ignore.save(self.db_conn)
180 self.assertEqual(todo_ignore.children, [])
181 # test create_with_children on step-less does nothing
182 todo_1 = Todo.create_with_children(self.db_conn, self.proc.id_,
184 self.assertEqual(todo_1.children, [])
185 self.assertEqual(len(Todo.all(self.db_conn)), 2)
186 # test create_with_children adopts and creates, and down tree too
187 todo_2 = Todo.create_with_children(self.db_conn, proc2.id_, self.date1)
188 self.assertEqual(3, len(todo_2.children))
189 self.assertEqual(todo_1, todo_2.children[0])
190 self.assertEqual(self.proc, todo_2.children[1].process)
191 self.assertEqual(proc3, todo_2.children[2].process)
192 todo_3 = todo_2.children[2]
193 self.assertEqual(len(todo_3.children), 1)
194 self.assertEqual(todo_3.children[0].process, proc4)
196 def test_Todo_singularity(self) -> None:
197 """Test pointers made for single object keep pointing to it."""
198 self.check_singularity('is_done', True, self.proc, False, self.date1)
200 def test_Todo_remove(self) -> None:
202 todo_1 = Todo(None, self.proc, False, self.date1)
203 todo_1.save(self.db_conn)
204 todo_0 = Todo(None, self.proc, False, self.date1)
205 todo_0.save(self.db_conn)
206 todo_0.add_child(todo_1)
207 todo_2 = Todo(None, self.proc, False, self.date1)
208 todo_2.save(self.db_conn)
209 todo_1.add_child(todo_2)
210 todo_1.remove(self.db_conn)
211 with self.assertRaises(NotFoundException):
212 Todo.by_id(self.db_conn, todo_1.id_)
213 self.assertEqual(todo_0.children, [])
214 self.assertEqual(todo_2.parents, [])
215 todo_2.comment = 'foo'
216 with self.assertRaises(HandledException):
217 todo_2.remove(self.db_conn)
220 with self.assertRaises(HandledException):
221 todo_2.remove(self.db_conn)
223 def test_Todo_autoremoval(self) -> None:
224 """"Test automatic removal for Todo.effort < 0."""
225 todo_1 = Todo(None, self.proc, False, self.date1)
226 todo_1.save(self.db_conn)
227 todo_1.comment = 'foo'
229 todo_1.save(self.db_conn)
230 Todo.by_id(self.db_conn, todo_1.id_)
232 todo_1.save(self.db_conn)
233 with self.assertRaises(NotFoundException):
234 Todo.by_id(self.db_conn, todo_1.id_)
237 class TestsWithServer(TestCaseWithServer):
238 """Tests against our HTTP server/handler (and database)."""
240 def test_do_POST_day(self) -> None:
241 """Test Todo posting of POST /day."""
244 proc = Process.by_id(self.db_conn, 1)
245 proc2 = Process.by_id(self.db_conn, 2)
246 form_data = {'day_comment': ''}
247 self.check_post(form_data, '/day?date=2024-01-01', 302)
248 self.assertEqual(Todo.by_date(self.db_conn, '2024-01-01'), [])
249 form_data['new_todo'] = str(proc.id_)
250 self.check_post(form_data, '/day?date=2024-01-01', 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 self.assertEqual(todo1.process.id_, proc.id_)
256 self.assertEqual(todo1.is_done, False)
257 form_data['new_todo'] = str(proc2.id_)
258 self.check_post(form_data, '/day?date=2024-01-01', 302)
259 todos = Todo.by_date(self.db_conn, '2024-01-01')
261 self.assertEqual(todo1.id_, 2)
262 self.assertEqual(todo1.process.id_, proc2.id_)
263 self.assertEqual(todo1.is_done, False)
265 def test_do_POST_todo(self) -> None:
266 """Test POST /todo."""
267 def post_and_reload(form_data: dict[str, object], status: int = 302,
268 redir_url: str = '/todo?id=1') -> Todo:
269 self.check_post(form_data, '/todo?id=1', status, redir_url)
270 return Todo.by_date(self.db_conn, '2024-01-01')[0]
273 self.check_post({'day_comment': '', 'new_todo': 1},
274 '/day?date=2024-01-01', 302)
275 # test posting to bad URLs
276 self.check_post({}, '/todo=', 404)
277 self.check_post({}, '/todo?id=', 400)
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 '/day?date=2024-01-01', 302)
298 # test todo 1 adopting todo 2
299 todo1 = post_and_reload({'adopt': 2})
300 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
301 self.assertEqual(todo1.children, [todo2])
302 self.assertEqual(todo1.parents, [])
303 self.assertEqual(todo2.children, [])
304 self.assertEqual(todo2.parents, [todo1])
305 # test todo1 cannot be set done with todo2 not done yet
306 todo1 = post_and_reload({'done': '', 'adopt': 2}, 400)
307 self.assertEqual(todo1.is_done, False)
308 # test todo1 un-adopting todo 2 by just not sending an adopt
309 todo1 = post_and_reload({}, 302)
310 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
311 self.assertEqual(todo1.children, [])
312 self.assertEqual(todo1.parents, [])
313 self.assertEqual(todo2.children, [])
314 self.assertEqual(todo2.parents, [])
315 # test todo1 deletion
316 todo1 = post_and_reload({'delete': ''}, 302, '/')
318 def test_do_POST_day_todo_adoption(self) -> None:
319 """Test Todos posted to Day view may adopt existing Todos."""
320 form_data = self.post_process()
321 form_data = self.post_process(2, form_data | {'new_top_step': 1})
322 form_data = {'day_comment': '', 'new_todo': 1}
323 self.check_post(form_data, '/day?date=2024-01-01', 302)
324 form_data['new_todo'] = 2
325 self.check_post(form_data, '/day?date=2024-01-01', 302)
326 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
327 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
328 self.assertEqual(todo1.children, [])
329 self.assertEqual(todo1.parents, [todo2])
330 self.assertEqual(todo2.children, [todo1])
331 self.assertEqual(todo2.parents, [])
333 def test_do_POST_day_todo_multiple(self) -> None:
334 """Test multiple Todos can be posted to Day view."""
335 form_data = self.post_process()
336 form_data = self.post_process(2)
337 form_data = {'day_comment': '', 'new_todo': [1, 2]}
338 self.check_post(form_data, '/day?date=2024-01-01', 302)
339 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
340 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
341 self.assertEqual(todo1.process.id_, 1)
342 self.assertEqual(todo2.process.id_, 2)
344 def test_do_POST_day_todo_multiple_inner_adoption(self) -> None:
345 """Test multiple Todos can be posted to Day view w. inner adoption."""
347 def key_order_func(t: Todo) -> int:
348 assert isinstance(t.process.id_, int)
351 def check_adoption(date: str, new_todos: list[int]) -> None:
352 form_data = {'day_comment': '', 'new_todo': new_todos}
353 self.check_post(form_data, f'/day?date={date}', 302)
354 day_todos = Todo.by_date(self.db_conn, date)
355 day_todos.sort(key=key_order_func)
358 self.assertEqual(todo1.children, [])
359 self.assertEqual(todo1.parents, [todo2])
360 self.assertEqual(todo2.children, [todo1])
361 self.assertEqual(todo2.parents, [])
363 def check_nesting_adoption(process_id: int, date: str,
364 new_top_steps: list[int]) -> None:
365 form_data = self.post_process()
366 form_data = self.post_process(process_id,
368 {'new_top_step': new_top_steps})
369 form_data = {'day_comment': '', 'new_todo': [process_id]}
370 self.check_post(form_data, f'/day?date={date}', 302)
371 day_todos = Todo.by_date(self.db_conn, date)
372 day_todos.sort(key=key_order_func, reverse=True)
373 self.assertEqual(len(day_todos), 3)
374 todo1 = day_todos[0] # process of process_id
375 todo2 = day_todos[1] # process 2
376 todo3 = day_todos[2] # process 1
377 self.assertEqual(sorted(todo1.children), sorted([todo2, todo3]))
378 self.assertEqual(todo1.parents, [])
379 self.assertEqual(todo2.children, [todo3])
380 self.assertEqual(todo2.parents, [todo1])
381 self.assertEqual(todo3.children, [])
382 self.assertEqual(sorted(todo3.parents), sorted([todo2, todo1]))
384 form_data = self.post_process()
385 form_data = self.post_process(2, form_data | {'new_top_step': 1})
386 check_adoption('2024-01-01', [1, 2])
387 check_adoption('2024-01-02', [2, 1])
388 check_nesting_adoption(3, '2024-01-03', [1, 2])
389 check_nesting_adoption(4, '2024-01-04', [2, 1])
391 def test_do_POST_day_todo_doneness(self) -> None:
392 """Test Todo doneness can be posted to Day view."""
393 form_data = self.post_process()
394 form_data = {'day_comment': '', 'new_todo': [1]}
395 self.check_post(form_data, '/day?date=2024-01-01', 302)
396 todo = Todo.by_date(self.db_conn, '2024-01-01')[0]
397 form_data = {'day_comment': '', 'todo_id': [1]}
398 self.check_post(form_data, '/day?date=2024-01-01', 302)
399 self.assertEqual(todo.is_done, False)
400 form_data = {'day_comment': '', 'todo_id': [1], 'done': [1]}
401 self.check_post(form_data, '/day?date=2024-01-01', 302)
402 self.assertEqual(todo.is_done, True)
404 def test_do_GET_todo(self) -> None:
405 """Test GET /todo response codes."""
407 form_data = {'day_comment': '', 'new_todo': 1}
408 self.check_post(form_data, '/day?date=2024-01-01', 302)
409 self.check_get('/todo', 400)
410 self.check_get('/todo?id=', 400)
411 self.check_get('/todo?id=foo', 400)
412 self.check_get('/todo?id=0', 404)
413 self.check_get('/todo?id=1', 200)