1 """Test Todos module."""
3 from tests.utils import (TestCaseSansDB, TestCaseWithDB, TestCaseWithServer,
5 from plomtask.todos import Todo
6 from plomtask.processes import Process
7 from plomtask.exceptions import BadFormatException, HandledException
10 class TestsWithDB(TestCaseWithDB, TestCaseSansDB):
11 """Tests requiring DB, but not server setup.
13 NB: We subclass TestCaseSansDB too, to run any tests there that due to any
14 Todo requiring a _saved_ Process wouldn't run without a DB.
17 default_init_kwargs = {'process': None, 'is_done': False,
20 def setUp(self) -> None:
22 self.proc = Process(None)
23 self.proc.save(self.db_conn)
24 self.default_init_kwargs['process'] = self.proc
26 def test_Todo_by_date(self) -> None:
27 """Test findability of Todos by date."""
28 date1, date2 = '2024-01-01', '2024-01-02'
29 t1 = Todo(None, self.proc, False, date1)
31 t2 = Todo(None, self.proc, False, date1)
33 self.assertEqual(Todo.by_date(self.db_conn, date1), [t1, t2])
34 self.assertEqual(Todo.by_date(self.db_conn, date2), [])
35 with self.assertRaises(BadFormatException):
36 self.assertEqual(Todo.by_date(self.db_conn, 'foo'), [])
38 def test_Todo_by_date_range_with_limits(self) -> None:
39 """Test .by_date_range_with_limits."""
40 self.check_by_date_range_with_limits('day')
42 def test_Todo_children(self) -> None:
43 """Test Todo.children relations."""
45 todo_1 = Todo(None, self.proc, False, date1)
46 todo_2 = Todo(None, self.proc, False, date1)
47 todo_2.save(self.db_conn)
48 # check un-saved Todo cannot parent
49 with self.assertRaises(HandledException):
50 todo_1.add_child(todo_2)
51 todo_1.save(self.db_conn)
52 todo_3 = Todo(None, self.proc, False, date1)
53 # check un-saved Todo cannot be parented
54 with self.assertRaises(HandledException):
55 todo_1.add_child(todo_3)
58 class ExpectedGetTodo(Expected):
59 """Builder of expectations for GET /todo."""
63 *args: Any, **kwargs: Any) -> None:
64 self._fields = {'todo': todo_id,
65 'steps_todo_to_process': []}
66 super().__init__(*args, **kwargs)
68 def recalc(self) -> None:
69 """Update internal dictionary by subclass-specific rules."""
71 def walk_steps(step: dict[str, Any]) -> None:
73 proc_id = step['process']
75 [t for t in todos if proc_id == t['process_id']
76 and t['id'] in self._fields['todo_candidates']])
77 self._fields['adoption_candidates_for'][str(proc_id)] = cands
78 for child in step['children']:
83 todos = self.lib_all('Todo')
84 procs = self.lib_all('Process')
85 conds = self.lib_all('Condition')
86 self._fields['todo_candidates'] = self.as_ids(
87 [t for t in todos if t['id'] != self._fields['todo']])
88 self._fields['process_candidates'] = self.as_ids(procs)
89 self._fields['condition_candidates'] = self.as_ids(conds)
90 self._fields['adoption_candidates_for'] = {}
91 for step in self._fields['steps_todo_to_process']:
95 def step_as_dict(node_id: int,
96 process: int | None = None,
97 todo: int | None = None,
98 fillable: bool = False,
99 children: None | list[dict[str, object]] = None
100 ) -> dict[str, object]:
101 """Return JSON of TodoOrProcStepsNode to expect."""
102 return {'node_id': node_id,
103 'children': children if children is not None else [],
105 'fillable': fillable,
109 class TestsWithServer(TestCaseWithServer):
110 """Tests against our HTTP server/handler (and database)."""
113 def test_basic_fail_POST_todo(self) -> None:
114 """Test basic malformed/illegal POST /todo requests."""
115 self.post_exp_process([], {}, 1)
116 # test we cannot just POST into non-existing Todo
117 self.check_post({}, '/todo', 404)
118 self.check_post({}, '/todo?id=FOO', 400)
119 self.check_post({}, '/todo?id=0', 400)
120 self.check_post({}, '/todo?id=1', 404)
121 # test malformed values on existing Todo
122 self.post_exp_day([], {'new_todo': [1]})
123 for name in ['adopt', 'effort', 'make_full', 'make_empty',
124 'conditions', 'disables', 'blockers', 'enables']:
125 self.check_post({name: 'x'}, '/todo?id=1', 400, '/todo')
126 for prefix in ['make_', '']:
127 for suffix in ['', 'x', '1.1']:
128 self.check_post({'step_filler_to_1': [f'{prefix}{suffix}']},
129 '/todo?id=1', 400, '/todo')
130 for suffix in ['', 'x', '1.1']:
131 self.check_post({'step_filler_to_{suffix}': ['1']},
132 '/todo?id=1', 400, '/todo')
134 def test_basic_POST_todo(self) -> None:
135 """Test basic POST /todo manipulations."""
136 exp = ExpectedGetTodo(1)
137 self.post_exp_process([exp], {'calendarize': 0}, 1)
138 self.post_exp_day([exp], {'new_todo': [1]})
139 # test posting naked entity at first changes nothing
140 self.check_json_get('/todo?id=1', exp)
141 self.check_post({}, '/todo?id=1')
142 self.check_json_get('/todo?id=1', exp)
143 # test posting doneness, comment, calendarization, effort
144 todo_post = {'is_done': 1, 'calendarize': 1,
145 'comment': 'foo', 'effort': 2.3}
146 self.post_exp_todo([exp], todo_post, 1)
147 self.check_json_get('/todo?id=1', exp)
148 # test implicitly un-setting comment/calendarize/is_done by empty post
149 self.post_exp_todo([exp], {}, 1)
150 self.check_json_get('/todo?id=1', exp)
151 # test effort post can be explicitly unset by "effort":"" post
152 self.check_post({'effort': ''}, '/todo?id=1')
153 exp.lib_get('Todo', 1)['effort'] = None
154 self.check_json_get('/todo?id=1', exp)
155 # test Condition posts
156 c1_post = {'title': 'foo', 'description': 'oof', 'is_active': 0}
157 c2_post = {'title': 'bar', 'description': 'rab', 'is_active': 1}
158 self.post_exp_cond([exp], c1_post, 1)
159 self.post_exp_cond([exp], c2_post, 2)
160 self.check_json_get('/todo?id=1', exp)
161 todo_post = {'conditions': [1], 'disables': [1],
162 'blockers': [2], 'enables': [2]}
163 self.post_exp_todo([exp], todo_post, 1)
164 self.check_json_get('/todo?id=1', exp)
166 def test_POST_todo_deletion(self) -> None:
167 """Test deletions via POST /todo."""
168 exp = ExpectedGetTodo(1)
169 self.post_exp_process([exp], {}, 1)
170 # test failure of deletion on non-existing Todo
171 self.check_post({'delete': ''}, '/todo?id=2', 404, '/')
172 # test deletion of existing Todo
173 self.post_exp_day([exp], {'new_todo': [1]})
174 self.check_post({'delete': ''}, '/todo?id=1', 302, '/')
175 self.check_get('/todo?id=1', 404)
176 exp.lib_del('Todo', 1)
177 # test deletion of adopted Todo
178 self.post_exp_day([exp], {'new_todo': [1]})
179 self.post_exp_day([exp], {'new_todo': [1]})
180 self.check_post({'adopt': 2}, '/todo?id=1')
181 self.check_post({'delete': ''}, '/todo?id=2', 302, '/')
182 exp.lib_del('Todo', 2)
183 self.check_get('/todo?id=2', 404)
184 self.check_json_get('/todo?id=1', exp)
185 # test deletion of adopting Todo
186 self.post_exp_day([exp], {'new_todo': [1]})
187 self.check_post({'adopt': 2}, '/todo?id=1')
188 self.check_post({'delete': ''}, '/todo?id=1', 302, '/')
190 exp.lib_del('Todo', 1)
191 self.check_json_get('/todo?id=2', exp)
192 # test cannot delete Todo with comment or effort
193 self.check_post({'comment': 'foo'}, '/todo?id=2')
194 self.check_post({'delete': ''}, '/todo?id=2', 500, '/')
195 self.check_post({'effort': 5}, '/todo?id=2')
196 self.check_post({'delete': ''}, '/todo?id=2', 500, '/')
197 # test deletion via effort < 0, but only if deletable
198 self.check_post({'effort': -1, 'comment': 'foo'}, '/todo?id=2')
199 self.check_post({}, '/todo?id=2')
200 self.check_get('/todo?id=2', 404)
202 def test_POST_todo_adoption(self) -> None:
203 """Test adoption via POST /todo with "adopt"."""
204 # post two Todos to Day, have first adopt second
205 exp = ExpectedGetTodo(1)
206 self.post_exp_process([exp], {}, 1)
207 self.post_exp_day([exp], {'new_todo': [1]})
208 self.post_exp_day([exp], {'new_todo': [1]})
209 self.post_exp_todo([exp], {'adopt': 2}, 1)
210 exp.set('steps_todo_to_process', [
211 exp.step_as_dict(node_id=1, process=None, todo=2)])
212 self.check_json_get('/todo?id=1', exp)
213 # test Todo un-adopting by just not sending an adopt
214 self.post_exp_todo([exp], {}, 1)
215 exp.set('steps_todo_to_process', [])
216 self.check_json_get('/todo?id=1', exp)
217 # test fail on trying to adopt non-existing Todo
218 self.check_post({'adopt': 3}, '/todo?id=1', 404)
219 # test cannot self-adopt
220 self.check_post({'adopt': 1}, '/todo?id=1', 400)
221 # test cannot do 1-step circular adoption
222 self.post_exp_todo([exp], {'adopt': 1}, 2)
223 self.check_post({'adopt': 2}, '/todo?id=1', 400)
224 # test cannot do 2-step circular adoption
225 self.post_exp_day([exp], {'new_todo': [1]})
226 self.post_exp_todo([exp], {'adopt': 2}, 3)
227 self.check_post({'adopt': 3}, '/todo?id=1', 400)
228 # test can adopt Todo into ProcessStep chain via its Process (with key
229 # 'step_filler' equivalent to single-element 'adopt' if intable)
230 self.post_exp_process([exp], {}, 2)
231 self.post_exp_process([exp], {}, 3)
232 self.post_exp_process([exp], {'new_top_step': [2, 3]}, 1)
233 exp.lib_set('ProcessStep',
234 [exp.procstep_as_dict(1, owner_id=1, step_process_id=2),
235 exp.procstep_as_dict(2, owner_id=1, step_process_id=3)])
237 exp.step_as_dict(node_id=1, process=2, todo=None, fillable=True),
238 exp.step_as_dict(node_id=2, process=3, todo=None, fillable=True)]
239 exp.set('steps_todo_to_process', slots)
240 self.post_exp_day([exp], {'new_todo': [2]})
241 self.post_exp_day([exp], {'new_todo': [3]})
242 self.check_json_get('/todo?id=1', exp)
243 self.post_exp_todo([exp], {'step_filler_to_1': 5, 'adopt': [4]}, 1)
244 exp.lib_get('Todo', 1)['children'] += [5]
247 self.check_json_get('/todo?id=1', exp)
248 # test 'ignore' values for 'step_filler' are ignored, and intable
249 # 'step_filler' values are interchangeable with those of 'adopt'
250 todo_post = {'adopt': 5, 'step_filler_to_1': ['ignore', 4]}
251 self.check_post(todo_post, '/todo?id=1')
252 self.check_json_get('/todo?id=1', exp)
253 # test cannot adopt into non-top-level elements of chain, instead
254 # creating new top-level steps when adopting of respective Process
255 self.post_exp_process([exp], {}, 4)
256 self.post_exp_process([exp], {'new_top_step': 4, 'step_of': [1]}, 3)
257 exp.lib_set('ProcessStep',
258 [exp.procstep_as_dict(3, owner_id=3, step_process_id=4)])
259 slots[1]['children'] = [exp.step_as_dict(
260 node_id=3, process=4, todo=None, fillable=True)]
261 self.post_exp_day([exp], {'new_todo': [4]})
262 self.post_exp_todo([exp], {'adopt': [4, 5, 6]}, 1)
263 slots += [exp.step_as_dict(
264 node_id=4, process=None, todo=6, fillable=False)]
265 self.check_json_get('/todo?id=1', exp)
267 def test_POST_todo_make_empty(self) -> None:
268 """Test creation via POST /todo "step_filler_to"/"make"."""
269 # create chain of Processes
270 exp = ExpectedGetTodo(1)
271 self.post_exp_process([exp], {}, 1)
272 for i in range(1, 4):
273 self.post_exp_process([exp], {'new_top_step': i}, i+1)
274 exp.lib_set('ProcessStep',
275 [exp.procstep_as_dict(1, owner_id=2, step_process_id=1),
276 exp.procstep_as_dict(2, owner_id=3, step_process_id=2),
277 exp.procstep_as_dict(3, owner_id=4, step_process_id=3)])
278 # post (childless) Todo of chain end, then make empty on next in line
279 self.post_exp_day([exp], {'new_todo': [4]})
280 slots = [exp.step_as_dict(
281 node_id=1, process=3, todo=None, fillable=True,
282 children=[exp.step_as_dict(
283 node_id=2, process=2, todo=None, fillable=False,
284 children=[exp.step_as_dict(
285 node_id=3, process=1, todo=None, fillable=False)])])]
286 exp.set('steps_todo_to_process', slots)
287 self.check_json_get('/todo?id=1', exp)
288 self.check_post({'step_filler_to_1': 'make_3'}, '/todo?id=1')
289 exp.set_todo_from_post(2, {'process_id': 3})
290 exp.set_todo_from_post(1, {'process_id': 4, 'children': [2]})
292 assert isinstance(slots[0]['children'], list)
293 slots[0]['children'][0]['fillable'] = True
294 self.check_json_get('/todo?id=1', exp)
295 # make new top-level Todo without chain implied by its Process
296 self.check_post({'make_empty': 2, 'adopt': [2]}, '/todo?id=1')
297 exp.set_todo_from_post(3, {'process_id': 2})
298 exp.set_todo_from_post(1, {'process_id': 4, 'children': [2, 3]})
299 slots += [exp.step_as_dict(
300 node_id=4, process=None, todo=3, fillable=False)]
301 self.check_json_get('/todo?id=1', exp)
302 # fail on trying to call make_empty on non-existing Process
303 self.check_post({'make_full': 5}, '/todo?id=1', 404)
305 def test_GET_todo(self) -> None:
306 """Test GET /todo response codes."""
307 # test malformed or illegal parameter values
308 self.check_get_defaults('/todo')
309 # test all existing Processes are shown as available
310 exp = ExpectedGetTodo(1)
311 self.post_exp_process([exp], {}, 1)
312 self.post_exp_day([exp], {'new_todo': [1]})
313 self.post_exp_process([exp], {}, 2)
314 self.check_json_get('/todo?id=1', exp)
315 # test chain of Processes shown as potential step nodes
316 self.post_exp_process([exp], {}, 3)
317 self.post_exp_process([exp], {}, 4)
318 self.post_exp_process([exp], {'new_top_step': 2}, 1)
319 self.post_exp_process([exp], {'new_top_step': 3, 'step_of': [1]}, 2)
320 self.post_exp_process([exp], {'new_top_step': 4, 'step_of': [2]}, 3)
321 exp.lib_set('ProcessStep', [
322 exp.procstep_as_dict(1, owner_id=1, step_process_id=2),
323 exp.procstep_as_dict(2, owner_id=2, step_process_id=3),
324 exp.procstep_as_dict(3, owner_id=3, step_process_id=4)])
325 slots = [exp.step_as_dict(
326 node_id=1, process=2, todo=None, fillable=True,
327 children=[exp.step_as_dict(
328 node_id=2, process=3, todo=None, fillable=False,
329 children=[exp.step_as_dict(
330 node_id=3, process=4, todo=None, fillable=False)])])]
331 exp.set('steps_todo_to_process', slots)
332 self.check_json_get('/todo?id=1', exp)
333 # test display of parallel chains
334 proc_steps_post = {'new_top_step': 4, 'kept_steps': [1, 3]}
335 self.post_exp_process([], proc_steps_post, 1)
336 exp.lib_set('ProcessStep', [
337 exp.procstep_as_dict(4, owner_id=1, step_process_id=4)])
338 slots += [exp.step_as_dict(
339 node_id=4, process=4, todo=None, fillable=True)]
340 self.check_json_get('/todo?id=1', exp)
342 def test_POST_todo_doneness_relations(self) -> None:
343 """Test Todo.is_done Condition, adoption relations for /todo POSTs."""
344 self.post_exp_process([], {}, 1)
345 # test Todo with adoptee can only be set done if adoptee is done too
346 self.post_exp_day([], {'new_todo': [1]})
347 self.post_exp_day([], {'new_todo': [1]})
348 self.check_post({'adopt': 2, 'is_done': 1}, '/todo?id=1', 400)
349 self.check_post({'is_done': 1}, '/todo?id=2')
350 self.check_post({'adopt': 2, 'is_done': 1}, '/todo?id=1', 302)
351 # test Todo cannot be set undone with adopted Todo not done yet
352 self.check_post({'is_done': 0}, '/todo?id=2')
353 self.check_post({'adopt': 2, 'is_done': 0}, '/todo?id=1', 400)
354 # test unadoption relieves block
355 self.check_post({'is_done': 0}, '/todo?id=1', 302)
356 # test Condition being set or unset can block doneness setting
357 c1_post = {'title': '', 'description': '', 'is_active': 0}
358 c2_post = {'title': '', 'description': '', 'is_active': 1}
359 self.check_post(c1_post, '/condition', redir='/condition?id=1')
360 self.check_post(c2_post, '/condition', redir='/condition?id=2')
361 self.check_post({'conditions': [1], 'is_done': 1}, '/todo?id=1', 400)
362 self.check_post({'is_done': 1}, '/todo?id=1', 302)
363 self.check_post({'is_done': 0}, '/todo?id=1', 302)
364 self.check_post({'blockers': [2], 'is_done': 1}, '/todo?id=1', 400)
365 self.check_post({'is_done': 1}, '/todo?id=1', 302)
366 # test setting Todo doneness can set/un-set Conditions, but only on
367 # doneness change, not by mere passive state
368 self.check_post({'is_done': 0}, '/todo?id=2', 302)
369 self.check_post({'enables': [1], 'is_done': 1}, '/todo?id=1')
370 self.check_post({'conditions': [1], 'is_done': 1}, '/todo?id=2', 400)
371 self.check_post({'enables': [1], 'is_done': 0}, '/todo?id=1')
372 self.check_post({'enables': [1], 'is_done': 1}, '/todo?id=1')
373 self.check_post({'conditions': [1], 'is_done': 1}, '/todo?id=2')
374 self.check_post({'blockers': [1], 'is_done': 0}, '/todo?id=2', 400)
375 self.check_post({'disables': [1], 'is_done': 1}, '/todo?id=1')
376 self.check_post({'blockers': [1], 'is_done': 0}, '/todo?id=2', 400)
377 self.check_post({'disables': [1]}, '/todo?id=1')
378 self.check_post({'disables': [1], 'is_done': 1}, '/todo?id=1')
379 self.check_post({'blockers': [1]}, '/todo?id=2')