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
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']
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)
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])
31 class TestsWithDB(TestCaseWithDB):
32 """Tests requiring DB, but not server setup."""
34 default_ids = ('2024-01-01', '2024-01-02', '2024-01-03')
36 def test_Day_by_date_range_filled(self) -> None:
37 """Test Day.by_date_range_filled."""
38 date1, date2, date3 = self.default_ids
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),
47 # check first date range value excludes what's earlier
48 self.assertEqual(Day.by_date_range_filled(self.db_conn, date2, date3),
50 # check second date range value excludes what's later
51 self.assertEqual(Day.by_date_range_filled(self.db_conn, date1, date2),
53 # check swapped (impossible) date range returns emptiness
54 self.assertEqual(Day.by_date_range_filled(self.db_conn, date3, date1),
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),
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,
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])
78 class TestsWithServer(TestCaseWithServer):
79 """Tests against our HTTP server/handler (and database)."""
82 def day_dict(date: str) -> dict[str, object]:
83 """Return JSON of Process to expect."""
84 return {'id': date, 'comment': '', 'todos': []}
86 def test_do_GET_day(self) -> None:
87 """Test GET /day basics."""
89 date = date_in_n_days(0)
90 day = self.day_dict(date)
91 expected: dict[str, object]
92 expected = {'day': date,
97 'conditions_present': [],
99 '_library': {'Day': self.as_refs([day])}}
100 self.check_json_get('/day', expected)
101 # check "today", "yesterday", "tomorrow" days
102 self.check_json_get('/day?date=today', expected)
103 day = self.day_dict(date_in_n_days(1))
104 expected['day'] = day['id']
105 assert isinstance(expected['_library'], dict)
106 expected['_library']['Day'] = self.as_refs([day])
107 self.check_json_get('/day?date=tomorrow', expected)
108 day = self.day_dict(date_in_n_days(-1))
109 expected['day'] = day['id']
110 expected['_library']['Day'] = self.as_refs([day])
111 self.check_json_get('/day?date=yesterday', expected)
112 # check wrong day strings
113 self.check_get('/day?date=foo', 400)
114 self.check_get('/day?date=2024-02-30', 400)
117 day = self.day_dict(date)
118 expected['day'] = day['id']
119 expected['_library']['Day'] = self.as_refs([day])
120 self.check_json_get(f'/day?date={date}', expected)
122 post_day = {'day_comment': 'foo', 'make_type': ''}
123 self.check_post(post_day, f'/day?date={date}', 302,
124 f'/day?date={date}&make_type=')
125 day['comment'] = post_day['day_comment']
126 self.check_json_get(f'/day?date={date}', expected)
127 # check GET parameter POST not affecting reply to non-parameter GET
128 post_day['make_type'] = 'foo'
129 self.check_post(post_day, f'/day?date={date}', 302,
130 f'/day?date={date}&make_type=foo')
131 self.check_json_get(f'/day?date={date}', expected)
132 expected['make_type'] = 'bar'
133 self.check_json_get(f'/day?date={date}&make_type=bar', expected)
135 def test_do_GET(self) -> None:
136 """Test /day and /calendar response codes, and / redirect."""
137 self.check_get('/calendar', 200)
138 self.check_get('/calendar?start=&end=', 200)
139 self.check_get('/calendar?start=today&end=today', 200)
140 self.check_get('/calendar?start=2024-01-01&end=2025-01-01', 200)
141 self.check_get('/calendar?start=foo', 400)
143 def test_do_POST_day(self) -> None:
144 """Test POST /day."""
145 form_data = {'day_comment': '', 'make_type': 'full'}
146 self.check_post(form_data, '/day', 400)
147 self.check_post(form_data, '/day?date=foo', 400)
148 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
149 self.check_post({'foo': ''}, '/day?date=2024-01-01', 400)