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
10 class TestsSansDB(TestCase):
11 """Days module tests not requiring DB setup."""
13 def test_Day_dates(self):
14 """Test Day's date format."""
15 with self.assertRaises(HandledException):
17 with self.assertRaises(HandledException):
19 with self.assertRaises(HandledException):
21 with self.assertRaises(HandledException):
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)
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])
35 def test_Day_weekday(self):
36 """Test Day.weekday."""
37 self.assertEqual(Day('2024-03-17').weekday, 'Sunday')
40 class TestsWithDB(TestCase):
41 """Days module tests not requiring DB setup."""
44 timestamp = datetime.now().timestamp()
45 self.db_file = DatabaseFile(f'test_db:{timestamp}')
47 self.db_conn = DatabaseConnection(self.db_file)
51 remove_file(self.db_file.path)
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'))
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),
70 self.assertEqual(Day.all(self.db_conn, ('', '')),
72 self.assertEqual(Day.all(self.db_conn, ('2024-01-01', '2024-01-03')),
74 self.assertEqual(Day.all(self.db_conn, ('2024-01-02', '2024-01-03')),
76 self.assertEqual(Day.all(self.db_conn, ('2024-01-03', '')),
78 self.assertEqual(Day.all(self.db_conn, ('2024-01-01', '')),
80 self.assertEqual(Day.all(self.db_conn, ('', '2024-01-02')),
82 self.assertEqual(Day.all(self.db_conn, ('2024-01-03, 2024-01-01')),
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')
90 day_retrieved = Day.by_date(self.db_conn, d1.date)
91 self.assertEqual(day_retrieved.comment, '')
92 d2 = Day('2024-01-02', 'foo')
94 day_retrieved = Day.by_date(self.db_conn, d2.date)
95 self.assertEqual(day_retrieved.comment, 'foo')