xfstestsdb: Create a generic Command class

This is intended to be used as a base class for implementing other
xfstestsdb commands.

Signed-off-by: Anna Schumaker <anna@nowheycreamery.com>
This commit is contained in:
Anna Schumaker 2023-01-31 14:33:29 -05:00
parent e3cf7e7017
commit 2a94b5e519
2 changed files with 51 additions and 0 deletions

32
tests/test_commands.py Normal file
View File

@ -0,0 +1,32 @@
# Copyright 2023 (c) Anna Schumaker.
"""Tests the generic Command class."""
import argparse
import unittest
import xfstestsdb.commands
class TestCommand(unittest.TestCase):
"""Tests the xfstestsdb Command class."""
def setUp(self):
"""Set up common variables."""
self.sql = xfstestsdb.sqlite.Connection()
self.parser = argparse.ArgumentParser()
self.subparser = self.parser.add_subparsers(title="commands")
self.command = xfstestsdb.commands.Command(self.subparser, self.sql,
"test", "help text",
description="description")
def test_init(self):
"""Test that the Command was initialized properly."""
self.assertIsInstance(self.command.parser, argparse.ArgumentParser)
self.assertEqual(self.subparser.choices["test"], self.command.parser)
self.assertEqual(self.command.parser.description, "description")
self.assertEqual(self.command.parser._defaults["function"],
self.command.do_command)
self.assertEqual(self.command.sql, self.sql)
def test_do_command(self):
"""Test the do_command() function."""
with self.assertRaises(NotImplementedError):
self.command.do_command(argparse.Namespace())

19
xfstestsdb/commands.py Normal file
View File

@ -0,0 +1,19 @@
# Copyright 2023 (c) Anna Schumaker.
"""A generic Command class."""
import argparse
from . import sqlite
class Command:
"""A class to help implement xfstestsdb subcommands."""
def __init__(self, subparser: argparse.Action, sql: sqlite.Connection,
name: str, help: str, **kwargs) -> None:
"""Set up the Command."""
self.parser = subparser.add_parser(name, help=help, **kwargs)
self.parser.set_defaults(function=self.do_command)
self.sql = sql
def do_command(self, args: argparse.Namespace) -> None:
"""Do something."""
raise NotImplementedError