home · contact · privacy
c3b1144de7563d956e5fd3a6d6474cb6180e726a
[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
13     def test_Process_id_setting(self) -> None:
14         """Test .id_ being set and its legal range being enforced."""
15         self.check_id_setting()
16
17     def test_Process_versioned_defaults(self) -> None:
18         """Test defaults of VersionedAttributes."""
19         self.check_versioned_defaults({
20             'title': 'UNNAMED',
21             'description': '',
22             'effort': 1.0})
23
24
25 class TestsWithDB(TestCaseWithDB):
26     """Module tests requiring DB setup."""
27     checked_class = Process
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 test_Process_saving_and_caching(self) -> None:
37         """Test .save/.save_core."""
38         kwargs = {'id_': 1}
39         self.check_saving_and_caching(**kwargs)
40         p = Process(None)
41         p.title.set('t1')
42         p.title.set('t2')
43         p.description.set('d1')
44         p.description.set('d2')
45         p.effort.set(0.5)
46         p.effort.set(1.5)
47         c1, c2, c3 = Condition(None), Condition(None), Condition(None)
48         for c in [c1, c2, c3]:
49             c.save(self.db_conn)
50         assert isinstance(c1.id_, int)
51         assert isinstance(c2.id_, int)
52         assert isinstance(c3.id_, int)
53         p.set_conditions(self.db_conn, [c1.id_, c2.id_])
54         p.set_enables(self.db_conn, [c2.id_, c3.id_])
55         p.set_disables(self.db_conn, [c1.id_, c3.id_])
56         p.save(self.db_conn)
57         r = Process.by_id(self.db_conn, p.id_)
58         self.assertEqual(sorted(r.title.history.values()), ['t1', 't2'])
59         self.assertEqual(sorted(r.description.history.values()), ['d1', 'd2'])
60         self.assertEqual(sorted(r.effort.history.values()), [0.5, 1.5])
61         self.assertEqual(sorted(r.conditions), sorted([c1, c2]))
62         self.assertEqual(sorted(r.enables), sorted([c2, c3]))
63         self.assertEqual(sorted(r.disables), sorted([c1, c3]))
64
65     def test_Process_steps(self) -> None:
66         """Test addition, nesting, and non-recursion of ProcessSteps"""
67         def add_step(proc: Process,
68                      steps_proc: list[tuple[int | None, int, int | None]],
69                      step_tuple: tuple[int | None, int, int | None],
70                      expected_id: int) -> None:
71             steps_proc += [step_tuple]
72             proc.set_steps(self.db_conn, steps_proc)
73             steps_proc[-1] = (expected_id, step_tuple[1], step_tuple[2])
74         p1, p2, p3 = self.three_processes()
75         assert isinstance(p1.id_, int)
76         assert isinstance(p2.id_, int)
77         assert isinstance(p3.id_, int)
78         steps_p1: list[tuple[int | None, int, int | None]] = []
79         add_step(p1, steps_p1, (None, p2.id_, None), 1)
80         p1_dict: dict[int, ProcessStepsNode] = {}
81         p1_dict[1] = ProcessStepsNode(p2, None, True, {}, False)
82         self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
83         add_step(p1, steps_p1, (None, p3.id_, None), 2)
84         step_2 = p1.explicit_steps[-1]
85         p1_dict[2] = ProcessStepsNode(p3, None, True, {}, False)
86         self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
87         steps_p2: list[tuple[int | None, int, int | None]] = []
88         add_step(p2, steps_p2, (None, p3.id_, None), 3)
89         p1_dict[1].steps[3] = ProcessStepsNode(p3, None, False, {}, False)
90         self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
91         add_step(p1, steps_p1, (None, p2.id_, step_2.id_), 4)
92         step_3 = ProcessStepsNode(p3, None, False, {}, True)
93         p1_dict[2].steps[4] = ProcessStepsNode(p2, step_2.id_, True,
94                                                {3: step_3}, False)
95         self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
96         add_step(p1, steps_p1, (None, p3.id_, 999), 5)
97         p1_dict[5] = ProcessStepsNode(p3, None, True, {}, False)
98         self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
99         add_step(p1, steps_p1, (None, p3.id_, 3), 6)
100         p1_dict[6] = ProcessStepsNode(p3, None, True, {}, False)
101         self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
102         self.assertEqual(p1.used_as_step_by(self.db_conn), [])
103         self.assertEqual(p2.used_as_step_by(self.db_conn), [p1])
104         self.assertEqual(p3.used_as_step_by(self.db_conn), [p1, p2])
105
106     def test_Process_conditions(self) -> None:
107         """Test setting Process.conditions/enables/disables."""
108         p = Process(None)
109         p.save(self.db_conn)
110         for target in ('conditions', 'enables', 'disables'):
111             method = getattr(p, f'set_{target}')
112             c1, c2 = Condition(None), Condition(None)
113             c1.save(self.db_conn)
114             c2.save(self.db_conn)
115             assert isinstance(c1.id_, int)
116             assert isinstance(c2.id_, int)
117             method(self.db_conn, [])
118             self.assertEqual(getattr(p, target), [])
119             method(self.db_conn, [c1.id_])
120             self.assertEqual(getattr(p, target), [c1])
121             method(self.db_conn, [c2.id_])
122             self.assertEqual(getattr(p, target), [c2])
123             method(self.db_conn, [c1.id_, c2.id_])
124             self.assertEqual(getattr(p, target), [c1, c2])
125
126     def test_Process_by_id(self) -> None:
127         """Test .by_id(), including creation"""
128         self.check_by_id()
129
130     def test_Process_all(self) -> None:
131         """Test .all()."""
132         self.check_all()
133
134     def test_Process_singularity(self) -> None:
135         """Test pointers made for single object keep pointing to it."""
136         self.check_singularity('conditions', [Condition(None)])
137
138     def test_Process_versioned_attributes_singularity(self) -> None:
139         """Test behavior of VersionedAttributes on saving (with .title)."""
140         self.check_versioned_singularity()
141
142     def test_Process_removal(self) -> None:
143         """Test removal of Processes and ProcessSteps."""
144         self.check_remove()
145         p1, p2, p3 = self.three_processes()
146         assert isinstance(p1.id_, int)
147         assert isinstance(p2.id_, int)
148         assert isinstance(p3.id_, int)
149         p2.set_steps(self.db_conn, [(None, p1.id_, None)])
150         with self.assertRaises(HandledException):
151             p1.remove(self.db_conn)
152         step = p2.explicit_steps[0]
153         p2.set_steps(self.db_conn, [])
154         with self.assertRaises(NotFoundException):
155             ProcessStep.by_id(self.db_conn, step.id_)
156         p1.remove(self.db_conn)
157         p2.set_steps(self.db_conn, [(None, p3.id_, None)])
158         step = p2.explicit_steps[0]
159         p2.remove(self.db_conn)
160         with self.assertRaises(NotFoundException):
161             ProcessStep.by_id(self.db_conn, step.id_)
162         todo = Todo(None, p3, False, '2024-01-01')
163         todo.save(self.db_conn)
164         with self.assertRaises(HandledException):
165             p3.remove(self.db_conn)
166         todo.remove(self.db_conn)
167         p3.remove(self.db_conn)
168
169
170 class TestsWithDBForProcessStep(TestCaseWithDB):
171     """Module tests requiring DB setup."""
172     checked_class = ProcessStep
173
174     def test_ProcessStep_saving_and_caching(self) -> None:
175         """Test .save/.save_core."""
176         kwargs = {'id_': 1,
177                   'owner_id': 2,
178                   'step_process_id': 3,
179                   'parent_step_id': 4}
180         self.check_saving_and_caching(**kwargs)
181
182     def test_ProcessStep_from_table_row(self) -> None:
183         """Test .from_table_row() properly reads in class from DB"""
184         self.check_from_table_row(2, 3, None)
185
186     def test_ProcessStep_singularity(self) -> None:
187         """Test pointers made for single object keep pointing to it."""
188         self.check_singularity('parent_step_id', 1, 2, 3, None)
189
190     def test_ProcessStep_remove(self) -> None:
191         """Test .remove and unsetting of owner's .explicit_steps entry."""
192         p1 = Process(None)
193         p2 = Process(None)
194         p1.save(self.db_conn)
195         p2.save(self.db_conn)
196         assert isinstance(p2.id_, int)
197         p1.set_steps(self.db_conn, [(None, p2.id_, None)])
198         step = p1.explicit_steps[0]
199         step.remove(self.db_conn)
200         self.assertEqual(p1.explicit_steps, [])
201         self.check_storage([])
202
203
204 class TestsWithServer(TestCaseWithServer):
205     """Module tests against our HTTP server/handler (and database)."""
206
207     def test_do_POST_process(self) -> None:
208         """Test POST /process and its effect on the database."""
209         self.assertEqual(0, len(Process.all(self.db_conn)))
210         form_data = self.post_process()
211         self.assertEqual(1, len(Process.all(self.db_conn)))
212         self.check_post(form_data, '/process?id=FOO', 400)
213         self.check_post(form_data | {'effort': 'foo'}, '/process?id=', 400)
214         self.check_post({}, '/process?id=', 400)
215         self.check_post({'title': '', 'description': ''}, '/process?id=', 400)
216         self.check_post({'title': '', 'effort': 1.1}, '/process?id=', 400)
217         self.check_post({'description': '', 'effort': 1.0},
218                         '/process?id=', 400)
219         self.assertEqual(1, len(Process.all(self.db_conn)))
220         form_data = {'title': 'foo', 'description': 'foo', 'effort': 1.0}
221         self.post_process(2, form_data | {'condition': []})
222         self.check_post(form_data | {'condition': [1]}, '/process?id=', 404)
223         self.check_post({'title': 'foo', 'description': 'foo'},
224                         '/condition', 302, '/condition?id=1')
225         self.post_process(3, form_data | {'condition': [1]})
226         self.post_process(4, form_data | {'disables': [1]})
227         self.post_process(5, form_data | {'enables': [1]})
228         form_data['delete'] = ''
229         self.check_post(form_data, '/process?id=', 404)
230         self.check_post(form_data, '/process?id=6', 404)
231         self.check_post(form_data, '/process?id=5', 302, '/processes')
232
233     def test_do_GET(self) -> None:
234         """Test /process and /processes response codes."""
235         self.post_process()
236         self.check_get_defaults('/process')
237         self.check_get('/processes', 200)