gtk: Create an XunitRow base class

This Object is intended to be returned by a Gio.ListModel to show
Xfstests xunit information. I change the TestCase and Summary classes to
inherit from this class, removing a lot of duplicated code.

Signed-off-by: Anna Schumaker <anna@nowheycreamery.com>
This commit is contained in:
Anna Schumaker 2023-08-15 11:58:27 -04:00
parent ad08357121
commit c45ec1909e
2 changed files with 82 additions and 75 deletions

View File

@ -20,6 +20,44 @@ class TestXunitCell(unittest.TestCase):
self.assertEqual(str(cell), "my xunit name")
class TestXunitRow(unittest.TestCase):
"""Test case for our base XunitRow object."""
def setUp(self):
"""Set up common variables."""
self.row = xfstestsdb.gtk.model.XunitRow(name="row-name")
def test_init(self):
"""Test that the XunitRow is set up properly."""
self.assertIsInstance(self.row, GObject.GObject)
self.assertEqual(self.row.name, "row-name")
def test_compare(self):
"""Test the less-than operator on XunitRows."""
row2 = xfstestsdb.gtk.model.XunitRow(name="row-name-2")
self.assertTrue(self.row < row2)
self.assertFalse(row2 < self.row)
self.assertFalse(self.row < self.row)
def test_xunits(self):
"""Test adding xunits to a XunitRow."""
self.assertIsNone(self.row["xunit-1"])
self.row.add_xunit("xunit-1")
self.assertSetEqual(self.row.get_results(), {"xunit-1"})
xunit = self.row["xunit-1"]
self.assertIsInstance(xunit, xfstestsdb.gtk.model.XunitCell)
self.assertEqual(xunit.name, "xunit-1")
self.row.add_xunit("xunit-2")
self.assertSetEqual(self.row.get_results(), {"xunit-1", "xunit-2"})
xunit = self.row["xunit-2"]
self.assertIsInstance(xunit, xfstestsdb.gtk.model.XunitCell)
self.assertEqual(xunit.name, "xunit-2")
class TestTestResult(unittest.TestCase):
"""Tests a single TestCase Xunit instance."""
@ -49,16 +87,9 @@ class TestTestCase(unittest.TestCase):
def test_init(self):
"""Check that the TestCase is set up properly."""
self.assertIsInstance(self.testcase, GObject.GObject)
self.assertIsInstance(self.testcase, xfstestsdb.gtk.model.XunitRow)
self.assertEqual(self.testcase.name, "test-case")
def test_compare(self):
"""Test the less-than operator on TestCases."""
testcase2 = xfstestsdb.gtk.model.TestCase(name="test-case-2")
self.assertTrue(self.testcase < testcase2)
self.assertFalse(testcase2 < self.testcase)
self.assertFalse(self.testcase < self.testcase)
def test_xunits(self):
"""Test adding xunits to a TestCase."""
self.assertIsNone(self.testcase["xunit-1"])
@ -84,18 +115,6 @@ class TestTestCase(unittest.TestCase):
self.assertEqual(xunit.stdout, "")
self.assertEqual(xunit.stderr, "")
def test_get_results(self):
"""Test getting a set of results for this test case."""
self.testcase.add_xunit("xunit-1", "passed", 123, "", "", "")
self.assertSetEqual(self.testcase.get_results(), {"passed"})
self.testcase.add_xunit("xunit-2", "passed", 123, "", "", "")
self.assertSetEqual(self.testcase.get_results(), {"passed"})
self.testcase.add_xunit("xunit-3", "skipped", 123, "", "", "")
self.assertSetEqual(self.testcase.get_results(), {"passed", "skipped"})
self.testcase.add_xunit("xunit-4", "failure", 123, "", "", "")
self.assertSetEqual(self.testcase.get_results(),
{"passed", "skipped", "failure"})
class TestCaseList(unittest.TestCase):
"""Tests our TestCaseList Gio.ListModel."""
@ -250,7 +269,7 @@ class TestSummary(unittest.TestCase):
def test_init(self):
"""Check that the Summary is set up properly."""
self.assertIsInstance(self.summary, GObject.GObject)
self.assertIsInstance(self.summary, xfstestsdb.gtk.model.XunitRow)
self.assertEqual(self.summary.name, "passed")
def test_compare(self):
@ -277,13 +296,6 @@ class TestSummary(unittest.TestCase):
self.assertEqual(xunit.value, 123)
self.assertEqual(xunit.unit, "unit")
def test_get_results(self):
"""Test getting a set of results for this summary."""
self.summary.add_xunit("xunit-1", 1, "unit")
self.assertSetEqual(self.summary.get_results(), {"1 unit"})
self.summary.add_xunit("xunit-2", 2, "unit")
self.assertSetEqual(self.summary.get_results(), {"1 unit", "2 units"})
class TestSummaryList(unittest.TestCase):
"""Test case for our summary list."""

