"""Collecting Day and date-related items."""
-from datetime import datetime
+from datetime import datetime, timedelta
from sqlite3 import Row
from plomtask.misc import HandledException
from plomtask.db import DatabaseConnection
def all(cls, db_conn: DatabaseConnection,
date_range: tuple[str, str] = ('', '')):
"""Return list of Days in database within date_range."""
- def date_from_range_element(date_str: str, default: str):
+ def date_from_range_str(date_str: str, default: str):
if date_str == '':
date_str = default
elif date_str == 'today':
elif not date_valid(date_str):
raise HandledException(f'Bad date: {date_str}')
return date_str
- start_date = date_from_range_element(date_range[0], '2024-01-01')
- end_date = date_from_range_element(date_range[1], '2030-01-01')
+ start_date = date_from_range_str(date_range[0], '2024-01-01')
+ end_date = date_from_range_str(date_range[1], '2030-01-01')
days = []
sql = 'SELECT * FROM days WHERE date >= ? AND date <= ?'
for row in db_conn.exec(sql, (start_date, end_date)):
"""Return what weekday matches self.date."""
return self.datetime.strftime('%A')
+ @property
+ def prev_date(self):
+ """Return date preceding date of this Day."""
+ prev_datetime = self.datetime - timedelta(days=1)
+ return prev_datetime.strftime(DATE_FORMAT)
+
+ @property
+ def next_date(self):
+ """Return date succeeding date of this Day."""
+ next_datetime = self.datetime + timedelta(days=1)
+ return next_datetime.strftime(DATE_FORMAT)
+
def save(self, db_conn: DatabaseConnection):
"""Add (or re-write) self to database."""
db_conn.exec('REPLACE INTO days VALUES (?, ?)',
self.assertEqual(Day.all(self.db_conn, ('today', 'today')), [today])
def test_Day_comment(self):
- """Test Day.by_date()."""
+ """Test Day.comment."""
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, d2.date)
self.assertEqual(day_retrieved.comment, 'foo')
+ def test_Day_neighbor_dates(self):
+ """Test Day.prev_date and Day.next_date."""
+ self.assertEqual(Day('2024-01-01').prev_date, '2023-12-31')
+ self.assertEqual(Day('2023-02-28').next_date, '2023-03-01')
+
class TestsWithServer(TestsWithDB):
"""Tests against our HTTP server/handler (and database)."""