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_singularity(self) -> None:
213 """Test pointers made for single object keep pointing to it."""
214 todo = Todo(None, self.proc, False, self.date1)
215 todo.save(self.db_conn)
216 retrieved_todo = Todo.by_id(self.db_conn, 1)
218 self.assertEqual(retrieved_todo.is_done, True)
219 retrieved_todo = Todo.by_date(self.db_conn, self.date1)[0]
220 retrieved_todo.is_done = False
221 self.assertEqual(todo.is_done, False)
224 class TestsWithServer(TestCaseWithServer):
225 """Tests against our HTTP server/handler (and database)."""
227 def test_do_POST_day(self) -> None:
228 """Test Todo posting of POST /day."""
229 form_data = {'title': '', 'description': '', 'effort': 1}
230 self.check_post(form_data, '/process?id=', 302, '/')
231 self.check_post(form_data, '/process?id=', 302, '/')
232 proc = Process.by_id(self.db_conn, 1)
233 proc2 = Process.by_id(self.db_conn, 2)
234 form_data = {'comment': ''}
235 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
236 self.assertEqual(Todo.by_date(self.db_conn, '2024-01-01'), [])
237 form_data['new_todo'] = str(proc.id_)
238 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
239 todos = Todo.by_date(self.db_conn, '2024-01-01')
240 self.assertEqual(1, len(todos))
242 self.assertEqual(todo1.id_, 1)
243 self.assertEqual(todo1.process.id_, proc.id_)
244 self.assertEqual(todo1.is_done, False)
245 form_data['new_todo'] = str(proc2.id_)
246 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
247 todos = Todo.by_date(self.db_conn, '2024-01-01')
249 self.assertEqual(todo1.id_, 2)
250 self.assertEqual(todo1.process.id_, proc2.id_)
251 self.assertEqual(todo1.is_done, False)
253 def test_do_POST_todo(self) -> None:
254 """Test POST /todo."""
255 def post_and_reload(form_data: dict[str, object],
256 status: int = 302) -> Todo:
257 self.check_post(form_data, '/todo?id=1', status, '/')
258 self.db_conn.cached_todos = {}
259 return Todo.by_date(self.db_conn, '2024-01-01')[0]
261 form_data = {'title': '', 'description': '', 'effort': 1}
262 self.check_post(form_data, '/process', 302)
263 form_data = {'comment': '', 'new_todo': 1}
264 self.check_post(form_data, '/day?date=2024-01-01', 302)
265 # test posting to bad URLs
267 self.check_post(form_data, '/todo=', 404)
268 self.check_post(form_data, '/todo?id=', 400)
269 self.check_post(form_data, '/todo?id=FOO', 400)
270 self.check_post(form_data, '/todo?id=0', 404)
271 # test posting naked entity
272 todo1 = post_and_reload(form_data)
273 self.assertEqual(todo1.children, [])
274 self.assertEqual(todo1.parents, [])
275 self.assertEqual(todo1.is_done, False)
276 # test posting doneness
277 form_data = {'done': ''}
278 todo1 = post_and_reload(form_data)
279 self.assertEqual(todo1.is_done, True)
280 # test implicitly posting non-doneness
282 todo1 = post_and_reload(form_data)
283 self.assertEqual(todo1.is_done, False)
284 # test malformed adoptions
285 form_data = {'adopt': 'foo'}
286 self.check_post(form_data, '/todo?id=1', 400)
287 form_data = {'adopt': 1}
288 self.check_post(form_data, '/todo?id=1', 400)
289 form_data = {'adopt': 2}
290 self.check_post(form_data, '/todo?id=1', 404)
291 # test posting second todo of same process
292 form_data = {'comment': '', 'new_todo': 1}
293 self.check_post(form_data, '/day?date=2024-01-01', 302)
294 # test todo 1 adopting todo 2
295 form_data = {'adopt': 2}
296 todo1 = post_and_reload(form_data)
297 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
298 self.assertEqual(todo1.children, [todo2])
299 self.assertEqual(todo1.parents, [])
300 self.assertEqual(todo2.children, [])
301 self.assertEqual(todo2.parents, [todo1])
302 # test todo1 cannot be set done with todo2 not done yet
303 form_data = {'done': '', 'adopt': 2}
304 todo1 = post_and_reload(form_data, 400)
305 self.assertEqual(todo1.is_done, False)
306 # test todo1 un-adopting todo 2 by just not sending an adopt
308 todo1 = post_and_reload(form_data, 302)
309 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
310 self.assertEqual(todo1.children, [])
311 self.assertEqual(todo1.parents, [])
312 self.assertEqual(todo2.children, [])
313 self.assertEqual(todo2.parents, [])
315 def test_do_GET_todo(self) -> None:
316 """Test GET /todo response codes."""
317 form_data = {'title': '', 'description': '', 'effort': 1}
318 self.check_post(form_data, '/process?id=', 302, '/')
319 form_data = {'comment': '', 'new_todo': 1}
320 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
321 self.check_get('/todo', 400)
322 self.check_get('/todo?id=', 400)
323 self.check_get('/todo?id=foo', 400)
324 self.check_get('/todo?id=0', 404)
325 self.check_get('/todo?id=1', 200)