home · contact · privacy
Re-factor TestCaseSansDB methods.
authorChristian Heller <c.heller@plomlompom.de>
Tue, 18 Jun 2024 08:40:02 +0000 (10:40 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Tue, 18 Jun 2024 08:40:02 +0000 (10:40 +0200)
tests/conditions.py
tests/days.py
tests/processes.py
tests/todos.py
tests/utils.py

index 4ac69a856e24ca731b6cff344a9cf9c720149e7b..468b1e8b13499ea3c6775baf40f68cad1b646a87 100644 (file)
@@ -9,7 +9,6 @@ from plomtask.exceptions import HandledException
 class TestsSansDB(TestCaseSansDB):
     """Tests requiring no DB setup."""
     checked_class = Condition
-    do_id_test = True
     versioned_defaults_to_test = {'title': 'UNNAMED', 'description': ''}
 
 
index 36d0285296b7b191e4217475a08fb94646ed5207..f1d13b3fe7d03a7f43e5ed263ebbe74996460e82 100644 (file)
@@ -10,6 +10,8 @@ from plomtask.exceptions import BadFormatException
 
 class TestsSansDB(TestCase):
     """Days module tests not requiring DB setup."""
+    legal_ids = ['2024-01-01']
+    illegal_ids = ['foo', '2024-02-30', '2024-02-01 23:00:00']
 
     def test_Day_valid_date(self) -> None:
         """Test Day's date format validation and parsing."""
index 481d2d4a691ab586035a05a6b938a33e0f48d612..5c3e70364d32b679a339b9ac9ed591ad3295e365 100644 (file)
@@ -9,7 +9,6 @@ from plomtask.todos import Todo
 class TestsSansDB(TestCaseSansDB):
     """Module tests not requiring DB setup."""
     checked_class = Process
-    do_id_test = True
     versioned_defaults_to_test = {'title': 'UNNAMED', 'description': '',
                                   'effort': 1.0}
 
@@ -17,7 +16,6 @@ class TestsSansDB(TestCaseSansDB):
 class TestsSansDBProcessStep(TestCaseSansDB):
     """Module tests not requiring DB setup."""
     checked_class = ProcessStep
-    do_id_test = True
     default_init_args = [2, 3, 4]
 
 
index 7632f39cf3b75674036452b609c7e90b3f379e34..51297a8ceadc1812713a681489cc718814b7f066 100644 (file)
@@ -10,8 +10,8 @@ from plomtask.exceptions import (NotFoundException, BadFormatException,
 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
+    NB: We subclass TestCaseSansDB too, to pull in its .test_id_validation,
+    which for Todo wouldn't run without a DB being set up due to the need for
     Processes with set IDs.
     """
     checked_class = Todo
@@ -19,7 +19,6 @@ class TestsWithDB(TestCaseWithDB, TestCaseSansDB):
                            '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()
index 0925b2d5b2adc0e415293526a4b01c04fc42b178..f473c180ba565355a3b6ac0a03acb85d2fa307de 100644 (file)
@@ -17,27 +17,36 @@ from plomtask.todos import Todo
 from plomtask.exceptions import NotFoundException, HandledException
 
 
+def _within_checked_class(f: Callable[..., None]) -> Callable[..., None]:
+    def wrapper(self: TestCase) -> None:
+        if hasattr(self, 'checked_class'):
+            f(self)
+    return wrapper
+
+
 class TestCaseSansDB(TestCase):
     """Tests requiring no DB setup."""
     checked_class: Any
-    do_id_test: bool = False
     default_init_args: list[Any] = []
     versioned_defaults_to_test: dict[str, str | float] = {}
+    legal_ids = [1, 5]
+    illegal_ids = [0]
 
-    def test_id_setting(self) -> None:
-        """Test .id_ being set and its legal range being enforced."""
-        if not self.do_id_test:
-            return
-        with self.assertRaises(HandledException):
-            self.checked_class(0, *self.default_init_args)
-        obj = self.checked_class(5, *self.default_init_args)
-        self.assertEqual(obj.id_, 5)
+    @_within_checked_class
+    def test_id_validation(self) -> None:
+        """Test .id_ validation/setting."""
+        for id_ in self.illegal_ids:
+            with self.assertRaises(HandledException):
+                self.checked_class(id_, *self.default_init_args)
+        for id_ in self.legal_ids:
+            obj = self.checked_class(id_, *self.default_init_args)
+            self.assertEqual(obj.id_, id_)
 
+    @_within_checked_class
     def test_versioned_defaults(self) -> None:
         """Test defaults of VersionedAttributes."""
-        if len(self.versioned_defaults_to_test) == 0:
-            return
-        obj = self.checked_class(1, *self.default_init_args)
+        id_ = self.legal_ids[0]
+        obj = self.checked_class(id_, *self.default_init_args)
         for k, v in self.versioned_defaults_to_test.items():
             self.assertEqual(getattr(obj, k).newest, v)
 
@@ -62,13 +71,6 @@ class TestCaseWithDB(TestCase):
         self.db_conn.close()
         remove_file(self.db_file.path)
 
-    @staticmethod
-    def _within_checked_class(f: Callable[..., None]) -> Callable[..., None]:
-        def wrapper(self: TestCaseWithDB) -> None:
-            if hasattr(self, 'checked_class'):
-                f(self)
-        return wrapper
-
     def _load_from_db(self, id_: int | str) -> list[object]:
         db_found: list[object] = []
         for row in self.db_conn.row_where(self.checked_class.table_name,