return cls(row[0])
@classmethod
- def all(cls, db_conn: DatabaseConnection):
- """Return list of all Days in database."""
+ def all(cls, db_conn: DatabaseConnection,
+ date_range: tuple[str, str] = ('', '')):
+ """Return list of Days in database within date_range."""
+ start_date = date_range[0] if date_range[0] else '2024-01-01'
+ end_date = date_range[1] if date_range[1] else '2030-12-31'
days = []
- for row in db_conn.exec('SELECT * FROM days'):
+ sql = 'SELECT * FROM days WHERE date >= ? AND date <= ?'
+ for row in db_conn.exec(sql, (start_date, end_date)):
days += [cls.from_table_row(row)]
+ days.sort()
return days
@property
"""Test days module."""
from unittest import TestCase
from datetime import datetime
+from os import remove as remove_file
from plomtask.days import Day
from plomtask.misc import HandledException
+from plomtask.db import DatabaseFile, DatabaseConnection
class DayTests(TestCase):
def test_Day_weekday(self):
"""Test Day.weekday."""
self.assertEqual(Day('2024-03-17').weekday, 'Sunday')
+
+ def test_Day_all(self):
+ """Test Day.all(), especially in regards to date range filtering."""
+ timestamp = datetime.now().timestamp()
+ db_file = DatabaseFile(f'test_db:{timestamp}')
+ db_file.remake()
+ conn = DatabaseConnection(db_file)
+ day1 = Day('2024-01-01')
+ day2 = Day('2024-01-02')
+ day3 = Day('2024-01-03')
+ day1.save(conn)
+ day2.save(conn)
+ day3.save(conn)
+ self.assertEqual(Day.all(conn), [day1, day2, day3])
+ self.assertEqual(Day.all(conn, ('2024-01-01', '2024-01-03')),
+ [day1, day2, day3])
+ self.assertEqual(Day.all(conn, ('2024-01-02', '2024-01-03')),
+ [day2, day3])
+ self.assertEqual(Day.all(conn, ('2024-01-03', '')), [day3])
+ self.assertEqual(Day.all(conn, ('2024-01-01', '')), [day1, day2, day3])
+ self.assertEqual(Day.all(conn, ('', '2024-01-02')), [day1, day2])
+ self.assertEqual(Day.all(conn, ('2024-01-03, 2024-01-01')), [])
+ conn.close()
+ remove_file(db_file.path)