0
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2025-07-13 11:11:49 +02:00

test(scan-history): compplement tests fixup

This commit is contained in:
2025-07-09 09:56:31 +02:00
parent 0ed8787832
commit 9644c75e95

View File

@ -11,6 +11,9 @@ from bec_widgets.widgets.services.scan_history_browser.components import (
ScanHistoryMetadataViewer,
ScanHistoryView,
)
from bec_widgets.widgets.services.scan_history_browser.scan_history_browser import (
ScanHistoryBrowser,
)
from .client_mocks import mocked_client
@ -33,6 +36,24 @@ def scan_history_msg():
)
@pytest.fixture
def scan_history_msg_2():
"""Fixture to create a second mock ScanHistoryMessage."""
yield ScanHistoryMessage(
scan_id="test_scan_2",
dataset_number=2,
scan_number=2,
scan_name="Test Scan 2",
file_path="/path/to/scan_2",
start_time=1751957908.3310962,
end_time=1751957909.3310962, # 1s later
exit_status="closed",
num_points=5,
request_inputs={"some_input": "new_value"},
device_data_info={"device0": (15,), "device5": (25,), "device2313": (3,), "device2": (20,)},
)
@pytest.fixture
def scan_history_device_viewer(qtbot, mocked_client):
widget = ScanHistoryDeviceViewer(client=mocked_client)
@ -57,8 +78,17 @@ def scan_history_view(qtbot, mocked_client):
yield widget
@pytest.fixture
def scan_history_browser(qtbot, mocked_client):
"""Fixture to create a ScanHistoryBrowser widget."""
widget = ScanHistoryBrowser(client=mocked_client)
qtbot.addWidget(widget)
qtbot.waitExposed(widget)
yield widget
def test_scan_history_device_viewer_receive_msg(
qtbot, scan_history_device_viewer, scan_history_msg
qtbot, scan_history_device_viewer, scan_history_msg, scan_history_msg_2
):
"""Test updating devices from scan history."""
# Update with first scan history message
@ -80,19 +110,7 @@ def test_scan_history_device_viewer_receive_msg(
assert device_name == "device2"
## Update of second message should not change the device if still available
new_msg = ScanHistoryMessage(
scan_id="test_scan_2",
dataset_number=2,
scan_number=2,
scan_name="Test Scan 2",
file_path="/path/to/scan_2",
start_time=1751957908.3310962,
end_time=1751957909.3310962, # 1s later
exit_status="closed",
num_points=5,
request_inputs={"some_input": "new_value"},
device_data_info={"device0": (15,), "device5": (25,), "device2313": (3,), "device2": (20,)},
)
new_msg = scan_history_msg_2
scan_history_device_viewer.update_devices_from_scan_history(new_msg)
assert scan_history_device_viewer.scan_history_msg == new_msg
assert scan_history_device_viewer.device_model.devices == [
@ -206,7 +224,7 @@ def test_scan_history_view_current_scan_item_changed(
def scan_selected_callback(msg):
"""Callback to check if the scan_selected signal is emitted."""
assert msg == scan_history_msg
return msg == scan_history_msg
scan_history_view.scan_selected.connect(scan_selected_callback)
@ -217,81 +235,92 @@ def test_scan_history_view_current_scan_item_changed(
)
# TODO add tests for compact view of popup, mostly relying on mouseclicks
# qtbot.mouseClick(scan_history_device_viewer.device_combo, QtCore.Qt.LeftButton)
# qtbot.waitUntil(
# lambda: scan_history_device_viewer.device_combo.view().isVisible(), timeout=1000
# )
# # qtbot.waitExposed(scan_history_device_viewer.device_combo.view())
# index = scan_history_device_viewer.device_combo.model().index(1, 0)
# rect = scan_history_device_viewer.device_combo.view().visualRect(index)
def test_scan_history_browser(qtbot, scan_history_browser, scan_history_msg, scan_history_msg_2):
"""Test the initialization of ScanHistoryBrowser."""
assert isinstance(scan_history_browser.scan_history_view, ScanHistoryView)
assert isinstance(scan_history_browser.scan_history_metadata_viewer, ScanHistoryMetadataViewer)
assert isinstance(scan_history_browser.scan_history_device_viewer, ScanHistoryDeviceViewer)
# index_changed = False
# Add 2 scans to the history browser, new item will be added to the top
scan_history_browser.scan_history_view.update_history(
scan_history_msg.content, scan_history_msg.metadata
)
scan_history_browser.scan_history_view.update_history(
scan_history_msg_2.content, scan_history_msg_2.metadata
)
# def index_cb(index):
# """Callback to check if the device combo text is updated."""
# device_name = scan_history_device_viewer.device_combo.model().data(
# scan_history_device_viewer.device_combo.model().index(index, 0), QtCore.Qt.UserRole
# )
# if device_name == "device2":
# index_changed = True
assert len(scan_history_browser.scan_history_view.scan_history) == 2
# Click on first scan item history to select it
qtbot.mouseClick(
scan_history_browser.scan_history_view.viewport(),
QtCore.Qt.LeftButton,
pos=scan_history_browser.scan_history_view.visualItemRect(
scan_history_browser.scan_history_view.topLevelItem(0)
).center(),
)
# scan_history_device_viewer.device_combo.currentIndexChanged.connect(index_cb)
# Both metadata and device viewers should be updated with the first scan
qtbot.waitUntil(
lambda: scan_history_browser.scan_history_metadata_viewer.scan_history_msg
== scan_history_msg_2,
timeout=1000,
)
qtbot.waitUntil(
lambda: scan_history_browser.scan_history_device_viewer.scan_history_msg
== scan_history_msg_2,
timeout=1000,
)
# qtbot.mouseClick(
# scan_history_device_viewer.device_combo.view().viewport(),
# QtCore.Qt.LeftButton,
# pos=rect.center(),
# )
# Click on second scan item history to select it
qtbot.mouseClick(
scan_history_browser.scan_history_view.viewport(),
QtCore.Qt.LeftButton,
pos=scan_history_browser.scan_history_view.visualItemRect(
scan_history_browser.scan_history_view.topLevelItem(1)
).center(),
)
# qtbot.waitUntil(lambda: index_changed is True, timeout=3000)
# assert scan_history_device_viewer.device_combo.currentText() == "device1"
# Both metadata and device viewers should be updated with the first scan
qtbot.waitUntil(
lambda: scan_history_browser.scan_history_metadata_viewer.scan_history_msg
== scan_history_msg,
timeout=1000,
)
qtbot.waitUntil(
lambda: scan_history_browser.scan_history_device_viewer.scan_history_msg
== scan_history_msg,
timeout=1000,
)
callback_args = []
# def test_scan_history_device_viewer_request_plotting(
# qtbot, scan_history_device_viewer, scan_history_msg
# ):
# """Test requesting plotting from the device viewer."""
# # Connect simple callback to request plotting signal
# data = []
def plotting_callback(device_name, msg):
"""Callback to check if the request plotting signal is emitted."""
# device_name should be the first device
callback_args.append((device_name, msg))
# def plot_cb(device_name, msg):
# """Simple callback to check request plotting signal."""
# data.append((device_name, msg))
scan_history_browser.scan_history_device_viewer.request_history_plot.connect(plotting_callback)
# Test emit plotting request
qtbot.mouseClick(
scan_history_browser.scan_history_device_viewer.request_plotting_button,
QtCore.Qt.LeftButton,
)
qtbot.waitUntil(lambda: len(callback_args) > 0, timeout=1000)
assert callback_args[0][1] == scan_history_msg
assert callback_args[0][0] in callback_args[0][1].device_data_info.keys()
# scan_history_device_viewer.update_devices_from_scan_history(scan_history_msg)
# Test clearing the view, removing both scans
scan_history_browser.scan_history_view.remove_scan(-1)
scan_history_browser.scan_history_view.remove_scan(-1)
# scan_history_device_viewer.request_history_plot.connect(plot_cb)
# with qtbot.waitExposed(scan_history_device_viewer.request_plotting_button):
# qtbot.mouseClick(scan_history_device_viewer.request_plotting_button, QtCore.Qt.LeftButton)
# assert len(data) == 1
# assert data[0][0] == "device2"
# assert data[0][1] == scan_history_msg
assert len(scan_history_browser.scan_history_view.scan_history) == 0
assert scan_history_browser.scan_history_view.topLevelItemCount() == 0
# # Now change the scan_history_msg and check if the device combo updates
# # But keep the same device name somewhere in the list
# new_msg = ScanHistoryMessage(
# scan_id="test_scan_2",
# dataset_number=2,
# scan_number=2,
# scan_name="Test Scan 2",
# file_path="/path/to/scan_2",
# start_time=1751957908.3310962,
# end_time=1751957909.3310962, # 1s later
# exit_status="closed",
# num_points=5,
# request_inputs={"some_input": "new_value"},
# device_data_info={"device0": 15, "device5": 25, "device2313": 3, "device2": 20},
# )
# # Update the device viewer with the new message
# scan_history_device_viewer.update_devices_from_scan_history(new_msg)
# assert scan_history_device_viewer.scan_history_msg == new_msg
# # The same device should be selected still
# with qtbot.waitExposed(scan_history_device_viewer.request_plotting_button):
# qtbot.mouseClick(
# scan_history_device_viewer.request_plotting_button, qtbot.MouseButton.LeftButton
# )
# assert len(data) == 2
# assert data[1][0] == "device2"
# assert data[1][1] == new_msg
qtbot.waitUntil(
lambda: scan_history_browser.scan_history_metadata_viewer.scan_history_msg is None,
timeout=1000,
)
qtbot.waitUntil(
lambda: scan_history_browser.scan_history_device_viewer.scan_history_msg is None,
timeout=1000,
)