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