From: Christian Heller Date: Wed, 1 May 2024 21:10:11 +0000 (+0200) Subject: Refactor Days tests. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/static/%7B%7Byoutube_prefix%7D%7D%7B%7Bvideo_id%7D%7D?a=commitdiff_plain;h=96a63958d64e4847cd462d12b227ccb4fecd0d9e;p=plomtask Refactor Days tests. --- diff --git a/tests/days.py b/tests/days.py index 3da78ee..f67c80a 100644 --- a/tests/days.py +++ b/tests/days.py @@ -40,20 +40,29 @@ 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)) + 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.assertEqual({}, Day.get_cache()) - self.assertEqual([], Day.all(self.db_conn)) + self.check_storage([]) # check saving stores in cache and DB day.save(self.db_conn) - assert isinstance(day.id_, str) - for row in self.db_conn.row_where(Day.table_name, 'id', day.id_): - self.assertEqual(day, Day.from_table_row(self.db_conn, row)) - self.assertEqual({day.id_: day}, Day.get_cache()) + self.check_storage([day]) # check attributes set properly (and not unset by saving) self.assertEqual(day.id_, date) self.assertEqual(day.comment, comment) @@ -72,8 +81,7 @@ class TestsWithDB(TestCaseWithDB): # 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.assertEqual({day1.id_: day1}, Day.get_cache()) - self.assertEqual([day1], Day.all(self.db_conn)) + self.check_storage([day1]) def test_Day_all(self) -> None: """Test Day.all(), especially in regards to date range filtering.""" @@ -94,7 +102,7 @@ class TestsWithDB(TestCaseWithDB): # check empty date range values show everything self.assertEqual(Day.all(self.db_conn, ('', '')), [day1, day2, day3]) - # check date range is open interval + # check date range is a closed interval self.assertEqual(Day.all(self.db_conn, (date1, date3)), [day1, day2, day3]) # check first date range value excludes what's earlier @@ -117,14 +125,7 @@ class TestsWithDB(TestCaseWithDB): self.assertEqual(Day.all(self.db_conn, (date2, '2024-01-07'), fill_gaps=True), [day2, day3, day4, day5, day6]) - self.assertEqual(Day.get_cache().keys(), - {date1, date2, date3, day6.date}) - assert isinstance(day4.id_, str) - assert isinstance(day5.id_, str) - self.assertEqual(self.db_conn.row_where(Day.table_name, - 'id', day4.id_), []) - self.assertEqual(self.db_conn.row_where(Day.table_name, - 'id', day5.id_), []) + self.check_storage([day1, day2, day3, day6]) # check 'today' is interpreted as today's date today = Day(todays_date()) today.save(self.db_conn) @@ -136,10 +137,7 @@ class TestsWithDB(TestCaseWithDB): day = Day(date) day.save(self.db_conn) day.remove(self.db_conn) - assert isinstance(day.id_, str) - self.assertEqual(self.db_conn.row_where(Day.table_name, - 'id', day.id_), []) - self.assertEqual(Day.get_cache(), {}) + self.check_storage([]) def test_Day_singularity(self) -> None: """Test pointers made for single object keep pointing to it."""