home · contact · privacy
Minor refactorings.
[plomtask] / tests / test_days.py
1 """Test days module."""
2 from unittest import TestCase
3 from datetime import datetime
4 from os import remove as remove_file
5 from plomtask.days import Day
6 from plomtask.misc import HandledException
7 from plomtask.db import DatabaseFile, DatabaseConnection
8
9
10 class TestsSansDB(TestCase):
11     """Days module tests not requiring DB setup."""
12
13     def test_Day_dates(self):
14         """Test Day's date format."""
15         with self.assertRaises(HandledException):
16             Day('foo')
17         with self.assertRaises(HandledException):
18             Day(None)
19         with self.assertRaises(HandledException):
20             Day(3)
21         with self.assertRaises(HandledException):
22             Day('2024-02-30')
23         with self.assertRaises(HandledException):
24             Day('2024-02-01 23:00:00')
25         self.assertEqual(datetime(2024, 1, 1), Day('2024-01-01').datetime)
26
27     def test_Day_sorting(self):
28         """Test Day.__lt__."""
29         day1 = Day('2024-01-01')
30         day2 = Day('2024-01-02')
31         day3 = Day('2024-01-03')
32         days = [day3, day1, day2]
33         self.assertEqual(sorted(days), [day1, day2, day3])
34
35     def test_Day_weekday(self):
36         """Test Day.weekday."""
37         self.assertEqual(Day('2024-03-17').weekday, 'Sunday')
38
39
40 class TestsWithDB(TestCase):
41     """Days module tests not requiring DB setup."""
42
43     def setUp(self):
44         timestamp = datetime.now().timestamp()
45         self.db_file = DatabaseFile(f'test_db:{timestamp}')
46         self.db_file.remake()
47         self.db_conn = DatabaseConnection(self.db_file)
48
49     def tearDown(self):
50         self.db_conn.close()
51         remove_file(self.db_file.path)
52
53     def test_Day_by_date(self):
54         """Test Day.by_date()."""
55         self.assertEqual(None, Day.by_date(self.db_conn, '2024-01-01'))
56         Day('2024-01-01').save(self.db_conn)
57         self.assertEqual(Day('2024-01-01'),
58                          Day.by_date(self.db_conn, '2024-01-01'))
59
60     def test_Day_all(self):
61         """Test Day.all(), especially in regards to date range filtering."""
62         day1 = Day('2024-01-01')
63         day2 = Day('2024-01-02')
64         day3 = Day('2024-01-03')
65         day1.save(self.db_conn)
66         day2.save(self.db_conn)
67         day3.save(self.db_conn)
68         self.assertEqual(Day.all(self.db_conn),
69                          [day1, day2, day3])
70         self.assertEqual(Day.all(self.db_conn, ('', '')),
71                          [day1, day2, day3])
72         self.assertEqual(Day.all(self.db_conn, ('2024-01-01', '2024-01-03')),
73                          [day1, day2, day3])
74         self.assertEqual(Day.all(self.db_conn, ('2024-01-02', '2024-01-03')),
75                          [day2, day3])
76         self.assertEqual(Day.all(self.db_conn, ('2024-01-03', '')),
77                          [day3])
78         self.assertEqual(Day.all(self.db_conn, ('2024-01-01', '')),
79                          [day1, day2, day3])
80         self.assertEqual(Day.all(self.db_conn, ('', '2024-01-02')),
81                          [day1, day2])
82         self.assertEqual(Day.all(self.db_conn, ('2024-01-03, 2024-01-01')),
83                          [])
84
85     def test_Day_comment(self):
86         """Test Day.by_date()."""
87         self.assertEqual(None, Day.by_date(self.db_conn, '2024-01-01'))
88         d1 = Day('2024-01-01')
89         d1.save(self.db_conn)
90         day_retrieved = Day.by_date(self.db_conn, d1.date)
91         self.assertEqual(day_retrieved.comment, '')
92         d2 = Day('2024-01-02', 'foo')
93         d2.save(self.db_conn)
94         day_retrieved = Day.by_date(self.db_conn, d2.date)
95         self.assertEqual(day_retrieved.comment, 'foo')