1 """Test Processes module."""
2 from tests.utils import TestCaseWithDB, TestCaseWithServer, TestCaseSansDB
3 from plomtask.processes import Process, ProcessStep, ProcessStepsNode
4 from plomtask.conditions import Condition
5 from plomtask.exceptions import HandledException, NotFoundException
6 from plomtask.todos import Todo
9 class TestsSansDB(TestCaseSansDB):
10 """Module tests not requiring DB setup."""
11 checked_class = Process
13 versioned_defaults_to_test = {'title': 'UNNAMED', 'description': '',
17 class TestsSansDBProcessStep(TestCaseSansDB):
18 """Module tests not requiring DB setup."""
19 checked_class = ProcessStep
21 default_init_args = [2, 3, 4]
24 class TestsWithDB(TestCaseWithDB):
25 """Module tests requiring DB setup."""
26 checked_class = Process
27 test_versioneds = {'title': str, 'description': str, 'effort': float}
29 def three_processes(self) -> tuple[Process, Process, Process]:
30 """Return three saved processes."""
31 p1, p2, p3 = Process(None), Process(None), Process(None)
32 for p in [p1, p2, p3]:
36 def p_of_conditions(self) -> tuple[Process, list[Condition],
37 list[Condition], list[Condition]]:
38 """Return Process and its three Condition sets."""
40 c1, c2, c3 = Condition(None), Condition(None), Condition(None)
41 for c in [c1, c2, c3]:
43 assert isinstance(c1.id_, int)
44 assert isinstance(c2.id_, int)
45 assert isinstance(c3.id_, int)
49 p.set_conditions(self.db_conn, [c.id_ for c in set_1
50 if isinstance(c.id_, int)])
51 p.set_enables(self.db_conn, [c.id_ for c in set_2
52 if isinstance(c.id_, int)])
53 p.set_disables(self.db_conn, [c.id_ for c in set_3
54 if isinstance(c.id_, int)])
56 return p, set_1, set_2, set_3
58 def test_Process_conditions_saving(self) -> None:
59 """Test .save/.save_core."""
60 p, set1, set2, set3 = self.p_of_conditions()
62 r = Process.by_id(self.db_conn, p.id_)
63 self.assertEqual(sorted(r.conditions), sorted(set1))
64 self.assertEqual(sorted(r.enables), sorted(set2))
65 self.assertEqual(sorted(r.disables), sorted(set3))
67 def test_Process_from_table_row(self) -> None:
68 """Test .from_table_row() properly reads in class from DB"""
69 self.check_from_table_row()
70 self.check_versioned_from_table_row('title', str)
71 self.check_versioned_from_table_row('description', str)
72 self.check_versioned_from_table_row('effort', float)
73 p, set1, set2, set3 = self.p_of_conditions()
75 assert isinstance(p.id_, int)
76 for row in self.db_conn.row_where(self.checked_class.table_name,
78 # pylint: disable=no-member
79 r = Process.from_table_row(self.db_conn, row)
80 self.assertEqual(sorted(r.conditions), sorted(set1))
81 self.assertEqual(sorted(r.enables), sorted(set2))
82 self.assertEqual(sorted(r.disables), sorted(set3))
84 def test_Process_steps(self) -> None:
85 """Test addition, nesting, and non-recursion of ProcessSteps"""
86 def add_step(proc: Process,
87 steps_proc: list[tuple[int | None, int, int | None]],
88 step_tuple: tuple[int | None, int, int | None],
89 expected_id: int) -> None:
90 steps_proc += [step_tuple]
91 proc.set_steps(self.db_conn, steps_proc)
92 steps_proc[-1] = (expected_id, step_tuple[1], step_tuple[2])
93 p1, p2, p3 = self.three_processes()
94 assert isinstance(p1.id_, int)
95 assert isinstance(p2.id_, int)
96 assert isinstance(p3.id_, int)
97 steps_p1: list[tuple[int | None, int, int | None]] = []
98 add_step(p1, steps_p1, (None, p2.id_, None), 1)
99 p1_dict: dict[int, ProcessStepsNode] = {}
100 p1_dict[1] = ProcessStepsNode(p2, None, True, {}, False)
101 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
102 add_step(p1, steps_p1, (None, p3.id_, None), 2)
103 step_2 = p1.explicit_steps[-1]
104 p1_dict[2] = ProcessStepsNode(p3, None, True, {}, False)
105 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
106 steps_p2: list[tuple[int | None, int, int | None]] = []
107 add_step(p2, steps_p2, (None, p3.id_, None), 3)
108 p1_dict[1].steps[3] = ProcessStepsNode(p3, None, False, {}, False)
109 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
110 add_step(p1, steps_p1, (None, p2.id_, step_2.id_), 4)
111 step_3 = ProcessStepsNode(p3, None, False, {}, True)
112 p1_dict[2].steps[4] = ProcessStepsNode(p2, step_2.id_, True,
114 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
115 add_step(p1, steps_p1, (None, p3.id_, 999), 5)
116 p1_dict[5] = ProcessStepsNode(p3, None, True, {}, False)
117 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
118 add_step(p1, steps_p1, (None, p3.id_, 3), 6)
119 p1_dict[6] = ProcessStepsNode(p3, None, True, {}, False)
120 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
121 self.assertEqual(p1.used_as_step_by(self.db_conn), [])
122 self.assertEqual(p2.used_as_step_by(self.db_conn), [p1])
123 self.assertEqual(p3.used_as_step_by(self.db_conn), [p1, p2])
125 def test_Process_conditions(self) -> None:
126 """Test setting Process.conditions/enables/disables."""
129 for target in ('conditions', 'enables', 'disables'):
130 method = getattr(p, f'set_{target}')
131 c1, c2 = Condition(None), Condition(None)
132 c1.save(self.db_conn)
133 c2.save(self.db_conn)
134 assert isinstance(c1.id_, int)
135 assert isinstance(c2.id_, int)
136 method(self.db_conn, [])
137 self.assertEqual(getattr(p, target), [])
138 method(self.db_conn, [c1.id_])
139 self.assertEqual(getattr(p, target), [c1])
140 method(self.db_conn, [c2.id_])
141 self.assertEqual(getattr(p, target), [c2])
142 method(self.db_conn, [c1.id_, c2.id_])
143 self.assertEqual(getattr(p, target), [c1, c2])
145 def test_Process_by_id(self) -> None:
146 """Test .by_id(), including creation"""
149 def test_Process_all(self) -> None:
153 def test_Process_singularity(self) -> None:
154 """Test pointers made for single object keep pointing to it."""
155 self.check_singularity('conditions', [Condition(None)])
157 def test_Process_versioned_attributes_singularity(self) -> None:
158 """Test behavior of VersionedAttributes on saving (with .title)."""
159 self.check_versioned_singularity()
161 def test_Process_removal(self) -> None:
162 """Test removal of Processes and ProcessSteps."""
164 p1, p2, p3 = self.three_processes()
165 assert isinstance(p1.id_, int)
166 assert isinstance(p2.id_, int)
167 assert isinstance(p3.id_, int)
168 p2.set_steps(self.db_conn, [(None, p1.id_, None)])
169 with self.assertRaises(HandledException):
170 p1.remove(self.db_conn)
171 step = p2.explicit_steps[0]
172 p2.set_steps(self.db_conn, [])
173 with self.assertRaises(NotFoundException):
174 ProcessStep.by_id(self.db_conn, step.id_)
175 p1.remove(self.db_conn)
176 p2.set_steps(self.db_conn, [(None, p3.id_, None)])
177 step = p2.explicit_steps[0]
178 p2.remove(self.db_conn)
179 with self.assertRaises(NotFoundException):
180 ProcessStep.by_id(self.db_conn, step.id_)
181 todo = Todo(None, p3, False, '2024-01-01')
182 todo.save(self.db_conn)
183 with self.assertRaises(HandledException):
184 p3.remove(self.db_conn)
185 todo.remove(self.db_conn)
186 p3.remove(self.db_conn)
189 class TestsWithDBForProcessStep(TestCaseWithDB):
190 """Module tests requiring DB setup."""
191 checked_class = ProcessStep
192 default_init_kwargs = {'owner_id': 2, 'step_process_id': 3,
195 def test_ProcessStep_from_table_row(self) -> None:
196 """Test .from_table_row() properly reads in class from DB"""
197 self.check_from_table_row(2, 3, None)
199 def test_ProcessStep_singularity(self) -> None:
200 """Test pointers made for single object keep pointing to it."""
201 self.check_singularity('parent_step_id', 1, 2, 3, None)
203 def test_ProcessStep_remove(self) -> None:
204 """Test .remove and unsetting of owner's .explicit_steps entry."""
207 p1.save(self.db_conn)
208 p2.save(self.db_conn)
209 assert isinstance(p2.id_, int)
210 p1.set_steps(self.db_conn, [(None, p2.id_, None)])
211 step = p1.explicit_steps[0]
212 step.remove(self.db_conn)
213 self.assertEqual(p1.explicit_steps, [])
214 self.check_storage([])
217 class TestsWithServer(TestCaseWithServer):
218 """Module tests against our HTTP server/handler (and database)."""
220 def test_do_POST_process(self) -> None:
221 """Test POST /process and its effect on the database."""
222 self.assertEqual(0, len(Process.all(self.db_conn)))
223 form_data = self.post_process()
224 self.assertEqual(1, len(Process.all(self.db_conn)))
225 self.check_post(form_data, '/process?id=FOO', 400)
226 self.check_post(form_data | {'effort': 'foo'}, '/process?id=', 400)
227 self.check_post({}, '/process?id=', 400)
228 self.check_post({'title': '', 'description': ''}, '/process?id=', 400)
229 self.check_post({'title': '', 'effort': 1.1}, '/process?id=', 400)
230 self.check_post({'description': '', 'effort': 1.0},
232 self.assertEqual(1, len(Process.all(self.db_conn)))
233 form_data = {'title': 'foo', 'description': 'foo', 'effort': 1.0}
234 self.post_process(2, form_data | {'condition': []})
235 self.check_post(form_data | {'condition': [1]}, '/process?id=', 404)
236 self.check_post({'title': 'foo', 'description': 'foo'},
237 '/condition', 302, '/condition?id=1')
238 self.post_process(3, form_data | {'condition': [1]})
239 self.post_process(4, form_data | {'disables': [1]})
240 self.post_process(5, form_data | {'enables': [1]})
241 form_data['delete'] = ''
242 self.check_post(form_data, '/process?id=', 404)
243 self.check_post(form_data, '/process?id=6', 404)
244 self.check_post(form_data, '/process?id=5', 302, '/processes')
246 def test_do_POST_process_steps(self) -> None:
247 """Test behavior of ProcessStep posting."""
248 form_data_1 = self.post_process(1)
251 form_data_1['new_top_step'] = [2]
252 self.post_process(1, form_data_1)
253 retrieved_process = Process.by_id(self.db_conn, 1)
254 self.assertEqual(len(retrieved_process.explicit_steps), 1)
255 retrieved_step = retrieved_process.explicit_steps[0]
256 self.assertEqual(retrieved_step.step_process_id, 2)
257 self.assertEqual(retrieved_step.owner_id, 1)
258 self.assertEqual(retrieved_step.parent_step_id, None)
259 form_data_1['new_top_step'] = []
260 self.post_process(1, form_data_1)
261 retrieved_process = Process.by_id(self.db_conn, 1)
262 self.assertEqual(retrieved_process.explicit_steps, [])
263 with self.assertRaises(NotFoundException):
264 ProcessStep.by_id(self.db_conn, retrieved_step.id_)
265 form_data_1['new_top_step'] = [3]
266 self.post_process(1, form_data_1)
267 retrieved_process = Process.by_id(self.db_conn, 1)
268 retrieved_step = retrieved_process.explicit_steps[0]
269 self.assertEqual(retrieved_step.step_process_id, 3)
270 self.assertEqual(retrieved_step.owner_id, 1)
271 self.assertEqual(retrieved_step.parent_step_id, None)
272 form_data_1['new_top_step'] = []
273 form_data_1['steps'] = [retrieved_step.id_]
274 self.post_process(1, form_data_1)
275 retrieved_process = Process.by_id(self.db_conn, 1)
276 self.assertEqual(retrieved_process.explicit_steps, [])
277 form_data_1['steps'] = []
278 form_data_1['keep_step'] = [retrieved_step.id_]
279 self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1')
280 form_data_1['steps'] = [retrieved_step.id_]
281 form_data_1['keep_step'] = [retrieved_step.id_]
282 self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1')
283 form_data_1[f'step_{retrieved_step.id_}_process_id'] = [2]
284 self.post_process(1, form_data_1)
285 retrieved_process = Process.by_id(self.db_conn, 1)
286 self.assertEqual(len(retrieved_process.explicit_steps), 1)
287 retrieved_step = retrieved_process.explicit_steps[0]
288 self.assertEqual(retrieved_step.step_process_id, 2)
289 self.assertEqual(retrieved_step.owner_id, 1)
290 self.assertEqual(retrieved_step.parent_step_id, None)
291 form_data_1['new_top_step'] = ['foo']
292 form_data_1['steps'] = []
293 form_data_1['keep_step'] = []
294 self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1')
295 retrieved_process = Process.by_id(self.db_conn, 1)
296 self.assertEqual(len(retrieved_process.explicit_steps), 1)
298 def test_do_GET(self) -> None:
299 """Test /process and /processes response codes."""
300 self.check_get_defaults('/process')
301 self.check_get('/processes', 200)