home · contact · privacy
Add basic date ranging to Day.all().
authorChristian Heller <c.heller@plomlompom.de>
Sun, 17 Mar 2024 04:03:05 +0000 (05:03 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 17 Mar 2024 04:03:05 +0000 (05:03 +0100)
plomtask/days.py
plomtask/http.py
tests/test_days.py

index 17bb46329760beb078a6497d68bd7b0a39a05e3e..3abd76974107d379813a888a8f6f39b7c44afdc8 100644 (file)
@@ -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
index 9956a7251501758d795032903afb0d52c0e195b6..1af49d16b197bc607310f0f7c710b407b93e905b 100644 (file)
@@ -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)
index 828e5e895f8a53c6ad71bb65c151721ec6b7a5a7..8f5f2678c002034f02a1f036c28b976dd1b589b6 100644 (file)
@@ -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)