home · contact · privacy
On posting new Todo to Day, make missing Todo children not found through adoption.
authorChristian Heller <c.heller@plomlompom.de>
Mon, 22 Apr 2024 05:12:11 +0000 (07:12 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Mon, 22 Apr 2024 05:12:11 +0000 (07:12 +0200)
plomtask/http.py
plomtask/todos.py
tests/todos.py

index deadb21f88c65726aa7544c810922f2712600e36..8f247cd68cbfff0980fba2c97c64b1a3f77776dd 100644 (file)
@@ -201,6 +201,7 @@ class TaskHandler(BaseHTTPRequestHandler):
             todo = Todo(None, process, False, day.date)
             todo.save(self.conn)
             todo.adopt_from(existing_todos)
+            todo.make_missing_children(self.conn)
             todo.save(self.conn)
 
     def do_POST_todo(self) -> None:
index ed78ca947b359286456e7dcc4c664fcb32961c12..80dc97c25ac481089b5760a0c8072af5f755ef08 100644 (file)
@@ -130,12 +130,12 @@ class Todo(BaseModel, ConditionsRelations):
     @property
     def unsatisfied_dependencies(self) -> list[int]:
         """Return Process IDs of .process.explicit_steps not in .children."""
-        child_process_ids = {c.process.id_ for c in self.children}
-        unsatisfied: list[int] = []
-        for process_id in [s.step_process_id
-                           for s in self.process.explicit_steps]:
-            if process_id not in child_process_ids:
-                unsatisfied += [process_id]
+        unsatisfied = [s.step_process_id for s in self.process.explicit_steps
+                       if s.parent_step_id is None]
+        for child_process_id in [c.process.id_ for c in self.children]:
+            if child_process_id in unsatisfied:
+                assert isinstance(child_process_id, int)
+                unsatisfied.remove(child_process_id)
         return unsatisfied
 
     @property
@@ -158,10 +158,19 @@ class Todo(BaseModel, ConditionsRelations):
     def adopt_from(self, todos: list[Todo]) -> None:
         """As far as possible, fill unsatisfied dependencies from todos."""
         for process_id in self.unsatisfied_dependencies:
-            for todo in [t for t in todos if t.process.id_ == process_id]:
+            for todo in [t for t in todos if t.process.id_ == process_id
+                         and t not in self.children]:
                 self.add_child(todo)
                 break
 
+    def make_missing_children(self, db_conn: DatabaseConnection) -> None:
+        """Fill unsatisfied dependencies with new Todos."""
+        for process_id in self.unsatisfied_dependencies:
+            process = Process.by_id(db_conn, process_id)
+            todo = self.__class__(None, process, False, self.date)
+            todo.save(db_conn)
+            self.add_child(todo)
+
     def get_step_tree(self, seen_todos: set[int],
                       seen_conditions: set[int]) -> TodoStepsNode:
         """Return tree of depended-on Todos and Conditions."""
index b5953dc69c0d593113095ddd3e82c463e03ec253..7aed5f83a0eee3e2eb50845c42de9fd57e3aee0f 100644 (file)
@@ -209,6 +209,52 @@ class TestsWithDB(TestCaseWithDB):
         node_2.children.remove(node_6)
         self.assertEqual(todo_1.get_step_tree(set(), set()), node_0)
 
+    def test_Todo_unsatisfied_steps(self) -> None:
+        """Test options of satisfying unfulfilled Process.explicit_steps."""
+        assert isinstance(self.proc.id_, int)
+        todo_1 = Todo(None, self.proc, False, self.date1)
+        todo_1.save(self.db_conn)
+        proc2 = Process(None)
+        proc2.save(self.db_conn)
+        assert isinstance(proc2.id_, int)
+        proc3 = Process(None)
+        proc3.save(self.db_conn)
+        assert isinstance(proc3.id_, int)
+        proc4 = Process(None)
+        proc4.save(self.db_conn)
+        assert isinstance(proc4.id_, int)
+        proc3.set_steps(self.db_conn, [(None, proc4.id_, None)])
+        proc2.set_steps(self.db_conn, [(None, self.proc.id_, None),
+                                       (None, self.proc.id_, None),
+                                       (None, proc3.id_, None)])
+        todo_2 = Todo(None, proc2, False, self.date1)
+        todo_2.save(self.db_conn)
+        # test empty adoption does nothing
+        todo_2.adopt_from([])
+        self.assertEqual(todo_2.children, [])
+        # test basic adoption
+        todo_2.adopt_from([todo_1])
+        self.assertEqual(todo_2.children, [todo_1])
+        self.assertEqual(todo_1.parents, [todo_2])
+        # test making missing children
+        todo_2.make_missing_children(self.db_conn)
+        todo_3 = Todo.by_id(self.db_conn, 3)
+        todo_4 = Todo.by_id(self.db_conn, 4)
+        self.assertEqual(todo_2.children, [todo_1, todo_3, todo_4])
+        self.assertEqual(todo_3.process, self.proc)
+        self.assertEqual(todo_3.parents, [todo_2])
+        self.assertEqual(todo_3.children, [])
+        self.assertEqual(todo_4.process, proc3)
+        self.assertEqual(todo_4.parents, [todo_2])
+        # test .make_missing_children doesn't further than top-level
+        self.assertEqual(todo_4.children, [])
+        # test .make_missing_children lower down the tree
+        todo_4.make_missing_children(self.db_conn)
+        todo_5 = Todo.by_id(self.db_conn, 5)
+        self.assertEqual(todo_5.process, proc4)
+        self.assertEqual(todo_4.children, [todo_5])
+        self.assertEqual(todo_5.parents, [todo_4])
+
     def test_Todo_singularity(self) -> None:
         """Test pointers made for single object keep pointing to it."""
         todo = Todo(None, self.proc, False, self.date1)