home · contact · privacy
Minor template improvements.
[plomtask] / tests / processes.py
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
7
8
9 class TestsSansDB(TestCaseSansDB):
10     """Module tests not requiring DB setup."""
11     checked_class = Process
12     do_id_test = True
13     versioned_defaults_to_test = {'title': 'UNNAMED', 'description': '',
14                                   'effort': 1.0}
15
16
17 class TestsSansDBProcessStep(TestCaseSansDB):
18     """Module tests not requiring DB setup."""
19     checked_class = ProcessStep
20     do_id_test = True
21     default_init_args = [2, 3, 4]
22
23
24 class TestsWithDB(TestCaseWithDB):
25     """Module tests requiring DB setup."""
26     checked_class = Process
27     test_versioneds = {'title': str, 'description': str, 'effort': float}
28
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]:
33             p.save(self.db_conn)
34         return p1, p2, p3
35
36     def p_of_conditions(self) -> tuple[Process, list[Condition],
37                                        list[Condition], list[Condition]]:
38         """Return Process and its three Condition sets."""
39         p = Process(None)
40         c1, c2, c3 = Condition(None), Condition(None), Condition(None)
41         for c in [c1, c2, c3]:
42             c.save(self.db_conn)
43         assert isinstance(c1.id_, int)
44         assert isinstance(c2.id_, int)
45         assert isinstance(c3.id_, int)
46         set_1 = [c1, c2]
47         set_2 = [c2, c3]
48         set_3 = [c1, c3]
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)])
55         p.save(self.db_conn)
56         return p, set_1, set_2, set_3
57
58     def test_Process_conditions_saving(self) -> None:
59         """Test .save/.save_core."""
60         p, set1, set2, set3 = self.p_of_conditions()
61         p.uncache()
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))
66
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()
74         p.save(self.db_conn)
75         assert isinstance(p.id_, int)
76         for row in self.db_conn.row_where(self.checked_class.table_name,
77                                           'id', p.id_):
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))
83
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,
113                                                {3: step_3}, False)
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])
124
125     def test_Process_conditions(self) -> None:
126         """Test setting Process.conditions/enables/disables."""
127         p = Process(None)
128         p.save(self.db_conn)
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])
144
145     def test_Process_by_id(self) -> None:
146         """Test .by_id(), including creation"""
147         self.check_by_id()
148
149     def test_Process_all(self) -> None:
150         """Test .all()."""
151         self.check_all()
152
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)])
156
157     def test_Process_versioned_attributes_singularity(self) -> None:
158         """Test behavior of VersionedAttributes on saving (with .title)."""
159         self.check_versioned_singularity()
160
161     def test_Process_removal(self) -> None:
162         """Test removal of Processes and ProcessSteps."""
163         self.check_remove()
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)
187
188
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,
193                            'parent_step_id': 4}
194
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)
198
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)
202
203     def test_ProcessStep_remove(self) -> None:
204         """Test .remove and unsetting of owner's .explicit_steps entry."""
205         p1 = Process(None)
206         p2 = Process(None)
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([])
215
216
217 class TestsWithServer(TestCaseWithServer):
218     """Module tests against our HTTP server/handler (and database)."""
219
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},
231                         '/process?id=', 400)
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')
245
246     def test_do_GET(self) -> None:
247         """Test /process and /processes response codes."""
248         self.post_process()
249         self.check_get_defaults('/process')
250         self.check_get('/processes', 200)