mirror of
https://github.com/bec-project/bec_widgets.git
synced 2026-11-03 01:03:35 +01:00
refactor(device_manager): share one SortTableItem with an optional sort key
This commit is contained in:
1 parent
83eb8748ae
commit
9c8800a8fa
4 files changed
+94
-43
No files matched your search
@@ -37,6 +37,9 @@ from bec_widgets.widgets.control.device_manager.components.device_config_templat
|
||||
ReadoutPriorityComboBox,
|
||||
_try_literal_eval,
|
||||
)
|
||||
from bec_widgets.widgets.control.device_manager.components.device_table.device_table import (
|
||||
SortTableItem,
|
||||
)
|
||||
from bec_widgets.widgets.control.device_manager.components.device_table.device_table_row import (
|
||||
DeviceTableRow,
|
||||
)
|
||||
@@ -295,6 +298,42 @@ class TestDeviceTableRow:
|
||||
)
|
||||
|
||||
|
||||
class TestSortTableItem:
|
||||
"""Test class for SortTableItem."""
|
||||
|
||||
@staticmethod
|
||||
def _item(data=None, text: str = "", sort_key=None) -> SortTableItem:
|
||||
item = SortTableItem(text, sort_key=sort_key)
|
||||
if data is not None:
|
||||
item.setData(QtCore.Qt.ItemDataRole.UserRole, data)
|
||||
return item
|
||||
|
||||
def test_compares_user_role_data(self):
|
||||
"""Without a sort key, the UserRole data is compared directly."""
|
||||
low, high = self._item(1, text="b"), self._item(2, text="a")
|
||||
assert low < high
|
||||
assert high > low
|
||||
assert not high < low
|
||||
|
||||
def test_sort_key(self):
|
||||
"""A sort key maps the UserRole data before comparing."""
|
||||
|
||||
def key(data: dict) -> tuple[int, int]:
|
||||
return data["first"], data["second"]
|
||||
|
||||
a = self._item({"first": 0, "second": 2}, sort_key=key)
|
||||
b = self._item({"first": 1, "second": 0}, sort_key=key)
|
||||
c = self._item({"first": 0, "second": 1}, sort_key=key)
|
||||
assert a < b and b > a
|
||||
assert c < a and a > c
|
||||
|
||||
def test_non_item_comparison(self):
|
||||
"""Comparing with a non-item returns NotImplemented."""
|
||||
item = self._item(1)
|
||||
assert item.__lt__(1) is NotImplemented
|
||||
assert item.__gt__(1) is NotImplemented
|
||||
|
||||
|
||||
class TestDeviceTable:
|
||||
"""Test class for DeviceTable component."""
|
||||
|
||||
|
||||
@@ -372,6 +372,22 @@ class TestDeviceManagerViewDialogs:
|
||||
section.update_summary("2 devices validated, 1 failed.")
|
||||
assert section.summary_label.text() == "2 devices validated, 1 failed."
|
||||
|
||||
def test_validation_section_sorts_by_status(self, device_config: dict, qtbot):
|
||||
"""Rows sort by config status first, then by connection status."""
|
||||
section = ValidationSection(title="Validation Results")
|
||||
qtbot.addWidget(section)
|
||||
for name, config_status, connection_status in (
|
||||
("dev_a", 1, 0),
|
||||
("dev_b", 0, 2),
|
||||
("dev_c", 0, 1),
|
||||
):
|
||||
config = device_config.copy()
|
||||
config["name"] = name
|
||||
section.add_device(config, config_status, connection_status)
|
||||
|
||||
names = [section.table.cellWidget(row, 0).device_name for row in range(3)]
|
||||
assert names == ["dev_c", "dev_b", "dev_a"]
|
||||
|
||||
@pytest.fixture
|
||||
def device_configs_valid(self, device_config: dict):
|
||||
"""Fixture for multiple device configurations."""
|
||||
|
||||
Reference in new issue
Block a user