home · contact · privacy
Some test utils refactoring.
[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_saving_and_caching(self) -> None:
48         """Test storage of instances.
49
50         We don't use the parent class's method here because the checked class
51         has too different a handling of IDs.
52         """
53         kwargs = {'date': self.default_ids[0], 'comment': 'foo'}
54         self.check_saving_and_caching(**kwargs)
55
56     def test_Day_by_id(self) -> None:
57         """Test .by_id()."""
58         self.check_by_id()
59
60     def test_Day_by_date_range_filled(self) -> None:
61         """Test Day.by_date_range_filled."""
62         date1, date2, date3 = self.default_ids
63         day1 = Day(date1)
64         day2 = Day(date2)
65         day3 = Day(date3)
66         day1.save(self.db_conn)
67         day2.save(self.db_conn)
68         day3.save(self.db_conn)
69         # check date range is a closed interval
70         self.assertEqual(Day.by_date_range_filled(self.db_conn, date1, date3),
71                          [day1, day2, day3])
72         # check first date range value excludes what's earlier
73         self.assertEqual(Day.by_date_range_filled(self.db_conn, date2, date3),
74                          [day2, day3])
75         # check second date range value excludes what's later
76         self.assertEqual(Day.by_date_range_filled(self.db_conn, date1, date2),
77                          [day1, day2])
78         # check swapped (impossible) date range returns emptiness
79         self.assertEqual(Day.by_date_range_filled(self.db_conn, date3, date1),
80                          [])
81         # check fill_gaps= instantiates unsaved dates within date range
82         # (but does not store them)
83         day5 = Day('2024-01-05')
84         day6 = Day('2024-01-06')
85         day6.save(self.db_conn)
86         day7 = Day('2024-01-07')
87         self.assertEqual(Day.by_date_range_filled(self.db_conn,
88                                                   day5.date, day7.date),
89                          [day5, day6, day7])
90         self.check_storage([day1, day2, day3, day6])
91         # check 'today' is interpreted as today's date
92         today = Day(date_in_n_days(0))
93         today.save(self.db_conn)
94         self.assertEqual(Day.by_date_range_filled(self.db_conn,
95                                                   'today', 'today'),
96                          [today])
97
98     def test_Day_remove(self) -> None:
99         """Test .remove() effects on DB and cache."""
100         self.check_remove()
101
102
103 class TestsWithServer(TestCaseWithServer):
104     """Tests against our HTTP server/handler (and database)."""
105
106     def test_get_json(self) -> None:
107         """Test /day for JSON response."""
108         self.conn.request('GET', '/day?date=2024-01-01')
109         response = self.conn.getresponse()
110         self.assertEqual(response.status, 200)
111         expected = {'day': {'id': '2024-01-01',
112                             'comment': '',
113                             'todos': []},
114                     'top_nodes': [],
115                     'make_type': '',
116                     'enablers_for': {},
117                     'disablers_for': {},
118                     'conditions_present': [],
119                     'processes': []}
120         retrieved = json_loads(response.read().decode())
121         self.assertEqual(expected, retrieved)
122
123     def test_do_GET(self) -> None:
124         """Test /day and /calendar response codes, and / redirect."""
125         self.check_get('/day', 200)
126         self.check_get('/day?date=3000-01-01', 200)
127         self.check_get('/day?date=FOO', 400)
128         self.check_get('/calendar', 200)
129         self.check_get('/calendar?start=&end=', 200)
130         self.check_get('/calendar?start=today&end=today', 200)
131         self.check_get('/calendar?start=2024-01-01&end=2025-01-01', 200)
132         self.check_get('/calendar?start=foo', 400)
133
134     def test_do_POST_day(self) -> None:
135         """Test POST /day."""
136         form_data = {'day_comment': '', 'make_type': 'full'}
137         self.check_post(form_data, '/day', 400)
138         self.check_post(form_data, '/day?date=foo', 400)
139         self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
140         self.check_post({'foo': ''}, '/day?date=2024-01-01', 400)