home · contact · privacy
Fix Process retrieval/display/saving bugs.
[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, NotFoundException
7
8
9 class TestsSansDB(TestCase):
10     """Days module tests not requiring DB setup."""
11
12     def test_Day_dates(self) -> None:
13         """Test Day's date format."""
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 Day.__lt__."""
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
35 class TestsWithDB(TestCaseWithDB):
36     """Tests requiring DB, but not server setup."""
37
38     def test_Day_by_id(self) -> None:
39         """Test Day.by_id()."""
40         with self.assertRaises(NotFoundException):
41             Day.by_id(self.db_conn, '2024-01-01')
42         Day('2024-01-01').save(self.db_conn)
43         self.assertEqual(Day('2024-01-01'),
44                          Day.by_id(self.db_conn, '2024-01-01'))
45         with self.assertRaises(NotFoundException):
46             Day.by_id(self.db_conn, '2024-01-02')
47         self.assertEqual(Day('2024-01-02'),
48                          Day.by_id(self.db_conn, '2024-01-02', create=True))
49
50     def test_Day_all(self) -> None:
51         """Test Day.all(), especially in regards to date range filtering."""
52         date1 = '2024-01-01'
53         date2 = '2024-01-02'
54         date3 = '2024-01-03'
55         day1 = Day(date1)
56         day2 = Day(date2)
57         day3 = Day(date3)
58         day1.save(self.db_conn)
59         day2.save(self.db_conn)
60         day3.save(self.db_conn)
61         self.assertEqual(Day.all(self.db_conn),
62                          [day1, day2, day3])
63         self.assertEqual(Day.all(self.db_conn, ('', '')),
64                          [day1, day2, day3])
65         self.assertEqual(Day.all(self.db_conn, (date1, date3)),
66                          [day1, day2, day3])
67         self.assertEqual(Day.all(self.db_conn, (date2, date3)),
68                          [day2, day3])
69         self.assertEqual(Day.all(self.db_conn, (date3, '')),
70                          [day3])
71         self.assertEqual(Day.all(self.db_conn, (date1, '')),
72                          [day1, day2, day3])
73         self.assertEqual(Day.all(self.db_conn, ('', date2)),
74                          [day1, day2])
75         self.assertEqual(Day.all(self.db_conn, (date3, date1)),
76                          [])
77         day4 = Day('2024-01-04')
78         day5 = Day('2024-01-05')
79         day6 = Day('2024-01-06')
80         day6.save(self.db_conn)
81         self.assertEqual(Day.all(self.db_conn, (date2, '2024-01-07'),
82                                  fill_gaps=True),
83                          [day2, day3, day4, day5, day6])
84         today = Day(todays_date())
85         today.save(self.db_conn)
86         self.assertEqual(Day.all(self.db_conn, ('today', 'today')), [today])
87
88     def test_Day_neighbor_dates(self) -> None:
89         """Test Day.prev_date and Day.next_date."""
90         self.assertEqual(Day('2024-01-01').prev_date, '2023-12-31')
91         self.assertEqual(Day('2023-02-28').next_date, '2023-03-01')
92
93     def test_Day_singularity(self) -> None:
94         """Test pointers made for single object keep pointing to it."""
95         day = Day('2024-01-01')
96         day.save(self.db_conn)
97         retrieved_day = Day.by_id(self.db_conn, '2024-01-01')
98         day.comment = 'foo'
99         self.assertEqual(retrieved_day.comment, 'foo')
100
101
102 class TestsWithServer(TestCaseWithServer):
103     """Tests against our HTTP server/handler (and database)."""
104
105     def test_do_GET(self) -> None:
106         """Test /day and /calendar response codes, and / redirect."""
107         self.check_get('/day', 200)
108         self.check_get('/day?date=3000-01-01', 200)
109         self.check_get('/day?date=FOO', 400)
110         self.check_get('/calendar', 200)
111         self.check_get('/calendar?start=&end=', 200)
112         self.check_get('/calendar?start=today&end=today', 200)
113         self.check_get('/calendar?start=2024-01-01&end=2025-01-01', 200)
114         self.check_get('/calendar?start=foo', 400)
115
116     def test_do_POST_day(self) -> None:
117         """Test POST /day."""
118         form_data = {'comment': ''}
119         self.check_post(form_data, '/day', 400)
120         self.check_post(form_data, '/day?date=foo', 400)
121         self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
122         self.check_post({'foo': ''}, '/day?date=2024-01-01', 400)