home · contact · privacy
Use same date ranging code for Day and Todo filtering.
[plomtask] / tests / days.py
1 """Test Days module."""
2 from unittest import TestCase
3 from datetime import datetime
4 from tests.utils import TestCaseWithDB, TestCaseWithServer
5 from plomtask.dating import todays_date
6 from plomtask.days import Day
7 from plomtask.exceptions import BadFormatException
8
9
10 class TestsSansDB(TestCase):
11     """Days module tests not requiring DB setup."""
12
13     def test_Day_valid_date(self) -> None:
14         """Test Day's date format validation and parsing."""
15         with self.assertRaises(BadFormatException):
16             Day('foo')
17         with self.assertRaises(BadFormatException):
18             Day('2024-02-30')
19         with self.assertRaises(BadFormatException):
20             Day('2024-02-01 23:00:00')
21         self.assertEqual(datetime(2024, 1, 1), Day('2024-01-01').datetime)
22
23     def test_Day_sorting(self) -> None:
24         """Test sorting by .__lt__ and Day.__eq__."""
25         day1 = Day('2024-01-01')
26         day2 = Day('2024-01-02')
27         day3 = Day('2024-01-03')
28         days = [day3, day1, day2]
29         self.assertEqual(sorted(days), [day1, day2, day3])
30
31     def test_Day_weekday(self) -> None:
32         """Test Day.weekday."""
33         self.assertEqual(Day('2024-03-17').weekday, 'Sunday')
34
35     def test_Day_neighbor_dates(self) -> None:
36         """Test Day.prev_date and Day.next_date."""
37         self.assertEqual(Day('2024-01-01').prev_date, '2023-12-31')
38         self.assertEqual(Day('2023-02-28').next_date, '2023-03-01')
39
40
41 class TestsWithDB(TestCaseWithDB):
42     """Tests requiring DB, but not server setup."""
43     checked_class = Day
44     default_ids = ('2024-01-01', '2024-01-02', '2024-01-03')
45
46     def test_saving_and_caching(self) -> None:
47         """Test storage of instances.
48
49         We don't use the parent class's method here because the checked class
50         has too different a handling of IDs.
51         """
52         kwargs = {'date': self.default_ids[0], 'comment': 'foo'}
53         self.check_saving_and_caching(**kwargs)
54
55     def test_Day_from_table_row(self) -> None:
56         """Test .from_table_row() properly reads in class from DB"""
57         self.check_from_table_row()
58
59     def test_Day_by_id(self) -> None:
60         """Test .by_id()."""
61         self.check_by_id()
62
63     def test_Day_all(self) -> None:
64         """Test Day.all(), especially in regards to date range filtering."""
65         date1, date2, date3 = self.default_ids
66         day1, day2, day3 = self.check_all()
67         self.assertEqual(Day.all(self.db_conn, ('', '')),
68                          [day1, day2, day3])
69         # check date range is a closed interval
70         self.assertEqual(Day.all(self.db_conn, (date1, date3)),
71                          [day1, day2, day3])
72         # check first date range value excludes what's earlier
73         self.assertEqual(Day.all(self.db_conn, (date2, date3)),
74                          [day2, day3])
75         self.assertEqual(Day.all(self.db_conn, (date3, '')),
76                          [day3])
77         # check second date range value excludes what's later
78         self.assertEqual(Day.all(self.db_conn, ('', date2)),
79                          [day1, day2])
80         # check swapped (impossible) date range returns emptiness
81         self.assertEqual(Day.all(self.db_conn, (date3, date1)),
82                          [])
83         # check fill_gaps= instantiates unsaved dates within date range
84         # (but does not store them)
85         day5 = Day('2024-01-05')
86         day6 = Day('2024-01-06')
87         day6.save(self.db_conn)
88         day7 = Day('2024-01-07')
89         self.assertEqual(Day.all(self.db_conn, (day5.date, day7.date),
90                                  fill_gaps=True),
91                          [day5, day6, day7])
92         self.check_storage([day1, day2, day3, day6])
93         # check 'today' is interpreted as today's date
94         today = Day(todays_date())
95         today.save(self.db_conn)
96         self.assertEqual(Day.all(self.db_conn, ('today', 'today')), [today])
97
98     def test_Day_remove(self) -> None:
99         """Test .remove() effects on DB and cache."""
100         self.check_remove()
101
102     def test_Day_singularity(self) -> None:
103         """Test pointers made for single object keep pointing to it."""
104         self.check_singularity('day_comment', 'boo')
105
106
107 class TestsWithServer(TestCaseWithServer):
108     """Tests against our HTTP server/handler (and database)."""
109
110     def test_do_GET(self) -> None:
111         """Test /day and /calendar response codes, and / redirect."""
112         self.check_get('/day', 200)
113         self.check_get('/day?date=3000-01-01', 200)
114         self.check_get('/day?date=FOO', 400)
115         self.check_get('/calendar', 200)
116         self.check_get('/calendar?start=&end=', 200)
117         self.check_get('/calendar?start=today&end=today', 200)
118         self.check_get('/calendar?start=2024-01-01&end=2025-01-01', 200)
119         self.check_get('/calendar?start=foo', 400)
120
121     def test_do_POST_day(self) -> None:
122         """Test POST /day."""
123         form_data = {'day_comment': ''}
124         self.check_post(form_data, '/day', 400)
125         self.check_post(form_data, '/day?date=foo', 400)
126         self.check_post(form_data, '/day?date=2024-01-01', 302)
127         self.check_post({'foo': ''}, '/day?date=2024-01-01', 400)