home · contact · privacy
Fix minor ProcessStep POST handling bugs.
[plomtask] / tests / processes.py
index 861600821e5095397be3633cd8cca00e1a9b0742..127a3f680d4a674030b1be83756f12921a78d4e5 100644 (file)
@@ -9,31 +9,22 @@ from plomtask.todos import Todo
 class TestsSansDB(TestCaseSansDB):
     """Module tests not requiring DB setup."""
     checked_class = Process
-
-    def test_Process_id_setting(self) -> None:
-        """Test .id_ being set and its legal range being enforced."""
-        self.check_id_setting()
-
-    def test_Process_versioned_defaults(self) -> None:
-        """Test defaults of VersionedAttributes."""
-        self.check_versioned_defaults({
-            'title': 'UNNAMED',
-            'description': '',
-            'effort': 1.0})
+    do_id_test = True
+    versioned_defaults_to_test = {'title': 'UNNAMED', 'description': '',
+                                  'effort': 1.0}
 
 
 class TestsSansDBProcessStep(TestCaseSansDB):
     """Module tests not requiring DB setup."""
     checked_class = ProcessStep
-
-    def test_ProcessStep_id_setting(self) -> None:
-        """Test .id_ being set and its legal range being enforced."""
-        self.check_id_setting(2, 3, 4)
+    do_id_test = True
+    default_init_args = [2, 3, 4]
 
 
 class TestsWithDB(TestCaseWithDB):
     """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."""
@@ -42,34 +33,53 @@ class TestsWithDB(TestCaseWithDB):
             p.save(self.db_conn)
         return p1, p2, p3
 
-    def test_Process_saving_and_caching(self) -> None:
-        """Test .save/.save_core."""
-        kwargs = {'id_': 1}
-        self.check_saving_and_caching(**kwargs)
+    def p_of_conditions(self) -> tuple[Process, list[Condition],
+                                       list[Condition], list[Condition]]:
+        """Return Process and its three Condition sets."""
         p = Process(None)
-        p.title.set('t1')
-        p.title.set('t2')
-        p.description.set('d1')
-        p.description.set('d2')
-        p.effort.set(0.5)
-        p.effort.set(1.5)
         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)
-        p.set_conditions(self.db_conn, [c1.id_, c2.id_])
-        p.set_enables(self.db_conn, [c2.id_, c3.id_])
-        p.set_disables(self.db_conn, [c1.id_, c3.id_])
+        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.title.history.values()), ['t1', 't2'])
-        self.assertEqual(sorted(r.description.history.values()), ['d1', 'd2'])
-        self.assertEqual(sorted(r.effort.history.values()), [0.5, 1.5])
-        self.assertEqual(sorted(r.conditions), sorted([c1, c2]))
-        self.assertEqual(sorted(r.enables), sorted([c2, c3]))
-        self.assertEqual(sorted(r.disables), sorted([c1, c3]))
+        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"""
@@ -179,14 +189,8 @@ class TestsWithDB(TestCaseWithDB):
 class TestsWithDBForProcessStep(TestCaseWithDB):
     """Module tests requiring DB setup."""
     checked_class = ProcessStep
-
-    def test_ProcessStep_saving_and_caching(self) -> None:
-        """Test .save/.save_core."""
-        kwargs = {'id_': 1,
-                  'owner_id': 2,
-                  'step_process_id': 3,
-                  'parent_step_id': 4}
-        self.check_saving_and_caching(**kwargs)
+    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"""
@@ -239,8 +243,59 @@ class TestsWithServer(TestCaseWithServer):
         self.check_post(form_data, '/process?id=6', 404)
         self.check_post(form_data, '/process?id=5', 302, '/processes')
 
+    def test_do_POST_process_steps(self) -> None:
+        """Test behavior of ProcessStep posting."""
+        form_data_1 = self.post_process(1)
+        self.post_process(2)
+        self.post_process(3)
+        form_data_1['new_top_step'] = [2]
+        self.post_process(1, form_data_1)
+        retrieved_process = Process.by_id(self.db_conn, 1)
+        self.assertEqual(len(retrieved_process.explicit_steps), 1)
+        retrieved_step = retrieved_process.explicit_steps[0]
+        self.assertEqual(retrieved_step.step_process_id, 2)
+        self.assertEqual(retrieved_step.owner_id, 1)
+        self.assertEqual(retrieved_step.parent_step_id, None)
+        form_data_1['new_top_step'] = []
+        self.post_process(1, form_data_1)
+        retrieved_process = Process.by_id(self.db_conn, 1)
+        self.assertEqual(retrieved_process.explicit_steps, [])
+        with self.assertRaises(NotFoundException):
+            ProcessStep.by_id(self.db_conn, retrieved_step.id_)
+        form_data_1['new_top_step'] = [3]
+        self.post_process(1, form_data_1)
+        retrieved_process = Process.by_id(self.db_conn, 1)
+        retrieved_step = retrieved_process.explicit_steps[0]
+        self.assertEqual(retrieved_step.step_process_id, 3)
+        self.assertEqual(retrieved_step.owner_id, 1)
+        self.assertEqual(retrieved_step.parent_step_id, None)
+        form_data_1['new_top_step'] = []
+        form_data_1['steps'] = [retrieved_step.id_]
+        self.post_process(1, form_data_1)
+        retrieved_process = Process.by_id(self.db_conn, 1)
+        self.assertEqual(retrieved_process.explicit_steps, [])
+        form_data_1['steps'] = []
+        form_data_1['keep_step'] = [retrieved_step.id_]
+        self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1')
+        form_data_1['steps'] = [retrieved_step.id_]
+        form_data_1['keep_step'] = [retrieved_step.id_]
+        self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1')
+        form_data_1[f'step_{retrieved_step.id_}_process_id'] = [2]
+        self.post_process(1, form_data_1)
+        retrieved_process = Process.by_id(self.db_conn, 1)
+        self.assertEqual(len(retrieved_process.explicit_steps), 1)
+        retrieved_step = retrieved_process.explicit_steps[0]
+        self.assertEqual(retrieved_step.step_process_id, 2)
+        self.assertEqual(retrieved_step.owner_id, 1)
+        self.assertEqual(retrieved_step.parent_step_id, None)
+        form_data_1['new_top_step'] = ['foo']
+        form_data_1['steps'] = []
+        form_data_1['keep_step'] = []
+        self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1')
+        retrieved_process = Process.by_id(self.db_conn, 1)
+        self.assertEqual(len(retrieved_process.explicit_steps), 1)
+
     def test_do_GET(self) -> None:
         """Test /process and /processes response codes."""
-        self.post_process()
         self.check_get_defaults('/process')
         self.check_get('/processes', 200)