home · contact · privacy
Refactor Days tests into those with and without DB setup need.
authorChristian Heller <c.heller@plomlompom.de>
Sun, 17 Mar 2024 05:08:45 +0000 (06:08 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 17 Mar 2024 05:08:45 +0000 (06:08 +0100)
tests/test_days.py

index 5e3713882ca622d207026792f2f47df23d807283..d3a7bba656d19d39f35b93535564e4e3d17b0a2c 100644 (file)
@@ -7,8 +7,8 @@ from plomtask.misc import HandledException
 from plomtask.db import DatabaseFile, DatabaseConnection
 
 
-class DayTests(TestCase):
-    """Tests for testing the days module."""
+class TestsSansDB(TestCase):
+    """Days module tests not requiring DB setup."""
 
     def test_Day_dates(self):
         """Test Day's date format."""
@@ -36,39 +36,48 @@ class DayTests(TestCase):
         """Test Day.weekday."""
         self.assertEqual(Day('2024-03-17').weekday, 'Sunday')
 
+
+class TestsWithDB(TestCase):
+    """Days module tests not requiring DB setup."""
+
+    def setUp(self):
+        timestamp = datetime.now().timestamp()
+        self.db_file = DatabaseFile(f'test_db:{timestamp}')
+        self.db_file.remake()
+        self.db_conn = DatabaseConnection(self.db_file)
+
+    def tearDown(self):
+        self.db_conn.close()
+        remove_file(self.db_file.path)
+
     def test_Day_by_date(self):
         """Test Day.by_date()."""
-        timestamp = datetime.now().timestamp()
-        db_file = DatabaseFile(f'test_db:{timestamp}')
-        db_file.remake()
-        conn = DatabaseConnection(db_file)
-        self.assertEqual(None, Day.by_date(conn, '2024-01-01'))
-        Day('2024-01-01').save(conn)
-        self.assertEqual(Day('2024-01-01'), Day.by_date(conn, '2024-01-01'))
-        conn.close()
-        remove_file(db_file.path)
+        self.assertEqual(None, Day.by_date(self.db_conn, '2024-01-01'))
+        Day('2024-01-01').save(self.db_conn)
+        self.assertEqual(Day('2024-01-01'),
+                         Day.by_date(self.db_conn, '2024-01-01'))
 
     def test_Day_all(self):
         """Test Day.all(), especially in regards to date range filtering."""
-        timestamp = datetime.now().timestamp()
-        db_file = DatabaseFile(f'test_db:{timestamp}')
-        db_file.remake()
-        conn = DatabaseConnection(db_file)
         day1 = Day('2024-01-01')
         day2 = Day('2024-01-02')
         day3 = Day('2024-01-03')
-        day1.save(conn)
-        day2.save(conn)
-        day3.save(conn)
-        self.assertEqual(Day.all(conn), [day1, day2, day3])
-        self.assertEqual(Day.all(conn, ('', '')), [day1, day2, day3])
-        self.assertEqual(Day.all(conn, ('2024-01-01', '2024-01-03')),
+        day1.save(self.db_conn)
+        day2.save(self.db_conn)
+        day3.save(self.db_conn)
+        self.assertEqual(Day.all(self.db_conn),
+                         [day1, day2, day3])
+        self.assertEqual(Day.all(self.db_conn, ('', '')),
                          [day1, day2, day3])
-        self.assertEqual(Day.all(conn, ('2024-01-02', '2024-01-03')),
+        self.assertEqual(Day.all(self.db_conn, ('2024-01-01', '2024-01-03')),
+                         [day1, day2, day3])
+        self.assertEqual(Day.all(self.db_conn, ('2024-01-02', '2024-01-03')),
                          [day2, day3])
-        self.assertEqual(Day.all(conn, ('2024-01-03', '')), [day3])
-        self.assertEqual(Day.all(conn, ('2024-01-01', '')), [day1, day2, day3])
-        self.assertEqual(Day.all(conn, ('', '2024-01-02')), [day1, day2])
-        self.assertEqual(Day.all(conn, ('2024-01-03, 2024-01-01')), [])
-        conn.close()
-        remove_file(db_file.path)
+        self.assertEqual(Day.all(self.db_conn, ('2024-01-03', '')),
+                         [day3])
+        self.assertEqual(Day.all(self.db_conn, ('2024-01-01', '')),
+                         [day1, day2, day3])
+        self.assertEqual(Day.all(self.db_conn, ('', '2024-01-02')),
+                         [day1, day2])
+        self.assertEqual(Day.all(self.db_conn, ('2024-01-03, 2024-01-01')),
+                         [])