home · contact · privacy
For Todos, on Save check for auto-deletion by .effort < 0, and on removal check if...
[plomtask] / tests / todos.py
index a8219fa7506a8c4c7649dd4eb4952948dd14c9f8..6ce5d6955be068355285c1532e12cd3ffb9e4204 100644 (file)
@@ -10,6 +10,8 @@ from plomtask.exceptions import (NotFoundException, BadFormatException,
 class TestsWithDB(TestCaseWithDB):
     """Tests requiring DB, but not server setup."""
     checked_class = Todo
+    default_init_kwargs = {'process': None, 'is_done': False,
+                           'date': '2024-01-01'}
 
     def setUp(self) -> None:
         super().setUp()
@@ -21,6 +23,7 @@ class TestsWithDB(TestCaseWithDB):
         self.cond1.save(self.db_conn)
         self.cond2 = Condition(None)
         self.cond2.save(self.db_conn)
+        self.default_init_kwargs['process'] = self.proc
 
     def test_Todo_init(self) -> None:
         """Test creation of Todo and what they default to."""
@@ -42,17 +45,6 @@ class TestsWithDB(TestCaseWithDB):
         self.assertEqual(todo_yes_id.enables, [])
         self.assertEqual(todo_yes_id.disables, [])
 
-    def test_Todo_saving_and_caching(self) -> None:
-        """Test .save."""
-        kwargs = {'id_': 1,
-                  'process': self.proc,
-                  'is_done': False,
-                  'date': self.date1}
-        self.check_saving_and_caching(**kwargs)
-        todo = Todo(None, self.proc, False, self.date1)
-        todo.save(self.db_conn)
-        self.assertEqual(todo.id_, 2)
-
     def test_Todo_by_id(self) -> None:
         """Test findability of Todos."""
         todo = Todo(1, self.proc, False, self.date1)
@@ -226,6 +218,26 @@ class TestsWithDB(TestCaseWithDB):
             Todo.by_id(self.db_conn, todo_1.id_)
         self.assertEqual(todo_0.children, [])
         self.assertEqual(todo_2.parents, [])
+        todo_2.comment = 'foo'
+        with self.assertRaises(HandledException):
+            todo_2.remove(self.db_conn)
+        todo_2.comment = ''
+        todo_2.effort = 5
+        with self.assertRaises(HandledException):
+            todo_2.remove(self.db_conn)
+
+    def test_Todo_autoremoval(self) -> None:
+        """"Test automatic removal for Todo.effort < 0."""
+        todo_1 = Todo(None, self.proc, False, self.date1)
+        todo_1.save(self.db_conn)
+        todo_1.comment = 'foo'
+        todo_1.effort = -0.1
+        todo_1.save(self.db_conn)
+        Todo.by_id(self.db_conn, todo_1.id_)
+        todo_1.comment = ''
+        todo_1.save(self.db_conn)
+        with self.assertRaises(NotFoundException):
+            Todo.by_id(self.db_conn, todo_1.id_)
 
 
 class TestsWithServer(TestCaseWithServer):
@@ -237,7 +249,7 @@ class TestsWithServer(TestCaseWithServer):
         self.post_process(2)
         proc = Process.by_id(self.db_conn, 1)
         proc2 = Process.by_id(self.db_conn, 2)
-        form_data = {'comment': ''}
+        form_data = {'day_comment': ''}
         self.check_post(form_data, '/day?date=2024-01-01', 302)
         self.assertEqual(Todo.by_date(self.db_conn, '2024-01-01'), [])
         form_data['new_todo'] = str(proc.id_)
@@ -264,7 +276,7 @@ class TestsWithServer(TestCaseWithServer):
             return Todo.by_date(self.db_conn, '2024-01-01')[0]
         # test minimum
         self.post_process()
-        self.check_post({'comment': '', 'new_todo': 1},
+        self.check_post({'day_comment': '', 'new_todo': 1},
                         '/day?date=2024-01-01', 302)
         # test posting to bad URLs
         self.check_post({}, '/todo=', 404)
@@ -287,7 +299,7 @@ class TestsWithServer(TestCaseWithServer):
         self.check_post({'adopt': 1}, '/todo?id=1', 400)
         self.check_post({'adopt': 2}, '/todo?id=1', 404)
         # test posting second todo of same process
-        self.check_post({'comment': '', 'new_todo': 1},
+        self.check_post({'day_comment': '', 'new_todo': 1},
                         '/day?date=2024-01-01', 302)
         # test todo 1 adopting todo 2
         todo1 = post_and_reload({'adopt': 2})
@@ -313,7 +325,7 @@ class TestsWithServer(TestCaseWithServer):
         """Test Todos posted to Day view may adopt existing Todos."""
         form_data = self.post_process()
         form_data = self.post_process(2, form_data | {'new_top_step': 1})
-        form_data = {'comment': '', 'new_todo': 1}
+        form_data = {'day_comment': '', 'new_todo': 1}
         self.check_post(form_data, '/day?date=2024-01-01', 302)
         form_data['new_todo'] = 2
         self.check_post(form_data, '/day?date=2024-01-01', 302)
@@ -324,10 +336,56 @@ class TestsWithServer(TestCaseWithServer):
         self.assertEqual(todo2.children, [todo1])
         self.assertEqual(todo2.parents, [])
 
+    def test_do_POST_day_todo_multiple(self) -> None:
+        """Test multiple Todos can be posted to Day view."""
+        form_data = self.post_process()
+        form_data = self.post_process(2)
+        form_data = {'day_comment': '', 'new_todo': [1, 2]}
+        self.check_post(form_data, '/day?date=2024-01-01', 302)
+        todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
+        todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
+        self.assertEqual(todo1.process.id_, 1)
+        self.assertEqual(todo2.process.id_, 2)
+
+    def test_do_POST_day_todo_multiple_inner_adoption(self) -> None:
+        """Test multiple Todos can be posted to Day view w. inner adoption."""
+        form_data = self.post_process()
+        form_data = self.post_process(2, form_data | {'new_top_step': 1})
+        form_data = {'day_comment': '', 'new_todo': [1, 2]}
+        self.check_post(form_data, '/day?date=2024-01-01', 302)
+        todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
+        todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
+        self.assertEqual(todo1.children, [])
+        self.assertEqual(todo1.parents, [todo2])
+        self.assertEqual(todo2.children, [todo1])
+        self.assertEqual(todo2.parents, [])
+        # check process ID order does not affect end result
+        form_data = {'day_comment': '', 'new_todo': [2, 1]}
+        self.check_post(form_data, '/day?date=2024-01-02', 302)
+        todo1 = Todo.by_date(self.db_conn, '2024-01-02')[1]
+        todo2 = Todo.by_date(self.db_conn, '2024-01-02')[0]
+        self.assertEqual(todo1.children, [])
+        self.assertEqual(todo1.parents, [todo2])
+        self.assertEqual(todo2.children, [todo1])
+        self.assertEqual(todo2.parents, [])
+
+    def test_do_POST_day_todo_doneness(self) -> None:
+        """Test Todo doneness can be posted to Day view."""
+        form_data = self.post_process()
+        form_data = {'day_comment': '', 'new_todo': [1]}
+        self.check_post(form_data, '/day?date=2024-01-01', 302)
+        todo = Todo.by_date(self.db_conn, '2024-01-01')[0]
+        form_data = {'day_comment': '', 'todo_id': [1]}
+        self.check_post(form_data, '/day?date=2024-01-01', 302)
+        self.assertEqual(todo.is_done, False)
+        form_data = {'day_comment': '', 'todo_id': [1], 'done': [1]}
+        self.check_post(form_data, '/day?date=2024-01-01', 302)
+        self.assertEqual(todo.is_done, True)
+
     def test_do_GET_todo(self) -> None:
         """Test GET /todo response codes."""
         self.post_process()
-        form_data = {'comment': '', 'new_todo': 1}
+        form_data = {'day_comment': '', 'new_todo': 1}
         self.check_post(form_data, '/day?date=2024-01-01', 302)
         self.check_get('/todo', 400)
         self.check_get('/todo?id=', 400)