home · contact · privacy
Turn TodoNode into full class with .as_dict, with result expand Day tests.
[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 date_in_n_days
6 from plomtask.days import Day
7
8
9 class TestsSansDB(TestCase):
10     """Days module tests not requiring DB setup."""
11     legal_ids = ['2024-01-01']
12     illegal_ids = ['foo', '2024-02-30', '2024-02-01 23:00:00']
13
14     def test_Day_datetime_weekday_neighbor_dates(self) -> None:
15         """Test Day's date parsing."""
16         self.assertEqual(datetime(2024, 5, 1), Day('2024-05-01').datetime)
17         self.assertEqual('Sunday', Day('2024-03-17').weekday)
18         self.assertEqual('March', Day('2024-03-17').month_name)
19         self.assertEqual('2023-12-31', Day('2024-01-01').prev_date)
20         self.assertEqual('2023-03-01', Day('2023-02-28').next_date)
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
31 class TestsWithDB(TestCaseWithDB):
32     """Tests requiring DB, but not server setup."""
33     checked_class = Day
34     default_ids = ('2024-01-01', '2024-01-02', '2024-01-03')
35
36     def test_Day_by_date_range_filled(self) -> None:
37         """Test Day.by_date_range_filled."""
38         date1, date2, date3 = self.default_ids
39         day1 = Day(date1)
40         day2 = Day(date2)
41         day3 = Day(date3)
42         for day in [day1, day2, day3]:
43             day.save(self.db_conn)
44         # check date range includes limiter days
45         self.assertEqual(Day.by_date_range_filled(self.db_conn, date1, date3),
46                          [day1, day2, day3])
47         # check first date range value excludes what's earlier
48         self.assertEqual(Day.by_date_range_filled(self.db_conn, date2, date3),
49                          [day2, day3])
50         # check second date range value excludes what's later
51         self.assertEqual(Day.by_date_range_filled(self.db_conn, date1, date2),
52                          [day1, day2])
53         # check swapped (impossible) date range returns emptiness
54         self.assertEqual(Day.by_date_range_filled(self.db_conn, date3, date1),
55                          [])
56         # check fill_gaps= instantiates unsaved dates within date range
57         # (but does not store them)
58         day5 = Day('2024-01-05')
59         day6 = Day('2024-01-06')
60         day6.save(self.db_conn)
61         day7 = Day('2024-01-07')
62         self.assertEqual(Day.by_date_range_filled(self.db_conn,
63                                                   day5.date, day7.date),
64                          [day5, day6, day7])
65         self.check_identity_with_cache_and_db([day1, day2, day3, day6])
66         # check 'today' is interpreted as today's date
67         today = Day(date_in_n_days(0))
68         self.assertEqual(Day.by_date_range_filled(self.db_conn,
69                                                   'today', 'today'),
70                          [today])
71         prev_day = Day(date_in_n_days(-1))
72         next_day = Day(date_in_n_days(1))
73         self.assertEqual(Day.by_date_range_filled(self.db_conn,
74                                                   'yesterday', 'tomorrow'),
75                          [prev_day, today, next_day])
76
77
78 class TestsWithServer(TestCaseWithServer):
79     """Tests against our HTTP server/handler (and database)."""
80
81     @staticmethod
82     def day_dict(date: str) -> dict[str, object]:
83         """Return JSON of Process to expect."""
84         return {'id': date, 'comment': '', 'todos': []}
85
86     @staticmethod
87     def todo_as_dict(id_: int = 1,
88                      process_id: int = 1,
89                      date: str = '2024-01-01',
90                      ) -> dict[str, object]:
91         """Return JSON of Process to expect."""
92         # pylint: disable=too-many-arguments
93         d = {'id': id_,
94              'date': date,
95              'process_id': process_id,
96              'is_done': False,
97              'calendarize': False,
98              'comment': '',
99              'children': [],
100              'parents': [],
101              'effort': None,
102              'conditions': [],
103              'disables': [],
104              'enables': [],
105              'blockers': []}
106         return d
107
108     @staticmethod
109     def todo_node_as_dict(todo_id: int) -> dict[str, object]:
110         """Return JSON of TodoNode to expect."""
111         return {'children': [], 'seen': False, 'todo': todo_id}
112
113     def test_do_GET_day(self) -> None:
114         """Test GET /day basics."""
115         # pylint: disable=too-many-statements
116         # check undefined day
117         date = date_in_n_days(0)
118         day = self.day_dict(date)
119         expected: dict[str, object]
120         expected = {'day': date,
121                     'top_nodes': [],
122                     'make_type': '',
123                     'enablers_for': {},
124                     'disablers_for': {},
125                     'conditions_present': [],
126                     'processes': [],
127                     '_library': {'Day': self.as_refs([day])}}
128         self.check_json_get('/day', expected)
129         # check "today", "yesterday", "tomorrow" days
130         self.check_json_get('/day?date=today', expected)
131         day = self.day_dict(date_in_n_days(1))
132         expected['day'] = day['id']
133         assert isinstance(expected['_library'], dict)
134         expected['_library']['Day'] = self.as_refs([day])
135         self.check_json_get('/day?date=tomorrow', expected)
136         day = self.day_dict(date_in_n_days(-1))
137         expected['day'] = day['id']
138         expected['_library']['Day'] = self.as_refs([day])
139         self.check_json_get('/day?date=yesterday', expected)
140         # check wrong day strings
141         self.check_get('/day?date=foo', 400)
142         self.check_get('/day?date=2024-02-30', 400)
143         # check defined day
144         date = '2024-01-01'
145         day = self.day_dict(date)
146         expected['day'] = day['id']
147         expected['_library']['Day'] = self.as_refs([day])
148         self.check_json_get(f'/day?date={date}', expected)
149         # check saved day
150         post_day: dict[str, object] = {'day_comment': 'foo', 'make_type': ''}
151         self.check_post(post_day, f'/day?date={date}', 302,
152                         f'/day?date={date}&make_type=')
153         day['comment'] = post_day['day_comment']
154         self.check_json_get(f'/day?date={date}', expected)
155         # check GET parameter POST not affecting reply to non-parameter GET
156         post_day['make_type'] = 'foo'
157         self.check_post(post_day, f'/day?date={date}', 302,
158                         f'/day?date={date}&make_type=foo')
159         self.check_json_get(f'/day?date={date}', expected)
160         expected['make_type'] = 'bar'
161         self.check_json_get(f'/day?date={date}&make_type=bar', expected)
162         # check GET with two Todos and Processes
163         expected['make_type'] = ''
164         form_data = self.post_process(1)
165         form_data['title'] = 'bar'
166         form_data['description'] = 'rab'
167         form_data['effort'] = 0.9
168         self.post_process(2, form_data)
169         post_day['new_todo'] = [1, 2]
170         self.check_post(post_day, f'/day?date={date}', 302,
171                         f'/day?date={date}&make_type=foo')
172         proc1 = self.proc_as_dict(1, 'foo', 'foo', 1.1)
173         proc2 = self.proc_as_dict(2, 'bar', 'rab', 0.9)
174         expected['_library']['Process'] = self.as_refs([proc1, proc2])
175         expected['processes'] = self.as_id_list([proc1, proc2])
176         t1 = self.todo_as_dict(1, 1, date)
177         t2 = self.todo_as_dict(2, 2, date)
178         expected['_library']['Todo'] = self.as_refs([t1, t2])
179         day['todos'] = self.as_id_list([t1, t2])
180         n1 = self.todo_node_as_dict(1)
181         n2 = self.todo_node_as_dict(2)
182         expected['top_nodes'] = [n1, n2]
183         self.check_json_get(f'/day?date={date}', expected)
184
185     def test_do_GET(self) -> None:
186         """Test /day and /calendar response codes, and / redirect."""
187         self.check_get('/calendar', 200)
188         self.check_get('/calendar?start=&end=', 200)
189         self.check_get('/calendar?start=today&end=today', 200)
190         self.check_get('/calendar?start=2024-01-01&end=2025-01-01', 200)
191         self.check_get('/calendar?start=foo', 400)
192
193     def test_do_POST_day(self) -> None:
194         """Test POST /day."""
195         form_data = {'day_comment': '', 'make_type': 'full'}
196         self.check_post(form_data, '/day', 400)
197         self.check_post(form_data, '/day?date=foo', 400)
198         self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
199         self.check_post({'foo': ''}, '/day?date=2024-01-01', 400)