xfstestsdb/tests/test_tag.py

70 lines
2.9 KiB
Python

# Copyright 2023 (c) Anna Schumaker.
"""Tests the `xfstestsdb tag` command."""
import errno
import io
import unittest
import unittest.mock
import xfstestsdb.tag
class TestTag(unittest.TestCase):
"""Tests the `xfstestsdb tag` command."""
def setUp(self):
"""Set up common variables."""
self.xfstestsdb = xfstestsdb.Command()
self.tag = self.xfstestsdb.commands["tag"]
def test_init(self):
"""Check that the tag command was set up properly."""
self.assertIsInstance(self.tag, xfstestsdb.commands.Command)
self.assertIsInstance(self.tag, xfstestsdb.tag.Command)
self.assertEqual(self.xfstestsdb.subparser.choices["tag"],
self.tag.parser)
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_tag(self, mock_stdout: io.StringIO):
"""Test the `xfstestsdb tag` command with valid input."""
self.xfstestsdb.run(["new", "/dev/test"])
self.xfstestsdb.run(["tag", "1", "mytag"])
self.assertRegex(mock_stdout.getvalue(), "tag 'mytag' added to run #1")
cur = self.xfstestsdb.sql("SELECT * FROM tags")
rows = cur.fetchall()
self.assertEqual(len(rows), 1)
self.assertEqual(rows[0]["runid"], 1)
self.assertEqual(rows[0]["tag"], "mytag")
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_tag_duplicate(self, mock_stdout: io.StringIO):
"""Test the `xfstestsdb tag` command with a duplicate tag."""
self.xfstestsdb.run(["new", "/dev/test"])
self.xfstestsdb.sql("INSERT INTO tags (runid, tag) VALUES (?, ?)",
1, "mytag")
self.xfstestsdb.run(["tag", "1", "mytag"])
self.assertNotRegex(mock_stdout.getvalue(),
"tag 'mytag' added to run #1")
@unittest.mock.patch("sys.stderr", new_callable=io.StringIO)
def test_tag_error(self, mock_stderr: io.StringIO):
"""Test the `xfstestsdb tag` command with invalid input."""
with self.assertRaises(SystemExit):
self.xfstestsdb.run(["tag"])
self.assertRegex(mock_stderr.getvalue(),
"error: the following arguments are required: runid")
with self.assertRaises(SystemExit):
self.xfstestsdb.run(["tag", "3"])
self.assertRegex(mock_stderr.getvalue(),
"error: the following arguments are required: tag")
@unittest.mock.patch("sys.stderr", new_callable=io.StringIO)
def test_tag_enoent(self, mock_stderr: io.StringIO):
"""Test the `xfstestsdb tag` command with an invalid runid."""
with self.assertRaises(SystemExit) as sys_exit:
self.xfstestsdb.run(["tag", "2", "mytag"])
self.assertEqual(sys_exit.exception.code, errno.ENOENT)
self.assertRegex(mock_stderr.getvalue(),
"error: run #2 does not exist")