1 """Test Days module."""
2 from unittest import TestCase
3 from datetime import datetime
4 from tests.utils import TestCaseWithDB, TestCaseWithServer
5 from plomtask.days import Day, todays_date
6 from plomtask.exceptions import BadFormatException, NotFoundException
9 class TestsSansDB(TestCase):
10 """Days module tests not requiring DB setup."""
12 def test_Day_valid_date(self) -> None:
13 """Test Day's date format validation and parsing."""
14 with self.assertRaises(BadFormatException):
16 with self.assertRaises(BadFormatException):
18 with self.assertRaises(BadFormatException):
19 Day('2024-02-01 23:00:00')
20 self.assertEqual(datetime(2024, 1, 1), Day('2024-01-01').datetime)
22 def test_Day_sorting(self) -> None:
23 """Test sorting by .__lt__ and Day.__eq__."""
24 day1 = Day('2024-01-01')
25 day2 = Day('2024-01-02')
26 day3 = Day('2024-01-03')
27 days = [day3, day1, day2]
28 self.assertEqual(sorted(days), [day1, day2, day3])
30 def test_Day_weekday(self) -> None:
31 """Test Day.weekday."""
32 self.assertEqual(Day('2024-03-17').weekday, 'Sunday')
34 def test_Day_neighbor_dates(self) -> None:
35 """Test Day.prev_date and Day.next_date."""
36 self.assertEqual(Day('2024-01-01').prev_date, '2023-12-31')
37 self.assertEqual(Day('2023-02-28').next_date, '2023-03-01')
40 class TestsWithDB(TestCaseWithDB):
41 """Tests requiring DB, but not server setup."""
43 def check_storage(self, content: list[Day]) -> None:
44 """Test cache and DB equal content."""
47 expected_cache[item.id_] = item
48 self.assertEqual(Day.get_cache(), expected_cache)
49 db_found: list[Day] = []
51 assert isinstance(item.id_, str)
52 for row in self.db_conn.row_where(Day.table_name, 'id', item.id_):
53 db_found += [Day.from_table_row(self.db_conn, row)]
54 self.assertEqual(sorted(content), sorted(db_found))
56 def test_Day_saving_and_caching(self) -> None:
57 """Test .save/.save_core."""
60 day = Day(date, comment)
61 # check object init itself doesn't store anything yet
62 self.check_storage([])
63 # check saving stores in cache and DB
64 day.save(self.db_conn)
65 self.check_storage([day])
66 # check attributes set properly (and not unset by saving)
67 self.assertEqual(day.id_, date)
68 self.assertEqual(day.comment, comment)
70 def test_Day_by_id(self) -> None:
74 # check failure if not yet saved
76 with self.assertRaises(NotFoundException):
77 Day.by_id(self.db_conn, date1)
78 # check identity of saved and retrieved
79 day1.save(self.db_conn)
80 self.assertEqual(day1, Day.by_id(self.db_conn, date1))
81 # check create=True acts like normal instantiation (sans saving)
82 by_id_created = Day.by_id(self.db_conn, date2, create=True)
83 self.assertEqual(Day(date2), by_id_created)
84 self.check_storage([day1])
86 def test_Day_all(self) -> None:
87 """Test Day.all(), especially in regards to date range filtering."""
94 day1.save(self.db_conn)
95 day3.save(self.db_conn)
96 # check that all() shows all saved, but no unsaved items
97 self.assertEqual(Day.all(self.db_conn),
99 day2.save(self.db_conn)
100 self.assertEqual(Day.all(self.db_conn),
102 # check empty date range values show everything
103 self.assertEqual(Day.all(self.db_conn, ('', '')),
105 # check date range is a closed interval
106 self.assertEqual(Day.all(self.db_conn, (date1, date3)),
108 # check first date range value excludes what's earlier
109 self.assertEqual(Day.all(self.db_conn, (date2, date3)),
111 self.assertEqual(Day.all(self.db_conn, (date3, '')),
113 # check second date range value excludes what's later
114 self.assertEqual(Day.all(self.db_conn, ('', date2)),
116 # check swapped (impossible) date range returns emptiness
117 self.assertEqual(Day.all(self.db_conn, (date3, date1)),
119 # check fill_gaps= instantiates unsaved dates within date range
120 # (but does not store them)
121 day4 = Day('2024-01-04')
122 day5 = Day('2024-01-05')
123 day6 = Day('2024-01-06')
124 day6.save(self.db_conn)
125 self.assertEqual(Day.all(self.db_conn, (date2, '2024-01-07'),
127 [day2, day3, day4, day5, day6])
128 self.check_storage([day1, day2, day3, day6])
129 # check 'today' is interpreted as today's date
130 today = Day(todays_date())
131 today.save(self.db_conn)
132 self.assertEqual(Day.all(self.db_conn, ('today', 'today')), [today])
134 def test_Day_remove(self) -> None:
135 """Test .remove() effects on DB and cache."""
138 day.save(self.db_conn)
139 day.remove(self.db_conn)
140 self.check_storage([])
142 def test_Day_singularity(self) -> None:
143 """Test pointers made for single object keep pointing to it."""
144 day = Day('2024-01-01')
145 day.save(self.db_conn)
146 retrieved_day = Day.by_id(self.db_conn, '2024-01-01')
148 self.assertEqual(retrieved_day.comment, 'foo')
151 class TestsWithServer(TestCaseWithServer):
152 """Tests against our HTTP server/handler (and database)."""
154 def test_do_GET(self) -> None:
155 """Test /day and /calendar response codes, and / redirect."""
156 self.check_get('/day', 200)
157 self.check_get('/day?date=3000-01-01', 200)
158 self.check_get('/day?date=FOO', 400)
159 self.check_get('/calendar', 200)
160 self.check_get('/calendar?start=&end=', 200)
161 self.check_get('/calendar?start=today&end=today', 200)
162 self.check_get('/calendar?start=2024-01-01&end=2025-01-01', 200)
163 self.check_get('/calendar?start=foo', 400)
165 def test_do_POST_day(self) -> None:
166 """Test POST /day."""
167 form_data = {'comment': ''}
168 self.check_post(form_data, '/day', 400)
169 self.check_post(form_data, '/day?date=foo', 400)
170 self.check_post(form_data, '/day?date=2024-01-01', 302)
171 self.check_post({'foo': ''}, '/day?date=2024-01-01', 400)