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."""
"""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')),
+ [])