- def test_Day_by_date(self) -> None:
- """Test Day.by_date()."""
- with self.assertRaises(NotFoundException):
- Day.by_date(self.db_conn, '2024-01-01')
- Day('2024-01-01').save(self.db_conn)
- self.assertEqual(Day('2024-01-01'),
- Day.by_date(self.db_conn, '2024-01-01'))
+ 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.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)
+
+ def test_Day_from_table_row(self) -> None:
+ """Test .from_table_row() properly reads in class from DB"""
+ day = Day('2024-01-01')
+ day.save(self.db_conn)
+ assert isinstance(day.id_, str)
+ for row in self.db_conn.row_where(Day.table_name, 'id', day.id_):
+ retrieved = Day.from_table_row(self.db_conn, row)
+ self.assertEqual(day, retrieved)
+ self.assertEqual({day.id_: day}, Day.get_cache())
+
+ 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)