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 run any tests there that due to any
14 Todo requiring a _saved_ Process wouldn't run without a DB.
17 default_init_kwargs = {'process': None, 'is_done': False,
20 def setUp(self) -> None:
22 self.date1 = '2024-01-01'
23 self.date2 = '2024-01-02'
24 self.proc = Process(None)
25 self.proc.save(self.db_conn)
26 self.cond1 = Condition(None)
27 self.cond1.save(self.db_conn)
28 self.cond2 = Condition(None)
29 self.cond2.save(self.db_conn)
30 self.default_init_kwargs['process'] = self.proc
32 def test_Todo_init(self) -> None:
33 """Test creation of Todo and what they default to."""
34 process = Process(None)
35 with self.assertRaises(NotFoundException):
36 Todo(None, process, False, self.date1)
37 process.save(self.db_conn)
38 assert isinstance(self.cond1.id_, int)
39 assert isinstance(self.cond2.id_, int)
40 process.set_conditions(self.db_conn, [self.cond1.id_, self.cond2.id_])
41 process.set_enables(self.db_conn, [self.cond1.id_])
42 process.set_disables(self.db_conn, [self.cond2.id_])
43 todo_no_id = Todo(None, process, False, self.date1)
44 self.assertEqual(todo_no_id.conditions, [self.cond1, self.cond2])
45 self.assertEqual(todo_no_id.enables, [self.cond1])
46 self.assertEqual(todo_no_id.disables, [self.cond2])
47 todo_yes_id = Todo(5, process, False, self.date1)
48 self.assertEqual(todo_yes_id.conditions, [])
49 self.assertEqual(todo_yes_id.enables, [])
50 self.assertEqual(todo_yes_id.disables, [])
52 def test_Todo_by_date(self) -> None:
53 """Test findability of Todos by date."""
54 t1 = Todo(None, self.proc, False, self.date1)
56 t2 = Todo(None, self.proc, False, self.date1)
58 self.assertEqual(Todo.by_date(self.db_conn, self.date1), [t1, t2])
59 self.assertEqual(Todo.by_date(self.db_conn, self.date2), [])
60 with self.assertRaises(BadFormatException):
61 self.assertEqual(Todo.by_date(self.db_conn, 'foo'), [])
63 def test_Todo_by_date_range_with_limits(self) -> None:
64 """Test .by_date_range_with_limits."""
65 self.check_by_date_range_with_limits('day')
67 def test_Todo_on_conditions(self) -> None:
68 """Test effect of Todos on Conditions."""
69 assert isinstance(self.cond1.id_, int)
70 assert isinstance(self.cond2.id_, int)
71 todo = Todo(None, self.proc, False, self.date1)
72 todo.save(self.db_conn)
73 todo.set_enables(self.db_conn, [self.cond1.id_])
74 todo.set_disables(self.db_conn, [self.cond2.id_])
76 self.assertEqual(self.cond1.is_active, True)
77 self.assertEqual(self.cond2.is_active, False)
79 self.assertEqual(self.cond1.is_active, True)
80 self.assertEqual(self.cond2.is_active, False)
82 def test_Todo_children(self) -> None:
83 """Test Todo.children relations."""
84 todo_1 = Todo(None, self.proc, False, self.date1)
85 todo_2 = Todo(None, self.proc, False, self.date1)
86 todo_2.save(self.db_conn)
87 with self.assertRaises(HandledException):
88 todo_1.add_child(todo_2)
89 todo_1.save(self.db_conn)
90 todo_3 = Todo(None, self.proc, False, self.date1)
91 with self.assertRaises(HandledException):
92 todo_1.add_child(todo_3)
93 todo_3.save(self.db_conn)
94 todo_1.add_child(todo_3)
95 todo_1.save(self.db_conn)
96 assert isinstance(todo_1.id_, int)
97 todo_retrieved = Todo.by_id(self.db_conn, todo_1.id_)
98 self.assertEqual(todo_retrieved.children, [todo_3])
99 with self.assertRaises(BadFormatException):
100 todo_3.add_child(todo_1)
102 def test_Todo_conditioning(self) -> None:
103 """Test Todo.doability conditions."""
104 assert isinstance(self.cond1.id_, int)
105 todo_1 = Todo(None, self.proc, False, self.date1)
106 todo_1.save(self.db_conn)
107 todo_2 = Todo(None, self.proc, False, self.date1)
108 todo_2.save(self.db_conn)
109 todo_2.add_child(todo_1)
110 with self.assertRaises(BadFormatException):
111 todo_2.is_done = True
112 todo_1.is_done = True
113 todo_2.is_done = True
114 todo_2.is_done = False
115 todo_2.set_conditions(self.db_conn, [self.cond1.id_])
116 with self.assertRaises(BadFormatException):
117 todo_2.is_done = True
118 self.cond1.is_active = True
119 todo_2.is_done = True
121 def test_Todo_step_tree(self) -> None:
122 """Test self-configuration of TodoStepsNode tree for Day view."""
123 todo_1 = Todo(None, self.proc, False, self.date1)
124 todo_1.save(self.db_conn)
125 assert isinstance(todo_1.id_, int)
127 node_0 = TodoNode(todo_1, False, [])
128 self.assertEqual(todo_1.get_step_tree(set()).as_dict, node_0.as_dict)
129 # test non_emtpy seen_todo does something
131 self.assertEqual(todo_1.get_step_tree({todo_1.id_}).as_dict,
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()).as_dict, node_0.as_dict)
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()).as_dict, node_0.as_dict)
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()).as_dict, node_0.as_dict)
156 def test_Todo_create_with_children(self) -> None:
157 """Test parenthood guaranteeds of Todo.create_with_children."""
158 assert isinstance(self.proc.id_, int)
159 proc2 = Process(None)
160 proc2.save(self.db_conn)
161 assert isinstance(proc2.id_, int)
162 proc3 = Process(None)
163 proc3.save(self.db_conn)
164 assert isinstance(proc3.id_, int)
165 proc4 = Process(None)
166 proc4.save(self.db_conn)
167 assert isinstance(proc4.id_, int)
168 # make proc4 step of proc3
169 step = ProcessStep(None, proc3.id_, proc4.id_, None)
170 proc3.set_steps(self.db_conn, [step])
171 # give proc2 three steps; 2× proc1, 1× proc3
172 step1 = ProcessStep(None, proc2.id_, self.proc.id_, None)
173 step2 = ProcessStep(None, proc2.id_, self.proc.id_, None)
174 step3 = ProcessStep(None, proc2.id_, proc3.id_, None)
175 proc2.set_steps(self.db_conn, [step1, step2, step3])
176 # test mere creation does nothing
177 todo_ignore = Todo(None, proc2, False, self.date1)
178 todo_ignore.save(self.db_conn)
179 self.assertEqual(todo_ignore.children, [])
180 # test create_with_children on step-less does nothing
181 todo_1 = Todo.create_with_children(self.db_conn, self.proc.id_,
183 self.assertEqual(todo_1.children, [])
184 self.assertEqual(len(Todo.all(self.db_conn)), 2)
185 # test create_with_children adopts and creates, and down tree too
186 todo_2 = Todo.create_with_children(self.db_conn, proc2.id_, self.date1)
187 self.assertEqual(3, len(todo_2.children))
188 self.assertEqual(todo_1, todo_2.children[0])
189 self.assertEqual(self.proc, todo_2.children[2].process)
190 self.assertEqual(proc3, todo_2.children[1].process)
191 todo_3 = todo_2.children[1]
192 self.assertEqual(len(todo_3.children), 1)
193 self.assertEqual(todo_3.children[0].process, proc4)
195 def test_Todo_remove(self) -> None:
197 todo_1 = Todo(None, self.proc, False, self.date1)
198 todo_1.save(self.db_conn)
199 assert todo_1.id_ is not None
200 todo_0 = Todo(None, self.proc, False, self.date1)
201 todo_0.save(self.db_conn)
202 todo_0.add_child(todo_1)
203 todo_2 = Todo(None, self.proc, False, self.date1)
204 todo_2.save(self.db_conn)
205 todo_1.add_child(todo_2)
206 todo_1_id = todo_1.id_
207 todo_1.remove(self.db_conn)
208 with self.assertRaises(NotFoundException):
209 Todo.by_id(self.db_conn, todo_1_id)
210 self.assertEqual(todo_0.children, [])
211 self.assertEqual(todo_2.parents, [])
212 todo_2.comment = 'foo'
213 with self.assertRaises(HandledException):
214 todo_2.remove(self.db_conn)
217 with self.assertRaises(HandledException):
218 todo_2.remove(self.db_conn)
220 def test_Todo_autoremoval(self) -> None:
221 """"Test automatic removal for Todo.effort < 0."""
222 todo_1 = Todo(None, self.proc, False, self.date1)
223 todo_1.save(self.db_conn)
224 todo_1.comment = 'foo'
226 todo_1.save(self.db_conn)
227 assert todo_1.id_ is not None
228 Todo.by_id(self.db_conn, todo_1.id_)
230 todo_1_id = todo_1.id_
231 todo_1.save(self.db_conn)
232 with self.assertRaises(NotFoundException):
233 Todo.by_id(self.db_conn, todo_1_id)
236 class TestsWithServer(TestCaseWithServer):
237 """Tests against our HTTP server/handler (and database)."""
239 def setUp(self) -> None:
241 self._proc1_form_data = self.post_process(1)
243 def test_basic_fail_POST_todo(self) -> None:
244 """Test basic malformed/illegal POST /todo requests."""
245 # test we cannot just POST into non-existing Todo
246 self.check_post({}, '/todo', 404)
247 self.check_post({}, '/todo?id=FOO', 400)
248 self.check_post({}, '/todo?id=0', 404)
249 self.check_post({}, '/todo?id=1', 404)
250 # test malformed values on existing Todo
251 day_post = {'day_comment': '', 'new_todo': 1, 'make_type': 'full'}
252 self.check_post(day_post, '/day?date=2024-01-01&make_type=full')
253 for name in ['adopt', 'effort', 'make_full', 'make_empty',
254 'conditions', 'disables', 'blockers', 'enables']:
255 self.check_post({name: 'x'}, '/todo?id=1', 400, '/todo')
256 # test we cannot POST adoption of self or non-existing Todo
257 self.check_post({'adopt': 1}, '/todo?id=1', 400)
258 self.check_post({'adopt': 2}, '/todo?id=1', 404)
260 def test_do_POST_day(self) -> None:
261 """Test Todo posting of POST /day."""
263 proc = Process.by_id(self.db_conn, 1)
264 proc2 = Process.by_id(self.db_conn, 2)
265 form_data = {'day_comment': '', 'make_type': 'full'}
266 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
267 self.assertEqual(Todo.by_date(self.db_conn, '2024-01-01'), [])
268 proc = Process.by_id(self.db_conn, 1)
269 form_data['new_todo'] = str(proc.id_)
270 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
271 todos = Todo.by_date(self.db_conn, '2024-01-01')
272 self.assertEqual(1, len(todos))
274 self.assertEqual(todo1.id_, 1)
275 proc = Process.by_id(self.db_conn, 1)
276 self.assertEqual(todo1.process.id_, proc.id_)
277 self.assertEqual(todo1.is_done, False)
278 proc2 = Process.by_id(self.db_conn, 2)
279 form_data['new_todo'] = str(proc2.id_)
280 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
281 todos = Todo.by_date(self.db_conn, '2024-01-01')
283 self.assertEqual(todo1.id_, 2)
284 proc2 = Process.by_id(self.db_conn, 1)
285 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
286 self.assertEqual(todo1.id_, 1)
287 self.assertEqual(todo1.process.id_, proc2.id_)
288 self.assertEqual(todo1.is_done, False)
290 def test_do_POST_todo(self) -> None:
291 """Test POST /todo."""
292 def post_and_reload(form_data: dict[str, object], status: int = 302,
293 redir_url: str = '/todo?id=1') -> Todo:
294 self.check_post(form_data, '/todo?id=1', status, redir_url)
295 return Todo.by_date(self.db_conn, '2024-01-01')[0]
296 self.check_post({'day_comment': '', 'new_todo': 1,
297 'make_type': 'full'},
298 '/day?date=2024-01-01&make_type=full', 302)
299 # test posting naked entity
300 todo1 = post_and_reload({})
301 self.assertEqual(todo1.children, [])
302 self.assertEqual(todo1.parents, [])
303 self.assertEqual(todo1.is_done, False)
304 # test posting doneness
305 todo1 = post_and_reload({'done': ''})
306 self.assertEqual(todo1.is_done, True)
307 # test implicitly posting non-doneness
308 todo1 = post_and_reload({})
309 self.assertEqual(todo1.is_done, False)
310 # test posting second todo of same process
311 self.check_post({'day_comment': '', 'new_todo': 1,
312 'make_type': 'full'},
313 '/day?date=2024-01-01&make_type=full', 302)
314 # test todo 1 adopting todo 2
315 todo1 = post_and_reload({'adopt': 2})
316 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
317 self.assertEqual(todo1.children, [todo2])
318 self.assertEqual(todo1.parents, [])
319 self.assertEqual(todo2.children, [])
320 self.assertEqual(todo2.parents, [todo1])
321 # test todo1 cannot be set done with todo2 not done yet
322 todo1 = post_and_reload({'done': '', 'adopt': 2}, 400)
323 self.assertEqual(todo1.is_done, False)
324 # test todo1 un-adopting todo 2 by just not sending an adopt
325 todo1 = post_and_reload({}, 302)
326 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
327 self.assertEqual(todo1.children, [])
328 self.assertEqual(todo1.parents, [])
329 self.assertEqual(todo2.children, [])
330 self.assertEqual(todo2.parents, [])
331 # test todo1 deletion
332 todo1 = post_and_reload({'delete': ''}, 302, '/')
334 def test_do_POST_day_todo_adoption(self) -> None:
335 """Test Todos posted to Day view may adopt existing Todos."""
336 form_data = self.post_process(
337 2, self._proc1_form_data | {'new_top_step': 1})
338 form_data = {'day_comment': '', 'new_todo': 1, 'make_type': 'full'}
339 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
340 form_data['new_todo'] = 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.children, [])
345 self.assertEqual(todo1.parents, [todo2])
346 self.assertEqual(todo2.children, [todo1])
347 self.assertEqual(todo2.parents, [])
349 def test_do_POST_day_todo_multiple(self) -> None:
350 """Test multiple Todos can be posted to Day view."""
351 # form_data = self.post_process()
352 form_data = self.post_process(2)
353 form_data = {'day_comment': '', 'new_todo': [1, 2],
355 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
356 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
357 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
358 self.assertEqual(todo1.process.id_, 1)
359 self.assertEqual(todo2.process.id_, 2)
361 def test_do_POST_day_todo_multiple_inner_adoption(self) -> None:
362 """Test multiple Todos can be posted to Day view w. inner adoption."""
364 def key_order_func(t: Todo) -> int:
365 assert isinstance(t.process.id_, int)
368 def check_adoption(date: str, new_todos: list[int]) -> None:
369 form_data = {'day_comment': '', 'new_todo': new_todos,
371 self.check_post(form_data, f'/day?date={date}&make_type=full', 302)
372 day_todos = Todo.by_date(self.db_conn, date)
373 day_todos.sort(key=key_order_func)
376 self.assertEqual(todo1.children, [])
377 self.assertEqual(todo1.parents, [todo2])
378 self.assertEqual(todo2.children, [todo1])
379 self.assertEqual(todo2.parents, [])
381 def check_nesting_adoption(process_id: int, date: str,
382 new_top_steps: list[int]) -> None:
383 form_data = {'title': '', 'description': '', 'effort': 1,
385 form_data = self.post_process(1, form_data)
386 form_data['new_top_step'] = new_top_steps
387 form_data['step_of'] = []
388 form_data = self.post_process(process_id, form_data)
389 form_data = {'day_comment': '', 'new_todo': [process_id],
391 self.check_post(form_data, f'/day?date={date}&make_type=full', 302)
392 day_todos = Todo.by_date(self.db_conn, date)
393 day_todos.sort(key=key_order_func, reverse=True)
394 self.assertEqual(len(day_todos), 3)
395 todo1 = day_todos[0] # process of process_id
396 todo2 = day_todos[1] # process 2
397 todo3 = day_todos[2] # process 1
398 self.assertEqual(sorted(todo1.children), sorted([todo2, todo3]))
399 self.assertEqual(todo1.parents, [])
400 self.assertEqual(todo2.children, [todo3])
401 self.assertEqual(todo2.parents, [todo1])
402 self.assertEqual(todo3.children, [])
403 self.assertEqual(sorted(todo3.parents), sorted([todo2, todo1]))
405 self.post_process(2, self._proc1_form_data | {'new_top_step': 1})
406 check_adoption('2024-01-01', [1, 2])
407 check_adoption('2024-01-02', [2, 1])
408 check_nesting_adoption(3, '2024-01-03', [1, 2])
409 check_nesting_adoption(4, '2024-01-04', [2, 1])
411 def test_do_POST_day_todo_doneness(self) -> None:
412 """Test Todo doneness can be posted to Day view."""
413 form_data = {'day_comment': '', 'new_todo': [1], 'make_type': 'full'}
414 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
415 todo = Todo.by_date(self.db_conn, '2024-01-01')[0]
416 form_data = {'day_comment': '', 'todo_id': [1], 'make_type': 'full',
417 'comment': [''], 'done': [], 'effort': ['']}
418 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
419 todo = Todo.by_date(self.db_conn, '2024-01-01')[0]
420 self.assertEqual(todo.is_done, False)
421 form_data = {'day_comment': '', 'todo_id': [1], 'done': [1],
422 'make_type': 'full', 'comment': [''], 'effort': ['']}
423 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
424 todo = Todo.by_date(self.db_conn, '2024-01-01')[0]
425 self.assertEqual(todo.is_done, True)
427 def test_do_GET_todo(self) -> None:
428 """Test GET /todo response codes."""
429 form_data = {'day_comment': '', 'new_todo': 1, 'make_type': 'full'}
430 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
431 self.check_get('/todo', 404)
432 self.check_get('/todo?id=', 404)
433 self.check_get('/todo?id=foo', 400)
434 self.check_get('/todo?id=0', 404)
435 self.check_get('/todo?id=1', 200)
436 self.check_get('/todo?id=2', 404)