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