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, [])
223 class TestsWithServer(TestCaseWithServer):
224 """Tests against our HTTP server/handler (and database)."""
226 def test_do_POST_day(self) -> None:
227 """Test Todo posting of POST /day."""
230 proc = Process.by_id(self.db_conn, 1)
231 proc2 = Process.by_id(self.db_conn, 2)
232 form_data = {'day_comment': ''}
233 self.check_post(form_data, '/day?date=2024-01-01', 302)
234 self.assertEqual(Todo.by_date(self.db_conn, '2024-01-01'), [])
235 form_data['new_todo'] = str(proc.id_)
236 self.check_post(form_data, '/day?date=2024-01-01', 302)
237 todos = Todo.by_date(self.db_conn, '2024-01-01')
238 self.assertEqual(1, len(todos))
240 self.assertEqual(todo1.id_, 1)
241 self.assertEqual(todo1.process.id_, proc.id_)
242 self.assertEqual(todo1.is_done, False)
243 form_data['new_todo'] = str(proc2.id_)
244 self.check_post(form_data, '/day?date=2024-01-01', 302)
245 todos = Todo.by_date(self.db_conn, '2024-01-01')
247 self.assertEqual(todo1.id_, 2)
248 self.assertEqual(todo1.process.id_, proc2.id_)
249 self.assertEqual(todo1.is_done, False)
251 def test_do_POST_todo(self) -> None:
252 """Test POST /todo."""
253 def post_and_reload(form_data: dict[str, object], status: int = 302,
254 redir_url: str = '/todo?id=1') -> Todo:
255 self.check_post(form_data, '/todo?id=1', status, redir_url)
256 return Todo.by_date(self.db_conn, '2024-01-01')[0]
259 self.check_post({'day_comment': '', 'new_todo': 1},
260 '/day?date=2024-01-01', 302)
261 # test posting to bad URLs
262 self.check_post({}, '/todo=', 404)
263 self.check_post({}, '/todo?id=', 400)
264 self.check_post({}, '/todo?id=FOO', 400)
265 self.check_post({}, '/todo?id=0', 404)
266 # test posting naked entity
267 todo1 = post_and_reload({})
268 self.assertEqual(todo1.children, [])
269 self.assertEqual(todo1.parents, [])
270 self.assertEqual(todo1.is_done, False)
271 # test posting doneness
272 todo1 = post_and_reload({'done': ''})
273 self.assertEqual(todo1.is_done, True)
274 # test implicitly posting non-doneness
275 todo1 = post_and_reload({})
276 self.assertEqual(todo1.is_done, False)
277 # test malformed adoptions
278 self.check_post({'adopt': 'foo'}, '/todo?id=1', 400)
279 self.check_post({'adopt': 1}, '/todo?id=1', 400)
280 self.check_post({'adopt': 2}, '/todo?id=1', 404)
281 # test posting second todo of same process
282 self.check_post({'day_comment': '', 'new_todo': 1},
283 '/day?date=2024-01-01', 302)
284 # test todo 1 adopting todo 2
285 todo1 = post_and_reload({'adopt': 2})
286 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
287 self.assertEqual(todo1.children, [todo2])
288 self.assertEqual(todo1.parents, [])
289 self.assertEqual(todo2.children, [])
290 self.assertEqual(todo2.parents, [todo1])
291 # test todo1 cannot be set done with todo2 not done yet
292 todo1 = post_and_reload({'done': '', 'adopt': 2}, 400)
293 self.assertEqual(todo1.is_done, False)
294 # test todo1 un-adopting todo 2 by just not sending an adopt
295 todo1 = post_and_reload({}, 302)
296 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
297 self.assertEqual(todo1.children, [])
298 self.assertEqual(todo1.parents, [])
299 self.assertEqual(todo2.children, [])
300 self.assertEqual(todo2.parents, [])
301 # test todo1 deletion
302 todo1 = post_and_reload({'delete': ''}, 302, '/')
304 def test_do_POST_day_todo_adoption(self) -> None:
305 """Test Todos posted to Day view may adopt existing Todos."""
306 form_data = self.post_process()
307 form_data = self.post_process(2, form_data | {'new_top_step': 1})
308 form_data = {'day_comment': '', 'new_todo': 1}
309 self.check_post(form_data, '/day?date=2024-01-01', 302)
310 form_data['new_todo'] = 2
311 self.check_post(form_data, '/day?date=2024-01-01', 302)
312 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
313 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
314 self.assertEqual(todo1.children, [])
315 self.assertEqual(todo1.parents, [todo2])
316 self.assertEqual(todo2.children, [todo1])
317 self.assertEqual(todo2.parents, [])
319 def test_do_POST_day_todo_multiple(self) -> None:
320 """Test multiple Todos can be posted to Day view."""
321 form_data = self.post_process()
322 form_data = self.post_process(2)
323 form_data = {'day_comment': '', 'new_todo': [1, 2]}
324 self.check_post(form_data, '/day?date=2024-01-01', 302)
325 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
326 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
327 self.assertEqual(todo1.process.id_, 1)
328 self.assertEqual(todo2.process.id_, 2)
330 def test_do_POST_day_todo_multiple_inner_adoption(self) -> None:
331 """Test multiple Todos can be posted to Day view w. inner adoption."""
332 form_data = self.post_process()
333 form_data = self.post_process(2, form_data | {'new_top_step': 1})
334 form_data = {'day_comment': '', 'new_todo': [1, 2]}
335 self.check_post(form_data, '/day?date=2024-01-01', 302)
336 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
337 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
338 self.assertEqual(todo1.children, [])
339 self.assertEqual(todo1.parents, [todo2])
340 self.assertEqual(todo2.children, [todo1])
341 self.assertEqual(todo2.parents, [])
342 # check process ID order does not affect end result
343 form_data = {'day_comment': '', 'new_todo': [2, 1]}
344 self.check_post(form_data, '/day?date=2024-01-02', 302)
345 todo1 = Todo.by_date(self.db_conn, '2024-01-02')[1]
346 todo2 = Todo.by_date(self.db_conn, '2024-01-02')[0]
347 self.assertEqual(todo1.children, [])
348 self.assertEqual(todo1.parents, [todo2])
349 self.assertEqual(todo2.children, [todo1])
350 self.assertEqual(todo2.parents, [])
352 def test_do_POST_day_todo_doneness(self) -> None:
353 """Test Todo doneness can be posted to Day view."""
354 form_data = self.post_process()
355 form_data = {'day_comment': '', 'new_todo': [1]}
356 self.check_post(form_data, '/day?date=2024-01-01', 302)
357 todo = Todo.by_date(self.db_conn, '2024-01-01')[0]
358 form_data = {'day_comment': '', 'todo_id': [1]}
359 self.check_post(form_data, '/day?date=2024-01-01', 302)
360 self.assertEqual(todo.is_done, False)
361 form_data = {'day_comment': '', 'todo_id': [1], 'done': [1]}
362 self.check_post(form_data, '/day?date=2024-01-01', 302)
363 self.assertEqual(todo.is_done, True)
365 def test_do_GET_todo(self) -> None:
366 """Test GET /todo response codes."""
368 form_data = {'day_comment': '', 'new_todo': 1}
369 self.check_post(form_data, '/day?date=2024-01-01', 302)
370 self.check_get('/todo', 400)
371 self.check_get('/todo?id=', 400)
372 self.check_get('/todo?id=foo', 400)
373 self.check_get('/todo?id=0', 404)
374 self.check_get('/todo?id=1', 200)