# Copyright 2023 (c) Anna Schumaker. """Tests the `xfstestsdb` command.""" import io import argparse import unittest import unittest.mock import xfstestsdb class TestXfstestsdb(unittest.TestCase): """Tests the xfstestsdb command operation.""" def setUp(self): """Set up common variables.""" self.xfstestsdb = xfstestsdb.Command() def test_init(self): """Test that the xfstestsdb instance is set up properly.""" self.assertIsInstance(self.xfstestsdb.parser, argparse.ArgumentParser) self.assertIsInstance(self.xfstestsdb.subparser, argparse.Action) self.assertIsInstance(self.xfstestsdb.sql, xfstestsdb.sqlite.Connection) def test_close(self): """Test that the xfstestsdb instance closes the database.""" self.xfstestsdb.__del__() self.assertFalse(self.xfstestsdb.sql.connected) @unittest.mock.patch("sys.stdout", new_callable=io.StringIO) def test_run_empty(self, mock_stdout: io.StringIO): """Test calling xfstestdb.run() with an empty argument list.""" self.xfstestsdb.run([]) self.assertRegex(mock_stdout.getvalue(), r"^usage: pytest \[\-h\] \[\-\-version\]" r"\s+\{.*?}\s+...$") def test_run(self): """Test running the xfstestsdb.""" parser = self.xfstestsdb.subparser.add_parser("test-run", help="help") test_passed = False def test_func(args: argparse.Namespace) -> None: nonlocal test_passed self.assertTrue(self.xfstestsdb.sql.sql.in_transaction) test_passed = True parser.set_defaults(function=test_func) self.xfstestsdb.run(["test-run"]) self.assertTrue(test_passed) @unittest.mock.patch("sys.stdout", new_callable=io.StringIO) def test_version(self, mock_stdout: io.StringIO): """Test printing version information.""" self.assertEqual(xfstestsdb.MAJOR, 1) self.assertEqual(xfstestsdb.MINOR, 6) self.xfstestsdb.run(["--version"]) self.assertEqual(mock_stdout.getvalue(), "xfstestsdb v1.6-debug\n")