home · contact · privacy
36d0285296b7b191e4217475a08fb94646ed5207
[plomtask] / tests / days.py
1 """Test Days module."""
2 from unittest import TestCase
3 from datetime import datetime
4 from json import loads as json_loads
5 from tests.utils import TestCaseWithDB, TestCaseWithServer
6 from plomtask.dating import date_in_n_days
7 from plomtask.days import Day
8 from plomtask.exceptions import BadFormatException
9
10
11 class TestsSansDB(TestCase):
12     """Days module tests not requiring DB setup."""
13
14     def test_Day_valid_date(self) -> None:
15         """Test Day's date format validation and parsing."""
16         with self.assertRaises(BadFormatException):
17             Day('foo')
18         with self.assertRaises(BadFormatException):
19             Day('2024-02-30')
20         with self.assertRaises(BadFormatException):
21             Day('2024-02-01 23:00:00')
22         self.assertEqual(datetime(2024, 1, 1), Day('2024-01-01').datetime)
23
24     def test_Day_sorting(self) -> None:
25         """Test sorting by .__lt__ and Day.__eq__."""
26         day1 = Day('2024-01-01')
27         day2 = Day('2024-01-02')
28         day3 = Day('2024-01-03')
29         days = [day3, day1, day2]
30         self.assertEqual(sorted(days), [day1, day2, day3])
31
32     def test_Day_weekday(self) -> None:
33         """Test Day.weekday."""
34         self.assertEqual(Day('2024-03-17').weekday, 'Sunday')
35
36     def test_Day_neighbor_dates(self) -> None:
37         """Test Day.prev_date and Day.next_date."""
38         self.assertEqual(Day('2024-01-01').prev_date, '2023-12-31')
39         self.assertEqual(Day('2023-02-28').next_date, '2023-03-01')
40
41
42 class TestsWithDB(TestCaseWithDB):
43     """Tests requiring DB, but not server setup."""
44     checked_class = Day
45     default_ids = ('2024-01-01', '2024-01-02', '2024-01-03')
46
47     def test_Day_by_date_range_filled(self) -> None:
48         """Test Day.by_date_range_filled."""
49         date1, date2, date3 = self.default_ids
50         day1 = Day(date1)
51         day2 = Day(date2)
52         day3 = Day(date3)
53         day1.save(self.db_conn)
54         day2.save(self.db_conn)
55         day3.save(self.db_conn)
56         # check date range is a closed interval
57         self.assertEqual(Day.by_date_range_filled(self.db_conn, date1, date3),
58                          [day1, day2, day3])
59         # check first date range value excludes what's earlier
60         self.assertEqual(Day.by_date_range_filled(self.db_conn, date2, date3),
61                          [day2, day3])
62         # check second date range value excludes what's later
63         self.assertEqual(Day.by_date_range_filled(self.db_conn, date1, date2),
64                          [day1, day2])
65         # check swapped (impossible) date range returns emptiness
66         self.assertEqual(Day.by_date_range_filled(self.db_conn, date3, date1),
67                          [])
68         # check fill_gaps= instantiates unsaved dates within date range
69         # (but does not store them)
70         day5 = Day('2024-01-05')
71         day6 = Day('2024-01-06')
72         day6.save(self.db_conn)
73         day7 = Day('2024-01-07')
74         self.assertEqual(Day.by_date_range_filled(self.db_conn,
75                                                   day5.date, day7.date),
76                          [day5, day6, day7])
77         self.check_identity_with_cache_and_db([day1, day2, day3, day6])
78         # check 'today' is interpreted as today's date
79         today = Day(date_in_n_days(0))
80         today.save(self.db_conn)
81         self.assertEqual(Day.by_date_range_filled(self.db_conn,
82                                                   'today', 'today'),
83                          [today])
84
85
86 class TestsWithServer(TestCaseWithServer):
87     """Tests against our HTTP server/handler (and database)."""
88
89     def test_get_json(self) -> None:
90         """Test /day for JSON response."""
91         self.conn.request('GET', '/day?date=2024-01-01')
92         response = self.conn.getresponse()
93         self.assertEqual(response.status, 200)
94         expected = {'day': {'id': '2024-01-01',
95                             'comment': '',
96                             'todos': []},
97                     'top_nodes': [],
98                     'make_type': '',
99                     'enablers_for': {},
100                     'disablers_for': {},
101                     'conditions_present': [],
102                     'processes': []}
103         retrieved = json_loads(response.read().decode())
104         self.assertEqual(expected, retrieved)
105
106     def test_do_GET(self) -> None:
107         """Test /day and /calendar response codes, and / redirect."""
108         self.check_get('/day', 200)
109         self.check_get('/day?date=3000-01-01', 200)
110         self.check_get('/day?date=FOO', 400)
111         self.check_get('/calendar', 200)
112         self.check_get('/calendar?start=&end=', 200)
113         self.check_get('/calendar?start=today&end=today', 200)
114         self.check_get('/calendar?start=2024-01-01&end=2025-01-01', 200)
115         self.check_get('/calendar?start=foo', 400)
116
117     def test_do_POST_day(self) -> None:
118         """Test POST /day."""
119         form_data = {'day_comment': '', 'make_type': 'full'}
120         self.check_post(form_data, '/day', 400)
121         self.check_post(form_data, '/day?date=foo', 400)
122         self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
123         self.check_post({'foo': ''}, '/day?date=2024-01-01', 400)