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."""
14 def setUp(self) -> None:
16 self.date1 = '2024-01-01'
17 self.date2 = '2024-01-02'
18 self.proc = Process(None)
19 self.proc.save(self.db_conn)
20 self.cond1 = Condition(None)
21 self.cond1.save(self.db_conn)
22 self.cond2 = Condition(None)
23 self.cond2.save(self.db_conn)
25 def test_Todo_init(self) -> None:
26 """Test creation of Todo and what they default to."""
27 process = Process(None)
28 with self.assertRaises(NotFoundException):
29 Todo(None, process, False, self.date1)
30 process.save(self.db_conn)
31 assert isinstance(self.cond1.id_, int)
32 assert isinstance(self.cond2.id_, int)
33 process.set_conditions(self.db_conn, [self.cond1.id_, self.cond2.id_])
34 process.set_enables(self.db_conn, [self.cond1.id_])
35 process.set_disables(self.db_conn, [self.cond2.id_])
36 todo_no_id = Todo(None, process, False, self.date1)
37 self.assertEqual(todo_no_id.conditions, [self.cond1, self.cond2])
38 self.assertEqual(todo_no_id.enables, [self.cond1])
39 self.assertEqual(todo_no_id.disables, [self.cond2])
40 todo_yes_id = Todo(5, process, False, self.date1)
41 self.assertEqual(todo_yes_id.conditions, [])
42 self.assertEqual(todo_yes_id.enables, [])
43 self.assertEqual(todo_yes_id.disables, [])
45 def test_Todo_saving_and_caching(self) -> None:
51 self.check_saving_and_caching(**kwargs)
52 todo = Todo(None, self.proc, False, self.date1)
53 todo.save(self.db_conn)
54 self.assertEqual(todo.id_, 2)
56 def test_Todo_by_id(self) -> None:
57 """Test findability of Todos."""
58 todo = Todo(1, self.proc, False, self.date1)
59 todo.save(self.db_conn)
60 self.assertEqual(Todo.by_id(self.db_conn, 1), todo)
61 with self.assertRaises(NotFoundException):
62 Todo.by_id(self.db_conn, 0)
63 with self.assertRaises(NotFoundException):
64 Todo.by_id(self.db_conn, 2)
66 def test_Todo_by_date(self) -> None:
67 """Test findability of Todos by date."""
68 t1 = Todo(None, self.proc, False, self.date1)
70 t2 = Todo(None, self.proc, False, self.date1)
72 self.assertEqual(Todo.by_date(self.db_conn, self.date1), [t1, t2])
73 self.assertEqual(Todo.by_date(self.db_conn, self.date2), [])
74 self.assertEqual(Todo.by_date(self.db_conn, 'foo'), [])
76 def test_Todo_on_conditions(self) -> None:
77 """Test effect of Todos on Conditions."""
78 assert isinstance(self.cond1.id_, int)
79 assert isinstance(self.cond2.id_, int)
80 todo = Todo(None, self.proc, False, self.date1)
81 todo.save(self.db_conn)
82 todo.set_enables(self.db_conn, [self.cond1.id_])
83 todo.set_disables(self.db_conn, [self.cond2.id_])
85 self.assertEqual(self.cond1.is_active, True)
86 self.assertEqual(self.cond2.is_active, False)
88 self.assertEqual(self.cond1.is_active, True)
89 self.assertEqual(self.cond2.is_active, False)
91 def test_Todo_children(self) -> None:
92 """Test Todo.children relations."""
93 todo_1 = Todo(None, self.proc, False, self.date1)
94 todo_2 = Todo(None, self.proc, False, self.date1)
95 todo_2.save(self.db_conn)
96 with self.assertRaises(HandledException):
97 todo_1.add_child(todo_2)
98 todo_1.save(self.db_conn)
99 todo_3 = Todo(None, self.proc, False, self.date1)
100 with self.assertRaises(HandledException):
101 todo_1.add_child(todo_3)
102 todo_3.save(self.db_conn)
103 todo_1.add_child(todo_3)
104 todo_1.save(self.db_conn)
105 assert isinstance(todo_1.id_, int)
106 todo_retrieved = Todo.by_id(self.db_conn, todo_1.id_)
107 self.assertEqual(todo_retrieved.children, [todo_3])
108 with self.assertRaises(BadFormatException):
109 todo_3.add_child(todo_1)
111 def test_Todo_conditioning(self) -> None:
112 """Test Todo.doability conditions."""
113 assert isinstance(self.cond1.id_, int)
114 todo_1 = Todo(None, self.proc, False, self.date1)
115 todo_1.save(self.db_conn)
116 todo_2 = Todo(None, self.proc, False, self.date1)
117 todo_2.save(self.db_conn)
118 todo_2.add_child(todo_1)
119 with self.assertRaises(BadFormatException):
120 todo_2.is_done = True
121 todo_1.is_done = True
122 todo_2.is_done = True
123 todo_2.is_done = False
124 todo_2.set_conditions(self.db_conn, [self.cond1.id_])
125 with self.assertRaises(BadFormatException):
126 todo_2.is_done = True
127 self.cond1.is_active = True
128 todo_2.is_done = True
130 def test_Todo_step_tree(self) -> None:
131 """Test self-configuration of TodoStepsNode tree for Day view."""
132 todo_1 = Todo(None, self.proc, False, self.date1)
133 todo_1.save(self.db_conn)
134 assert isinstance(todo_1.id_, int)
136 node_0 = TodoNode(todo_1, False, [])
137 self.assertEqual(todo_1.get_step_tree(set()), node_0)
138 # test non_emtpy seen_todo does something
140 self.assertEqual(todo_1.get_step_tree({todo_1.id_}), node_0)
141 # test child shows up
142 todo_2 = Todo(None, self.proc, False, self.date1)
143 todo_2.save(self.db_conn)
144 assert isinstance(todo_2.id_, int)
145 todo_1.add_child(todo_2)
146 node_2 = TodoNode(todo_2, False, [])
147 node_0.children = [node_2]
149 self.assertEqual(todo_1.get_step_tree(set()), node_0)
150 # test child shows up with child
151 todo_3 = Todo(None, self.proc, False, self.date1)
152 todo_3.save(self.db_conn)
153 assert isinstance(todo_3.id_, int)
154 todo_2.add_child(todo_3)
155 node_3 = TodoNode(todo_3, False, [])
156 node_2.children = [node_3]
157 self.assertEqual(todo_1.get_step_tree(set()), node_0)
158 # test same todo can be child-ed multiple times at different locations
159 todo_1.add_child(todo_3)
160 node_4 = TodoNode(todo_3, True, [])
161 node_0.children += [node_4]
162 self.assertEqual(todo_1.get_step_tree(set()), node_0)
164 def test_Todo_unsatisfied_steps(self) -> None:
165 """Test options of satisfying unfulfilled Process.explicit_steps."""
166 assert isinstance(self.proc.id_, int)
167 todo_1 = Todo(None, self.proc, False, self.date1)
168 todo_1.save(self.db_conn)
169 proc2 = Process(None)
170 proc2.save(self.db_conn)
171 assert isinstance(proc2.id_, int)
172 proc3 = Process(None)
173 proc3.save(self.db_conn)
174 assert isinstance(proc3.id_, int)
175 proc4 = Process(None)
176 proc4.save(self.db_conn)
177 assert isinstance(proc4.id_, int)
178 proc3.set_steps(self.db_conn, [(None, proc4.id_, None)])
179 proc2.set_steps(self.db_conn, [(None, self.proc.id_, None),
180 (None, self.proc.id_, None),
181 (None, proc3.id_, None)])
182 todo_2 = Todo(None, proc2, False, self.date1)
183 todo_2.save(self.db_conn)
184 # test empty adoption does nothing
185 todo_2.adopt_from([])
186 self.assertEqual(todo_2.children, [])
187 # test basic adoption
188 todo_2.adopt_from([todo_1])
189 self.assertEqual(todo_2.children, [todo_1])
190 self.assertEqual(todo_1.parents, [todo_2])
191 # test making missing children
192 todo_2.make_missing_children(self.db_conn)
193 todo_3 = Todo.by_id(self.db_conn, 3)
194 todo_4 = Todo.by_id(self.db_conn, 4)
195 self.assertEqual(todo_2.children, [todo_1, todo_3, todo_4])
196 self.assertEqual(todo_3.process, self.proc)
197 self.assertEqual(todo_3.parents, [todo_2])
198 self.assertEqual(todo_3.children, [])
199 self.assertEqual(todo_4.process, proc3)
200 self.assertEqual(todo_4.parents, [todo_2])
201 # test .make_missing_children doesn't further than top-level
202 self.assertEqual(todo_4.children, [])
203 # test .make_missing_children lower down the tree
204 todo_4.make_missing_children(self.db_conn)
205 todo_5 = Todo.by_id(self.db_conn, 5)
206 self.assertEqual(todo_5.process, proc4)
207 self.assertEqual(todo_4.children, [todo_5])
208 self.assertEqual(todo_5.parents, [todo_4])
210 def test_Todo_singularity(self) -> None:
211 """Test pointers made for single object keep pointing to it."""
212 self.check_singularity('is_done', True, self.proc, False, self.date1)
214 def test_Todo_remove(self) -> None:
216 todo_1 = Todo(None, self.proc, False, self.date1)
217 todo_1.save(self.db_conn)
218 todo_0 = Todo(None, self.proc, False, self.date1)
219 todo_0.save(self.db_conn)
220 todo_0.add_child(todo_1)
221 todo_2 = Todo(None, self.proc, False, self.date1)
222 todo_2.save(self.db_conn)
223 todo_1.add_child(todo_2)
224 todo_1.remove(self.db_conn)
225 with self.assertRaises(NotFoundException):
226 Todo.by_id(self.db_conn, todo_1.id_)
227 self.assertEqual(todo_0.children, [])
228 self.assertEqual(todo_2.parents, [])
231 class TestsWithServer(TestCaseWithServer):
232 """Tests against our HTTP server/handler (and database)."""
234 def test_do_POST_day(self) -> None:
235 """Test Todo posting of POST /day."""
238 proc = Process.by_id(self.db_conn, 1)
239 proc2 = Process.by_id(self.db_conn, 2)
240 form_data = {'day_comment': ''}
241 self.check_post(form_data, '/day?date=2024-01-01', 302)
242 self.assertEqual(Todo.by_date(self.db_conn, '2024-01-01'), [])
243 form_data['new_todo'] = str(proc.id_)
244 self.check_post(form_data, '/day?date=2024-01-01', 302)
245 todos = Todo.by_date(self.db_conn, '2024-01-01')
246 self.assertEqual(1, len(todos))
248 self.assertEqual(todo1.id_, 1)
249 self.assertEqual(todo1.process.id_, proc.id_)
250 self.assertEqual(todo1.is_done, False)
251 form_data['new_todo'] = str(proc2.id_)
252 self.check_post(form_data, '/day?date=2024-01-01', 302)
253 todos = Todo.by_date(self.db_conn, '2024-01-01')
255 self.assertEqual(todo1.id_, 2)
256 self.assertEqual(todo1.process.id_, proc2.id_)
257 self.assertEqual(todo1.is_done, False)
259 def test_do_POST_todo(self) -> None:
260 """Test POST /todo."""
261 def post_and_reload(form_data: dict[str, object], status: int = 302,
262 redir_url: str = '/todo?id=1') -> Todo:
263 self.check_post(form_data, '/todo?id=1', status, redir_url)
264 return Todo.by_date(self.db_conn, '2024-01-01')[0]
267 self.check_post({'day_comment': '', 'new_todo': 1},
268 '/day?date=2024-01-01', 302)
269 # test posting to bad URLs
270 self.check_post({}, '/todo=', 404)
271 self.check_post({}, '/todo?id=', 400)
272 self.check_post({}, '/todo?id=FOO', 400)
273 self.check_post({}, '/todo?id=0', 404)
274 # test posting naked entity
275 todo1 = post_and_reload({})
276 self.assertEqual(todo1.children, [])
277 self.assertEqual(todo1.parents, [])
278 self.assertEqual(todo1.is_done, False)
279 # test posting doneness
280 todo1 = post_and_reload({'done': ''})
281 self.assertEqual(todo1.is_done, True)
282 # test implicitly posting non-doneness
283 todo1 = post_and_reload({})
284 self.assertEqual(todo1.is_done, False)
285 # test malformed adoptions
286 self.check_post({'adopt': 'foo'}, '/todo?id=1', 400)
287 self.check_post({'adopt': 1}, '/todo?id=1', 400)
288 self.check_post({'adopt': 2}, '/todo?id=1', 404)
289 # test posting second todo of same process
290 self.check_post({'day_comment': '', 'new_todo': 1},
291 '/day?date=2024-01-01', 302)
292 # test todo 1 adopting todo 2
293 todo1 = post_and_reload({'adopt': 2})
294 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
295 self.assertEqual(todo1.children, [todo2])
296 self.assertEqual(todo1.parents, [])
297 self.assertEqual(todo2.children, [])
298 self.assertEqual(todo2.parents, [todo1])
299 # test todo1 cannot be set done with todo2 not done yet
300 todo1 = post_and_reload({'done': '', 'adopt': 2}, 400)
301 self.assertEqual(todo1.is_done, False)
302 # test todo1 un-adopting todo 2 by just not sending an adopt
303 todo1 = post_and_reload({}, 302)
304 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
305 self.assertEqual(todo1.children, [])
306 self.assertEqual(todo1.parents, [])
307 self.assertEqual(todo2.children, [])
308 self.assertEqual(todo2.parents, [])
309 # test todo1 deletion
310 todo1 = post_and_reload({'delete': ''}, 302, '/')
312 def test_do_POST_day_todo_adoption(self) -> None:
313 """Test Todos posted to Day view may adopt existing Todos."""
314 form_data = self.post_process()
315 form_data = self.post_process(2, form_data | {'new_top_step': 1})
316 form_data = {'day_comment': '', 'new_todo': 1}
317 self.check_post(form_data, '/day?date=2024-01-01', 302)
318 form_data['new_todo'] = 2
319 self.check_post(form_data, '/day?date=2024-01-01', 302)
320 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
321 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
322 self.assertEqual(todo1.children, [])
323 self.assertEqual(todo1.parents, [todo2])
324 self.assertEqual(todo2.children, [todo1])
325 self.assertEqual(todo2.parents, [])
327 def test_do_POST_day_todo_multiple(self) -> None:
328 """Test multiple Todos can be posted to Day view."""
329 form_data = self.post_process()
330 form_data = self.post_process(2)
331 form_data = {'day_comment': '', 'new_todo': [1, 2]}
332 self.check_post(form_data, '/day?date=2024-01-01', 302)
333 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
334 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
335 self.assertEqual(todo1.process.id_, 1)
336 self.assertEqual(todo2.process.id_, 2)
338 def test_do_POST_day_todo_multiple_inner_adoption(self) -> None:
339 """Test multiple Todos can be posted to Day view w. inner adoption."""
340 form_data = self.post_process()
341 form_data = self.post_process(2, form_data | {'new_top_step': 1})
342 form_data = {'day_comment': '', 'new_todo': [1, 2]}
343 self.check_post(form_data, '/day?date=2024-01-01', 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.children, [])
347 self.assertEqual(todo1.parents, [todo2])
348 self.assertEqual(todo2.children, [todo1])
349 self.assertEqual(todo2.parents, [])
350 # check process ID order does not affect end result
351 form_data = {'day_comment': '', 'new_todo': [2, 1]}
352 self.check_post(form_data, '/day?date=2024-01-02', 302)
353 todo1 = Todo.by_date(self.db_conn, '2024-01-02')[1]
354 todo2 = Todo.by_date(self.db_conn, '2024-01-02')[0]
355 self.assertEqual(todo1.children, [])
356 self.assertEqual(todo1.parents, [todo2])
357 self.assertEqual(todo2.children, [todo1])
358 self.assertEqual(todo2.parents, [])
360 def test_do_POST_day_todo_doneness(self) -> None:
361 """Test Todo doneness can be posted to Day view."""
362 form_data = self.post_process()
363 form_data = {'day_comment': '', 'new_todo': [1]}
364 self.check_post(form_data, '/day?date=2024-01-01', 302)
365 todo = Todo.by_date(self.db_conn, '2024-01-01')[0]
366 form_data = {'day_comment': '', 'todo_id': [1]}
367 self.check_post(form_data, '/day?date=2024-01-01', 302)
368 self.assertEqual(todo.is_done, False)
369 form_data = {'day_comment': '', 'todo_id': [1], 'done': [1]}
370 self.check_post(form_data, '/day?date=2024-01-01', 302)
371 self.assertEqual(todo.is_done, True)
373 def test_do_GET_todo(self) -> None:
374 """Test GET /todo response codes."""
376 form_data = {'day_comment': '', 'new_todo': 1}
377 self.check_post(form_data, '/day?date=2024-01-01', 302)
378 self.check_get('/todo', 400)
379 self.check_get('/todo?id=', 400)
380 self.check_get('/todo?id=foo', 400)
381 self.check_get('/todo?id=0', 404)
382 self.check_get('/todo?id=1', 200)