X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=tests%2Fdays.py;h=1f0e55d871b282881dc0fee2f7e2fc91a591d243;hb=c4ccb784bb3a83c1c614c9bab7fc007ee17f6615;hp=f67c80adfa96d144585af2b59f0f59b0efa7d607;hpb=96a63958d64e4847cd462d12b227ccb4fecd0d9e;p=plomtask diff --git a/tests/days.py b/tests/days.py index f67c80a..1f0e55d 100644 --- a/tests/days.py +++ b/tests/days.py @@ -3,7 +3,7 @@ from unittest import TestCase from datetime import datetime from tests.utils import TestCaseWithDB, TestCaseWithServer from plomtask.days import Day, todays_date -from plomtask.exceptions import BadFormatException, NotFoundException +from plomtask.exceptions import BadFormatException class TestsSansDB(TestCase): @@ -39,67 +39,26 @@ class TestsSansDB(TestCase): class TestsWithDB(TestCaseWithDB): """Tests requiring DB, but not server setup.""" - - def check_storage(self, content: list[Day]) -> None: - """Test cache and DB equal content.""" - expected_cache = {} - for item in content: - expected_cache[item.id_] = item - self.assertEqual(Day.get_cache(), expected_cache) - db_found: list[Day] = [] - for item in content: - assert isinstance(item.id_, str) - for row in self.db_conn.row_where(Day.table_name, 'id', item.id_): - db_found += [Day.from_table_row(self.db_conn, row)] - self.assertEqual(sorted(content), sorted(db_found)) + 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.""" - date = '2024-01-01' - comment = 'comment' - day = Day(date, comment) - # check object init itself doesn't store anything yet - self.check_storage([]) - # check saving stores in cache and DB - day.save(self.db_conn) - self.check_storage([day]) - # check attributes set properly (and not unset by saving) - self.assertEqual(day.id_, date) - self.assertEqual(day.comment, comment) + kwargs = {'date': self.default_ids[0], 'comment': 'foo'} + self.check_saving_and_caching(**kwargs) + + def test_Day_from_table_row(self) -> None: + """Test .from_table_row() properly reads in class from DB""" + self.check_from_table_row() def test_Day_by_id(self) -> None: """Test .by_id().""" - date1 = '2024-01-01' - date2 = '2024-01-02' - # check failure if not yet saved - day1 = Day(date1) - with self.assertRaises(NotFoundException): - Day.by_id(self.db_conn, date1) - # check identity of saved and retrieved - day1.save(self.db_conn) - self.assertEqual(day1, Day.by_id(self.db_conn, date1)) - # check create=True acts like normal instantiation (sans saving) - by_id_created = Day.by_id(self.db_conn, date2, create=True) - self.assertEqual(Day(date2), by_id_created) - self.check_storage([day1]) + self.check_by_id() def test_Day_all(self) -> None: """Test Day.all(), especially in regards to date range filtering.""" - date1 = '2024-01-01' - date2 = '2024-01-02' - date3 = '2024-01-03' - day1 = Day(date1) - day2 = Day(date2) - day3 = Day(date3) - day1.save(self.db_conn) - day3.save(self.db_conn) - # check that all() shows all saved, but no unsaved items - self.assertEqual(Day.all(self.db_conn), - [day1, day3]) - day2.save(self.db_conn) - self.assertEqual(Day.all(self.db_conn), - [day1, day2, day3]) - # check empty date range values show everything + date1, date2, date3 = self.default_ids + day1, day2, day3 = self.check_all() self.assertEqual(Day.all(self.db_conn, ('', '')), [day1, day2, day3]) # check date range is a closed interval @@ -133,19 +92,11 @@ class TestsWithDB(TestCaseWithDB): def test_Day_remove(self) -> None: """Test .remove() effects on DB and cache.""" - date = '2024-01-01' - day = Day(date) - day.save(self.db_conn) - day.remove(self.db_conn) - self.check_storage([]) + self.check_remove() def test_Day_singularity(self) -> None: """Test pointers made for single object keep pointing to it.""" - day = Day('2024-01-01') - day.save(self.db_conn) - retrieved_day = Day.by_id(self.db_conn, '2024-01-01') - day.comment = 'foo' - self.assertEqual(retrieved_day.comment, 'foo') + self.check_singularity('comment', 'boo') class TestsWithServer(TestCaseWithServer):