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 self.assertEqual(Todo.by_id(self.db_conn, 0), todo)
35 with self.assertRaises(NotFoundException):
36 self.assertEqual(Todo.by_id(self.db_conn, 2), todo)
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)
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)
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)
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)
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)
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)
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)
270 class TestsWithServer(TestCaseWithServer):
271 """Tests against our HTTP server/handler (and database)."""
273 def test_do_POST_day(self) -> None:
274 """Test Todo posting of POST /day."""
275 form_data = {'title': '', 'description': '', 'effort': 1}
276 self.check_post(form_data, '/process?id=', 302, '/')
277 self.check_post(form_data, '/process?id=', 302, '/')
278 proc = Process.by_id(self.db_conn, 1)
279 proc2 = Process.by_id(self.db_conn, 2)
280 form_data = {'comment': ''}
281 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
282 self.assertEqual(Todo.by_date(self.db_conn, '2024-01-01'), [])
283 form_data['new_todo'] = str(proc.id_)
284 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
285 todos = Todo.by_date(self.db_conn, '2024-01-01')
286 self.assertEqual(1, len(todos))
288 self.assertEqual(todo1.id_, 1)
289 self.assertEqual(todo1.process.id_, proc.id_)
290 self.assertEqual(todo1.is_done, False)
291 form_data['new_todo'] = str(proc2.id_)
292 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
293 todos = Todo.by_date(self.db_conn, '2024-01-01')
295 self.assertEqual(todo1.id_, 2)
296 self.assertEqual(todo1.process.id_, proc2.id_)
297 self.assertEqual(todo1.is_done, False)
299 def test_do_POST_todo(self) -> None:
300 """Test POST /todo."""
301 def post_and_reload(form_data: dict[str, object],
302 status: int = 302) -> Todo:
303 self.check_post(form_data, '/todo?id=1', status, '/')
304 self.db_conn.cached_todos = {}
305 return Todo.by_date(self.db_conn, '2024-01-01')[0]
307 form_data = {'title': '', 'description': '', 'effort': 1}
308 self.check_post(form_data, '/process', 302)
309 form_data = {'comment': '', 'new_todo': 1}
310 self.check_post(form_data, '/day?date=2024-01-01', 302)
311 # test posting to bad URLs
313 self.check_post(form_data, '/todo=', 404)
314 self.check_post(form_data, '/todo?id=', 400)
315 self.check_post(form_data, '/todo?id=FOO', 400)
316 self.check_post(form_data, '/todo?id=0', 404)
317 # test posting naked entity
318 todo1 = post_and_reload(form_data)
319 self.assertEqual(todo1.children, [])
320 self.assertEqual(todo1.parents, [])
321 self.assertEqual(todo1.is_done, False)
322 # test posting doneness
323 form_data = {'done': ''}
324 todo1 = post_and_reload(form_data)
325 self.assertEqual(todo1.is_done, True)
326 # test implicitly posting non-doneness
328 todo1 = post_and_reload(form_data)
329 self.assertEqual(todo1.is_done, False)
330 # test malformed adoptions
331 form_data = {'adopt': 'foo'}
332 self.check_post(form_data, '/todo?id=1', 400)
333 form_data = {'adopt': 1}
334 self.check_post(form_data, '/todo?id=1', 400)
335 form_data = {'adopt': 2}
336 self.check_post(form_data, '/todo?id=1', 404)
337 # test posting second todo of same process
338 form_data = {'comment': '', 'new_todo': 1}
339 self.check_post(form_data, '/day?date=2024-01-01', 302)
340 # test todo 1 adopting todo 2
341 form_data = {'adopt': 2}
342 todo1 = post_and_reload(form_data)
343 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
344 self.assertEqual(todo1.children, [todo2])
345 self.assertEqual(todo1.parents, [])
346 self.assertEqual(todo2.children, [])
347 self.assertEqual(todo2.parents, [todo1])
348 # test todo1 cannot be set done with todo2 not done yet
349 form_data = {'done': '', 'adopt': 2}
350 todo1 = post_and_reload(form_data, 400)
351 self.assertEqual(todo1.is_done, False)
352 # test todo1 un-adopting todo 2 by just not sending an adopt
354 todo1 = post_and_reload(form_data, 302)
355 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
356 self.assertEqual(todo1.children, [])
357 self.assertEqual(todo1.parents, [])
358 self.assertEqual(todo2.children, [])
359 self.assertEqual(todo2.parents, [])
361 def test_do_POST_day_todo_adoption(self) -> None:
362 """Test Todos posted to Day view may adopt existing Todos."""
363 form_data = {'title': '', 'description': '', 'effort': 1}
364 self.check_post(form_data, '/process', 302, '/')
365 form_data['new_top_step'] = 1
366 self.check_post(form_data, '/process', 302, '/')
367 form_data = {'comment': '', 'new_todo': 1}
368 self.check_post(form_data, '/day?date=2024-01-01', 302)
369 form_data = {'comment': '', 'new_todo': 2}
370 self.check_post(form_data, '/day?date=2024-01-01', 302)
371 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
372 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
373 self.assertEqual(todo1.children, [])
374 self.assertEqual(todo1.parents, [todo2])
375 self.assertEqual(todo2.children, [todo1])
376 self.assertEqual(todo2.parents, [])
378 def test_do_GET_todo(self) -> None:
379 """Test GET /todo response codes."""
380 form_data = {'title': '', 'description': '', 'effort': 1}
381 self.check_post(form_data, '/process?id=', 302, '/')
382 form_data = {'comment': '', 'new_todo': 1}
383 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
384 self.check_get('/todo', 400)
385 self.check_get('/todo?id=', 400)
386 self.check_get('/todo?id=foo', 400)
387 self.check_get('/todo?id=0', 404)
388 self.check_get('/todo?id=1', 200)