1 """Test Processes module."""
3 from tests.utils import (TestCaseSansDB, TestCaseWithDB, TestCaseWithServer,
5 from plomtask.processes import Process, ProcessStep
6 from plomtask.conditions import Condition
7 from plomtask.exceptions import NotFoundException
10 class TestsSansDB(TestCaseSansDB):
11 """Module tests not requiring DB setup."""
12 checked_class = Process
15 class TestsSansDBProcessStep(TestCaseSansDB):
16 """Module tests not requiring DB setup."""
17 checked_class = ProcessStep
18 default_init_kwargs = {'owner_id': 2, 'step_process_id': 3,
22 class TestsWithDB(TestCaseWithDB):
23 """Module tests requiring DB setup."""
24 checked_class = Process
26 def test_from_table_row(self) -> None:
27 """Test .from_table_row() properly reads in class from DB."""
28 super().test_from_table_row()
29 p, set1, set2, set3 = self.p_of_conditions()
31 assert isinstance(p.id_, int)
32 for row in self.db_conn.row_where(self.checked_class.table_name,
34 r = Process.from_table_row(self.db_conn, row)
35 self.assertEqual(sorted(r.conditions), sorted(set1))
36 self.assertEqual(sorted(r.enables), sorted(set2))
37 self.assertEqual(sorted(r.disables), sorted(set3))
39 def test_Process_steps(self) -> None:
40 """Test addition, nesting, and non-recursion of ProcessSteps"""
41 # pylint: disable=too-many-locals
42 # pylint: disable=too-many-statements
43 p1, p2, p3 = self.three_processes()
44 assert isinstance(p1.id_, int)
45 assert isinstance(p2.id_, int)
46 assert isinstance(p3.id_, int)
47 steps_p1: list[ProcessStep] = []
48 # # add step of process p2 as first (top-level) step to p1
49 # s_p2_to_p1 = ProcessStep(None, p1.id_, p2.id_, None)
50 # steps_p1 += [s_p2_to_p1]
51 # p1.set_steps(self.db_conn, steps_p1)
52 # p1_dict: dict[int, ProcessStepsNode] = {}
53 # p1_dict[1] = ProcessStepsNode(p2, None, True, {})
54 # self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
55 # # add step of process p3 as second (top-level) step to p1
56 # s_p3_to_p1 = ProcessStep(None, p1.id_, p3.id_, None)
57 # steps_p1 += [s_p3_to_p1]
58 # p1.set_steps(self.db_conn, steps_p1)
59 # p1_dict[2] = ProcessStepsNode(p3, None, True, {})
60 # self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
61 # # add step of process p3 as first (top-level) step to p2,
62 # steps_p2: list[ProcessStep] = []
63 # s_p3_to_p2 = ProcessStep(None, p2.id_, p3.id_, None)
64 # steps_p2 += [s_p3_to_p2]
65 # p2.set_steps(self.db_conn, steps_p2)
66 # # expect it as implicit sub-step of p1's second (p3) step
67 # p2_dict = {3: ProcessStepsNode(p3, None, False, {})}
68 # p1_dict[1].steps[3] = p2_dict[3]
69 # self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
70 # # add step of process p2 as explicit sub-step to p1's second sub-step
71 # s_p2_to_p1_first = ProcessStep(None, p1.id_, p2.id_, s_p3_to_p1.id_)
72 # steps_p1 += [s_p2_to_p1_first]
73 # p1.set_steps(self.db_conn, steps_p1)
74 # seen_3 = ProcessStepsNode(p3, None, False, {}, False)
75 # p1_dict[1].steps[3].seen = True
76 # p1_dict[2].steps[4] = ProcessStepsNode(p2, s_p3_to_p1.id_, True,
78 # self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
80 # add step of process p3 as explicit sub-step to non-existing p1
81 # sub-step (of id=999), expect it to become another p1 top-level step
82 s_p3_to_p1_999 = ProcessStep(None, p1.id_, p3.id_, 999)
83 steps_p1 += [s_p3_to_p1_999]
84 p1.set_steps(self.db_conn, steps_p1)
85 p1_dict[5] = ProcessStepsNode(p3, None, True, {})
86 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
87 # add step of process p3 as explicit sub-step to p1's implicit p3
88 # sub-step, expect it to become another p1 top-level step
89 s_p3_to_p1_impl_p3 = ProcessStep(None, p1.id_, p3.id_,
91 steps_p1 += [s_p3_to_p1_impl_p3]
92 p1.set_steps(self.db_conn, steps_p1)
93 p1_dict[6] = ProcessStepsNode(p3, None, True, {})
94 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
95 self.assertEqual(p1.used_as_step_by(self.db_conn), [])
96 self.assertEqual(p2.used_as_step_by(self.db_conn), [p1])
97 self.assertEqual(p3.used_as_step_by(self.db_conn), [p1, p2])
98 # add step of process p3 as explicit sub-step to p1's first
99 # sub-step, expect it to eliminate implicit p3 sub-step
100 s_p3_to_p1_first_explicit = ProcessStep(None, p1.id_, p3.id_,
102 p1_dict[1].steps = {7: ProcessStepsNode(p3, 1, True, {})}
103 p1_dict[2].steps[4].steps[3].seen = False
104 steps_p1 += [s_p3_to_p1_first_explicit]
105 p1.set_steps(self.db_conn, steps_p1)
106 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
107 # ensure implicit steps non-top explicit steps are shown
108 s_p3_to_p2_first = ProcessStep(None, p2.id_, p3.id_, s_p3_to_p2.id_)
109 steps_p2 += [s_p3_to_p2_first]
110 p2.set_steps(self.db_conn, steps_p2)
111 p1_dict[1].steps[3].steps[7] = ProcessStepsNode(p3, 3, False, {},
113 p1_dict[2].steps[4].steps[3].steps[7] = ProcessStepsNode(
114 p3, 3, False, {}, False)
115 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
116 # ensure suppressed step nodes are hidden
117 assert isinstance(s_p3_to_p2.id_, int)
118 p1.set_step_suppressions(self.db_conn, [s_p3_to_p2.id_])
119 p1_dict[1].steps[3].steps = {}
120 p1_dict[1].steps[3].is_suppressed = True
121 p1_dict[2].steps[4].steps[3].steps = {}
122 p1_dict[2].steps[4].steps[3].is_suppressed = True
123 self.assertEqual(p1.get_steps(self.db_conn), p1_dict)
125 def test_remove(self) -> None:
126 """Test removal of Processes and ProcessSteps."""
127 super().test_remove()
128 p1, p2, p3 = Process(None), Process(None), Process(None)
129 for p in [p1, p2, p3]:
131 assert isinstance(p1.id_, int)
132 assert isinstance(p2.id_, int)
133 assert isinstance(p3.id_, int)
134 step = ProcessStep(None, p2.id_, p1.id_, None)
135 p2.set_steps(self.db_conn, [step])
137 p2.set_steps(self.db_conn, [])
138 with self.assertRaises(NotFoundException):
139 # check unset ProcessSteps actually cannot be found anymore
140 assert step_id is not None
141 ProcessStep.by_id(self.db_conn, step_id)
142 p1.remove(self.db_conn)
143 step = ProcessStep(None, p2.id_, p3.id_, None)
144 p2.set_steps(self.db_conn, [step])
146 # check _can_ remove Process pointed to by ProcessStep.owner_id, and …
147 p2.remove(self.db_conn)
148 with self.assertRaises(NotFoundException):
149 # … being dis-owned eliminates ProcessStep
150 assert step_id is not None
151 ProcessStep.by_id(self.db_conn, step_id)
154 class TestsWithDBForProcessStep(TestCaseWithDB):
155 """Module tests requiring DB setup."""
156 checked_class = ProcessStep
157 default_init_kwargs = {'owner_id': 1, 'step_process_id': 2,
160 def setUp(self) -> None:
163 self.p1.save(self.db_conn)
165 def test_remove(self) -> None:
166 """Test .remove and unsetting of owner's .explicit_steps entry."""
168 p2.save(self.db_conn)
169 assert isinstance(self.p1.id_, int)
170 assert isinstance(p2.id_, int)
171 step = ProcessStep(None, self.p1.id_, p2.id_, None)
172 self.p1.set_steps(self.db_conn, [step])
173 step.remove(self.db_conn)
174 self.assertEqual(self.p1.explicit_steps, [])
175 self.check_identity_with_cache_and_db([])
178 class ExpectedGetProcess(Expected):
179 """Builder of expectations for GET /processes."""
180 _default_dict = {'is_new': False, 'preset_top_step': None, 'n_todos': 0}
181 _on_empty_make_temp = ('Process', 'proc_as_dict')
185 *args: Any, **kwargs: Any) -> None:
186 self._fields = {'process': proc_id, 'steps': []}
187 super().__init__(*args, **kwargs)
190 def stepnode_as_dict(step_id: int,
193 steps: None | list[dict[str, object]] = None,
194 is_explicit: bool = True,
195 is_suppressed: bool = False) -> dict[str, object]:
196 # pylint: disable=too-many-arguments
197 """Return JSON of ProcessStepNode to expect."""
198 return {'step': step_id,
201 'steps': steps if steps else [],
202 'is_explicit': is_explicit,
203 'is_suppressed': is_suppressed}
205 def recalc(self) -> None:
206 """Update internal dictionary by subclass-specific rules."""
208 self._fields['process_candidates'] = self.as_ids(
209 self.lib_all('Process'))
210 self._fields['condition_candidates'] = self.as_ids(
211 self.lib_all('Condition'))
212 self._fields['owners'] = [
213 s['owner_id'] for s in self.lib_all('ProcessStep')
214 if s['step_process_id'] == self._fields['process']]
217 class ExpectedGetProcesses(Expected):
218 """Builder of expectations for GET /processes."""
219 _default_dict = {'sort_by': 'title', 'pattern': ''}
221 def recalc(self) -> None:
222 """Update internal dictionary by subclass-specific rules."""
224 self._fields['processes'] = self.as_ids(self.lib_all('Process'))
227 class TestsWithServer(TestCaseWithServer):
228 """Module tests against our HTTP server/handler (and database)."""
229 checked_class = Process
231 def test_fail_POST_process(self) -> None:
232 """Test POST /process and its effect on the database."""
233 valid_post = {'title': '', 'description': '', 'effort': 1.0}
234 # check payloads lacking minimum expecteds
235 self.check_minimal_inputs('/process', valid_post)
236 # check payloads of bad data types
237 self.check_post(valid_post | {'effort': ''}, '/process', 400)
238 # check references to non-existant items
239 self.check_post(valid_post | {'conditions': [1]}, '/process', 404)
240 self.check_post(valid_post | {'disables': [1]}, '/process', 404)
241 self.check_post(valid_post | {'blockers': [1]}, '/process', 404)
242 self.check_post(valid_post | {'enables': [1]}, '/process', 404)
243 self.check_post(valid_post | {'new_top_step': 2}, '/process', 404)
244 # check deletion of non-existant
245 self.check_post({'delete': ''}, '/process?id=1', 404)
247 def test_basic_POST_process(self) -> None:
248 """Test basic GET/POST /process operations."""
250 exp = ExpectedGetProcess(1)
251 exp.force('process_candidates', [])
252 exp.set('is_new', True)
253 self.check_json_get('/process?id=1', exp)
254 # check on minimal payload post
255 exp = ExpectedGetProcess(1)
256 self.post_exp_process([exp], {}, 1)
257 self.check_json_get('/process?id=1', exp)
258 # check boolean 'calendarize'
259 self.post_exp_process([exp], {'calendarize': True}, 1)
260 self.check_json_get('/process?id=1', exp)
261 self.post_exp_process([exp], {}, 1)
262 self.check_json_get('/process?id=1', exp)
263 # check conditions posting
265 self.post_exp_cond([exp], {}, i+1)
266 p = {'conditions': [1, 2], 'disables': [1],
267 'blockers': [3], 'enables': [2, 3]}
268 self.post_exp_process([exp], p, 1)
269 self.check_json_get('/process?id=1', exp)
270 # check n_todos field
271 self.post_exp_day([], {'new_todo': ['1']}, '2024-01-01')
272 self.post_exp_day([], {'new_todo': ['1']}, '2024-01-02')
273 exp.set('n_todos', 2)
274 self.check_json_get('/process?id=1', exp)
275 # check cannot delete if Todos to Process
276 self.check_post({'delete': ''}, '/process?id=1', 500)
277 # check cannot delete if some ProcessStep's .step_process_id
278 self.post_exp_process([exp], {}, 2)
279 self.post_exp_process([exp], {'new_top_step': 2}, 3)
280 self.check_post({'delete': ''}, '/process?id=2', 500)
281 # check successful deletion
282 self.post_exp_process([exp], {}, 4)
283 self.check_post({'delete': ''}, '/process?id=4', 302, '/processes')
284 exp = ExpectedGetProcess(4)
285 exp.set('is_new', True)
287 self.post_exp_cond([exp], {}, i+1)
288 self.post_exp_process([exp], {}, i+1)
289 exp.force('process_candidates', [1, 2, 3])
290 self.check_json_get('/process?id=4', exp)
292 def test_POST_process_steps(self) -> None:
293 """Test behavior of ProcessStep posting."""
294 # pylint: disable=too-many-statements
295 url = '/process?id=1'
296 exp = ExpectedGetProcess(1)
297 self.post_exp_process([exp], {}, 1)
298 # post first (top-level) step of proc2 to proc1 by 'step_of' in 2
299 self.post_exp_process([exp], {'step_of': 1}, 2)
300 exp.lib_set('ProcessStep', [exp.procstep_as_dict(1, owner_id=1, step_process_id=2)])
302 exp.stepnode_as_dict(
305 self.check_json_get(url, exp)
306 # post empty/absent steps list to process, expect clean slate, and old
307 # step to completely disappear
308 self.post_exp_process([exp], {}, 1)
309 exp.lib_wipe('ProcessStep')
311 self.check_json_get(url, exp)
312 # post anew (as only step yet) step of proc2 to proc1 by 'new_top_step'
313 self.post_exp_process([exp], {'new_top_step': 2}, 1)
314 exp.lib_set('ProcessStep',
315 [exp.procstep_as_dict(1, owner_id=1, step_process_id=2)])
316 self.post_exp_process([exp], {'kept_steps': [1]}, 1)
317 step_nodes = [exp.stepnode_as_dict(step_id=1, proc_id=2)]
318 exp.set('steps', step_nodes)
319 self.check_json_get(url, exp)
320 # fail on single--step recursion
321 p_min = {'title': '', 'description': '', 'effort': 0}
322 self.check_post(p_min | {'new_top_step': 1}, url, 400)
323 self.check_post(p_min | {'step_of': 1}, url, 400)
325 self.post_exp_process([exp], {}, 3)
326 self.post_exp_process([exp], {'kept_steps': [1], 'new_top_step': 3}, 1)
327 exp.lib_set('ProcessStep',
328 [exp.procstep_as_dict(2, owner_id=1, step_process_id=3)])
329 step_nodes += [exp.stepnode_as_dict(step_id=2, proc_id=3)]
330 self.check_json_get(url, exp)
331 # # post implicit sub-step via post to proc2
332 self.post_exp_process([exp], {}, 4)
333 self.post_exp_process([exp], {'step_of': [1], 'new_top_step': 4}, 2)
334 exp.lib_set('ProcessStep',
335 [exp.procstep_as_dict(3, owner_id=2, step_process_id=4)])
336 step_nodes[0]['steps'] = [
337 exp.stepnode_as_dict(step_id=3, proc_id=4, is_explicit=False)]
338 self.check_json_get(url, exp)
339 # post explicit sub-step via post to proc1
340 p = {'kept_steps': [1, 2], 'new_step_to_2': 4}
341 self.post_exp_process([exp], p, 1)
342 exp.lib_set('ProcessStep', [exp.procstep_as_dict(
343 4, owner_id=1, step_process_id=4, parent_step_id=2)])
344 step_nodes[1]['steps'] = [
345 exp.stepnode_as_dict(step_id=4, proc_id=4)]
346 self.check_json_get(url, exp)
347 # fail on multi-step recursion via new step(s)
348 self.post_exp_process([exp], {}, 5)
349 self.post_exp_process([exp], {'new_top_step': 1}, 5)
350 exp.lib_set('ProcessStep', [exp.procstep_as_dict(
351 5, owner_id=5, step_process_id=1)])
352 self.check_post(p_min | {'step_of': 5, 'new_top_step': 5}, url, 400)
353 self.post_exp_process([exp], {}, 6)
354 self.post_exp_process([exp], {'new_top_step': 5}, 6)
355 exp.lib_set('ProcessStep', [exp.procstep_as_dict(
356 6, owner_id=6, step_process_id=5)])
357 self.check_post(p_min | {'step_of': 5, 'new_top_step': 6}, url, 400)
358 # fail on multi-step recursion via explicit sub-step
359 self.check_json_get(url, exp)
360 p = {'step_of': 5, 'kept_steps': [1, 2, 4], 'new_step_to_2': 6}
361 self.check_post(p_min | p, url, 400)
363 def test_fail_GET_process(self) -> None:
364 """Test invalid GET /process params."""
365 # check for invalid IDs
366 self.check_get_defaults('/process')
367 # check we catch invalid base64
368 self.check_get('/process?title_b64=foo', 400)
369 # check failure on references to unknown processes; we create Process
370 # of ID=1 here so we know the 404 comes from step_to=2 etc. (that tie
371 # the Process displayed by /process to others), not from not finding
372 # the main Process itself
373 self.post_exp_process([], {}, 1)
374 self.check_get('/process?id=1&step_to=2', 404)
375 self.check_get('/process?id=1&has_step=2', 404)
377 def test_GET_processes(self) -> None:
378 """Test GET /processes."""
379 # pylint: disable=too-many-statements
380 # test empty result on empty DB, default-settings on empty params
381 exp = ExpectedGetProcesses()
382 self.check_json_get('/processes', exp)
383 # test on meaningless non-empty params (incl. entirely un-used key),
384 # that 'sort_by' default to 'title' (even if set to something else, as
385 # long as without handler) and 'pattern' get preserved
386 exp.set('pattern', 'bar')
387 url = '/processes?sort_by=foo&pattern=bar&foo=x'
388 self.check_json_get(url, exp)
389 # test non-empty result, automatic (positive) sorting by title
390 for i, t in enumerate([('foo', 'oof', 1.0, []),
391 ('bar', 'rab', 1.1, [1]),
392 ('baz', 'zab', 0.9, [1, 2])]):
393 payload = {'title': t[0], 'description': t[1], 'effort': t[2],
394 'new_top_step': t[3]}
395 self.post_exp_process([exp], payload, i+1)
396 exp.lib_set('ProcessStep',
397 [exp.procstep_as_dict(1, owner_id=2, step_process_id=1),
398 exp.procstep_as_dict(2, owner_id=3, step_process_id=1),
399 exp.procstep_as_dict(3, owner_id=3, step_process_id=2)])
400 exp.set('pattern', '')
401 self.check_filter(exp, 'processes', 'sort_by', 'title', [2, 3, 1])
402 # test other sortings
403 self.check_filter(exp, 'processes', 'sort_by', '-title', [1, 3, 2])
404 self.check_filter(exp, 'processes', 'sort_by', 'effort', [3, 1, 2])
405 self.check_filter(exp, 'processes', 'sort_by', '-effort', [2, 1, 3])
406 self.check_filter(exp, 'processes', 'sort_by', 'steps', [1, 2, 3])
407 self.check_filter(exp, 'processes', 'sort_by', '-steps', [3, 2, 1])
408 self.check_filter(exp, 'processes', 'sort_by', 'owners', [3, 2, 1])
409 self.check_filter(exp, 'processes', 'sort_by', '-owners', [1, 2, 3])
410 # test pattern matching on title
411 exp.set('sort_by', 'title')
412 exp.lib_del('Process', '1')
413 self.check_filter(exp, 'processes', 'pattern', 'ba', [2, 3])
414 # test pattern matching on description
415 exp.lib_wipe('Process')
416 exp.lib_wipe('ProcessStep')
417 self.post_exp_process([exp], {'description': 'oof', 'effort': 1.0}, 1)
418 self.check_filter(exp, 'processes', 'pattern', 'of', [1])