1 """Test Todos module."""
2 from tests.utils import TestCaseWithDB, TestCaseWithServer
3 from plomtask.todos import Todo, TodoStepsNode
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 def setUp(self) -> None:
15 self.date1 = '2024-01-01'
16 self.date2 = '2024-01-02'
17 self.proc = Process(None)
18 self.proc.save(self.db_conn)
19 self.cond1 = Condition(None)
20 self.cond1.save(self.db_conn)
21 self.cond2 = Condition(None)
22 self.cond2.save(self.db_conn)
24 def test_Todo_by_id(self) -> None:
25 """Test creation and findability of Todos."""
26 process_unsaved = Process(None)
27 todo = Todo(None, process_unsaved, False, self.date1)
28 with self.assertRaises(NotFoundException):
29 todo.save(self.db_conn)
30 process_unsaved.save(self.db_conn)
31 todo.save(self.db_conn)
32 self.assertEqual(Todo.by_id(self.db_conn, 1), todo)
33 with self.assertRaises(NotFoundException):
34 Todo.by_id(self.db_conn, 0)
35 with self.assertRaises(NotFoundException):
36 Todo.by_id(self.db_conn, 2)
38 def test_Todo_by_date(self) -> None:
39 """Test findability of Todos by date."""
40 t1 = Todo(None, self.proc, False, self.date1)
42 t2 = Todo(None, self.proc, False, self.date1)
44 self.assertEqual(Todo.by_date(self.db_conn, self.date1), [t1, t2])
45 self.assertEqual(Todo.by_date(self.db_conn, self.date2), [])
46 self.assertEqual(Todo.by_date(self.db_conn, 'foo'), [])
48 def test_Todo_from_process(self) -> None:
49 """Test spawning of Todo attributes from Process."""
50 assert isinstance(self.cond1.id_, int)
51 assert isinstance(self.cond2.id_, int)
52 self.proc.set_conditions(self.db_conn, [self.cond1.id_])
53 todo = Todo(None, self.proc, False, self.date1)
54 self.assertEqual(todo.conditions, [self.cond1])
55 todo.set_conditions(self.db_conn, [self.cond2.id_])
56 self.assertEqual(todo.conditions, [self.cond2])
57 self.assertEqual(self.proc.conditions, [self.cond1])
58 self.proc.set_enables(self.db_conn, [self.cond1.id_])
59 todo = Todo(None, self.proc, False, self.date1)
60 self.assertEqual(todo.enables, [self.cond1])
61 todo.set_enables(self.db_conn, [self.cond2.id_])
62 self.assertEqual(todo.enables, [self.cond2])
63 self.assertEqual(self.proc.enables, [self.cond1])
64 self.proc.set_disables(self.db_conn, [self.cond1.id_])
65 todo = Todo(None, self.proc, False, self.date1)
66 self.assertEqual(todo.disables, [self.cond1])
67 todo.set_disables(self.db_conn, [self.cond2.id_])
68 self.assertEqual(todo.disables, [self.cond2])
69 self.assertEqual(self.proc.disables, [self.cond1])
71 def test_Todo_on_conditions(self) -> None:
72 """Test effect of Todos on Conditions."""
73 assert isinstance(self.cond1.id_, int)
74 assert isinstance(self.cond2.id_, int)
75 todo = Todo(None, self.proc, False, self.date1)
76 todo.save(self.db_conn)
77 todo.set_enables(self.db_conn, [self.cond1.id_])
78 todo.set_disables(self.db_conn, [self.cond2.id_])
80 self.assertEqual(self.cond1.is_active, True)
81 self.assertEqual(self.cond2.is_active, False)
83 self.assertEqual(self.cond1.is_active, True)
84 self.assertEqual(self.cond2.is_active, False)
86 def test_Todo_enablers_disablers(self) -> None:
87 """Test Todo.enablers_for_at/disablers_for_at."""
88 assert isinstance(self.cond1.id_, int)
89 assert isinstance(self.cond2.id_, int)
90 todo1 = Todo(None, self.proc, False, self.date1)
91 todo1.save(self.db_conn)
92 todo1.set_enables(self.db_conn, [self.cond1.id_])
93 todo1.set_disables(self.db_conn, [self.cond2.id_])
94 todo1.save(self.db_conn)
95 todo2 = Todo(None, self.proc, False, self.date1)
96 todo2.save(self.db_conn)
97 todo2.set_enables(self.db_conn, [self.cond2.id_])
98 todo2.save(self.db_conn)
99 todo3 = Todo(None, self.proc, False, self.date2)
100 todo3.save(self.db_conn)
101 todo3.set_enables(self.db_conn, [self.cond2.id_])
102 todo3.save(self.db_conn)
103 enablers = Todo.enablers_for_at(self.db_conn, self.cond1, self.date1)
104 self.assertEqual(enablers, [todo1])
105 enablers = Todo.enablers_for_at(self.db_conn, self.cond1, self.date2)
106 self.assertEqual(enablers, [])
107 disablers = Todo.disablers_for_at(self.db_conn, self.cond1, self.date1)
108 self.assertEqual(disablers, [])
109 disablers = Todo.disablers_for_at(self.db_conn, self.cond1, self.date2)
110 self.assertEqual(disablers, [])
111 enablers = Todo.enablers_for_at(self.db_conn, self.cond2, self.date1)
112 self.assertEqual(enablers, [todo2])
113 enablers = Todo.enablers_for_at(self.db_conn, self.cond2, self.date2)
114 self.assertEqual(enablers, [todo3])
115 disablers = Todo.disablers_for_at(self.db_conn, self.cond2, self.date1)
116 self.assertEqual(disablers, [todo1])
117 disablers = Todo.disablers_for_at(self.db_conn, self.cond2, self.date2)
118 self.assertEqual(disablers, [])
120 def test_Todo_children(self) -> None:
121 """Test Todo.children relations."""
122 todo_1 = Todo(None, self.proc, False, self.date1)
123 todo_2 = Todo(None, self.proc, False, self.date1)
124 todo_2.save(self.db_conn)
125 with self.assertRaises(HandledException):
126 todo_1.add_child(todo_2)
127 todo_1.save(self.db_conn)
128 todo_3 = Todo(None, self.proc, False, self.date1)
129 with self.assertRaises(HandledException):
130 todo_1.add_child(todo_3)
131 todo_3.save(self.db_conn)
132 todo_1.add_child(todo_3)
133 todo_1.save(self.db_conn)
134 assert isinstance(todo_1.id_, int)
135 todo_retrieved = Todo.by_id(self.db_conn, todo_1.id_)
136 self.assertEqual(todo_retrieved.children, [todo_3])
137 with self.assertRaises(BadFormatException):
138 todo_3.add_child(todo_1)
140 def test_Todo_conditioning(self) -> None:
141 """Test Todo.doability conditions."""
142 assert isinstance(self.cond1.id_, int)
143 todo_1 = Todo(None, self.proc, False, self.date1)
144 todo_1.save(self.db_conn)
145 todo_2 = Todo(None, self.proc, False, self.date1)
146 todo_2.save(self.db_conn)
147 todo_2.add_child(todo_1)
148 with self.assertRaises(BadFormatException):
149 todo_2.is_done = True
150 todo_1.is_done = True
151 todo_2.is_done = True
152 todo_2.is_done = False
153 todo_2.set_conditions(self.db_conn, [self.cond1.id_])
154 with self.assertRaises(BadFormatException):
155 todo_2.is_done = True
156 self.cond1.is_active = True
157 todo_2.is_done = True
159 def test_Todo_step_tree(self) -> None:
160 """Test self-configuration of TodoStepsNode tree for Day view."""
161 assert isinstance(self.cond1.id_, int)
162 assert isinstance(self.cond2.id_, int)
163 todo_1 = Todo(None, self.proc, False, self.date1)
164 todo_1.save(self.db_conn)
165 assert isinstance(todo_1.id_, int)
167 node_0 = TodoStepsNode(todo_1, True, [], False, False)
168 self.assertEqual(todo_1.get_step_tree(set(), set()), node_0)
169 # test non_emtpy seen_todo does something
171 self.assertEqual(todo_1.get_step_tree({todo_1.id_}, set()), node_0)
172 # test child shows up
173 todo_2 = Todo(None, self.proc, False, self.date1)
174 todo_2.save(self.db_conn)
175 assert isinstance(todo_2.id_, int)
176 todo_1.add_child(todo_2)
177 node_2 = TodoStepsNode(todo_2, True, [], False, False)
178 node_0.children = [node_2]
180 self.assertEqual(todo_1.get_step_tree(set(), set()), node_0)
181 # test child shows up with child
182 todo_3 = Todo(None, self.proc, False, self.date1)
183 todo_3.save(self.db_conn)
184 assert isinstance(todo_3.id_, int)
185 todo_2.add_child(todo_3)
186 node_3 = TodoStepsNode(todo_3, True, [], False, False)
187 node_2.children = [node_3]
188 self.assertEqual(todo_1.get_step_tree(set(), set()), node_0)
189 # test same todo can be child-ed multiple times at different locations
190 todo_1.add_child(todo_3)
191 node_4 = TodoStepsNode(todo_3, True, [], True, False)
192 node_0.children += [node_4]
193 self.assertEqual(todo_1.get_step_tree(set(), set()), node_0)
194 # test condition shows up
195 todo_1.set_conditions(self.db_conn, [self.cond1.id_])
196 node_5 = TodoStepsNode(self.cond1, False, [], False, False)
197 node_0.children += [node_5]
198 self.assertEqual(todo_1.get_step_tree(set(), set()), node_0)
199 # test second condition shows up
200 todo_2.set_conditions(self.db_conn, [self.cond2.id_])
201 node_6 = TodoStepsNode(self.cond2, False, [], False, False)
202 node_2.children += [node_6]
203 self.assertEqual(todo_1.get_step_tree(set(), set()), node_0)
204 # test second condition is not hidden if fulfilled by non-sibling
205 todo_1.set_enables(self.db_conn, [self.cond2.id_])
206 self.assertEqual(todo_1.get_step_tree(set(), set()), node_0)
207 # test second condition is hidden if fulfilled by sibling
208 todo_3.set_enables(self.db_conn, [self.cond2.id_])
209 node_2.children.remove(node_6)
210 self.assertEqual(todo_1.get_step_tree(set(), set()), node_0)
212 def test_Todo_unsatisfied_steps(self) -> None:
213 """Test options of satisfying unfulfilled Process.explicit_steps."""
214 assert isinstance(self.proc.id_, int)
215 todo_1 = Todo(None, self.proc, False, self.date1)
216 todo_1.save(self.db_conn)
217 proc2 = Process(None)
218 proc2.save(self.db_conn)
219 assert isinstance(proc2.id_, int)
220 proc3 = Process(None)
221 proc3.save(self.db_conn)
222 assert isinstance(proc3.id_, int)
223 proc4 = Process(None)
224 proc4.save(self.db_conn)
225 assert isinstance(proc4.id_, int)
226 proc3.set_steps(self.db_conn, [(None, proc4.id_, None)])
227 proc2.set_steps(self.db_conn, [(None, self.proc.id_, None),
228 (None, self.proc.id_, None),
229 (None, proc3.id_, None)])
230 todo_2 = Todo(None, proc2, False, self.date1)
231 todo_2.save(self.db_conn)
232 # test empty adoption does nothing
233 todo_2.adopt_from([])
234 self.assertEqual(todo_2.children, [])
235 # test basic adoption
236 todo_2.adopt_from([todo_1])
237 self.assertEqual(todo_2.children, [todo_1])
238 self.assertEqual(todo_1.parents, [todo_2])
239 # test making missing children
240 todo_2.make_missing_children(self.db_conn)
241 todo_3 = Todo.by_id(self.db_conn, 3)
242 todo_4 = Todo.by_id(self.db_conn, 4)
243 self.assertEqual(todo_2.children, [todo_1, todo_3, todo_4])
244 self.assertEqual(todo_3.process, self.proc)
245 self.assertEqual(todo_3.parents, [todo_2])
246 self.assertEqual(todo_3.children, [])
247 self.assertEqual(todo_4.process, proc3)
248 self.assertEqual(todo_4.parents, [todo_2])
249 # test .make_missing_children doesn't further than top-level
250 self.assertEqual(todo_4.children, [])
251 # test .make_missing_children lower down the tree
252 todo_4.make_missing_children(self.db_conn)
253 todo_5 = Todo.by_id(self.db_conn, 5)
254 self.assertEqual(todo_5.process, proc4)
255 self.assertEqual(todo_4.children, [todo_5])
256 self.assertEqual(todo_5.parents, [todo_4])
258 def test_Todo_singularity(self) -> None:
259 """Test pointers made for single object keep pointing to it."""
260 todo = Todo(None, self.proc, False, self.date1)
261 todo.save(self.db_conn)
262 retrieved_todo = Todo.by_id(self.db_conn, 1)
264 self.assertEqual(retrieved_todo.is_done, True)
265 retrieved_todo = Todo.by_date(self.db_conn, self.date1)[0]
266 retrieved_todo.is_done = False
267 self.assertEqual(todo.is_done, False)
269 def test_Todo_remove(self) -> None:
271 todo_1 = Todo(None, self.proc, False, self.date1)
272 todo_1.save(self.db_conn)
273 todo_0 = Todo(None, self.proc, False, self.date1)
274 todo_0.save(self.db_conn)
275 todo_0.add_child(todo_1)
276 todo_2 = Todo(None, self.proc, False, self.date1)
277 todo_2.save(self.db_conn)
278 todo_1.add_child(todo_2)
279 todo_1.remove(self.db_conn)
280 with self.assertRaises(NotFoundException):
281 Todo.by_id(self.db_conn, todo_1.id_)
282 self.assertEqual(todo_0.children, [])
283 self.assertEqual(todo_2.parents, [])
286 class TestsWithServer(TestCaseWithServer):
287 """Tests against our HTTP server/handler (and database)."""
289 def test_do_POST_day(self) -> None:
290 """Test Todo posting of POST /day."""
291 form_data = {'title': '', 'description': '', 'effort': 1}
292 self.check_post(form_data, '/process?id=', 302, '/process?id=1')
293 self.check_post(form_data, '/process?id=', 302, '/process?id=2')
294 proc = Process.by_id(self.db_conn, 1)
295 proc2 = Process.by_id(self.db_conn, 2)
296 form_data = {'comment': ''}
297 self.check_post(form_data, '/day?date=2024-01-01', 302)
298 self.assertEqual(Todo.by_date(self.db_conn, '2024-01-01'), [])
299 form_data['new_todo'] = str(proc.id_)
300 self.check_post(form_data, '/day?date=2024-01-01', 302)
301 todos = Todo.by_date(self.db_conn, '2024-01-01')
302 self.assertEqual(1, len(todos))
304 self.assertEqual(todo1.id_, 1)
305 self.assertEqual(todo1.process.id_, proc.id_)
306 self.assertEqual(todo1.is_done, False)
307 form_data['new_todo'] = str(proc2.id_)
308 self.check_post(form_data, '/day?date=2024-01-01', 302)
309 todos = Todo.by_date(self.db_conn, '2024-01-01')
311 self.assertEqual(todo1.id_, 2)
312 self.assertEqual(todo1.process.id_, proc2.id_)
313 self.assertEqual(todo1.is_done, False)
315 def test_do_POST_todo(self) -> None:
316 """Test POST /todo."""
317 def post_and_reload(form_data: dict[str, object], status: int = 302,
318 redir_url: str = '/todo?id=1') -> Todo:
319 self.check_post(form_data, '/todo?id=1', status, redir_url)
320 return Todo.by_date(self.db_conn, '2024-01-01')[0]
322 form_data = {'title': '', 'description': '', 'effort': 1}
323 self.check_post(form_data, '/process', 302, '/process?id=1')
324 form_data = {'comment': '', 'new_todo': 1}
325 self.check_post(form_data, '/day?date=2024-01-01', 302)
326 # test posting to bad URLs
328 self.check_post(form_data, '/todo=', 404)
329 self.check_post(form_data, '/todo?id=', 400)
330 self.check_post(form_data, '/todo?id=FOO', 400)
331 self.check_post(form_data, '/todo?id=0', 404)
332 # test posting naked entity
333 todo1 = post_and_reload(form_data)
334 self.assertEqual(todo1.children, [])
335 self.assertEqual(todo1.parents, [])
336 self.assertEqual(todo1.is_done, False)
337 # test posting doneness
338 form_data = {'done': ''}
339 todo1 = post_and_reload(form_data)
340 self.assertEqual(todo1.is_done, True)
341 # test implicitly posting non-doneness
343 todo1 = post_and_reload(form_data)
344 self.assertEqual(todo1.is_done, False)
345 # test malformed adoptions
346 form_data = {'adopt': 'foo'}
347 self.check_post(form_data, '/todo?id=1', 400)
348 form_data = {'adopt': 1}
349 self.check_post(form_data, '/todo?id=1', 400)
350 form_data = {'adopt': 2}
351 self.check_post(form_data, '/todo?id=1', 404)
352 # test posting second todo of same process
353 form_data = {'comment': '', 'new_todo': 1}
354 self.check_post(form_data, '/day?date=2024-01-01', 302)
355 # test todo 1 adopting todo 2
356 form_data = {'adopt': 2}
357 todo1 = post_and_reload(form_data)
358 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
359 self.assertEqual(todo1.children, [todo2])
360 self.assertEqual(todo1.parents, [])
361 self.assertEqual(todo2.children, [])
362 self.assertEqual(todo2.parents, [todo1])
363 # test todo1 cannot be set done with todo2 not done yet
364 form_data = {'done': '', 'adopt': 2}
365 todo1 = post_and_reload(form_data, 400)
366 self.assertEqual(todo1.is_done, False)
367 # test todo1 un-adopting todo 2 by just not sending an adopt
369 todo1 = post_and_reload(form_data, 302)
370 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
371 self.assertEqual(todo1.children, [])
372 self.assertEqual(todo1.parents, [])
373 self.assertEqual(todo2.children, [])
374 self.assertEqual(todo2.parents, [])
375 # test todo1 deletion
376 form_data = {'delete': ''}
377 todo1 = post_and_reload(form_data, 302, '/')
379 def test_do_POST_day_todo_adoption(self) -> None:
380 """Test Todos posted to Day view may adopt existing Todos."""
381 form_data = {'title': '', 'description': '', 'effort': 1}
382 self.check_post(form_data, '/process', 302, '/process?id=1')
383 form_data['new_top_step'] = 1
384 self.check_post(form_data, '/process', 302, '/process?id=2')
385 form_data = {'comment': '', 'new_todo': 1}
386 self.check_post(form_data, '/day?date=2024-01-01', 302)
387 form_data = {'comment': '', 'new_todo': 2}
388 self.check_post(form_data, '/day?date=2024-01-01', 302)
389 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
390 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
391 self.assertEqual(todo1.children, [])
392 self.assertEqual(todo1.parents, [todo2])
393 self.assertEqual(todo2.children, [todo1])
394 self.assertEqual(todo2.parents, [])
396 def test_do_GET_todo(self) -> None:
397 """Test GET /todo response codes."""
398 form_data = {'title': '', 'description': '', 'effort': 1}
399 self.check_post(form_data, '/process?id=', 302, '/process?id=1')
400 form_data = {'comment': '', 'new_todo': 1}
401 self.check_post(form_data, '/day?date=2024-01-01', 302)
402 self.check_get('/todo', 400)
403 self.check_get('/todo?id=', 400)
404 self.check_get('/todo?id=foo', 400)
405 self.check_get('/todo?id=0', 404)
406 self.check_get('/todo?id=1', 200)