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': '', 'make_type': 'full'}
247 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 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&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 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&make_type=full', 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 'make_type': 'full'},
275 '/day?date=2024-01-01&make_type=full', 302)
276 # test posting to bad URLs
277 self.check_post({}, '/todo=', 404)
278 self.check_post({}, '/todo?id=', 400)
279 self.check_post({}, '/todo?id=FOO', 400)
280 self.check_post({}, '/todo?id=0', 404)
281 # test posting naked entity
282 todo1 = post_and_reload({})
283 self.assertEqual(todo1.children, [])
284 self.assertEqual(todo1.parents, [])
285 self.assertEqual(todo1.is_done, False)
286 # test posting doneness
287 todo1 = post_and_reload({'done': ''})
288 self.assertEqual(todo1.is_done, True)
289 # test implicitly posting non-doneness
290 todo1 = post_and_reload({})
291 self.assertEqual(todo1.is_done, False)
292 # test malformed adoptions
293 self.check_post({'adopt': 'foo'}, '/todo?id=1', 400)
294 self.check_post({'adopt': 1}, '/todo?id=1', 400)
295 self.check_post({'adopt': 2}, '/todo?id=1', 404)
296 # test posting second todo of same process
297 self.check_post({'day_comment': '', 'new_todo': 1,
298 'make_type': 'full'},
299 '/day?date=2024-01-01&make_type=full', 302)
300 # test todo 1 adopting todo 2
301 todo1 = post_and_reload({'adopt': 2})
302 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
303 self.assertEqual(todo1.children, [todo2])
304 self.assertEqual(todo1.parents, [])
305 self.assertEqual(todo2.children, [])
306 self.assertEqual(todo2.parents, [todo1])
307 # test todo1 cannot be set done with todo2 not done yet
308 todo1 = post_and_reload({'done': '', 'adopt': 2}, 400)
309 self.assertEqual(todo1.is_done, False)
310 # test todo1 un-adopting todo 2 by just not sending an adopt
311 todo1 = post_and_reload({}, 302)
312 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
313 self.assertEqual(todo1.children, [])
314 self.assertEqual(todo1.parents, [])
315 self.assertEqual(todo2.children, [])
316 self.assertEqual(todo2.parents, [])
317 # test todo1 deletion
318 todo1 = post_and_reload({'delete': ''}, 302, '/')
320 def test_do_POST_day_todo_adoption(self) -> None:
321 """Test Todos posted to Day view may adopt existing Todos."""
322 form_data = self.post_process()
323 form_data = self.post_process(2, form_data | {'new_top_step': 1})
324 form_data = {'day_comment': '', 'new_todo': 1, 'make_type': 'full'}
325 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
326 form_data['new_todo'] = 2
327 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
328 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
329 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
330 self.assertEqual(todo1.children, [])
331 self.assertEqual(todo1.parents, [todo2])
332 self.assertEqual(todo2.children, [todo1])
333 self.assertEqual(todo2.parents, [])
335 def test_do_POST_day_todo_multiple(self) -> None:
336 """Test multiple Todos can be posted to Day view."""
337 form_data = self.post_process()
338 form_data = self.post_process(2)
339 form_data = {'day_comment': '', 'new_todo': [1, 2],
341 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
342 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
343 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
344 self.assertEqual(todo1.process.id_, 1)
345 self.assertEqual(todo2.process.id_, 2)
347 def test_do_POST_day_todo_multiple_inner_adoption(self) -> None:
348 """Test multiple Todos can be posted to Day view w. inner adoption."""
350 def key_order_func(t: Todo) -> int:
351 assert isinstance(t.process.id_, int)
354 def check_adoption(date: str, new_todos: list[int]) -> None:
355 form_data = {'day_comment': '', 'new_todo': new_todos,
357 self.check_post(form_data, f'/day?date={date}&make_type=full', 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 form_data = {'title': '', 'description': '', 'effort': 1,
371 form_data = self.post_process(1, form_data)
372 form_data['new_top_step'] = new_top_steps
373 form_data['step_of'] = []
374 form_data = self.post_process(process_id, form_data)
375 form_data = {'day_comment': '', 'new_todo': [process_id],
377 self.check_post(form_data, f'/day?date={date}&make_type=full', 302)
378 day_todos = Todo.by_date(self.db_conn, date)
379 day_todos.sort(key=key_order_func, reverse=True)
380 self.assertEqual(len(day_todos), 3)
381 todo1 = day_todos[0] # process of process_id
382 todo2 = day_todos[1] # process 2
383 todo3 = day_todos[2] # process 1
384 self.assertEqual(sorted(todo1.children), sorted([todo2, todo3]))
385 self.assertEqual(todo1.parents, [])
386 self.assertEqual(todo2.children, [todo3])
387 self.assertEqual(todo2.parents, [todo1])
388 self.assertEqual(todo3.children, [])
389 self.assertEqual(sorted(todo3.parents), sorted([todo2, todo1]))
391 form_data = self.post_process()
392 form_data = self.post_process(2, form_data | {'new_top_step': 1})
393 check_adoption('2024-01-01', [1, 2])
394 check_adoption('2024-01-02', [2, 1])
395 check_nesting_adoption(3, '2024-01-03', [1, 2])
396 check_nesting_adoption(4, '2024-01-04', [2, 1])
398 def test_do_POST_day_todo_doneness(self) -> None:
399 """Test Todo doneness can be posted to Day view."""
400 form_data = self.post_process()
401 form_data = {'day_comment': '', 'new_todo': [1], 'make_type': 'full'}
402 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
403 todo = Todo.by_date(self.db_conn, '2024-01-01')[0]
404 form_data = {'day_comment': '', 'todo_id': [1], 'make_type': 'full'}
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],
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', 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)