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."""
13 legal_ids = ['2024-01-01']
14 illegal_ids = ['foo', '2024-02-30', '2024-02-01 23:00:00']
16 def test_Day_valid_date(self) -> None:
17 """Test Day's date format validation and parsing."""
18 with self.assertRaises(BadFormatException):
20 with self.assertRaises(BadFormatException):
22 with self.assertRaises(BadFormatException):
23 Day('2024-02-01 23:00:00')
24 self.assertEqual(datetime(2024, 1, 1), Day('2024-01-01').datetime)
26 def test_Day_sorting(self) -> None:
27 """Test sorting by .__lt__ and Day.__eq__."""
28 day1 = Day('2024-01-01')
29 day2 = Day('2024-01-02')
30 day3 = Day('2024-01-03')
31 days = [day3, day1, day2]
32 self.assertEqual(sorted(days), [day1, day2, day3])
34 def test_Day_weekday(self) -> None:
35 """Test Day.weekday."""
36 self.assertEqual(Day('2024-03-17').weekday, 'Sunday')
38 def test_Day_neighbor_dates(self) -> None:
39 """Test Day.prev_date and Day.next_date."""
40 self.assertEqual(Day('2024-01-01').prev_date, '2023-12-31')
41 self.assertEqual(Day('2023-02-28').next_date, '2023-03-01')
44 class TestsWithDB(TestCaseWithDB):
45 """Tests requiring DB, but not server setup."""
47 default_ids = ('2024-01-01', '2024-01-02', '2024-01-03')
49 def test_Day_by_date_range_filled(self) -> None:
50 """Test Day.by_date_range_filled."""
51 date1, date2, date3 = self.default_ids
55 day1.save(self.db_conn)
56 day2.save(self.db_conn)
57 day3.save(self.db_conn)
58 # check date range is a closed interval
59 self.assertEqual(Day.by_date_range_filled(self.db_conn, date1, date3),
61 # check first date range value excludes what's earlier
62 self.assertEqual(Day.by_date_range_filled(self.db_conn, date2, date3),
64 # check second date range value excludes what's later
65 self.assertEqual(Day.by_date_range_filled(self.db_conn, date1, date2),
67 # check swapped (impossible) date range returns emptiness
68 self.assertEqual(Day.by_date_range_filled(self.db_conn, date3, date1),
70 # check fill_gaps= instantiates unsaved dates within date range
71 # (but does not store them)
72 day5 = Day('2024-01-05')
73 day6 = Day('2024-01-06')
74 day6.save(self.db_conn)
75 day7 = Day('2024-01-07')
76 self.assertEqual(Day.by_date_range_filled(self.db_conn,
77 day5.date, day7.date),
79 self.check_identity_with_cache_and_db([day1, day2, day3, day6])
80 # check 'today' is interpreted as today's date
81 today = Day(date_in_n_days(0))
82 today.save(self.db_conn)
83 self.assertEqual(Day.by_date_range_filled(self.db_conn,
88 class TestsWithServer(TestCaseWithServer):
89 """Tests against our HTTP server/handler (and database)."""
91 def test_get_json(self) -> None:
92 """Test /day for JSON response."""
93 self.conn.request('GET', '/day?date=2024-01-01')
94 response = self.conn.getresponse()
95 self.assertEqual(response.status, 200)
96 expected = {'day': {'id': '2024-01-01',
103 'conditions_present': [],
105 retrieved = json_loads(response.read().decode())
106 self.assertEqual(expected, retrieved)
108 def test_do_GET(self) -> None:
109 """Test /day and /calendar response codes, and / redirect."""
110 self.check_get('/day', 200)
111 self.check_get('/day?date=3000-01-01', 200)
112 self.check_get('/day?date=FOO', 400)
113 self.check_get('/calendar', 200)
114 self.check_get('/calendar?start=&end=', 200)
115 self.check_get('/calendar?start=today&end=today', 200)
116 self.check_get('/calendar?start=2024-01-01&end=2025-01-01', 200)
117 self.check_get('/calendar?start=foo', 400)
119 def test_do_POST_day(self) -> None:
120 """Test POST /day."""
121 form_data = {'day_comment': '', 'make_type': 'full'}
122 self.check_post(form_data, '/day', 400)
123 self.check_post(form_data, '/day?date=foo', 400)
124 self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
125 self.check_post({'foo': ''}, '/day?date=2024-01-01', 400)