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_singularity(self) -> None:
206 """Test pointers made for single object keep pointing to it."""
207 self.check_singularity('is_done', True, self.proc, False, self.date1)
209 def test_Todo_remove(self) -> None:
211 todo_1 = Todo(None, self.proc, False, self.date1)
212 todo_1.save(self.db_conn)
213 todo_0 = Todo(None, self.proc, False, self.date1)
214 todo_0.save(self.db_conn)
215 todo_0.add_child(todo_1)
216 todo_2 = Todo(None, self.proc, False, self.date1)
217 todo_2.save(self.db_conn)
218 todo_1.add_child(todo_2)
219 todo_1_id = todo_1.id_
220 todo_1.remove(self.db_conn)
221 with self.assertRaises(NotFoundException):
222 Todo.by_id(self.db_conn, todo_1_id)
223 self.assertEqual(todo_0.children, [])
224 self.assertEqual(todo_2.parents, [])
225 todo_2.comment = 'foo'
226 with self.assertRaises(HandledException):
227 todo_2.remove(self.db_conn)
230 with self.assertRaises(HandledException):
231 todo_2.remove(self.db_conn)
233 def test_Todo_autoremoval(self) -> None:
234 """"Test automatic removal for Todo.effort < 0."""
235 todo_1 = Todo(None, self.proc, False, self.date1)
236 todo_1.save(self.db_conn)
237 todo_1.comment = 'foo'
239 todo_1.save(self.db_conn)
240 Todo.by_id(self.db_conn, todo_1.id_)
242 todo_1_id = todo_1.id_
243 todo_1.save(self.db_conn)
244 with self.assertRaises(NotFoundException):
245 Todo.by_id(self.db_conn, todo_1_id)
248 class TestsWithServer(TestCaseWithServer):
249 """Tests against our HTTP server/handler (and database)."""
251 def test_do_POST_day(self) -> None:
252 """Test Todo posting of POST /day."""
255 proc = Process.by_id(self.db_conn, 1)
256 proc2 = Process.by_id(self.db_conn, 2)
257 form_data = {'day_comment': '', 'make_type': 'full'}
258 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
259 self.assertEqual(Todo.by_date(self.db_conn, '2024-01-01'), [])
260 form_data['new_todo'] = str(proc.id_)
261 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
262 todos = Todo.by_date(self.db_conn, '2024-01-01')
263 self.assertEqual(1, len(todos))
265 self.assertEqual(todo1.id_, 1)
266 self.assertEqual(todo1.process.id_, proc.id_)
267 self.assertEqual(todo1.is_done, False)
268 form_data['new_todo'] = str(proc2.id_)
269 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
270 todos = Todo.by_date(self.db_conn, '2024-01-01')
272 self.assertEqual(todo1.id_, 2)
273 self.assertEqual(todo1.process.id_, proc2.id_)
274 self.assertEqual(todo1.is_done, False)
276 def test_do_POST_todo(self) -> None:
277 """Test POST /todo."""
278 def post_and_reload(form_data: dict[str, object], status: int = 302,
279 redir_url: str = '/todo?id=1') -> Todo:
280 self.check_post(form_data, '/todo?id=1', status, redir_url)
281 return Todo.by_date(self.db_conn, '2024-01-01')[0]
284 self.check_post({'day_comment': '', 'new_todo': 1,
285 'make_type': 'full'},
286 '/day?date=2024-01-01&make_type=full', 302)
287 # test posting to bad URLs
288 self.check_post({}, '/todo=', 404)
289 self.check_post({}, '/todo?id=', 400)
290 self.check_post({}, '/todo?id=FOO', 400)
291 self.check_post({}, '/todo?id=0', 404)
292 # test posting naked entity
293 todo1 = post_and_reload({})
294 self.assertEqual(todo1.children, [])
295 self.assertEqual(todo1.parents, [])
296 self.assertEqual(todo1.is_done, False)
297 # test posting doneness
298 todo1 = post_and_reload({'done': ''})
299 self.assertEqual(todo1.is_done, True)
300 # test implicitly posting non-doneness
301 todo1 = post_and_reload({})
302 self.assertEqual(todo1.is_done, False)
303 # test malformed adoptions
304 self.check_post({'adopt': 'foo'}, '/todo?id=1', 400)
305 self.check_post({'adopt': 1}, '/todo?id=1', 400)
306 self.check_post({'adopt': 2}, '/todo?id=1', 404)
307 # test posting second todo of same process
308 self.check_post({'day_comment': '', 'new_todo': 1,
309 'make_type': 'full'},
310 '/day?date=2024-01-01&make_type=full', 302)
311 # test todo 1 adopting todo 2
312 todo1 = post_and_reload({'adopt': 2})
313 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
314 self.assertEqual(todo1.children, [todo2])
315 self.assertEqual(todo1.parents, [])
316 self.assertEqual(todo2.children, [])
317 self.assertEqual(todo2.parents, [todo1])
318 # test todo1 cannot be set done with todo2 not done yet
319 todo1 = post_and_reload({'done': '', 'adopt': 2}, 400)
320 self.assertEqual(todo1.is_done, False)
321 # test todo1 un-adopting todo 2 by just not sending an adopt
322 todo1 = post_and_reload({}, 302)
323 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
324 self.assertEqual(todo1.children, [])
325 self.assertEqual(todo1.parents, [])
326 self.assertEqual(todo2.children, [])
327 self.assertEqual(todo2.parents, [])
328 # test todo1 deletion
329 todo1 = post_and_reload({'delete': ''}, 302, '/')
331 def test_do_POST_day_todo_adoption(self) -> None:
332 """Test Todos posted to Day view may adopt existing Todos."""
333 form_data = self.post_process()
334 form_data = self.post_process(2, form_data | {'new_top_step': 1})
335 form_data = {'day_comment': '', 'new_todo': 1, 'make_type': 'full'}
336 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
337 form_data['new_todo'] = 2
338 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 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.children, [])
342 self.assertEqual(todo1.parents, [todo2])
343 self.assertEqual(todo2.children, [todo1])
344 self.assertEqual(todo2.parents, [])
346 def test_do_POST_day_todo_multiple(self) -> None:
347 """Test multiple Todos can be posted to Day view."""
348 form_data = self.post_process()
349 form_data = self.post_process(2)
350 form_data = {'day_comment': '', 'new_todo': [1, 2],
352 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
353 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
354 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
355 self.assertEqual(todo1.process.id_, 1)
356 self.assertEqual(todo2.process.id_, 2)
358 def test_do_POST_day_todo_multiple_inner_adoption(self) -> None:
359 """Test multiple Todos can be posted to Day view w. inner adoption."""
361 def key_order_func(t: Todo) -> int:
362 assert isinstance(t.process.id_, int)
365 def check_adoption(date: str, new_todos: list[int]) -> None:
366 form_data = {'day_comment': '', 'new_todo': new_todos,
368 self.check_post(form_data, f'/day?date={date}&make_type=full', 302)
369 day_todos = Todo.by_date(self.db_conn, date)
370 day_todos.sort(key=key_order_func)
373 self.assertEqual(todo1.children, [])
374 self.assertEqual(todo1.parents, [todo2])
375 self.assertEqual(todo2.children, [todo1])
376 self.assertEqual(todo2.parents, [])
378 def check_nesting_adoption(process_id: int, date: str,
379 new_top_steps: list[int]) -> None:
380 form_data = {'title': '', 'description': '', 'effort': 1,
382 form_data = self.post_process(1, form_data)
383 form_data['new_top_step'] = new_top_steps
384 form_data['step_of'] = []
385 form_data = self.post_process(process_id, form_data)
386 form_data = {'day_comment': '', 'new_todo': [process_id],
388 self.check_post(form_data, f'/day?date={date}&make_type=full', 302)
389 day_todos = Todo.by_date(self.db_conn, date)
390 day_todos.sort(key=key_order_func, reverse=True)
391 self.assertEqual(len(day_todos), 3)
392 todo1 = day_todos[0] # process of process_id
393 todo2 = day_todos[1] # process 2
394 todo3 = day_todos[2] # process 1
395 self.assertEqual(sorted(todo1.children), sorted([todo2, todo3]))
396 self.assertEqual(todo1.parents, [])
397 self.assertEqual(todo2.children, [todo3])
398 self.assertEqual(todo2.parents, [todo1])
399 self.assertEqual(todo3.children, [])
400 self.assertEqual(sorted(todo3.parents), sorted([todo2, todo1]))
402 form_data = self.post_process()
403 form_data = self.post_process(2, form_data | {'new_top_step': 1})
404 check_adoption('2024-01-01', [1, 2])
405 check_adoption('2024-01-02', [2, 1])
406 check_nesting_adoption(3, '2024-01-03', [1, 2])
407 check_nesting_adoption(4, '2024-01-04', [2, 1])
409 def test_do_POST_day_todo_doneness(self) -> None:
410 """Test Todo doneness can be posted to Day view."""
412 form_data = {'day_comment': '', 'new_todo': [1], 'make_type': 'full'}
413 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
414 todo = Todo.by_date(self.db_conn, '2024-01-01')[0]
415 form_data = {'day_comment': '', 'todo_id': [1], 'make_type': 'full'}
416 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
417 self.assertEqual(todo.is_done, False)
418 form_data = {'day_comment': '', 'todo_id': [1], 'done': [1],
420 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
421 self.assertEqual(todo.is_done, True)
423 def test_do_GET_todo(self) -> None:
424 """Test GET /todo response codes."""
426 form_data = {'day_comment': '', 'new_todo': 1, 'make_type': 'full'}
427 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
428 self.check_get('/todo', 400)
429 self.check_get('/todo?id=', 400)
430 self.check_get('/todo?id=foo', 400)
431 self.check_get('/todo?id=0', 404)
432 self.check_get('/todo?id=1', 200)