+ """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"""
+ def add_step(proc: Process,
+ steps_proc: list[tuple[int | None, int, int | None]],
+ step_tuple: tuple[int | None, int, int | None],
+ expected_id: int) -> None:
+ steps_proc += [step_tuple]
+ proc.set_steps(self.db_conn, steps_proc)
+ steps_proc[-1] = (expected_id, step_tuple[1], step_tuple[2])
+ p1, p2, p3 = self.three_processes()
+ assert isinstance(p1.id_, int)
+ assert isinstance(p2.id_, int)
+ assert isinstance(p3.id_, int)
+ steps_p1: list[tuple[int | None, int, int | None]] = []
+ add_step(p1, steps_p1, (None, p2.id_, None), 1)
+ 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(p1, steps_p1, (None, p3.id_, None), 2)
+ step_2 = p1.explicit_steps[-1]
+ p1_dict[2] = ProcessStepsNode(p3, None, True, {}, False)
+ self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
+ steps_p2: list[tuple[int | None, int, int | None]] = []
+ add_step(p2, steps_p2, (None, p3.id_, None), 3)
+ p1_dict[1].steps[3] = ProcessStepsNode(p3, None, False, {}, False)
+ self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
+ add_step(p1, steps_p1, (None, p2.id_, step_2.id_), 4)
+ step_3 = ProcessStepsNode(p3, None, False, {}, True)
+ p1_dict[2].steps[4] = ProcessStepsNode(p2, step_2.id_, True,
+ {3: step_3}, False)
+ self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
+ add_step(p1, steps_p1, (None, p3.id_, 999), 5)
+ p1_dict[5] = ProcessStepsNode(p3, None, True, {}, False)
+ self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict)
+ add_step(p1, steps_p1, (None, p3.id_, 3), 6)
+ 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])
+
+ 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])