home · contact · privacy
Extend Todo tests to also run ID checking from TestCaseSansDB.
[plomtask] / tests / todos.py
index b28ebd8cdda37c973cf468daa2683140f855dd39..b73f5d7fcb7b0a2c53256593e8e9867e86d2b89e 100644 (file)
@@ -1,5 +1,5 @@
 """Test Todos module."""
-from tests.utils import TestCaseWithDB, TestCaseWithServer
+from tests.utils import TestCaseSansDB, TestCaseWithDB, TestCaseWithServer
 from plomtask.todos import Todo, TodoNode
 from plomtask.processes import Process, ProcessStep
 from plomtask.conditions import Condition
@@ -7,11 +7,19 @@ from plomtask.exceptions import (NotFoundException, BadFormatException,
                                  HandledException)
 
 
-class TestsWithDB(TestCaseWithDB):
-    """Tests requiring DB, but not server setup."""
+class TestsWithDB(TestCaseWithDB, TestCaseSansDB):
+    """Tests requiring DB, but not server setup.
+
+    NB: We subclass TestCaseSansDB too, to pull in its .test_id_setting, which
+    for Todo wouldn't run without a DB being set up due to the need for
+    Processes with set IDs.
+    """
     checked_class = Todo
     default_init_kwargs = {'process': None, 'is_done': False,
                            'date': '2024-01-01'}
+    # solely used for TestCaseSansDB.test_id_setting
+    default_init_args = [None, False, '2024-01-01']
+    do_id_test = True
 
     def setUp(self) -> None:
         super().setUp()
@@ -24,6 +32,7 @@ class TestsWithDB(TestCaseWithDB):
         self.cond2 = Condition(None)
         self.cond2.save(self.db_conn)
         self.default_init_kwargs['process'] = self.proc
+        self.default_init_args[0] = self.proc
 
     def test_Todo_init(self) -> None:
         """Test creation of Todo and what they default to."""
@@ -187,9 +196,9 @@ class TestsWithDB(TestCaseWithDB):
         todo_2 = Todo.create_with_children(self.db_conn, proc2.id_, self.date1)
         self.assertEqual(3, len(todo_2.children))
         self.assertEqual(todo_1, todo_2.children[0])
-        self.assertEqual(self.proc, todo_2.children[1].process)
-        self.assertEqual(proc3, todo_2.children[2].process)
-        todo_3 = todo_2.children[2]
+        self.assertEqual(self.proc, todo_2.children[2].process)
+        self.assertEqual(proc3, todo_2.children[1].process)
+        todo_3 = todo_2.children[1]
         self.assertEqual(len(todo_3.children), 1)
         self.assertEqual(todo_3.children[0].process, proc4)
 
@@ -207,9 +216,10 @@ class TestsWithDB(TestCaseWithDB):
         todo_2 = Todo(None, self.proc, False, self.date1)
         todo_2.save(self.db_conn)
         todo_1.add_child(todo_2)
+        todo_1_id = todo_1.id_
         todo_1.remove(self.db_conn)
         with self.assertRaises(NotFoundException):
-            Todo.by_id(self.db_conn, todo_1.id_)
+            Todo.by_id(self.db_conn, todo_1_id)
         self.assertEqual(todo_0.children, [])
         self.assertEqual(todo_2.parents, [])
         todo_2.comment = 'foo'
@@ -229,9 +239,10 @@ class TestsWithDB(TestCaseWithDB):
         todo_1.save(self.db_conn)
         Todo.by_id(self.db_conn, todo_1.id_)
         todo_1.comment = ''
+        todo_1_id = todo_1.id_
         todo_1.save(self.db_conn)
         with self.assertRaises(NotFoundException):
-            Todo.by_id(self.db_conn, todo_1.id_)
+            Todo.by_id(self.db_conn, todo_1_id)
 
 
 class TestsWithServer(TestCaseWithServer):
@@ -243,11 +254,11 @@ 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 = {'day_comment': ''}
-        self.check_post(form_data, '/day?date=2024-01-01', 302)
+        form_data = {'day_comment': '', 'make_type': 'full'}
+        self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
         self.assertEqual(Todo.by_date(self.db_conn, '2024-01-01'), [])
         form_data['new_todo'] = str(proc.id_)
-        self.check_post(form_data, '/day?date=2024-01-01', 302)
+        self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
         todos = Todo.by_date(self.db_conn, '2024-01-01')
         self.assertEqual(1, len(todos))
         todo1 = todos[0]
