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