class Day:
"""Individual days defined by their dates."""
- def __init__(self, date: str):
+ def __init__(self, date: str, comment: str = ''):
self.date = date
self.datetime = date_valid(self.date)
if not self.datetime:
raise HandledException(f'Given date of wrong format: {self.date}')
+ self.comment = comment
def __eq__(self, other: object):
return isinstance(other, self.__class__) and self.date == other.date
@classmethod
def from_table_row(cls, row: Row):
"""Make new Day from database row."""
- return cls(row[0])
+ return cls(row[0], row[1])
@classmethod
def all(cls, db_conn: DatabaseConnection,
def save(self, db_conn: DatabaseConnection):
"""Add (or re-write) self to database."""
- db_conn.exec('REPLACE INTO days VALUES (?)', (self.date,))
+ db_conn.exec('REPLACE INTO days VALUES (?, ?)',
+ (self.date, self.comment))
[day1, day2])
self.assertEqual(Day.all(self.db_conn, ('2024-01-03, 2024-01-01')),
[])
+
+ def test_Day_comment(self):
+ """Test Day.by_date()."""
+ self.assertEqual(None, Day.by_date(self.db_conn, '2024-01-01'))
+ d1 = Day('2024-01-01')
+ d1.save(self.db_conn)
+ day_retrieved = Day.by_date(self.db_conn, d1.date)
+ self.assertEqual(day_retrieved.comment, '')
+ d2 = Day('2024-01-02', 'foo')
+ d2.save(self.db_conn)
+ day_retrieved = Day.by_date(self.db_conn, d2.date)
+ self.assertEqual(day_retrieved.comment, 'foo')