xfstestsdb/tests/test_show.py

186 lines
7.0 KiB
Python

# Copyright 2023 (c) Anna Schumaker.
"""Tests the `xfstestsdb testcase show` command."""
import errno
import io
import unittest
import unittest.mock
import xfstestsdb.show
import tests.xunit
class TestTestCaseShow(unittest.TestCase):
"""Tests the `xfstestsdb testcase show` command."""
def setUp(self):
"""Set up common variables."""
self.xfstestsdb = xfstestsdb.Command()
self.show = self.xfstestsdb.commands["show"]
def setup_run(self, mock_stdout: io.StringIO):
"""Set up runs in the database and clear stdout."""
self.xfstestsdb.run(["new", "/dev/vda1"])
self.xfstestsdb.run(["xunit", "read", "1", str(tests.xunit.XUNIT_1)])
self.xfstestsdb.run(["xunit", "read", "1", str(tests.xunit.XUNIT_1),
"--name", "test-2"])
mock_stdout.seek(0)
mock_stdout.truncate(0)
def test_init(self):
"""Check that the show command was set up properly."""
self.assertIsInstance(xfstestsdb.show.TestCaseTable(["col"]),
xfstestsdb.table.Table)
self.assertIsInstance(self.show, xfstestsdb.commands.Command)
self.assertIsInstance(self.show, xfstestsdb.show.Command)
self.assertEqual(self.xfstestsdb.subparser.choices["show"],
self.show.parser)
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_show_empty(self, mock_stdout: io.StringIO):
"""Test printing out an empty list."""
self.setup_run(mock_stdout)
self.xfstestsdb.sql("DELETE FROM testcases")
self.assertEqual(mock_stdout.getvalue(), "")
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_show_run(self, mock_stdout: io.StringIO):
"""Test showing a run with no filters."""
self.setup_run(mock_stdout)
print()
self.xfstestsdb.run(["show", "1", "--color", "none"])
self.assertEqual(mock_stdout.getvalue(),
"""
+----------+---------+---------+
| testcase | test-1 | test-2 |
+----------+---------+---------+
| test/01 | passed | passed |
| test/02 | skipped | skipped |
| test/03 | skipped | skipped |
| test/04 | passed | passed |
| test/05 | passed | passed |
| test/06 | passed | passed |
| test/07 | skipped | skipped |
| test/08 | passed | passed |
| test/09 | failure | failure |
| test/10 | passed | passed |
+----------+---------+---------+
""")
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_show_filter_testcase(self, mock_stdout: io.StringIO):
"""Test showing a run filtering by testcase."""
self.setup_run(mock_stdout)
print()
self.xfstestsdb.run(["show", "1", "--testcase", "test/0[45]",
"--color", "none"])
self.assertEqual(mock_stdout.getvalue(),
"""
+----------+--------+--------+
| testcase | test-1 | test-2 |
+----------+--------+--------+
| test/04 | passed | passed |
| test/05 | passed | passed |
+----------+--------+--------+
""")
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_show_filter_passed(self, mock_stdout: io.StringIO):
"""Test showing a run filtering for passing tests."""
self.setup_run(mock_stdout)
print()
self.xfstestsdb.run(["show", "1", "--passed", "--color", "none"])
self.assertEqual(mock_stdout.getvalue(),
"""
+----------+--------+--------+
| testcase | test-1 | test-2 |
+----------+--------+--------+
| test/01 | passed | passed |
| test/04 | passed | passed |
| test/05 | passed | passed |
| test/06 | passed | passed |
| test/08 | passed | passed |
| test/10 | passed | passed |
+----------+--------+--------+
""")
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_show_filter_skipped(self, mock_stdout: io.StringIO):
"""Test showing a run filtering for skipped testss."""
self.setup_run(mock_stdout)
print()
self.xfstestsdb.run(["show", "1", "--skipped", "--color", "none"])
self.assertEqual(mock_stdout.getvalue(),
"""
+----------+---------+---------+
| testcase | test-1 | test-2 |
+----------+---------+---------+
| test/02 | skipped | skipped |
| test/03 | skipped | skipped |
| test/07 | skipped | skipped |
+----------+---------+---------+
""")
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_show_filter_failure(self, mock_stdout: io.StringIO):
"""Test showing a run filtering for failing testss."""
self.setup_run(mock_stdout)
print()
self.xfstestsdb.run(["show", "1", "--failure", "--color", "none"])
self.assertEqual(mock_stdout.getvalue(),
"""
+----------+---------+---------+
| testcase | test-1 | test-2 |
+----------+---------+---------+
| test/09 | failure | failure |
+----------+---------+---------+
""")
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_show_uneven_xunits(self, mock_stdout: io.StringIO):
"""Test showing a run where xunits have different testcase sets."""
self.setup_run(mock_stdout)
self.xfstestsdb.sql("DELETE FROM testcases WHERE "
"xunitid=? AND testcase=?", 1, "test/03")
self.xfstestsdb.sql("DELETE FROM testcases WHERE "
"xunitid=? AND testcase=?", 2, "test/08")
print()
self.xfstestsdb.run(["show", "1", "--color", "none"])
self.assertEqual(mock_stdout.getvalue(),
"""
+----------+---------+---------+
| testcase | test-1 | test-2 |
+----------+---------+---------+
| test/01 | passed | passed |
| test/02 | skipped | skipped |
| test/03 | | skipped |
| test/04 | passed | passed |
| test/05 | passed | passed |
| test/06 | passed | passed |
| test/07 | skipped | skipped |
| test/08 | passed | |
| test/09 | failure | failure |
| test/10 | passed | passed |
+----------+---------+---------+
""")
@unittest.mock.patch("sys.stderr", new_callable=io.StringIO)
def test_show_error(self, mock_stderr: io.StringIO):
"""Test the `xfstestsdb show` command with invalid input."""
with self.assertRaises(SystemExit):
self.xfstestsdb.run(["testcase", "show"])
self.assertRegex(mock_stderr.getvalue(),
"error: the following arguments are required: runid")
@unittest.mock.patch("sys.stderr", new_callable=io.StringIO)
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_show_enoent(self, mock_stdout: io.StringIO,
mock_stderr: io.StringIO):
"""Test the `xfstestsdb show` command with an invalid runid."""
self.setup_run(mock_stderr)
with self.assertRaises(SystemExit) as sys_exit:
self.xfstestsdb.run(["show", "2"])
self.assertEqual(sys_exit.exception.code, errno.ENOENT)
self.assertEqual(mock_stderr.getvalue(),
"error: run #2 does not exist\n")