gtk: Add SummaryValue and Summary objects

These are the underlying GObjects that will be returned by a SummaryList
ListModel.

Signed-off-by: Anna Schumaker <anna@nowheycreamery.com>
This commit is contained in:
Anna Schumaker 2023-08-14 11:34:46 -04:00
parent 8a54cb5d98
commit b143114108
2 changed files with 109 additions and 0 deletions

View File

@ -1,5 +1,6 @@
# Copyright 2023 (c) Anna Schumaker.
"""Tests our Testcase Gio.ListModel."""
import random
import unittest
import tests.xunit
import xfstestsdb.gtk.model
@ -203,3 +204,70 @@ class TestCaseFilter(unittest.TestCase):
self.assertFalse(self.filter.match(testcase))
self.filter.failure = True
self.assertTrue(self.filter.match(testcase))
class TestSummaryValue(unittest.TestCase):
"""Tests a single Summary Value object."""
def test_init(self):
"""Test creating a summary result instance."""
value = xfstestsdb.gtk.model.SummaryValue(name="my xunit name",
value=12345,
unit="testcase")
self.assertIsInstance(value, GObject.GObject)
self.assertEqual(value.name, "my xunit name")
self.assertEqual(value.unit, "testcase")
self.assertEqual(value.value, 12345)
def test_str(self):
"""Test converting a summary result to a string."""
value = xfstestsdb.gtk.model.SummaryValue(name="my xunit name",
value=1, unit="unit")
self.assertEqual(str(value), "1 unit")
value.value = 2
self.assertEqual(str(value), "2 units")
class TestSummary(unittest.TestCase):
"""Tests our Summary GObject."""
def setUp(self):
"""Set up common variables."""
self.summary = xfstestsdb.gtk.model.Summary(name="passed")
def test_init(self):
"""Check that the Summary is set up properly."""
self.assertIsInstance(self.summary, GObject.GObject)
self.assertEqual(self.summary.name, "passed")
def test_compare(self):
"""Test the less-than operator on Summaries."""
failed = xfstestsdb.gtk.model.Summary(name="failed")
skipped = xfstestsdb.gtk.model.Summary(name="skipped")
time = xfstestsdb.gtk.model.Summary(name="time")
expected = [self.summary, failed, skipped, time]
for i in range(10):
with self.subTest(i=i):
shuffled = [time, skipped, failed, self.summary]
random.shuffle(shuffled)
self.assertListEqual(sorted(shuffled), expected)
def test_xunits(self):
"""Test adding xunits to a Summary."""
self.assertIsNone(self.summary["xunit-1"])
self.summary.add_xunit("xunit-1", 123, "unit")
xunit = self.summary["xunit-1"]
self.assertIsInstance(xunit, xfstestsdb.gtk.model.SummaryValue)
self.assertEqual(xunit.name, "xunit-1")
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"})

View File

@ -132,3 +132,44 @@ class TestCaseFilter(Gtk.Filter):
if self.failure and "failure" in results:
return True
return False
class SummaryValue(GObject.GObject):
"""The summary of a single Xfstests xunit field."""
name = GObject.Property(type=str)
unit = GObject.Property(type=str)
value = GObject.Property(type=int)
def __str__(self) -> str:
"""Convert a Summary Value to a string."""
s = '' if self.value == 1 else 's'
return f"{self.value} {self.unit}{s}"
class Summary(GObject.GObject):
"""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:
"""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()}