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
11 class TestsSansDB(TestCase):
12 """Days module tests not requiring DB setup."""
14 def test_Day_valid_date(self) -> None:
15 """Test Day's date format validation and parsing."""
16 with self.assertRaises(BadFormatException):
18 with self.assertRaises(BadFormatException):
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)
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])
32 def test_Day_weekday(self) -> None:
33 """Test Day.weekday."""
34 self.assertEqual(Day('2024-03-17').weekday, 'Sunday')
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')
42 class TestsWithDB(TestCaseWithDB):
43 """Tests requiring DB, but not server setup."""
45 default_ids = ('2024-01-01', '2024-01-02', '2024-01-03')
47 def test_saving_and_caching(self) -> None:
48 """Test storage of instances.
50 We don't use the parent class's method here because the checked class
51 has too different a handling of IDs.
53 kwargs = {'date': self.default_ids[0], 'comment': 'foo'}
54 self.check_saving_and_caching(**kwargs)
56 def test_Day_by_id(self) -> None:
60 def test_Day_by_date_range_filled(self) -> None:
61 """Test Day.by_date_range_filled."""
62 date1, date2, date3 = self.default_ids
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),
72 # check first date range value excludes what's earlier
73 self.assertEqual(Day.by_date_range_filled(self.db_conn, date2, date3),
75 # check second date range value excludes what's later
76 self.assertEqual(Day.by_date_range_filled(self.db_conn, date1, date2),
78 # check swapped (impossible) date range returns emptiness
79 self.assertEqual(Day.by_date_range_filled(self.db_conn, date3, date1),
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),
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,
98 def test_Day_remove(self) -> None:
99 """Test .remove() effects on DB and cache."""
103 class TestsWithServer(TestCaseWithServer):
104 """Tests against our HTTP server/handler (and database)."""
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',
118 'conditions_present': [],
120 retrieved = json_loads(response.read().decode())
121 self.assertEqual(expected, retrieved)
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)
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)