From: Christian Heller Date: Sun, 17 Mar 2024 05:08:45 +0000 (+0100) Subject: Refactor Days tests into those with and without DB setup need. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/static/%7B%7Byoutube_prefix%7D%7D%7B%7Bvideo_id%7D%7D?a=commitdiff_plain;h=a8ee9d57c7ec297ef93d616141ac8970869fb6ef;p=plomtask Refactor Days tests into those with and without DB setup need. --- diff --git a/tests/test_days.py b/tests/test_days.py index 5e37138..d3a7bba 100644 --- a/tests/test_days.py +++ b/tests/test_days.py @@ -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')), + [])