From: Christian Heller Date: Tue, 18 Jun 2024 08:40:02 +0000 (+0200) Subject: Re-factor TestCaseSansDB methods. X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/decks/%7B%7Bdeck_id%7D%7D/balance?a=commitdiff_plain;h=bdb93117ce0f2b08b7b70cf43ac086afa4689c0f;p=plomtask Re-factor TestCaseSansDB methods. --- diff --git a/tests/conditions.py b/tests/conditions.py index 4ac69a8..468b1e8 100644 --- a/tests/conditions.py +++ b/tests/conditions.py @@ -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': ''} diff --git a/tests/days.py b/tests/days.py index 36d0285..f1d13b3 100644 --- a/tests/days.py +++ b/tests/days.py @@ -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.""" diff --git a/tests/processes.py b/tests/processes.py index 481d2d4..5c3e703 100644 --- a/tests/processes.py +++ b/tests/processes.py @@ -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] diff --git a/tests/todos.py b/tests/todos.py index 7632f39..51297a8 100644 --- a/tests/todos.py +++ b/tests/todos.py @@ -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() diff --git a/tests/utils.py b/tests/utils.py index 0925b2d..f473c18 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -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,