+ """Module tests requiring DB setup."""
+ checked_class = Process
+ test_versioneds = {'title': str, 'description': str, 'effort': float}
+
+ def three_processes(self) -> tuple[Process, Process, Process]:
+ """Return three saved processes."""
+ p1, p2, p3 = Process(None), Process(None), Process(None)
+ for p in [p1, p2, p3]:
+ p.save(self.db_conn)
+ return p1, p2, p3
+
+ def p_of_conditions(self) -> tuple[Process, list[Condition],
+ list[Condition], list[Condition]]:
+ """Return Process and its three Condition sets."""
+ p = Process(None)
+ c1, c2, c3 = Condition(None), Condition(None), Condition(None)
+ for c in [c1, c2, c3]:
+ c.save(self.db_conn)
+ assert isinstance(c1.id_, int)
+ assert isinstance(c2.id_, int)
+ assert isinstance(c3.id_, int)
+ set_1 = [c1, c2]
+ set_2 = [c2, c3]
+ set_3 = [c1, c3]
+ p.set_conditions(self.db_conn, [c.id_ for c in set_1
+ if isinstance(c.id_, int)])
+ p.set_enables(self.db_conn, [c.id_ for c in set_2
+ if isinstance(c.id_, int)])
+ p.set_disables(self.db_conn, [c.id_ for c in set_3
+ if isinstance(c.id_, int)])
+ p.save(self.db_conn)
+ return p, set_1, set_2, set_3
+
+ def test_Process_conditions_saving(self) -> None:
+ """Test .save/.save_core."""
+ p, set1, set2, set3 = self.p_of_conditions()
+ p.uncache()
+ r = Process.by_id(self.db_conn, p.id_)
+ self.assertEqual(sorted(r.conditions), sorted(set1))
+ self.assertEqual(sorted(r.enables), sorted(set2))
+ self.assertEqual(sorted(r.disables), sorted(set3))
+
+ def test_Process_from_table_row(self) -> None:
+ """Test .from_table_row() properly reads in class from DB"""
+ self.check_from_table_row()
+ self.check_versioned_from_table_row('title', str)
+ self.check_versioned_from_table_row('description', str)
+ self.check_versioned_from_table_row('effort', float)
+ p, set1, set2, set3 = self.p_of_conditions()
+ p.save(self.db_conn)
+ assert isinstance(p.id_, int)
+ for row in self.db_conn.row_where(self.checked_class.table_name,
+ 'id', p.id_):
+ # pylint: disable=no-member
+ r = Process.from_table_row(self.db_conn, row)
+ self.assertEqual(sorted(r.conditions), sorted(set1))
+ self.assertEqual(sorted(r.enables), sorted(set2))
+ self.assertEqual(sorted(r.disables), sorted(set3))
+
+ def test_Process_steps(self) -> None:
+ """Test addition, nesting, and non-recursion of ProcessSteps"""
+ # pylint: disable=too-many-locals
+ p1, p2, p3 = self.three_processes()
+ assert isinstance(p1.id_, int)
+ assert isinstance(p2.id_, int)
+ assert isinstance(p3.id_, int)
+ steps_p1: list[ProcessStep] = []
+ # add step of process p2 as first (top-level) step to p1
+ s_p2_to_p1 = ProcessStep(None, p1.id_, p2.id_, None)
+ steps_p1 += [s_p2_to_p1]
+ p1.set_steps(self.db_conn, steps_p1)
+ p1_dict: dict[int, ProcessStepsNode] = {}
+ p1_dict[1] = ProcessStepsNode(p2, None, True, {}, False)
+ self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
+ # add step of process p3 as second (top-level) step to p1
+ s_p3_to_p1 = ProcessStep(None, p1.id_, p3.id_, None)
+ steps_p1 += [s_p3_to_p1]
+ p1.set_steps(self.db_conn, steps_p1)
+ p1_dict[2] = ProcessStepsNode(p3, None, True, {}, False)
+ self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
+ # add step of process p3 as first (top-level) step to p2,
+ steps_p2: list[ProcessStep] = []
+ s_p3_to_p2 = ProcessStep(None, p2.id_, p3.id_, None)
+ steps_p2 += [s_p3_to_p2]
+ p2.set_steps(self.db_conn, steps_p2)
+ # expect it as implicit sub-step of p1's second (p3) step
+ p2_dict = {3: ProcessStepsNode(p3, None, False, {}, False)}
+ p1_dict[1].steps[3] = p2_dict[3]
+ self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
+ # add step of process p2 as explicit sub-step to p1's second sub-step
+ s_p2_to_p1_first = ProcessStep(None, p1.id_, p2.id_, s_p3_to_p1.id_)
+ steps_p1 += [s_p2_to_p1_first]
+ p1.set_steps(self.db_conn, steps_p1)
+ seen_3 = ProcessStepsNode(p3, None, False, {}, True)
+ p1_dict[2].steps[4] = ProcessStepsNode(p2, s_p3_to_p1.id_, True,
+ {3: seen_3}, False)
+ self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
+ # add step of process p3 as explicit sub-step to non-existing p1
+ # sub-step (of id=999), expect it to become another p1 top-level step
+ s_p3_to_p1_999 = ProcessStep(None, p1.id_, p3.id_, 999)
+ steps_p1 += [s_p3_to_p1_999]
+ p1.set_steps(self.db_conn, steps_p1)
+ p1_dict[5] = ProcessStepsNode(p3, None, True, {}, False)
+ self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
+ # add step of process p3 as explicit sub-step to p1's implicit p3
+ # sub-step, expect it to become another p1 top-level step
+ s_p3_to_p1_impl_p3 = ProcessStep(None, p1.id_, p3.id_, s_p3_to_p2.id_)
+ steps_p1 += [s_p3_to_p1_impl_p3]
+ p1.set_steps(self.db_conn, steps_p1)
+ p1_dict[6] = ProcessStepsNode(p3, None, True, {}, False)
+ self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
+ self.assertEqual(p1.used_as_step_by(self.db_conn), [])
+ self.assertEqual(p2.used_as_step_by(self.db_conn), [p1])
+ self.assertEqual(p3.used_as_step_by(self.db_conn), [p1, p2])
+ # # add step of process p3 as explicit sub-step to p1's first sub-step,
+ # # expect it to eliminate implicit p3 sub-step
+ # s_p3_to_p1_first_explicit = ProcessStep(None, p1.id_, p3.id_,
+ # s_p2_to_p1.id_)
+ # p1_dict[1].steps = {7: ProcessStepsNode(p3, 1, True, {}, False)}
+ # p1_dict[2].steps[4].steps[3].seen = False
+ # steps_p1 += [s_p3_to_p1_first_explicit]
+ # p1.set_steps(self.db_conn, steps_p1)
+ # self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
+ # ensure implicit steps non-top explicit steps are shown
+ s_p3_to_p2_first = ProcessStep(None, p2.id_, p3.id_, s_p3_to_p2.id_)
+ steps_p2 += [s_p3_to_p2_first]
+ p2.set_steps(self.db_conn, steps_p2)
+ p1_dict[1].steps[3].steps[7] = ProcessStepsNode(p3, 3, False, {},
+ False)
+ p1_dict[2].steps[4].steps[3].steps[7] = ProcessStepsNode(p3, 3, False,
+ {}, True)
+ self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
+
+ def test_Process_conditions(self) -> None:
+ """Test setting Process.conditions/enables/disables."""
+ p = Process(None)
+ p.save(self.db_conn)
+ for target in ('conditions', 'enables', 'disables'):
+ method = getattr(p, f'set_{target}')
+ c1, c2 = Condition(None), Condition(None)
+ c1.save(self.db_conn)
+ c2.save(self.db_conn)
+ assert isinstance(c1.id_, int)
+ assert isinstance(c2.id_, int)
+ method(self.db_conn, [])
+ self.assertEqual(getattr(p, target), [])
+ method(self.db_conn, [c1.id_])
+ self.assertEqual(getattr(p, target), [c1])
+ method(self.db_conn, [c2.id_])
+ self.assertEqual(getattr(p, target), [c2])
+ method(self.db_conn, [c1.id_, c2.id_])
+ self.assertEqual(getattr(p, target), [c1, c2])
+
+ def test_Process_by_id(self) -> None:
+ """Test .by_id(), including creation"""
+ self.check_by_id()
+
+ def test_Process_all(self) -> None:
+ """Test .all()."""
+ self.check_all()
+
+ def test_Process_singularity(self) -> None:
+ """Test pointers made for single object keep pointing to it."""
+ self.check_singularity('conditions', [Condition(None)])
+
+ def test_Process_versioned_attributes_singularity(self) -> None:
+ """Test behavior of VersionedAttributes on saving (with .title)."""
+ self.check_versioned_singularity()
+
+ def test_Process_removal(self) -> None:
+ """Test removal of Processes and ProcessSteps."""
+ self.check_remove()
+ p1, p2, p3 = self.three_processes()
+ assert isinstance(p1.id_, int)
+ assert isinstance(p2.id_, int)
+ assert isinstance(p3.id_, int)
+ step = ProcessStep(None, p2.id_, p1.id_, None)
+ p2.set_steps(self.db_conn, [step])
+ with self.assertRaises(HandledException):
+ p1.remove(self.db_conn)
+ p2.set_steps(self.db_conn, [])
+ with self.assertRaises(NotFoundException):
+ ProcessStep.by_id(self.db_conn, step.id_)
+ p1.remove(self.db_conn)
+ step = ProcessStep(None, p2.id_, p3.id_, None)
+ p2.set_steps(self.db_conn, [step])
+ p2.remove(self.db_conn)
+ with self.assertRaises(NotFoundException):
+ ProcessStep.by_id(self.db_conn, step.id_)
+ todo = Todo(None, p3, False, '2024-01-01')
+ todo.save(self.db_conn)
+ with self.assertRaises(HandledException):
+ p3.remove(self.db_conn)
+ todo.remove(self.db_conn)
+ p3.remove(self.db_conn)
+
+
+class TestsWithDBForProcessStep(TestCaseWithDB):
+ """Module tests requiring DB setup."""
+ checked_class = ProcessStep
+ default_init_kwargs = {'owner_id': 2, 'step_process_id': 3,
+ 'parent_step_id': 4}
+
+ def test_ProcessStep_from_table_row(self) -> None:
+ """Test .from_table_row() properly reads in class from DB"""
+ self.check_from_table_row(2, 3, None)
+
+ def test_ProcessStep_singularity(self) -> None:
+ """Test pointers made for single object keep pointing to it."""
+ self.check_singularity('parent_step_id', 1, 2, 3, None)
+
+ def test_ProcessStep_remove(self) -> None:
+ """Test .remove and unsetting of owner's .explicit_steps entry."""
+ p1 = Process(None)
+ p2 = Process(None)
+ p1.save(self.db_conn)
+ p2.save(self.db_conn)
+ assert isinstance(p1.id_, int)
+ assert isinstance(p2.id_, int)
+ step = ProcessStep(None, p1.id_, p2.id_, None)
+ p1.set_steps(self.db_conn, [step])
+ step.remove(self.db_conn)
+ self.assertEqual(p1.explicit_steps, [])
+ self.check_storage([])