From: Christian Heller <c.heller@plomlompom.de>
Date: Fri, 17 May 2024 23:53:05 +0000 (+0200)
Subject: More tests refactoring.
X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/static/%7B%7B%20web_path%20%7D%7D/pick_tasks?a=commitdiff_plain;h=ec2996e0036ceec72a1be79cea1166c4ab116582;p=plomtask

More tests refactoring.
---

diff --git a/tests/conditions.py b/tests/conditions.py
index 37a97a5..c9b5164 100644
--- a/tests/conditions.py
+++ b/tests/conditions.py
@@ -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"""
diff --git a/tests/days.py b/tests/days.py
index 41bcf9c..9e12d3f 100644
--- a/tests/days.py
+++ b/tests/days.py
@@ -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)
 
diff --git a/tests/processes.py b/tests/processes.py
index 160442b..578d545 100644
--- a/tests/processes.py
+++ b/tests/processes.py
@@ -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"""
diff --git a/tests/todos.py b/tests/todos.py
index 419d3db..059bd9f 100644
--- a/tests/todos.py
+++ b/tests/todos.py
@@ -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)
diff --git a/tests/utils.py b/tests/utils.py
index 69037cc..a42b3f3 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -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 = {}