xfstestsdb: Create the xfstestsdb delete
command
This command deletes a row from the xfstests_runs table and prints either a success or failure message depending on if the row exists. I use an sqlite trigger to automatically remove tags for the matching run. Implements: #2 (`xfstestsdb delete`) Signed-off-by: Anna Schumaker <anna@nowheycreamery.com>
This commit is contained in:
parent
b4043b2fa9
commit
6987a13f54
61
tests/test_delete.py
Normal file
61
tests/test_delete.py
Normal file
|
@ -0,0 +1,61 @@
|
|||
# Copyright 2023 (c) Anna Schumaker.
|
||||
"""Tests the `xfstestsdb delete` command."""
|
||||
import errno
|
||||
import io
|
||||
import unittest
|
||||
import unittest.mock
|
||||
import xfstestsdb.delete
|
||||
|
||||
|
||||
class TestDelete(unittest.TestCase):
|
||||
"""Tests the `xfstestsdb delete` command."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up common variables."""
|
||||
self.xfstestsdb = xfstestsdb.Command()
|
||||
self.delete = self.xfstestsdb.commands["delete"]
|
||||
|
||||
def test_init(self):
|
||||
"""Check that the delete command was set up properly."""
|
||||
self.assertIsInstance(self.delete, xfstestsdb.commands.Command)
|
||||
self.assertIsInstance(self.delete, xfstestsdb.delete.Command)
|
||||
self.assertEqual(self.xfstestsdb.subparser.choices["delete"],
|
||||
self.delete.parser)
|
||||
|
||||
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
||||
def test_delete(self, mock_stdout: io.StringIO):
|
||||
"""Test the `xfstestsdb delete` command with valid input."""
|
||||
self.xfstestsdb.run(["new", "/dev/test"])
|
||||
self.xfstestsdb.run(["delete", "1"])
|
||||
self.assertRegex(mock_stdout.getvalue(), "run #1 has been deleted")
|
||||
|
||||
cur = self.xfstestsdb.sql("SELECT COUNT(*) FROM xfstests_runs")
|
||||
self.assertEqual(cur.fetchone()["COUNT(*)"], 0)
|
||||
|
||||
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
||||
def test_delete_tags(self, mock_stdout: io.StringIO):
|
||||
"""Test the `xfstestsdb delete` command on a tagged run."""
|
||||
self.xfstestsdb.run(["new", "/dev/test"])
|
||||
self.xfstestsdb.run(["tag", "1", "mytag"])
|
||||
self.xfstestsdb.run(["delete", "1"])
|
||||
|
||||
cur = self.xfstestsdb.sql("SELECT COUNT(*) FROM tags")
|
||||
self.assertEqual(cur.fetchone()["COUNT(*)"], 0)
|
||||
|
||||
@unittest.mock.patch("sys.stderr", new_callable=io.StringIO)
|
||||
def test_delete_error(self, mock_stderr: io.StringIO):
|
||||
"""Test the `xfstestsdb delete` command with invalid input."""
|
||||
with self.assertRaises(SystemExit):
|
||||
self.xfstestsdb.run(["delete"])
|
||||
self.assertRegex(mock_stderr.getvalue(),
|
||||
"error: the following arguments are required: runid")
|
||||
|
||||
@unittest.mock.patch("sys.stderr", new_callable=io.StringIO)
|
||||
def test_delete_enoent(self, mock_stderr: io.StringIO):
|
||||
"""Test the `xfstestsdb delete` command with an invalid runid."""
|
||||
with self.assertRaises(SystemExit) as sys_exit:
|
||||
self.xfstestsdb.run(["delete", "2"])
|
||||
|
||||
self.assertEqual(sys_exit.exception.code, errno.ENOENT)
|
||||
self.assertRegex(mock_stderr.getvalue(),
|
||||
"error: run #2 does not exist")
|
|
@ -2,6 +2,7 @@
|
|||
"""Implements the toplevel xfstestsdb command."""
|
||||
import argparse
|
||||
from . import sqlite
|
||||
from . import delete
|
||||
from . import new
|
||||
from . import rename
|
||||
from . import tag
|
||||
|
@ -22,7 +23,8 @@ class Command:
|
|||
help="show version number and exit")
|
||||
self.subparser = self.parser.add_subparsers(title="commands")
|
||||
self.sql = sqlite.Connection()
|
||||
self.commands = {"new": new.Command(self.subparser, self.sql),
|
||||
self.commands = {"delete": delete.Command(self.subparser, self.sql),
|
||||
"new": new.Command(self.subparser, self.sql),
|
||||
"rename": rename.Command(self.subparser, self.sql),
|
||||
"tag": tag.Command(self.subparser, self.sql),
|
||||
"untag": untag.Command(self.subparser, self.sql)}
|
||||
|
|
29
xfstestsdb/delete.py
Normal file
29
xfstestsdb/delete.py
Normal file
|
@ -0,0 +1,29 @@
|
|||
# Copyright 2023 (c) Anna Schumaker.
|
||||
"""The `xfstestsdb delete` command."""
|
||||
import argparse
|
||||
import errno
|
||||
import sys
|
||||
from . import commands
|
||||
from . import sqlite
|
||||
|
||||
|
||||
class Command(commands.Command):
|
||||
"""The `xfstestsdb delete` command."""
|
||||
|
||||
def __init__(self, subparser: argparse.Action,
|
||||
sql: sqlite.Connection) -> None:
|
||||
"""Set up the delete command."""
|
||||
super().__init__(subparser, sql, "delete",
|
||||
help="delete an xfstests run")
|
||||
self.parser.add_argument("runid", metavar="runid", nargs=1, type=int,
|
||||
help="runid of the xfstests run to delete")
|
||||
|
||||
def do_command(self, args: argparse.Namespace) -> None:
|
||||
"""Delete a row from the xfstests_runs table."""
|
||||
cur = self.sql("DELETE FROM xfstests_runs WHERE rowid=?",
|
||||
args.runid[0])
|
||||
if cur.rowcount == 0:
|
||||
print(f"error: run #{args.runid[0]} does not exist",
|
||||
file=sys.stderr)
|
||||
sys.exit(errno.ENOENT)
|
||||
print(f"run #{args.runid[0]} has been deleted")
|
|
@ -28,4 +28,6 @@ CREATE TABLE tags (
|
|||
tag TEXT NOT NULL,
|
||||
PRIMARY KEY (runid, tag),
|
||||
FOREIGN KEY (runid) REFERENCES xfstests_runs (runid)
|
||||
ON DELETE CASCADE
|
||||
ON UPDATE CASCADE
|
||||
);
|
||||
|
|
Loading…
Reference in New Issue
Block a user