1 """Test Todos module."""
2 from tests.utils import TestCaseWithDB, TestCaseWithServer
3 from plomtask.todos import Todo, TodoNode
4 from plomtask.processes import Process
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 proc3.set_steps(self.db_conn, [(None, proc4.id_, None)])
171 # give proc2 three steps; 2× proc1, 1× proc3
172 proc2.set_steps(self.db_conn, [(None, self.proc.id_, None),
173 (None, self.proc.id_, None),
174 (None, proc3.id_, None)])
175 # test mere creation does nothing
176 todo_ignore = Todo(None, proc2, False, self.date1)
177 todo_ignore.save(self.db_conn)
178 self.assertEqual(todo_ignore.children, [])
179 # test create_with_children on step-less does nothing
180 todo_1 = Todo.create_with_children(self.db_conn, self.proc.id_,
182 self.assertEqual(todo_1.children, [])
183 self.assertEqual(len(Todo.all(self.db_conn)), 2)
184 # test create_with_children adopts and creates, and down tree too
185 todo_2 = Todo.create_with_children(self.db_conn, proc2.id_, self.date1)
186 self.assertEqual(3, len(todo_2.children))
187 self.assertEqual(todo_1, todo_2.children[0])
188 self.assertEqual(self.proc, todo_2.children[1].process)
189 self.assertEqual(proc3, todo_2.children[2].process)
190 todo_3 = todo_2.children[2]
191 self.assertEqual(len(todo_3.children), 1)
192 self.assertEqual(todo_3.children[0].process, proc4)
194 def test_Todo_singularity(self) -> None:
195 """Test pointers made for single object keep pointing to it."""
196 self.check_singularity('is_done', True, self.proc, False, self.date1)
198 def test_Todo_remove(self) -> None:
200 todo_1 = Todo(None, self.proc, False, self.date1)
201 todo_1.save(self.db_conn)
202 todo_0 = Todo(None, self.proc, False, self.date1)
203 todo_0.save(self.db_conn)
204 todo_0.add_child(todo_1)
205 todo_2 = Todo(None, self.proc, False, self.date1)
206 todo_2.save(self.db_conn)
207 todo_1.add_child(todo_2)
208 todo_1.remove(self.db_conn)
209 with self.assertRaises(NotFoundException):
210 Todo.by_id(self.db_conn, todo_1.id_)
211 self.assertEqual(todo_0.children, [])
212 self.assertEqual(todo_2.parents, [])
213 todo_2.comment = 'foo'
214 with self.assertRaises(HandledException):
215 todo_2.remove(self.db_conn)
218 with self.assertRaises(HandledException):
219 todo_2.remove(self.db_conn)
221 def test_Todo_autoremoval(self) -> None:
222 """"Test automatic removal for Todo.effort < 0."""
223 todo_1 = Todo(None, self.proc, False, self.date1)
224 todo_1.save(self.db_conn)
225 todo_1.comment = 'foo'
227 todo_1.save(self.db_conn)
228 Todo.by_id(self.db_conn, todo_1.id_)
230 todo_1.save(self.db_conn)
231 with self.assertRaises(NotFoundException):
232 Todo.by_id(self.db_conn, todo_1.id_)
235 class TestsWithServer(TestCaseWithServer):
236 """Tests against our HTTP server/handler (and database)."""
238 def test_do_POST_day(self) -> None:
239 """Test Todo posting of POST /day."""
242 proc = Process.by_id(self.db_conn, 1)
243 proc2 = Process.by_id(self.db_conn, 2)
244 form_data = {'day_comment': ''}
245 self.check_post(form_data, '/day?date=2024-01-01', 302)
246 self.assertEqual(Todo.by_date(self.db_conn, '2024-01-01'), [])
247 form_data['new_todo'] = str(proc.id_)
248 self.check_post(form_data, '/day?date=2024-01-01', 302)
249 todos = Todo.by_date(self.db_conn, '2024-01-01')
250 self.assertEqual(1, len(todos))
252 self.assertEqual(todo1.id_, 1)
253 self.assertEqual(todo1.process.id_, proc.id_)
254 self.assertEqual(todo1.is_done, False)
255 form_data['new_todo'] = str(proc2.id_)
256 self.check_post(form_data, '/day?date=2024-01-01', 302)
257 todos = Todo.by_date(self.db_conn, '2024-01-01')
259 self.assertEqual(todo1.id_, 2)
260 self.assertEqual(todo1.process.id_, proc2.id_)
261 self.assertEqual(todo1.is_done, False)
263 def test_do_POST_todo(self) -> None:
264 """Test POST /todo."""
265 def post_and_reload(form_data: dict[str, object], status: int = 302,
266 redir_url: str = '/todo?id=1') -> Todo:
267 self.check_post(form_data, '/todo?id=1', status, redir_url)
268 return Todo.by_date(self.db_conn, '2024-01-01')[0]
271 self.check_post({'day_comment': '', 'new_todo': 1},
272 '/day?date=2024-01-01', 302)
273 # test posting to bad URLs
274 self.check_post({}, '/todo=', 404)
275 self.check_post({}, '/todo?id=', 400)
276 self.check_post({}, '/todo?id=FOO', 400)
277 self.check_post({}, '/todo?id=0', 404)
278 # test posting naked entity
279 todo1 = post_and_reload({})
280 self.assertEqual(todo1.children, [])
281 self.assertEqual(todo1.parents, [])
282 self.assertEqual(todo1.is_done, False)
283 # test posting doneness
284 todo1 = post_and_reload({'done': ''})
285 self.assertEqual(todo1.is_done, True)
286 # test implicitly posting non-doneness
287 todo1 = post_and_reload({})
288 self.assertEqual(todo1.is_done, False)
289 # test malformed adoptions
290 self.check_post({'adopt': 'foo'}, '/todo?id=1', 400)
291 self.check_post({'adopt': 1}, '/todo?id=1', 400)
292 self.check_post({'adopt': 2}, '/todo?id=1', 404)
293 # test posting second todo of same process
294 self.check_post({'day_comment': '', 'new_todo': 1},
295 '/day?date=2024-01-01', 302)
296 # test todo 1 adopting todo 2
297 todo1 = post_and_reload({'adopt': 2})
298 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
299 self.assertEqual(todo1.children, [todo2])
300 self.assertEqual(todo1.parents, [])
301 self.assertEqual(todo2.children, [])
302 self.assertEqual(todo2.parents, [todo1])
303 # test todo1 cannot be set done with todo2 not done yet
304 todo1 = post_and_reload({'done': '', 'adopt': 2}, 400)
305 self.assertEqual(todo1.is_done, False)
306 # test todo1 un-adopting todo 2 by just not sending an adopt
307 todo1 = post_and_reload({}, 302)
308 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
309 self.assertEqual(todo1.children, [])
310 self.assertEqual(todo1.parents, [])
311 self.assertEqual(todo2.children, [])
312 self.assertEqual(todo2.parents, [])
313 # test todo1 deletion
314 todo1 = post_and_reload({'delete': ''}, 302, '/')
316 def test_do_POST_day_todo_adoption(self) -> None:
317 """Test Todos posted to Day view may adopt existing Todos."""
318 form_data = self.post_process()
319 form_data = self.post_process(2, form_data | {'new_top_step': 1})
320 form_data = {'day_comment': '', 'new_todo': 1}
321 self.check_post(form_data, '/day?date=2024-01-01', 302)
322 form_data['new_todo'] = 2
323 self.check_post(form_data, '/day?date=2024-01-01', 302)
324 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
325 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
326 self.assertEqual(todo1.children, [])
327 self.assertEqual(todo1.parents, [todo2])
328 self.assertEqual(todo2.children, [todo1])
329 self.assertEqual(todo2.parents, [])
331 def test_do_POST_day_todo_multiple(self) -> None:
332 """Test multiple Todos can be posted to Day view."""
333 form_data = self.post_process()
334 form_data = self.post_process(2)
335 form_data = {'day_comment': '', 'new_todo': [1, 2]}
336 self.check_post(form_data, '/day?date=2024-01-01', 302)
337 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
338 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
339 self.assertEqual(todo1.process.id_, 1)
340 self.assertEqual(todo2.process.id_, 2)
342 def test_do_POST_day_todo_multiple_inner_adoption(self) -> None:
343 """Test multiple Todos can be posted to Day view w. inner adoption."""
345 def key_order_func(t: Todo) -> int:
346 assert isinstance(t.process.id_, int)
349 def check_adoption(date: str, new_todos: list[int]) -> None:
350 form_data = {'day_comment': '', 'new_todo': new_todos}
351 self.check_post(form_data, f'/day?date={date}', 302)
352 day_todos = Todo.by_date(self.db_conn, date)
353 day_todos.sort(key=key_order_func)
356 self.assertEqual(todo1.children, [])
357 self.assertEqual(todo1.parents, [todo2])
358 self.assertEqual(todo2.children, [todo1])
359 self.assertEqual(todo2.parents, [])
361 def check_nesting_adoption(process_id: int, date: str,
362 new_top_steps: list[int]) -> None:
363 form_data = self.post_process()
364 form_data = self.post_process(process_id,
366 {'new_top_step': new_top_steps})
367 form_data = {'day_comment': '', 'new_todo': [process_id]}
368 self.check_post(form_data, f'/day?date={date}', 302)
369 day_todos = Todo.by_date(self.db_conn, date)
370 day_todos.sort(key=key_order_func, reverse=True)
371 self.assertEqual(len(day_todos), 3)
372 todo1 = day_todos[0] # process of process_id
373 todo2 = day_todos[1] # process 2
374 todo3 = day_todos[2] # process 1
375 self.assertEqual(sorted(todo1.children), sorted([todo2, todo3]))
376 self.assertEqual(todo1.parents, [])
377 self.assertEqual(todo2.children, [todo3])
378 self.assertEqual(todo2.parents, [todo1])
379 self.assertEqual(todo3.children, [])
380 self.assertEqual(sorted(todo3.parents), sorted([todo2, todo1]))
382 form_data = self.post_process()
383 form_data = self.post_process(2, form_data | {'new_top_step': 1})
384 check_adoption('2024-01-01', [1, 2])
385 check_adoption('2024-01-02', [2, 1])
386 check_nesting_adoption(3, '2024-01-03', [1, 2])
387 check_nesting_adoption(4, '2024-01-04', [2, 1])
389 def test_do_POST_day_todo_doneness(self) -> None:
390 """Test Todo doneness can be posted to Day view."""
391 form_data = self.post_process()
392 form_data = {'day_comment': '', 'new_todo': [1]}
393 self.check_post(form_data, '/day?date=2024-01-01', 302)
394 todo = Todo.by_date(self.db_conn, '2024-01-01')[0]
395 form_data = {'day_comment': '', 'todo_id': [1]}
396 self.check_post(form_data, '/day?date=2024-01-01', 302)
397 self.assertEqual(todo.is_done, False)
398 form_data = {'day_comment': '', 'todo_id': [1], 'done': [1]}
399 self.check_post(form_data, '/day?date=2024-01-01', 302)
400 self.assertEqual(todo.is_done, True)
402 def test_do_GET_todo(self) -> None:
403 """Test GET /todo response codes."""
405 form_data = {'day_comment': '', 'new_todo': 1}
406 self.check_post(form_data, '/day?date=2024-01-01', 302)
407 self.check_get('/todo', 400)
408 self.check_get('/todo?id=', 400)
409 self.check_get('/todo?id=foo', 400)
410 self.check_get('/todo?id=0', 404)
411 self.check_get('/todo?id=1', 200)