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
28 def three_processes(self) -> tuple[Process, Process, Process]:
29 """Return three saved processes."""
30 p1, p2, p3 = Process(None), Process(None), Process(None)
31 for p in [p1, p2, p3]:
35 def p_of_conditions(self) -> tuple[Process, list[Condition],
36 list[Condition], list[Condition]]:
37 """Return Process and its three Condition sets."""
39 c1, c2, c3 = Condition(None), Condition(None), Condition(None)
40 for c in [c1, c2, c3]:
42 assert isinstance(c1.id_, int)
43 assert isinstance(c2.id_, int)
44 assert isinstance(c3.id_, int)
48 p.set_conditions(self.db_conn, [c.id_ for c in set_1
49 if isinstance(c.id_, int)])
50 p.set_enables(self.db_conn, [c.id_ for c in set_2
51 if isinstance(c.id_, int)])
52 p.set_disables(self.db_conn, [c.id_ for c in set_3
53 if isinstance(c.id_, int)])
55 return p, set_1, set_2, set_3
57 def test_Process_saving_and_caching(self) -> None:
58 """Test .save/.save_core."""
60 self.check_saving_and_caching(**kwargs)
61 self.check_saving_of_versioned('title', str)
62 self.check_saving_of_versioned('description', str)
63 self.check_saving_of_versioned('effort', float)
64 p, set1, set2, set3 = self.p_of_conditions()
66 r = Process.by_id(self.db_conn, p.id_)
67 self.assertEqual(sorted(r.conditions), sorted(set1))
68 self.assertEqual(sorted(r.enables), sorted(set2))
69 self.assertEqual(sorted(r.disables), sorted(set3))
71 def test_Process_from_table_row(self) -> None:
72 """Test .from_table_row() properly reads in class from DB"""
73 self.check_from_table_row()
74 self.check_versioned_from_table_row('title', str)
75 self.check_versioned_from_table_row('description', str)
76 self.check_versioned_from_table_row('effort', float)
77 p, set1, set2, set3 = self.p_of_conditions()
79 assert isinstance(p.id_, int)
80 for row in self.db_conn.row_where(self.checked_class.table_name,
82 # pylint: disable=no-member
83 r = Process.from_table_row(self.db_conn, row)
84 self.assertEqual(sorted(r.conditions), sorted(set1))
85 self.assertEqual(sorted(r.enables), sorted(set2))
86 self.assertEqual(sorted(r.disables), sorted(set3))
88 def test_Process_steps(self) -> None:
89 """Test addition, nesting, and non-recursion of ProcessSteps"""
90 def add_step(proc: Process,
91 steps_proc: list[tuple[int | None, int, int | None]],
92 step_tuple: tuple[int | None, int, int | None],
93 expected_id: int) -> None:
94 steps_proc += [step_tuple]
95 proc.set_steps(self.db_conn, steps_proc)
96 steps_proc[-1] = (expected_id, step_tuple[1], step_tuple[2])
97 p1, p2, p3 = self.three_processes()
98 assert isinstance(p1.id_, int)
99 assert isinstance(p2.id_, int)
100 assert isinstance(p3.id_, int)
101 steps_p1: list[tuple[int | None, int, int | None]] = []
102 add_step(p1, steps_p1, (None, p2.id_, None), 1)
103 p1_dict: dict[int, ProcessStepsNode] = {}
104 p1_dict[1] = ProcessStepsNode(p2, None, True, {}, False)
105 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
106 add_step(p1, steps_p1, (None, p3.id_, None), 2)
107 step_2 = p1.explicit_steps[-1]
108 p1_dict[2] = ProcessStepsNode(p3, None, True, {}, False)
109 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
110 steps_p2: list[tuple[int | None, int, int | None]] = []
111 add_step(p2, steps_p2, (None, p3.id_, None), 3)
112 p1_dict[1].steps[3] = ProcessStepsNode(p3, None, False, {}, False)
113 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
114 add_step(p1, steps_p1, (None, p2.id_, step_2.id_), 4)
115 step_3 = ProcessStepsNode(p3, None, False, {}, True)
116 p1_dict[2].steps[4] = ProcessStepsNode(p2, step_2.id_, True,
118 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
119 add_step(p1, steps_p1, (None, p3.id_, 999), 5)
120 p1_dict[5] = ProcessStepsNode(p3, None, True, {}, False)
121 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
122 add_step(p1, steps_p1, (None, p3.id_, 3), 6)
123 p1_dict[6] = ProcessStepsNode(p3, None, True, {}, False)
124 self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
125 self.assertEqual(p1.used_as_step_by(self.db_conn), [])
126 self.assertEqual(p2.used_as_step_by(self.db_conn), [p1])
127 self.assertEqual(p3.used_as_step_by(self.db_conn), [p1, p2])
129 def test_Process_conditions(self) -> None:
130 """Test setting Process.conditions/enables/disables."""
133 for target in ('conditions', 'enables', 'disables'):
134 method = getattr(p, f'set_{target}')
135 c1, c2 = Condition(None), Condition(None)
136 c1.save(self.db_conn)
137 c2.save(self.db_conn)
138 assert isinstance(c1.id_, int)
139 assert isinstance(c2.id_, int)
140 method(self.db_conn, [])
141 self.assertEqual(getattr(p, target), [])
142 method(self.db_conn, [c1.id_])
143 self.assertEqual(getattr(p, target), [c1])
144 method(self.db_conn, [c2.id_])
145 self.assertEqual(getattr(p, target), [c2])
146 method(self.db_conn, [c1.id_, c2.id_])
147 self.assertEqual(getattr(p, target), [c1, c2])
149 def test_Process_by_id(self) -> None:
150 """Test .by_id(), including creation"""
153 def test_Process_all(self) -> None:
157 def test_Process_singularity(self) -> None:
158 """Test pointers made for single object keep pointing to it."""
159 self.check_singularity('conditions', [Condition(None)])
161 def test_Process_versioned_attributes_singularity(self) -> None:
162 """Test behavior of VersionedAttributes on saving (with .title)."""
163 self.check_versioned_singularity()
165 def test_Process_removal(self) -> None:
166 """Test removal of Processes and ProcessSteps."""
168 p1, p2, p3 = self.three_processes()
169 assert isinstance(p1.id_, int)
170 assert isinstance(p2.id_, int)
171 assert isinstance(p3.id_, int)
172 p2.set_steps(self.db_conn, [(None, p1.id_, None)])
173 with self.assertRaises(HandledException):
174 p1.remove(self.db_conn)
175 step = p2.explicit_steps[0]
176 p2.set_steps(self.db_conn, [])
177 with self.assertRaises(NotFoundException):
178 ProcessStep.by_id(self.db_conn, step.id_)
179 p1.remove(self.db_conn)
180 p2.set_steps(self.db_conn, [(None, p3.id_, None)])
181 step = p2.explicit_steps[0]
182 p2.remove(self.db_conn)
183 with self.assertRaises(NotFoundException):
184 ProcessStep.by_id(self.db_conn, step.id_)
185 todo = Todo(None, p3, False, '2024-01-01')
186 todo.save(self.db_conn)
187 with self.assertRaises(HandledException):
188 p3.remove(self.db_conn)
189 todo.remove(self.db_conn)
190 p3.remove(self.db_conn)
193 class TestsWithDBForProcessStep(TestCaseWithDB):
194 """Module tests requiring DB setup."""
195 checked_class = ProcessStep
197 def test_ProcessStep_saving_and_caching(self) -> None:
198 """Test .save/.save_core."""
201 'step_process_id': 3,
203 self.check_saving_and_caching(**kwargs)
205 def test_ProcessStep_from_table_row(self) -> None:
206 """Test .from_table_row() properly reads in class from DB"""
207 self.check_from_table_row(2, 3, None)
209 def test_ProcessStep_singularity(self) -> None:
210 """Test pointers made for single object keep pointing to it."""
211 self.check_singularity('parent_step_id', 1, 2, 3, None)
213 def test_ProcessStep_remove(self) -> None:
214 """Test .remove and unsetting of owner's .explicit_steps entry."""
217 p1.save(self.db_conn)
218 p2.save(self.db_conn)
219 assert isinstance(p2.id_, int)
220 p1.set_steps(self.db_conn, [(None, p2.id_, None)])
221 step = p1.explicit_steps[0]
222 step.remove(self.db_conn)
223 self.assertEqual(p1.explicit_steps, [])
224 self.check_storage([])
227 class TestsWithServer(TestCaseWithServer):
228 """Module tests against our HTTP server/handler (and database)."""
230 def test_do_POST_process(self) -> None:
231 """Test POST /process and its effect on the database."""
232 self.assertEqual(0, len(Process.all(self.db_conn)))
233 form_data = self.post_process()
234 self.assertEqual(1, len(Process.all(self.db_conn)))
235 self.check_post(form_data, '/process?id=FOO', 400)
236 self.check_post(form_data | {'effort': 'foo'}, '/process?id=', 400)
237 self.check_post({}, '/process?id=', 400)
238 self.check_post({'title': '', 'description': ''}, '/process?id=', 400)
239 self.check_post({'title': '', 'effort': 1.1}, '/process?id=', 400)
240 self.check_post({'description': '', 'effort': 1.0},
242 self.assertEqual(1, len(Process.all(self.db_conn)))
243 form_data = {'title': 'foo', 'description': 'foo', 'effort': 1.0}
244 self.post_process(2, form_data | {'condition': []})
245 self.check_post(form_data | {'condition': [1]}, '/process?id=', 404)
246 self.check_post({'title': 'foo', 'description': 'foo'},
247 '/condition', 302, '/condition?id=1')
248 self.post_process(3, form_data | {'condition': [1]})
249 self.post_process(4, form_data | {'disables': [1]})
250 self.post_process(5, form_data | {'enables': [1]})
251 form_data['delete'] = ''
252 self.check_post(form_data, '/process?id=', 404)
253 self.check_post(form_data, '/process?id=6', 404)
254 self.check_post(form_data, '/process?id=5', 302, '/processes')
256 def test_do_GET(self) -> None:
257 """Test /process and /processes response codes."""
259 self.check_get_defaults('/process')
260 self.check_get('/processes', 200)