def remove(self, db_conn: DatabaseConnection) -> None:
"""Remove from DB and cache."""
- assert isinstance(self.id_, int | str)
+ if self.id_ is None or self.__class__.get_cached(self.id_) is None:
+ raise HandledException('cannot remove unsaved item')
self.uncache()
db_conn.delete_where(self.table_name, 'id', self.id_)
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, NotFoundException,
+ HandledException)
class TestsSansDB(TestCase):
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'
day1 = Day(date1)
day2 = Day(date2)
day3 = Day(date3)
+ # check pre-save .all() returns empty list
+ self.assertEqual(Day.all(self.db_conn), [])
+ # check that all() shows all saved, but no unsaved items
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)
"""Test .remove() effects on DB and cache."""
date = '2024-01-01'
day = Day(date)
+ with self.assertRaises(HandledException):
+ day.remove(self.db_conn)
day.save(self.db_conn)
day.remove(self.db_conn)
self.check_storage([])