xfstestsdb: Add a sqlite.Connection instance to the root Command

And take some extra steps to make sure we clean it up when the Command
is finished.

Signed-off-by: Anna Schumaker <anna@nowheycreamery.com>
This commit is contained in:
Anna Schumaker 2023-01-31 16:55:29 -05:00
parent 929c1dd5eb
commit e3cf7e7017
2 changed files with 16 additions and 1 deletions

View File

@ -18,6 +18,13 @@ class TestXfstestsdb(unittest.TestCase):
"""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):
@ -34,6 +41,7 @@ class TestXfstestsdb(unittest.TestCase):
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)

View File

@ -1,6 +1,7 @@
# Copyright 2023 (c) Anna Schumaker
"""Implements the toplevel xfstestsdb command."""
import argparse
from . import sqlite
MAJOR = 1
MINOR = 0
@ -16,8 +17,13 @@ class Command:
self.parser.add_argument("--version", action="store_true",
help="show version number and exit")
self.subparser = self.parser.add_subparsers(title="commands")
self.sql = sqlite.Connection()
self.commands = {}
def __del__(self) -> None:
"""Clean up."""
self.sql.close()
def run(self, args: list = None) -> None:
"""Run the xfstestsdb command."""
parsed = self.parser.parse_args(args)
@ -25,4 +31,5 @@ class Command:
print(f"xfstestsdb v{MAJOR}.{MINOR}"
f"{'-debug' if __debug__ else ''}")
else:
parsed.function(parsed)
with self.sql:
parsed.function(parsed)