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 self.assertEqual(Todo.by_date(self.db_conn, 'foo'), [])
68 def test_Todo_on_conditions(self) -> None:
69 """Test effect of Todos on Conditions."""
70 assert isinstance(self.cond1.id_, int)
71 assert isinstance(self.cond2.id_, int)
72 todo = Todo(None, self.proc, False, self.date1)
73 todo.save(self.db_conn)
74 todo.set_enables(self.db_conn, [self.cond1.id_])
75 todo.set_disables(self.db_conn, [self.cond2.id_])
77 self.assertEqual(self.cond1.is_active, True)
78 self.assertEqual(self.cond2.is_active, False)
80 self.assertEqual(self.cond1.is_active, True)
81 self.assertEqual(self.cond2.is_active, False)
83 def test_Todo_children(self) -> None:
84 """Test Todo.children relations."""
85 todo_1 = Todo(None, self.proc, False, self.date1)
86 todo_2 = Todo(None, self.proc, False, self.date1)
87 todo_2.save(self.db_conn)
88 with self.assertRaises(HandledException):
89 todo_1.add_child(todo_2)
90 todo_1.save(self.db_conn)
91 todo_3 = Todo(None, self.proc, False, self.date1)
92 with self.assertRaises(HandledException):
93 todo_1.add_child(todo_3)
94 todo_3.save(self.db_conn)
95 todo_1.add_child(todo_3)
96 todo_1.save(self.db_conn)
97 assert isinstance(todo_1.id_, int)
98 todo_retrieved = Todo.by_id(self.db_conn, todo_1.id_)
99 self.assertEqual(todo_retrieved.children, [todo_3])
100 with self.assertRaises(BadFormatException):
101 todo_3.add_child(todo_1)
103 def test_Todo_conditioning(self) -> None:
104 """Test Todo.doability conditions."""
105 assert isinstance(self.cond1.id_, int)
106 todo_1 = Todo(None, self.proc, False, self.date1)
107 todo_1.save(self.db_conn)
108 todo_2 = Todo(None, self.proc, False, self.date1)
109 todo_2.save(self.db_conn)
110 todo_2.add_child(todo_1)
111 with self.assertRaises(BadFormatException):
112 todo_2.is_done = True
113 todo_1.is_done = True
114 todo_2.is_done = True
115 todo_2.is_done = False
116 todo_2.set_conditions(self.db_conn, [self.cond1.id_])
117 with self.assertRaises(BadFormatException):
118 todo_2.is_done = True
119 self.cond1.is_active = True
120 todo_2.is_done = True
122 def test_Todo_step_tree(self) -> None:
123 """Test self-configuration of TodoStepsNode tree for Day view."""
124 todo_1 = Todo(None, self.proc, False, self.date1)
125 todo_1.save(self.db_conn)
126 assert isinstance(todo_1.id_, int)
128 node_0 = TodoNode(todo_1, False, [])
129 self.assertEqual(todo_1.get_step_tree(set()), node_0)
130 # test non_emtpy seen_todo does something
132 self.assertEqual(todo_1.get_step_tree({todo_1.id_}), node_0)
133 # test child shows up
134 todo_2 = Todo(None, self.proc, False, self.date1)
135 todo_2.save(self.db_conn)
136 assert isinstance(todo_2.id_, int)
137 todo_1.add_child(todo_2)
138 node_2 = TodoNode(todo_2, False, [])
139 node_0.children = [node_2]
141 self.assertEqual(todo_1.get_step_tree(set()), node_0)
142 # test child shows up with child
143 todo_3 = Todo(None, self.proc, False, self.date1)
144 todo_3.save(self.db_conn)
145 assert isinstance(todo_3.id_, int)
146 todo_2.add_child(todo_3)
147 node_3 = TodoNode(todo_3, False, [])
148 node_2.children = [node_3]
149 self.assertEqual(todo_1.get_step_tree(set()), node_0)
150 # test same todo can be child-ed multiple times at different locations
151 todo_1.add_child(todo_3)
152 node_4 = TodoNode(todo_3, True, [])
153 node_0.children += [node_4]
154 self.assertEqual(todo_1.get_step_tree(set()), node_0)
156 def test_Todo_unsatisfied_steps(self) -> None:
157 """Test options of satisfying unfulfilled Process.explicit_steps."""
158 assert isinstance(self.proc.id_, int)
159 todo_1 = Todo(None, self.proc, False, self.date1)
160 todo_1.save(self.db_conn)
161 proc2 = Process(None)
162 proc2.save(self.db_conn)
163 assert isinstance(proc2.id_, int)
164 proc3 = Process(None)
165 proc3.save(self.db_conn)
166 assert isinstance(proc3.id_, int)
167 proc4 = Process(None)
168 proc4.save(self.db_conn)
169 assert isinstance(proc4.id_, int)
170 proc3.set_steps(self.db_conn, [(None, proc4.id_, None)])
171 proc2.set_steps(self.db_conn, [(None, self.proc.id_, None),
172 (None, self.proc.id_, None),
173 (None, proc3.id_, None)])
174 todo_2 = Todo(None, proc2, False, self.date1)
175 todo_2.save(self.db_conn)
176 # test empty adoption does nothing
177 todo_2.adopt_from([])
178 self.assertEqual(todo_2.children, [])
179 # test basic adoption
180 todo_2.adopt_from([todo_1])
181 self.assertEqual(todo_2.children, [todo_1])
182 self.assertEqual(todo_1.parents, [todo_2])
183 # test making missing children
184 todo_2.make_missing_children(self.db_conn)
185 todo_3 = Todo.by_id(self.db_conn, 3)
186 todo_4 = Todo.by_id(self.db_conn, 4)
187 self.assertEqual(todo_2.children, [todo_1, todo_3, todo_4])
188 self.assertEqual(todo_3.process, self.proc)
189 self.assertEqual(todo_3.parents, [todo_2])
190 self.assertEqual(todo_3.children, [])
191 self.assertEqual(todo_4.process, proc3)
192 self.assertEqual(todo_4.parents, [todo_2])
193 # test .make_missing_children doesn't further than top-level
194 self.assertEqual(todo_4.children, [])
195 # test .make_missing_children lower down the tree
196 todo_4.make_missing_children(self.db_conn)
197 todo_5 = Todo.by_id(self.db_conn, 5)
198 self.assertEqual(todo_5.process, proc4)
199 self.assertEqual(todo_4.children, [todo_5])
200 self.assertEqual(todo_5.parents, [todo_4])
202 def test_Todo_singularity(self) -> None:
203 """Test pointers made for single object keep pointing to it."""
204 self.check_singularity('is_done', True, self.proc, False, self.date1)
206 def test_Todo_remove(self) -> None:
208 todo_1 = Todo(None, self.proc, False, self.date1)
209 todo_1.save(self.db_conn)
210 todo_0 = Todo(None, self.proc, False, self.date1)
211 todo_0.save(self.db_conn)
212 todo_0.add_child(todo_1)
213 todo_2 = Todo(None, self.proc, False, self.date1)
214 todo_2.save(self.db_conn)
215 todo_1.add_child(todo_2)
216 todo_1.remove(self.db_conn)
217 with self.assertRaises(NotFoundException):
218 Todo.by_id(self.db_conn, todo_1.id_)
219 self.assertEqual(todo_0.children, [])
220 self.assertEqual(todo_2.parents, [])
221 todo_2.comment = 'foo'
222 with self.assertRaises(HandledException):
223 todo_2.remove(self.db_conn)
226 with self.assertRaises(HandledException):
227 todo_2.remove(self.db_conn)
229 def test_Todo_autoremoval(self) -> None:
230 """"Test automatic removal for Todo.effort < 0."""
231 todo_1 = Todo(None, self.proc, False, self.date1)
232 todo_1.save(self.db_conn)
233 todo_1.comment = 'foo'
235 todo_1.save(self.db_conn)
236 Todo.by_id(self.db_conn, todo_1.id_)
238 todo_1.save(self.db_conn)
239 with self.assertRaises(NotFoundException):
240 Todo.by_id(self.db_conn, todo_1.id_)
243 class TestsWithServer(TestCaseWithServer):
244 """Tests against our HTTP server/handler (and database)."""
246 def test_do_POST_day(self) -> None:
247 """Test Todo posting of POST /day."""
250 proc = Process.by_id(self.db_conn, 1)
251 proc2 = Process.by_id(self.db_conn, 2)
252 form_data = {'day_comment': ''}
253 self.check_post(form_data, '/day?date=2024-01-01', 302)
254 self.assertEqual(Todo.by_date(self.db_conn, '2024-01-01'), [])
255 form_data['new_todo'] = str(proc.id_)
256 self.check_post(form_data, '/day?date=2024-01-01', 302)
257 todos = Todo.by_date(self.db_conn, '2024-01-01')
258 self.assertEqual(1, len(todos))
260 self.assertEqual(todo1.id_, 1)
261 self.assertEqual(todo1.process.id_, proc.id_)
262 self.assertEqual(todo1.is_done, False)
263 form_data['new_todo'] = str(proc2.id_)
264 self.check_post(form_data, '/day?date=2024-01-01', 302)
265 todos = Todo.by_date(self.db_conn, '2024-01-01')
267 self.assertEqual(todo1.id_, 2)
268 self.assertEqual(todo1.process.id_, proc2.id_)
269 self.assertEqual(todo1.is_done, False)
271 def test_do_POST_todo(self) -> None:
272 """Test POST /todo."""
273 def post_and_reload(form_data: dict[str, object], status: int = 302,
274 redir_url: str = '/todo?id=1') -> Todo:
275 self.check_post(form_data, '/todo?id=1', status, redir_url)
276 return Todo.by_date(self.db_conn, '2024-01-01')[0]
279 self.check_post({'day_comment': '', 'new_todo': 1},
280 '/day?date=2024-01-01', 302)
281 # test posting to bad URLs
282 self.check_post({}, '/todo=', 404)
283 self.check_post({}, '/todo?id=', 400)
284 self.check_post({}, '/todo?id=FOO', 400)
285 self.check_post({}, '/todo?id=0', 404)
286 # test posting naked entity
287 todo1 = post_and_reload({})
288 self.assertEqual(todo1.children, [])
289 self.assertEqual(todo1.parents, [])
290 self.assertEqual(todo1.is_done, False)
291 # test posting doneness
292 todo1 = post_and_reload({'done': ''})
293 self.assertEqual(todo1.is_done, True)
294 # test implicitly posting non-doneness
295 todo1 = post_and_reload({})
296 self.assertEqual(todo1.is_done, False)
297 # test malformed adoptions
298 self.check_post({'adopt': 'foo'}, '/todo?id=1', 400)
299 self.check_post({'adopt': 1}, '/todo?id=1', 400)
300 self.check_post({'adopt': 2}, '/todo?id=1', 404)
301 # test posting second todo of same process
302 self.check_post({'day_comment': '', 'new_todo': 1},
303 '/day?date=2024-01-01', 302)
304 # test todo 1 adopting todo 2
305 todo1 = post_and_reload({'adopt': 2})
306 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
307 self.assertEqual(todo1.children, [todo2])
308 self.assertEqual(todo1.parents, [])
309 self.assertEqual(todo2.children, [])
310 self.assertEqual(todo2.parents, [todo1])
311 # test todo1 cannot be set done with todo2 not done yet
312 todo1 = post_and_reload({'done': '', 'adopt': 2}, 400)
313 self.assertEqual(todo1.is_done, False)
314 # test todo1 un-adopting todo 2 by just not sending an adopt
315 todo1 = post_and_reload({}, 302)
316 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
317 self.assertEqual(todo1.children, [])
318 self.assertEqual(todo1.parents, [])
319 self.assertEqual(todo2.children, [])
320 self.assertEqual(todo2.parents, [])
321 # test todo1 deletion
322 todo1 = post_and_reload({'delete': ''}, 302, '/')
324 def test_do_POST_day_todo_adoption(self) -> None:
325 """Test Todos posted to Day view may adopt existing Todos."""
326 form_data = self.post_process()
327 form_data = self.post_process(2, form_data | {'new_top_step': 1})
328 form_data = {'day_comment': '', 'new_todo': 1}
329 self.check_post(form_data, '/day?date=2024-01-01', 302)
330 form_data['new_todo'] = 2
331 self.check_post(form_data, '/day?date=2024-01-01', 302)
332 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
333 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
334 self.assertEqual(todo1.children, [])
335 self.assertEqual(todo1.parents, [todo2])
336 self.assertEqual(todo2.children, [todo1])
337 self.assertEqual(todo2.parents, [])
339 def test_do_POST_day_todo_multiple(self) -> None:
340 """Test multiple Todos can be posted to Day view."""
341 form_data = self.post_process()
342 form_data = self.post_process(2)
343 form_data = {'day_comment': '', 'new_todo': [1, 2]}
344 self.check_post(form_data, '/day?date=2024-01-01', 302)
345 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
346 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
347 self.assertEqual(todo1.process.id_, 1)
348 self.assertEqual(todo2.process.id_, 2)
350 def test_do_POST_day_todo_multiple_inner_adoption(self) -> None:
351 """Test multiple Todos can be posted to Day view w. inner adoption."""
352 form_data = self.post_process()
353 form_data = self.post_process(2, form_data | {'new_top_step': 1})
354 form_data = {'day_comment': '', 'new_todo': [1, 2]}
355 self.check_post(form_data, '/day?date=2024-01-01', 302)
356 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
357 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
358 self.assertEqual(todo1.children, [])
359 self.assertEqual(todo1.parents, [todo2])
360 self.assertEqual(todo2.children, [todo1])
361 self.assertEqual(todo2.parents, [])
362 # check process ID order does not affect end result
363 form_data = {'day_comment': '', 'new_todo': [2, 1]}
364 self.check_post(form_data, '/day?date=2024-01-02', 302)
365 todo1 = Todo.by_date(self.db_conn, '2024-01-02')[1]
366 todo2 = Todo.by_date(self.db_conn, '2024-01-02')[0]
367 self.assertEqual(todo1.children, [])
368 self.assertEqual(todo1.parents, [todo2])
369 self.assertEqual(todo2.children, [todo1])
370 self.assertEqual(todo2.parents, [])
372 def test_do_POST_day_todo_doneness(self) -> None:
373 """Test Todo doneness can be posted to Day view."""
374 form_data = self.post_process()
375 form_data = {'day_comment': '', 'new_todo': [1]}
376 self.check_post(form_data, '/day?date=2024-01-01', 302)
377 todo = Todo.by_date(self.db_conn, '2024-01-01')[0]
378 form_data = {'day_comment': '', 'todo_id': [1]}
379 self.check_post(form_data, '/day?date=2024-01-01', 302)
380 self.assertEqual(todo.is_done, False)
381 form_data = {'day_comment': '', 'todo_id': [1], 'done': [1]}
382 self.check_post(form_data, '/day?date=2024-01-01', 302)
383 self.assertEqual(todo.is_done, True)
385 def test_do_GET_todo(self) -> None:
386 """Test GET /todo response codes."""
388 form_data = {'day_comment': '', 'new_todo': 1}
389 self.check_post(form_data, '/day?date=2024-01-01', 302)
390 self.check_get('/todo', 400)
391 self.check_get('/todo?id=', 400)
392 self.check_get('/todo?id=foo', 400)
393 self.check_get('/todo?id=0', 404)
394 self.check_get('/todo?id=1', 200)