diff --git a/tests/gtk/test_model.py b/tests/gtk/test_model.py index a9700e4..6ce21fd 100644 --- a/tests/gtk/test_model.py +++ b/tests/gtk/test_model.py @@ -101,6 +101,103 @@ class TestXunitList(unittest.TestCase): self.assertListEqual(self.xulist.get_xunits(), ["xunit-1", "xunit-2"]) +class TestPropertyValue(unittest.TestCase): + """Tests a single Xunit Property instance.""" + + def test_xunit_property(self): + """Test creating an xunit property instance.""" + property = xfstestsdb.gtk.model.PropertyValue(name="my xunit name", + key="key", value="123") + self.assertIsInstance(property, xfstestsdb.gtk.model.XunitCell) + self.assertEqual(property.name, "my xunit name") + self.assertEqual(property.key, "key") + self.assertEqual(property.value, "123") + self.assertEqual(str(property), "key = 123") + + +class TestProperty(unittest.TestCase): + """Tests our Property GObject.""" + + def setUp(self): + """Set up common variables.""" + self.property = xfstestsdb.gtk.model.Property(name="property") + + def test_init(self): + """Check that the Property is set up properly.""" + self.assertIsInstance(self.property, xfstestsdb.gtk.model.XunitRow) + self.assertEqual(self.property.name, "property") + + def test_xunits(self): + """Test adding xunits to a Property.""" + self.assertIsNone(self.property["xunit-1"]) + + self.property.add_xunit("xunit-1", "PLATFORM", "linux-123") + property = self.property["xunit-1"] + self.assertIsInstance(property, xfstestsdb.gtk.model.PropertyValue) + self.assertEqual(property.name, "xunit-1") + self.assertEqual(property.key, "PLATFORM") + self.assertEqual(property.value, "linux-123") + self.assertTrue(self.property.all_same_value()) + + self.property.add_xunit("xunit-2", "PLATFORM", "linux-123") + property = self.property["xunit-2"] + self.assertIsInstance(property, xfstestsdb.gtk.model.PropertyValue) + self.assertEqual(property.name, "xunit-2") + self.assertEqual(property.key, "PLATFORM") + self.assertEqual(property.value, "linux-123") + self.assertTrue(self.property.all_same_value()) + + self.property.add_xunit("xunit-3", "PLATFORM", "linux-456") + self.assertFalse(self.property.all_same_value()) + + +class TestPropertyList(unittest.TestCase): + """Tests our PropertyList GObject.""" + + def setUp(self): + """Set up common variables.""" + self.xfstestsdb = xfstestsdb.Command() + with unittest.mock.patch("sys.stdout"): + self.xfstestsdb.run(["new", "/dev/vda1"]) + self.xfstestsdb.run(["xunit", "read", "--name", "xunit-1", + "1", str(tests.xunit.XUNIT_1)]) + self.xfstestsdb.run(["xunit", "read", "--name", "xunit-2", + "1", str(tests.xunit.XUNIT_2)]) + + self.prlist = xfstestsdb.gtk.model.PropertyList(self.xfstestsdb.sql, 1) + + def test_init(self): + """Test that the PropertyList was set up properly.""" + self.assertIsInstance(self.prlist, xfstestsdb.gtk.model.XunitList) + self.assertEqual(self.prlist.runid, 1) + self.assertEqual(self.prlist.n_items, 15) + + +class TestPropertyFilter(unittest.TestCase): + """Tests our Gtk.Filter customized for filtering Properties.""" + + def setUp(self): + """Set up common variables.""" + self.filter = xfstestsdb.gtk.model.PropertyFilter() + + def test_init(self): + """Test that the TestCaseFilter is set up properly.""" + self.assertIsInstance(self.filter, Gtk.Filter) + self.assertEqual(self.filter.get_strictness(), Gtk.FilterMatch.SOME) + + def test_match(self): + """Test matching Properties with the Filter.""" + property = xfstestsdb.gtk.model.Property("name") + self.assertTrue(self.filter.match(property)) + + for prop in ["CPUS", "HOST_OPTIONS", "MEM_KB", "LOAD_FACTOR", + "NUMA_NODES", "OVL_LOWER", "OVL_UPPER", "OVL_WORK", + "PLATFORM", "SECTION", "SWAP_KB", "TIME_FACTOR"]: + with self.subTest(property=prop): + property.name = prop + self.assertFalse(self.filter.match(property)) + + class TestTestResult(unittest.TestCase): """Tests a single TestCase Xunit instance.""" diff --git a/tests/xunit/__init__.py b/tests/xunit/__init__.py index 9cf8494..604eb90 100644 --- a/tests/xunit/__init__.py +++ b/tests/xunit/__init__.py @@ -3,3 +3,4 @@ import pathlib XUNIT_1 = pathlib.Path(__file__).parent / "test-1.xunit" +XUNIT_2 = pathlib.Path(__file__).parent / "test-2.xunit" diff --git a/tests/xunit/test-2.xunit b/tests/xunit/test-2.xunit new file mode 100644 index 0000000..e70c7b0 --- /dev/null +++ b/tests/xunit/test-2.xunit @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/xfstestsdb/gtk/model.py b/xfstestsdb/gtk/model.py index e5839dd..59bf5bf 100644 --- a/xfstestsdb/gtk/model.py +++ b/xfstestsdb/gtk/model.py @@ -93,6 +93,58 @@ class XunitList(GObject.GObject, Gio.ListModel): return sorted(self.__xunits) +class PropertyValue(XunitCell): + """A single Property for a specific Xunit.""" + + key = GObject.Property(type=str) + value = GObject.Property(type=str) + + def __str__(self) -> str: + """Get a string representation of this Property.""" + return f"{self.key} = {self.value}" + + +class Property(XunitRow): + """Collects one property across multiple xunits.""" + + def do_make_xunit(self, name: str, key: str, value: str) -> PropertyValue: + """Add a PropertyValue to the Property.""" + return PropertyValue(name=name, key=key, value=value) + + def all_same_value(self) -> bool: + """Check if all the xunits have the same value.""" + return len(self.get_results()) == 1 + + +class PropertyList(XunitList): + """A list of Properties for a specific Xfstests Run.""" + + def do_query(self, sql: sqlite.Connection) -> sqlite3.Cursor: + """Query the database for properties.""" + return sql("""SELECT xunit, key, value FROM xunit_properties_view + WHERE runid=?""", self.runid) + + def do_parse(self, rows: dict[Property], row: sqlite3.Cursor) -> None: + """Parse the data in the row and add it to the rows dict.""" + property = rows.setdefault(row["key"], Property(row["key"])) + property.add_xunit(row["xunit"], row["key"], row["value"]) + + +class PropertyFilter(Gtk.Filter): + """A filter for Properties.""" + + def do_get_strictness(self) -> Gtk.FilterMatch: + """Get the strictness of the filter.""" + return Gtk.FilterMatch.SOME + + def do_match(self, property: Property) -> bool: + """Check if a property matches the filter.""" + hidden = {"CPUS", "HOST_OPTIONS", "LOAD_FACTOR", "MEM_KB", + "NUMA_NODES", "OVL_LOWER", "OVL_UPPER", "OVL_WORK", + "PLATFORM", "SECTION", "SWAP_KB", "TIME_FACTOR"} + return property.name not in hidden + + class TestResult(XunitCell): """The results for a single TestCase with a specific Xunit."""