home · contact · privacy
More tests refactoring.
authorChristian Heller <c.heller@plomlompom.de>
Fri, 17 May 2024 23:53:05 +0000 (01:53 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Fri, 17 May 2024 23:53:05 +0000 (01:53 +0200)
tests/conditions.py
tests/days.py
tests/processes.py
tests/todos.py
tests/utils.py

index 37a97a5c91a4dcf6a25f26b2110b77e33c91519a..c9b516418f73bf3700f213bc36d0c00235bab5dc 100644 (file)
@@ -16,17 +16,8 @@ class TestsSansDB(TestCaseSansDB):
 class TestsWithDB(TestCaseWithDB):
     """Tests requiring DB, but not server setup."""
     checked_class = Condition
-
-    def test_Condition_saving_and_caching(self) -> None:
-        """Test .save/.save_core."""
-        kwargs = {'id_': 1, 'is_active': False}
-        self.check_saving_and_caching(**kwargs)
-        # check .id_ set if None, and versioned attributes too
-        c = Condition(None)
-        c.save(self.db_conn)
-        self.assertEqual(c.id_, 2)
-        self.check_saving_of_versioned('title', str)
-        self.check_saving_of_versioned('description', str)
+    default_init_kwargs = {'is_active': False}
+    test_versioneds = {'title': str, 'description': str}
 
     def test_Condition_from_table_row(self) -> None:
         """Test .from_table_row() properly reads in class from DB"""
index 41bcf9ce11a7db458ed5c03ce2b3f3abf9150722..9e12d3ff665550f3fb23bb603e639ce8efbf60ff 100644 (file)
@@ -42,8 +42,12 @@ class TestsWithDB(TestCaseWithDB):
     checked_class = Day
     default_ids = ('2024-01-01', '2024-01-02', '2024-01-03')
 
-    def test_Day_saving_and_caching(self) -> None:
-        """Test .save/.save_core."""
+    def test_saving_and_caching(self) -> None:
+        """Test storage of instances.
+
+        We don't use the parent class's method here because the checked class
+        has too different a handling of IDs.
+        """
         kwargs = {'date': self.default_ids[0], 'comment': 'foo'}
         self.check_saving_and_caching(**kwargs)
 
index 160442b287a08733ac6c4546ec77e696216ca4f7..578d545a9d1004c53121a2f2b785b434e791856d 100644 (file)
@@ -24,6 +24,7 @@ class TestsSansDBProcessStep(TestCaseSansDB):
 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."""
@@ -54,13 +55,8 @@ class TestsWithDB(TestCaseWithDB):
         p.save(self.db_conn)
         return p, set_1, set_2, set_3
 
-    def test_Process_saving_and_caching(self) -> None:
+    def test_Process_conditions_saving(self) -> None:
         """Test .save/.save_core."""
-        kwargs = {'id_': 1}
-        self.check_saving_and_caching(**kwargs)
-        self.check_saving_of_versioned('title', str)
-        self.check_saving_of_versioned('description', str)
-        self.check_saving_of_versioned('effort', float)
         p, set1, set2, set3 = self.p_of_conditions()
         p.uncache()
         r = Process.by_id(self.db_conn, p.id_)
@@ -193,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"""
index 419d3dba122cb274110e30a6697cb5f40f8849d2..059bd9f4fb47fac79a710b8421995f3f4b869da1 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)
index 69037cc8b5d628a8da68e4d3f40921b3d80f2c7c..a42b3f3ba61b1e28031bf35dca1d263048495830 100644 (file)
@@ -44,6 +44,8 @@ class TestCaseWithDB(TestCase):
     """Module tests not requiring DB setup."""
     checked_class: Any
     default_ids: tuple[int | str, int | str, int | str] = (1, 2, 3)
+    default_init_kwargs: dict[str, Any] = {}
+    test_versioneds: dict[str, type] = {}
 
     def setUp(self) -> None:
         Condition.empty_cache()
@@ -59,6 +61,17 @@ class TestCaseWithDB(TestCase):
         self.db_conn.close()
         remove_file(self.db_file.path)
 
+    def test_saving_and_caching(self) -> None:
+        """Test storage and initialization of instances and attributes."""
+        if not hasattr(self, 'checked_class'):
+            return
+        self.check_saving_and_caching(id_=1, **self.default_init_kwargs)
+        obj = self.checked_class(None, **self.default_init_kwargs)
+        obj.save(self.db_conn)
+        self.assertEqual(obj.id_, 2)
+        for k, v in self.test_versioneds.items():
+            self.check_saving_of_versioned(k, v)
+
     def check_storage(self, content: list[Any]) -> None:
         """Test cache and DB equal content."""
         expected_cache = {}