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_setting, which
14 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']
24 def setUp(self) -> None:
26 self.date1 = '2024-01-01'
27 self.date2 = '2024-01-02'
28 self.proc = Process(None)
29 self.proc.save(self.db_conn)
30 self.cond1 = Condition(None)
31 self.cond1.save(self.db_conn)
32 self.cond2 = Condition(None)
33 self.cond2.save(self.db_conn)
34 self.default_init_kwargs['process'] = self.proc
35 self.default_init_args[0] = self.proc
37 def test_Todo_init(self) -> None:
38 """Test creation of Todo and what they default to."""
39 process = Process(None)
40 with self.assertRaises(NotFoundException):
41 Todo(None, process, False, self.date1)
42 process.save(self.db_conn)
43 assert isinstance(self.cond1.id_, int)
44 assert isinstance(self.cond2.id_, int)
45 process.set_conditions(self.db_conn, [self.cond1.id_, self.cond2.id_])
46 process.set_enables(self.db_conn, [self.cond1.id_])
47 process.set_disables(self.db_conn, [self.cond2.id_])
48 todo_no_id = Todo(None, process, False, self.date1)
49 self.assertEqual(todo_no_id.conditions, [self.cond1, self.cond2])
50 self.assertEqual(todo_no_id.enables, [self.cond1])
51 self.assertEqual(todo_no_id.disables, [self.cond2])
52 todo_yes_id = Todo(5, process, False, self.date1)
53 self.assertEqual(todo_yes_id.conditions, [])
54 self.assertEqual(todo_yes_id.enables, [])
55 self.assertEqual(todo_yes_id.disables, [])
57 def test_Todo_by_id(self) -> None:
58 """Test findability of Todos."""
59 todo = Todo(1, self.proc, False, self.date1)
60 todo.save(self.db_conn)
61 self.assertEqual(Todo.by_id(self.db_conn, 1), todo)
62 with self.assertRaises(NotFoundException):
63 Todo.by_id(self.db_conn, 0)
64 with self.assertRaises(NotFoundException):
65 Todo.by_id(self.db_conn, 2)
67 def test_Todo_by_date(self) -> None:
68 """Test findability of Todos by date."""
69 t1 = Todo(None, self.proc, False, self.date1)
71 t2 = Todo(None, self.proc, False, self.date1)
73 self.assertEqual(Todo.by_date(self.db_conn, self.date1), [t1, t2])
74 self.assertEqual(Todo.by_date(self.db_conn, self.date2), [])
75 with self.assertRaises(BadFormatException):
76 self.assertEqual(Todo.by_date(self.db_conn, 'foo'), [])
78 def test_Todo_on_conditions(self) -> None:
79 """Test effect of Todos on Conditions."""
80 assert isinstance(self.cond1.id_, int)
81 assert isinstance(self.cond2.id_, int)
82 todo = Todo(None, self.proc, False, self.date1)
83 todo.save(self.db_conn)
84 todo.set_enables(self.db_conn, [self.cond1.id_])
85 todo.set_disables(self.db_conn, [self.cond2.id_])
87 self.assertEqual(self.cond1.is_active, True)
88 self.assertEqual(self.cond2.is_active, False)
90 self.assertEqual(self.cond1.is_active, True)
91 self.assertEqual(self.cond2.is_active, False)
93 def test_Todo_children(self) -> None:
94 """Test Todo.children relations."""
95 todo_1 = Todo(None, self.proc, False, self.date1)
96 todo_2 = Todo(None, self.proc, False, self.date1)
97 todo_2.save(self.db_conn)
98 with self.assertRaises(HandledException):
99 todo_1.add_child(todo_2)
100 todo_1.save(self.db_conn)
101 todo_3 = Todo(None, self.proc, False, self.date1)
102 with self.assertRaises(HandledException):
103 todo_1.add_child(todo_3)
104 todo_3.save(self.db_conn)
105 todo_1.add_child(todo_3)
106 todo_1.save(self.db_conn)
107 assert isinstance(todo_1.id_, int)
108 todo_retrieved = Todo.by_id(self.db_conn, todo_1.id_)
109 self.assertEqual(todo_retrieved.children, [todo_3])
110 with self.assertRaises(BadFormatException):
111 todo_3.add_child(todo_1)
113 def test_Todo_conditioning(self) -> None:
114 """Test Todo.doability conditions."""
115 assert isinstance(self.cond1.id_, int)
116 todo_1 = Todo(None, self.proc, False, self.date1)
117 todo_1.save(self.db_conn)
118 todo_2 = Todo(None, self.proc, False, self.date1)
119 todo_2.save(self.db_conn)
120 todo_2.add_child(todo_1)
121 with self.assertRaises(BadFormatException):
122 todo_2.is_done = True
123 todo_1.is_done = True
124 todo_2.is_done = True
125 todo_2.is_done = False
126 todo_2.set_conditions(self.db_conn, [self.cond1.id_])
127 with self.assertRaises(BadFormatException):
128 todo_2.is_done = True
129 self.cond1.is_active = True
130 todo_2.is_done = True
132 def test_Todo_step_tree(self) -> None:
133 """Test self-configuration of TodoStepsNode tree for Day view."""
134 todo_1 = Todo(None, self.proc, False, self.date1)
135 todo_1.save(self.db_conn)
136 assert isinstance(todo_1.id_, int)
138 node_0 = TodoNode(todo_1, False, [])
139 self.assertEqual(todo_1.get_step_tree(set()), node_0)
140 # test non_emtpy seen_todo does something
142 self.assertEqual(todo_1.get_step_tree({todo_1.id_}), node_0)
143 # test child shows up
144 todo_2 = Todo(None, self.proc, False, self.date1)
145 todo_2.save(self.db_conn)
146 assert isinstance(todo_2.id_, int)
147 todo_1.add_child(todo_2)
148 node_2 = TodoNode(todo_2, False, [])
149 node_0.children = [node_2]
151 self.assertEqual(todo_1.get_step_tree(set()), node_0)
152 # test child shows up with child
153 todo_3 = Todo(None, self.proc, False, self.date1)
154 todo_3.save(self.db_conn)
155 assert isinstance(todo_3.id_, int)
156 todo_2.add_child(todo_3)
157 node_3 = TodoNode(todo_3, False, [])
158 node_2.children = [node_3]
159 self.assertEqual(todo_1.get_step_tree(set()), node_0)
160 # test same todo can be child-ed multiple times at different locations
161 todo_1.add_child(todo_3)
162 node_4 = TodoNode(todo_3, True, [])
163 node_0.children += [node_4]
164 self.assertEqual(todo_1.get_step_tree(set()), node_0)
166 def test_Todo_create_with_children(self) -> None:
167 """Test parenthood guaranteeds of Todo.create_with_children."""
168 assert isinstance(self.proc.id_, int)
169 proc2 = Process(None)
170 proc2.save(self.db_conn)
171 assert isinstance(proc2.id_, int)
172 proc3 = Process(None)
173 proc3.save(self.db_conn)
174 assert isinstance(proc3.id_, int)
175 proc4 = Process(None)
176 proc4.save(self.db_conn)
177 assert isinstance(proc4.id_, int)
178 # make proc4 step of proc3
179 step = ProcessStep(None, proc3.id_, proc4.id_, None)
180 proc3.set_steps(self.db_conn, [step])
181 # give proc2 three steps; 2× proc1, 1× proc3
182 step1 = ProcessStep(None, proc2.id_, self.proc.id_, None)
183 step2 = ProcessStep(None, proc2.id_, self.proc.id_, None)
184 step3 = ProcessStep(None, proc2.id_, proc3.id_, None)
185 proc2.set_steps(self.db_conn, [step1, step2, step3])
186 # test mere creation does nothing
187 todo_ignore = Todo(None, proc2, False, self.date1)
188 todo_ignore.save(self.db_conn)
189 self.assertEqual(todo_ignore.children, [])
190 # test create_with_children on step-less does nothing
191 todo_1 = Todo.create_with_children(self.db_conn, self.proc.id_,
193 self.assertEqual(todo_1.children, [])
194 self.assertEqual(len(Todo.all(self.db_conn)), 2)
195 # test create_with_children adopts and creates, and down tree too
196 todo_2 = Todo.create_with_children(self.db_conn, proc2.id_, self.date1)
197 self.assertEqual(3, len(todo_2.children))
198 self.assertEqual(todo_1, todo_2.children[0])
199 self.assertEqual(self.proc, todo_2.children[2].process)
200 self.assertEqual(proc3, todo_2.children[1].process)
201 todo_3 = todo_2.children[1]
202 self.assertEqual(len(todo_3.children), 1)
203 self.assertEqual(todo_3.children[0].process, proc4)
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_id = todo_1.id_
216 todo_1.remove(self.db_conn)
217 with self.assertRaises(NotFoundException):
218 Todo.by_id(self.db_conn, todo_1_id)
219 self.assertEqual(todo_0.children, [])
220 self.assertEqual(todo_2.parents, [])
221 todo_2.comment = 'foo'
222 with self.assertRaises(HandledException):
223 todo_2.remove(self.db_conn)
226 with self.assertRaises(HandledException):
227 todo_2.remove(self.db_conn)
229 def test_Todo_autoremoval(self) -> None:
230 """"Test automatic removal for Todo.effort < 0."""
231 todo_1 = Todo(None, self.proc, False, self.date1)
232 todo_1.save(self.db_conn)
233 todo_1.comment = 'foo'
235 todo_1.save(self.db_conn)
236 Todo.by_id(self.db_conn, todo_1.id_)
238 todo_1_id = todo_1.id_
239 todo_1.save(self.db_conn)
240 with self.assertRaises(NotFoundException):
241 Todo.by_id(self.db_conn, todo_1_id)
244 class TestsWithServer(TestCaseWithServer):
245 """Tests against our HTTP server/handler (and database)."""
247 def test_do_POST_day(self) -> None:
248 """Test Todo posting of POST /day."""
251 proc = Process.by_id(self.db_conn, 1)
252 proc2 = Process.by_id(self.db_conn, 2)
253 form_data = {'day_comment': '', 'make_type': 'full'}
254 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
255 self.assertEqual(Todo.by_date(self.db_conn, '2024-01-01'), [])
256 form_data['new_todo'] = str(proc.id_)
257 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
258 todos = Todo.by_date(self.db_conn, '2024-01-01')
259 self.assertEqual(1, len(todos))
261 self.assertEqual(todo1.id_, 1)
262 self.assertEqual(todo1.process.id_, proc.id_)
263 self.assertEqual(todo1.is_done, False)
264 form_data['new_todo'] = str(proc2.id_)
265 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
266 todos = Todo.by_date(self.db_conn, '2024-01-01')
268 self.assertEqual(todo1.id_, 2)
269 self.assertEqual(todo1.process.id_, proc2.id_)
270 self.assertEqual(todo1.is_done, False)
272 def test_do_POST_todo(self) -> None:
273 """Test POST /todo."""
274 def post_and_reload(form_data: dict[str, object], status: int = 302,
275 redir_url: str = '/todo?id=1') -> Todo:
276 self.check_post(form_data, '/todo?id=1', status, redir_url)
277 return Todo.by_date(self.db_conn, '2024-01-01')[0]
280 self.check_post({'day_comment': '', 'new_todo': 1,
281 'make_type': 'full'},
282 '/day?date=2024-01-01&make_type=full', 302)
283 # test posting to bad URLs
284 self.check_post({}, '/todo=', 404)
285 self.check_post({}, '/todo?id=', 400)
286 self.check_post({}, '/todo?id=FOO', 400)
287 self.check_post({}, '/todo?id=0', 404)
288 # test posting naked entity
289 todo1 = post_and_reload({})
290 self.assertEqual(todo1.children, [])
291 self.assertEqual(todo1.parents, [])
292 self.assertEqual(todo1.is_done, False)
293 # test posting doneness
294 todo1 = post_and_reload({'done': ''})
295 self.assertEqual(todo1.is_done, True)
296 # test implicitly posting non-doneness
297 todo1 = post_and_reload({})
298 self.assertEqual(todo1.is_done, False)
299 # test malformed adoptions
300 self.check_post({'adopt': 'foo'}, '/todo?id=1', 400)
301 self.check_post({'adopt': 1}, '/todo?id=1', 400)
302 self.check_post({'adopt': 2}, '/todo?id=1', 404)
303 # test posting second todo of same process
304 self.check_post({'day_comment': '', 'new_todo': 1,
305 'make_type': 'full'},
306 '/day?date=2024-01-01&make_type=full', 302)
307 # test todo 1 adopting todo 2
308 todo1 = post_and_reload({'adopt': 2})
309 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
310 self.assertEqual(todo1.children, [todo2])
311 self.assertEqual(todo1.parents, [])
312 self.assertEqual(todo2.children, [])
313 self.assertEqual(todo2.parents, [todo1])
314 # test todo1 cannot be set done with todo2 not done yet
315 todo1 = post_and_reload({'done': '', 'adopt': 2}, 400)
316 self.assertEqual(todo1.is_done, False)
317 # test todo1 un-adopting todo 2 by just not sending an adopt
318 todo1 = post_and_reload({}, 302)
319 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
320 self.assertEqual(todo1.children, [])
321 self.assertEqual(todo1.parents, [])
322 self.assertEqual(todo2.children, [])
323 self.assertEqual(todo2.parents, [])
324 # test todo1 deletion
325 todo1 = post_and_reload({'delete': ''}, 302, '/')
327 def test_do_POST_day_todo_adoption(self) -> None:
328 """Test Todos posted to Day view may adopt existing Todos."""
329 form_data = self.post_process()
330 form_data = self.post_process(2, form_data | {'new_top_step': 1})
331 form_data = {'day_comment': '', 'new_todo': 1, 'make_type': 'full'}
332 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
333 form_data['new_todo'] = 2
334 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
335 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
336 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
337 self.assertEqual(todo1.children, [])
338 self.assertEqual(todo1.parents, [todo2])
339 self.assertEqual(todo2.children, [todo1])
340 self.assertEqual(todo2.parents, [])
342 def test_do_POST_day_todo_multiple(self) -> None:
343 """Test multiple Todos can be posted to Day view."""
344 form_data = self.post_process()
345 form_data = self.post_process(2)
346 form_data = {'day_comment': '', 'new_todo': [1, 2],
348 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
349 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
350 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
351 self.assertEqual(todo1.process.id_, 1)
352 self.assertEqual(todo2.process.id_, 2)
354 def test_do_POST_day_todo_multiple_inner_adoption(self) -> None:
355 """Test multiple Todos can be posted to Day view w. inner adoption."""
357 def key_order_func(t: Todo) -> int:
358 assert isinstance(t.process.id_, int)
361 def check_adoption(date: str, new_todos: list[int]) -> None:
362 form_data = {'day_comment': '', 'new_todo': new_todos,
364 self.check_post(form_data, f'/day?date={date}&make_type=full', 302)
365 day_todos = Todo.by_date(self.db_conn, date)
366 day_todos.sort(key=key_order_func)
369 self.assertEqual(todo1.children, [])
370 self.assertEqual(todo1.parents, [todo2])
371 self.assertEqual(todo2.children, [todo1])
372 self.assertEqual(todo2.parents, [])
374 def check_nesting_adoption(process_id: int, date: str,
375 new_top_steps: list[int]) -> None:
376 form_data = {'title': '', 'description': '', 'effort': 1,
378 form_data = self.post_process(1, form_data)
379 form_data['new_top_step'] = new_top_steps
380 form_data['step_of'] = []
381 form_data = self.post_process(process_id, form_data)
382 form_data = {'day_comment': '', 'new_todo': [process_id],
384 self.check_post(form_data, f'/day?date={date}&make_type=full', 302)
385 day_todos = Todo.by_date(self.db_conn, date)
386 day_todos.sort(key=key_order_func, reverse=True)
387 self.assertEqual(len(day_todos), 3)
388 todo1 = day_todos[0] # process of process_id
389 todo2 = day_todos[1] # process 2
390 todo3 = day_todos[2] # process 1
391 self.assertEqual(sorted(todo1.children), sorted([todo2, todo3]))
392 self.assertEqual(todo1.parents, [])
393 self.assertEqual(todo2.children, [todo3])
394 self.assertEqual(todo2.parents, [todo1])
395 self.assertEqual(todo3.children, [])
396 self.assertEqual(sorted(todo3.parents), sorted([todo2, todo1]))
398 form_data = self.post_process()
399 form_data = self.post_process(2, form_data | {'new_top_step': 1})
400 check_adoption('2024-01-01', [1, 2])
401 check_adoption('2024-01-02', [2, 1])
402 check_nesting_adoption(3, '2024-01-03', [1, 2])
403 check_nesting_adoption(4, '2024-01-04', [2, 1])
405 def test_do_POST_day_todo_doneness(self) -> None:
406 """Test Todo doneness can be posted to Day view."""
408 form_data = {'day_comment': '', 'new_todo': [1], 'make_type': 'full'}
409 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
410 todo = Todo.by_date(self.db_conn, '2024-01-01')[0]
411 form_data = {'day_comment': '', 'todo_id': [1], 'make_type': 'full'}
412 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
413 self.assertEqual(todo.is_done, False)
414 form_data = {'day_comment': '', 'todo_id': [1], 'done': [1],
416 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
417 self.assertEqual(todo.is_done, True)
419 def test_do_GET_todo(self) -> None:
420 """Test GET /todo response codes."""
422 form_data = {'day_comment': '', 'new_todo': 1, 'make_type': 'full'}
423 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
424 self.check_get('/todo', 400)
425 self.check_get('/todo?id=', 400)
426 self.check_get('/todo?id=foo', 400)
427 self.check_get('/todo?id=0', 404)
428 self.check_get('/todo?id=1', 200)