home · contact · privacy
Slightly improve and re-organize Condition tests.
[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_from_table_row(self) -> None:
57         """Test .from_table_row() properly reads in class from DB"""
58         self.check_from_table_row()
59
60     def test_Day_by_id(self) -> None:
61         """Test .by_id()."""
62         self.check_by_id()
63
64     def test_Day_by_date_range_filled(self) -> None:
65         """Test Day.by_date_range_filled."""
66         date1, date2, date3 = self.default_ids
67         day1, day2, day3 = self.check_all()
68         # check date range is a closed interval
69         self.assertEqual(Day.by_date_range_filled(self.db_conn, date1, date3),
70                          [day1, day2, day3])
71         # check first date range value excludes what's earlier
72         self.assertEqual(Day.by_date_range_filled(self.db_conn, date2, date3),
73                          [day2, day3])
74         # check second date range value excludes what's later
75         self.assertEqual(Day.by_date_range_filled(self.db_conn, date1, date2),
76                          [day1, day2])
77         # check swapped (impossible) date range returns emptiness
78         self.assertEqual(Day.by_date_range_filled(self.db_conn, date3, date1),
79                          [])
80         # check fill_gaps= instantiates unsaved dates within date range
81         # (but does not store them)
82         day5 = Day('2024-01-05')
83         day6 = Day('2024-01-06')
84         day6.save(self.db_conn)
85         day7 = Day('2024-01-07')
86         self.assertEqual(Day.by_date_range_filled(self.db_conn,
87                                                   day5.date, day7.date),
88                          [day5, day6, day7])
89         self.check_storage([day1, day2, day3, day6])
90         # check 'today' is interpreted as today's date
91         today = Day(date_in_n_days(0))
92         today.save(self.db_conn)
93         self.assertEqual(Day.by_date_range_filled(self.db_conn,
94                                                   'today', 'today'),
95                          [today])
96
97     def test_Day_remove(self) -> None:
98         """Test .remove() effects on DB and cache."""
99         self.check_remove()
100
101     def test_Day_singularity(self) -> None:
102         """Test pointers made for single object keep pointing to it."""
103         self.check_singularity('day_comment', 'boo')
104
105
106 class TestsWithServer(TestCaseWithServer):
107     """Tests against our HTTP server/handler (and database)."""
108
109     def test_get_json(self) -> None:
110         """Test /day for JSON response."""
111         self.conn.request('GET', '/day?date=2024-01-01')
112         response = self.conn.getresponse()
113         self.assertEqual(response.status, 200)
114         expected = {'day': {'id': '2024-01-01',
115                             'comment': '',
116                             'todos': []},
117                     'top_nodes': [],
118                     'make_type': '',
119                     'enablers_for': {},
120                     'disablers_for': {},
121                     'conditions_present': [],
122                     'processes': []}
123         retrieved = json_loads(response.read().decode())
124         self.assertEqual(expected, retrieved)
125
126     def test_do_GET(self) -> None:
127         """Test /day and /calendar response codes, and / redirect."""
128         self.check_get('/day', 200)
129         self.check_get('/day?date=3000-01-01', 200)
130         self.check_get('/day?date=FOO', 400)
131         self.check_get('/calendar', 200)
132         self.check_get('/calendar?start=&end=', 200)
133         self.check_get('/calendar?start=today&end=today', 200)
134         self.check_get('/calendar?start=2024-01-01&end=2025-01-01', 200)
135         self.check_get('/calendar?start=foo', 400)
136
137     def test_do_POST_day(self) -> None:
138         """Test POST /day."""
139         form_data = {'day_comment': '', 'make_type': 'full'}
140         self.check_post(form_data, '/day', 400)
141         self.check_post(form_data, '/day?date=foo', 400)
142         self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
143         self.check_post({'foo': ''}, '/day?date=2024-01-01', 400)