View File

@ -17,6 +17,37 @@ class XunitCell(GObject.GObject):
return self.name
class XunitRow(GObject.GObject):
"""Collects results for a single row across multiple Xunits."""
name = GObject.Property(type=str)
def __init__(self, name: str) -> None:
"""Initialize an XunitRow."""
super().__init__(name=name)
self.__xunits = {}
def __getitem__(self, xunit: str) -> XunitCell | None:
"""Get the value of a specific Xunit."""
return self.__xunits.get(xunit)
def __lt__(self, rhs: typing.Self) -> bool:
"""Compare the names of two XunitRows."""
return self.name < rhs.name
def add_xunit(self, name: str, *args, **kwargs) -> None:
"""Add an XunitCell to the XunitRow."""
self.__xunits[name] = self.do_make_xunit(name, *args, **kwargs)
def do_make_xunit(self, name: str) -> XunitCell:
"""Create and return a new XunitCell."""
return XunitCell(name=name)
def get_results(self) -> set[str]:
"""Get a set of results for each added xunit."""
return {str(xunit) for xunit in self.__xunits.values()}
class TestResult(XunitCell):
"""The results for a single TestCase with a specific Xunit."""
@ -31,38 +62,17 @@ class TestResult(XunitCell):
return self.status
class TestCase(GObject.GObject):
class TestCase(XunitRow):
"""Collects results for a single TestCase with multiple Xunits."""
name = GObject.Property(type=str)
def __init__(self, name: str) -> None:
"""Initialize a TestCase object."""
super().__init__(name=name)
self.__xunits = {}
def __getitem__(self, xunit: str) -> TestResult | None:
"""Get the results for a specific Xunit."""
return self.__xunits.get(xunit)
def __lt__(self, rhs: typing.Self) -> bool:
"""Compare the names of two TestCases."""
return self.name < rhs.name
def add_xunit(self, name: str, status: str, time: int,
message: str | None, stdout: str | None,
stderr: str | None) -> None:
def do_make_xunit(self, name: str, status: str, time: int,
message: str | None, stdout: str | None,
stderr: str | None) -> TestResult:
"""Add an xunit result to the TestCase."""
message = "" if message is None else message
stdout = "" if stdout is None else stdout
stderr = "" if stderr is None else stderr
self.__xunits[name] = TestResult(name=name, status=status, time=time,
message=message, stdout=stdout,
stderr=stderr)
def get_results(self) -> set[str]:
"""Get a set of results for each added xunit."""
return {xunit.status for xunit in self.__xunits.values()}
return TestResult(name=name, status=status, time=time,
message=("" if message is None else message),
stdout=("" if stdout is None else stdout),
stderr=("" if stderr is None else stderr))
class TestCaseList(GObject.GObject, Gio.ListModel):
@ -160,32 +170,17 @@ class SummaryValue(XunitCell):
return f"{self.value} {self.unit}{s}"
class Summary(GObject.GObject):
class Summary(XunitRow):
"""Collects values for each summary field with multiple Xunits."""
name = GObject.Property(type=str)
def __init__(self, name: str) -> None:
"""Initialize a Summary object."""
super().__init__(name=name)
self.__xunits = {}
def __getitem__(self, xunit: str) -> SummaryValue | None:
"""Get the summary for a specific Xunit."""
return self.__xunits.get(xunit)
def __lt__(self, rhs: typing.Self) -> bool:
"""Compare the fields of two Summaries."""
order = ["passed", "failed", "skipped", "time"]
return order.index(self.name) < order.index(rhs.name)
def add_xunit(self, name: str, value: int, unit: str) -> None:
def do_make_xunit(self, name: str, value: int, unit: str) -> SummaryValue:
"""Add an xunit summary to the Summary."""
self.__xunits[name] = SummaryValue(name=name, value=value, unit=unit)
def get_results(self) -> set[str]:
"""Get a set of results for each added xunit."""
return {str(value) for value in self.__xunits.values()}
return SummaryValue(name=name, value=value, unit=unit)
class SummaryList(GObject.GObject, Gio.ListModel):