xfstestsdb/tests/xunit/test_rename.py
Anna Schumaker 42ad3ad0fb xunit: Create the xfstestsdb xunit rename command
This command renames an xunit file associated with an xfstests run.

Implement: #4 (`xfstestsdb xunit rename`)
Signed-off-by: Anna Schumaker <anna@nowheycreamery.com>
2023-02-16 14:22:32 -05:00

95 lines
4.5 KiB
Python

# Copyright 2023 (c) Anna Schumaker.
"""Tests the `xfstestsdb xunit rename` command."""
import errno
import io
import unittest
import unittest.mock
import xfstestsdb.rename
import tests.xunit
class TestXunitRename(unittest.TestCase):
"""Tests the `xfstestsdb xunit rename` command."""
def setUp(self):
"""Set up common variables."""
self.xfstestsdb = xfstestsdb.Command()
self.xunit = self.xfstestsdb.commands["xunit"]
self.rename = self.xunit.commands["rename"]
def test_init(self):
"""Check that the rename command was set up properly."""
self.assertIsInstance(self.rename, xfstestsdb.commands.Command)
self.assertIsInstance(self.rename, xfstestsdb.xunit.rename.Command)
self.assertEqual(self.xunit.subparser.choices["rename"],
self.rename.parser)
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_rename(self, mock_stdout: io.StringIO):
"""Test the `xfstestsdb xunit rename` command with valid input."""
self.xfstestsdb.run(["new", "/dev/test"])
self.xfstestsdb.run(["xunit", "read", "1", str(tests.xunit.XUNIT_1)])
self.xfstestsdb.run(["xunit", "rename", "1", "test-1", "test-new"])
self.assertRegex(mock_stdout.getvalue(),
"renamed run #1 xunit from 'test-1' to 'test-new'")
cur = self.xfstestsdb.sql("SELECT name FROM xunits WHERE runid=1")
self.assertEqual(cur.fetchone()["name"], "test-new")
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_rename_same(self, mock_stdout: io.StringIO):
"""Test the `xfstestsdb xunit rename` command with the same name."""
self.xfstestsdb.run(["new", "/dev/test"])
self.xfstestsdb.run(["xunit", "read", "1", str(tests.xunit.XUNIT_1)])
self.xfstestsdb.run(["xunit", "rename", "1", "test-1", "test-1"])
self.assertNotRegex(mock_stdout.getvalue(),
"renamed run #1 xunit from 'test-1' to 'test-1'")
@unittest.mock.patch("sys.stderr", new_callable=io.StringIO)
def test_rename_error(self, mock_stderr: io.StringIO):
"""Test the `xfstestsdb xunit rename` command with invalid input."""
with self.assertRaises(SystemExit):
self.xfstestsdb.run(["xunit", "rename"])
self.assertRegex(mock_stderr.getvalue(),
"error: the following arguments are required: runid")
with self.assertRaises(SystemExit):
self.xfstestsdb.run(["xunit", "rename", "3"])
self.assertRegex(mock_stderr.getvalue(),
"error: the following arguments are required: old")
with self.assertRaises(SystemExit):
self.xfstestsdb.run(["xunit", "rename", "3", "old"])
self.assertRegex(mock_stderr.getvalue(),
"error: the following arguments are required: new")
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
@unittest.mock.patch("sys.stderr", new_callable=io.StringIO)
def test_rename_runid_enoent(self, mock_stderr: io.StringIO,
mock_stdout: io.StringIO):
"""Test the `xfstestsdb xunit rename` command with an invalid runid."""
self.xfstestsdb.run(["new", "/dev/test"])
with self.assertRaises(SystemExit) as sys_exit:
self.xfstestsdb.run(["xunit", "rename", "2", "old", "new"])
self.assertEqual(sys_exit.exception.code, errno.ENOENT)
self.assertRegex(mock_stderr.getvalue(),
"error: either run #2 or xunit 'old' do not exist")
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
@unittest.mock.patch("sys.stderr", new_callable=io.StringIO)
def test_rename_runid_eexist(self, mock_stderr: io.StringIO,
mock_stdout: io.StringIO):
"""Test the `xfstestsdb xunit rename` command with an existing name."""
self.xfstestsdb.run(["new", "/dev/test"])
self.xfstestsdb.run(["xunit", "read", "1", str(tests.xunit.XUNIT_1)])
self.xfstestsdb.run(["xunit", "read", "1", str(tests.xunit.XUNIT_1),
"--name", "test-name"])
with self.assertRaises(SystemExit) as sys_exit:
self.xfstestsdb.run(["xunit", "rename", "1",
"test-1", "test-name"])
self.assertEqual(sys_exit.exception.code, errno.EEXIST)
self.assertRegex(mock_stderr.getvalue(),
"error: run #1 xunit 'test-name' already exists")