@@ -255,7 +266,7 @@ class TestsWithServer(TestCaseWithServer):
         self.assertEqual(todo1.process.id_, proc.id_)
         self.assertEqual(todo1.is_done, False)
         form_data['new_todo'] = str(proc2.id_)
-        self.check_post(form_data, '/day?date=2024-01-01', 302)
+        self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
         todos = Todo.by_date(self.db_conn, '2024-01-01')
         todo1 = todos[1]
         self.assertEqual(todo1.id_, 2)
@@ -270,8 +281,9 @@ class TestsWithServer(TestCaseWithServer):
             return Todo.by_date(self.db_conn, '2024-01-01')[0]
         # test minimum
         self.post_process()
-        self.check_post({'day_comment': '', 'new_todo': 1},
-                        '/day?date=2024-01-01', 302)
+        self.check_post({'day_comment': '', 'new_todo': 1,
+                         'make_type': 'full'},
+                        '/day?date=2024-01-01&make_type=full', 302)
         # test posting to bad URLs
         self.check_post({}, '/todo=', 404)
         self.check_post({}, '/todo?id=', 400)
@@ -293,8 +305,9 @@ 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({'day_comment': '', 'new_todo': 1},
-                        '/day?date=2024-01-01', 302)
+        self.check_post({'day_comment': '', 'new_todo': 1,
+                         'make_type': 'full'},
+                        '/day?date=2024-01-01&make_type=full', 302)
         # test todo 1 adopting todo 2
         todo1 = post_and_reload({'adopt': 2})
         todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
@@ -319,10 +332,10 @@ 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 = {'day_comment': '', 'new_todo': 1}
-        self.check_post(form_data, '/day?date=2024-01-01', 302)
+        form_data = {'day_comment': '', 'new_todo': 1, 'make_type': 'full'}
+        self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
         form_data['new_todo'] = 2
-        self.check_post(form_data, '/day?date=2024-01-01', 302)
+        self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 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, [])
@@ -334,8 +347,9 @@ class TestsWithServer(TestCaseWithServer):
         """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)
+        form_data = {'day_comment': '', 'new_todo': [1, 2],
+                     'make_type': 'full'}
+        self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 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)
@@ -349,8 +363,9 @@ class TestsWithServer(TestCaseWithServer):
             return t.process.id_
 
         def check_adoption(date: str, new_todos: list[int]) -> None:
-            form_data = {'day_comment': '', 'new_todo': new_todos}
-            self.check_post(form_data, f'/day?date={date}', 302)
+            form_data = {'day_comment': '', 'new_todo': new_todos,
+                         'make_type': 'full'}
+            self.check_post(form_data, f'/day?date={date}&make_type=full', 302)
             day_todos = Todo.by_date(self.db_conn, date)
             day_todos.sort(key=key_order_func)
             todo1 = day_todos[0]
@@ -368,8 +383,9 @@ class TestsWithServer(TestCaseWithServer):
             form_data['new_top_step'] = new_top_steps
             form_data['step_of'] = []
             form_data = self.post_process(process_id, form_data)
-            form_data = {'day_comment': '', 'new_todo': [process_id]}
-            self.check_post(form_data, f'/day?date={date}', 302)
+            form_data = {'day_comment': '', 'new_todo': [process_id],
+                         'make_type': 'full'}
+            self.check_post(form_data, f'/day?date={date}&make_type=full', 302)
             day_todos = Todo.by_date(self.db_conn, date)
             day_todos.sort(key=key_order_func, reverse=True)
             self.assertEqual(len(day_todos), 3)
@@ -392,22 +408,23 @@ class TestsWithServer(TestCaseWithServer):
 
     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)
+        self.post_process()
+        form_data = {'day_comment': '', 'new_todo': [1], 'make_type': 'full'}
+        self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 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)
+        form_data = {'day_comment': '', 'todo_id': [1], 'make_type': 'full'}
+        self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 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)
+        form_data = {'day_comment': '', 'todo_id': [1], 'done': [1],
+                     'make_type': 'full'}
+        self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
         self.assertEqual(todo.is_done, True)
 
     def test_do_GET_todo(self) -> None:
         """Test GET /todo response codes."""
         self.post_process()
-        form_data = {'day_comment': '', 'new_todo': 1}
-        self.check_post(form_data, '/day?date=2024-01-01', 302)
+        form_data = {'day_comment': '', 'new_todo': 1, 'make_type': 'full'}
+        self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
         self.check_get('/todo', 400)
         self.check_get('/todo?id=', 400)
         self.check_get('/todo?id=foo', 400)