From: Christian Heller Date: Sun, 17 Mar 2024 04:03:05 +0000 (+0100) Subject: Add basic date ranging to Day.all(). X-Git-Url: https://plomlompom.com/repos/%22https:/validator.w3.org/%7B%7Bdb.prefix%7D%7D/process?a=commitdiff_plain;h=a8a7b5d0a5007274539e528968c68fe9f25e422e;p=plomtask Add basic date ranging to Day.all(). --- diff --git a/plomtask/days.py b/plomtask/days.py index 17bb463..3abd769 100644 --- a/plomtask/days.py +++ b/plomtask/days.py @@ -35,11 +35,16 @@ class Day: 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 diff --git a/plomtask/http.py b/plomtask/http.py index 9956a72..1af49d1 100644 --- a/plomtask/http.py +++ b/plomtask/http.py @@ -55,6 +55,5 @@ class TaskHandler(BaseHTTPRequestHandler): days = Day.all(conn) conn.commit() conn.close() - days.sort() return self.server.jinja.get_template('calendar.html').render( days=days) diff --git a/tests/test_days.py b/tests/test_days.py index 828e5e8..8f5f267 100644 --- a/tests/test_days.py +++ b/tests/test_days.py @@ -1,8 +1,10 @@ """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): @@ -33,3 +35,27 @@ 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)