home · contact · privacy
Fix disablers_for/enablers_for in GET_day breaking as_dict.
[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 todo_as_dict(id_: int = 1,
83                      process_id: int = 1,
84                      date: str = '2024-01-01',
85                      ) -> dict[str, object]:
86         """Return JSON of Process to expect."""
87         # pylint: disable=too-many-arguments
88         d = {'id': id_,
89              'date': date,
90              'process_id': process_id,
91              'is_done': False,
92              'calendarize': False,
93              'comment': '',
94              'children': [],
95              'parents': [],
96              'effort': None,
97              'conditions': [],
98              'disables': [],
99              'enables': [],
100              'blockers': []}
101         return d
102
103     @staticmethod
104     def todo_node_as_dict(todo_id: int) -> dict[str, object]:
105         """Return JSON of TodoNode to expect."""
106         return {'children': [], 'seen': False, 'todo': todo_id}
107
108     @staticmethod
109     def get_day_dict(date: str) -> dict[str, object]:
110         """Return JSON of GET /day to expect."""
111         day: dict[str, object] = {'id': date, 'comment': '', 'todos': []}
112         d: dict[str, object]
113         d = {'day': date,
114              'top_nodes': [],
115              'make_type': '',
116              'enablers_for': {},
117              'disablers_for': {},
118              'conditions_present': [],
119              'processes': [],
120              '_library': {'Day': TestsWithServer.as_refs([day])}}
121         return d
122
123     def test_do_GET_day_basics(self) -> None:
124         """Test GET /day basics."""
125         # check undefined day
126         date = date_in_n_days(0)
127         expected = self.get_day_dict(date)
128         self.check_json_get('/day', expected)
129         # check "today", "yesterday", "tomorrow" days
130         self.check_json_get('/day?date=today', expected)
131         expected = self.get_day_dict(date_in_n_days(1))
132         self.check_json_get('/day?date=tomorrow', expected)
133         expected = self.get_day_dict(date_in_n_days(-1))
134         self.check_json_get('/day?date=yesterday', expected)
135         # check wrong day strings
136         self.check_get('/day?date=foo', 400)
137         self.check_get('/day?date=2024-02-30', 400)
138         # check defined day
139         date = '2024-01-01'
140         expected = self.get_day_dict(date)
141         self.check_json_get(f'/day?date={date}', expected)
142         # check saved day
143         post_day: dict[str, object] = {'day_comment': 'foo', 'make_type': ''}
144         self.check_post(post_day, f'/day?date={date}', 302,
145                         f'/day?date={date}&make_type=')
146         assert isinstance(expected['_library'], dict)
147         day = expected['_library']['Day'][date]
148         day['comment'] = post_day['day_comment']
149         self.check_json_get(f'/day?date={date}', expected)
150         # check GET parameter POST not affecting reply to non-parameter GET
151         post_day['make_type'] = 'foo'
152         self.check_post(post_day, f'/day?date={date}', 302,
153                         f'/day?date={date}&make_type=foo')
154         self.check_json_get(f'/day?date={date}', expected)
155         expected['make_type'] = 'bar'
156         self.check_json_get(f'/day?date={date}&make_type=bar', expected)
157
158     def test_do_GET_day_with_todos(self) -> None:
159         """Test GET /day displaying posted Todos (no tree structure)."""
160         # check GET with two Todos and Processes
161         date = '2024-01-01'
162         post_day: dict[str, object] = {'day_comment': '', 'make_type': ''}
163         self.check_post(post_day, f'/day?date={date}', 302,
164                         f'/day?date={date}&make_type=')
165         expected = self.get_day_dict(date)
166         self.post_process(1)
167         post_proc2 = {'title': 'bar', 'description': 'rab', 'effort': 0.9}
168         self.post_process(2, post_proc2)
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=')
172         proc1 = self.proc_as_dict(1, 'foo', 'foo', 1.1)
173         proc2 = self.proc_as_dict(2, 'bar', 'rab', 0.9)
174         assert isinstance(expected['_library'], dict)
175         expected['_library']['Process'] = self.as_refs([proc1, proc2])
176         expected['processes'] = self.as_id_list([proc1, proc2])
177         t1 = self.todo_as_dict(1, 1, date)
178         t2 = self.todo_as_dict(2, 2, date)
179         expected['_library']['Todo'] = self.as_refs([t1, t2])
180         day = expected['_library']['Day'][date]
181         day['todos'] = self.as_id_list([t1, t2])
182         n1 = self.todo_node_as_dict(1)
183         n2 = self.todo_node_as_dict(2)
184         expected['top_nodes'] = [n1, n2]
185         self.check_json_get(f'/day?date={date}', expected)
186
187     def test_do_GET(self) -> None:
188         """Test /day and /calendar response codes, and / redirect."""
189         self.check_get('/calendar', 200)
190         self.check_get('/calendar?start=&end=', 200)
191         self.check_get('/calendar?start=today&end=today', 200)
192         self.check_get('/calendar?start=2024-01-01&end=2025-01-01', 200)
193         self.check_get('/calendar?start=foo', 400)
194
195     def test_do_POST_day(self) -> None:
196         """Test POST /day."""
197         form_data = {'day_comment': '', 'make_type': 'full'}
198         self.check_post(form_data, '/day', 400)
199         self.check_post(form_data, '/day?date=foo', 400)
200         self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
201         self.check_post({'foo': ''}, '/day?date=2024-01-01', 400)