home · contact · privacy
Fix miswritten test.
[plomtask] / tests / test_days.py
1 """Test days module."""
2 from unittest import TestCase
3 from threading import Thread
4 from http.client import HTTPConnection
5 from urllib.parse import urlencode
6 from datetime import datetime
7 from os import remove as remove_file
8 from plomtask.http import TaskHandler, TaskServer
9 from plomtask.days import Day
10 from plomtask.misc import HandledException
11 from plomtask.db import DatabaseFile, DatabaseConnection
12
13
14 class TestsSansDB(TestCase):
15     """Days module tests not requiring DB setup."""
16
17     def test_Day_dates(self):
18         """Test Day's date format."""
19         with self.assertRaises(HandledException):
20             Day('foo')
21         with self.assertRaises(HandledException):
22             Day(None)
23         with self.assertRaises(HandledException):
24             Day(3)
25         with self.assertRaises(HandledException):
26             Day('2024-02-30')
27         with self.assertRaises(HandledException):
28             Day('2024-02-01 23:00:00')
29         self.assertEqual(datetime(2024, 1, 1), Day('2024-01-01').datetime)
30
31     def test_Day_sorting(self):
32         """Test Day.__lt__."""
33         day1 = Day('2024-01-01')
34         day2 = Day('2024-01-02')
35         day3 = Day('2024-01-03')
36         days = [day3, day1, day2]
37         self.assertEqual(sorted(days), [day1, day2, day3])
38
39     def test_Day_weekday(self):
40         """Test Day.weekday."""
41         self.assertEqual(Day('2024-03-17').weekday, 'Sunday')
42
43
44 class TestsWithDB(TestCase):
45     """Days module tests not requiring DB setup."""
46
47     def setUp(self):
48         timestamp = datetime.now().timestamp()
49         self.db_file = DatabaseFile(f'test_db:{timestamp}')
50         self.db_file.remake()
51         self.db_conn = DatabaseConnection(self.db_file)
52
53     def tearDown(self):
54         self.db_conn.close()
55         remove_file(self.db_file.path)
56
57     def test_Day_by_date(self):
58         """Test Day.by_date()."""
59         self.assertEqual(None, Day.by_date(self.db_conn, '2024-01-01'))
60         Day('2024-01-01').save(self.db_conn)
61         self.assertEqual(Day('2024-01-01'),
62                          Day.by_date(self.db_conn, '2024-01-01'))
63         self.assertEqual(None,
64                          Day.by_date(self.db_conn, '2024-01-02'))
65         self.assertEqual(Day('2024-01-02'),
66                          Day.by_date(self.db_conn, '2024-01-02', create=True))
67
68     def test_Day_all(self):
69         """Test Day.all(), especially in regards to date range filtering."""
70         day1 = Day('2024-01-01')
71         day2 = Day('2024-01-02')
72         day3 = Day('2024-01-03')
73         day1.save(self.db_conn)
74         day2.save(self.db_conn)
75         day3.save(self.db_conn)
76         self.assertEqual(Day.all(self.db_conn),
77                          [day1, day2, day3])
78         self.assertEqual(Day.all(self.db_conn, ('', '')),
79                          [day1, day2, day3])
80         self.assertEqual(Day.all(self.db_conn, ('2024-01-01', '2024-01-03')),
81                          [day1, day2, day3])
82         self.assertEqual(Day.all(self.db_conn, ('2024-01-02', '2024-01-03')),
83                          [day2, day3])
84         self.assertEqual(Day.all(self.db_conn, ('2024-01-03', '')),
85                          [day3])
86         self.assertEqual(Day.all(self.db_conn, ('2024-01-01', '')),
87                          [day1, day2, day3])
88         self.assertEqual(Day.all(self.db_conn, ('', '2024-01-02')),
89                          [day1, day2])
90         self.assertEqual(Day.all(self.db_conn, ('2024-01-03', '2024-01-01')),
91                          [])
92
93     def test_Day_comment(self):
94         """Test Day.by_date()."""
95         self.assertEqual(None, Day.by_date(self.db_conn, '2024-01-01'))
96         d1 = Day('2024-01-01')
97         d1.save(self.db_conn)
98         day_retrieved = Day.by_date(self.db_conn, d1.date)
99         self.assertEqual(day_retrieved.comment, '')
100         d2 = Day('2024-01-02', 'foo')
101         d2.save(self.db_conn)
102         day_retrieved = Day.by_date(self.db_conn, d2.date)
103         self.assertEqual(day_retrieved.comment, 'foo')
104
105
106 class TestsWithServer(TestsWithDB):
107     """Tests against our HTTP server/handler (and database)."""
108
109     def setUp(self):
110         super().setUp()
111         self.httpd = TaskServer(self.db_file, ('localhost', 0), TaskHandler)
112         self.server_thread = Thread(target=self.httpd.serve_forever)
113         self.server_thread.daemon = True
114         self.server_thread.start()
115
116     def test_do_POST_day(self):
117         """Test POST /day and its effect on the database."""
118         http_conn = HTTPConnection(*self.httpd.server_address)
119         form_data = {'comment': 'foo'}
120         encoded_form_data = urlencode(form_data).encode('utf-8')
121         headers = {'Content-Type': 'application/x-www-form-urlencoded',
122                    'Content-Length': str(len(encoded_form_data))}
123         http_conn.request('POST', '/day?date=FOO',
124                           body=encoded_form_data, headers=headers)
125         self.assertEqual(http_conn.getresponse().status, 400)
126         self.assertEqual(Day.all(self.db_conn), [])
127         http_conn.request('POST', '/day?date=2024-01-01',
128                           body=encoded_form_data, headers=headers)
129         self.assertEqual(http_conn.getresponse().status, 302)
130         retrieved = Day.by_date(self.db_conn, '2024-01-01')
131         self.assertEqual(retrieved.comment, 'foo')
132         self.assertEqual(Day.all(self.db_conn), [retrieved])
133
134     def test_do_GET(self):
135         """Test /day and /calendar response codes."""
136         http_conn = HTTPConnection(*self.httpd.server_address)
137         http_conn.request('GET', '/day')
138         self.assertEqual(http_conn.getresponse().status, 200)
139         http_conn.request('GET', '/day?date=3000-01-01')
140         self.assertEqual(http_conn.getresponse().status, 200)
141         http_conn.request('GET', '/day?date=FOO')
142         self.assertEqual(http_conn.getresponse().status, 400)
143         http_conn.request('GET', '/calendar')
144         self.assertEqual(http_conn.getresponse().status, 200)
145
146     def tearDown(self):
147         self.httpd.shutdown()
148         self.httpd.server_close()
149         self.server_thread.join()
150         super().tearDown()