0
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2025-07-13 19:21:50 +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, ScanHistoryMetadataViewer,
ScanHistoryView, ScanHistoryView,
) )
from bec_widgets.widgets.services.scan_history_browser.scan_history_browser import (
ScanHistoryBrowser,
)
from .client_mocks import mocked_client 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 @pytest.fixture
def scan_history_device_viewer(qtbot, mocked_client): def scan_history_device_viewer(qtbot, mocked_client):
widget = ScanHistoryDeviceViewer(client=mocked_client) widget = ScanHistoryDeviceViewer(client=mocked_client)
@ -57,8 +78,17 @@ def scan_history_view(qtbot, mocked_client):
yield widget 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( 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.""" """Test updating devices from scan history."""
# Update with first scan history message # Update with first scan history message
@ -80,19 +110,7 @@ def test_scan_history_device_viewer_receive_msg(
assert device_name == "device2" assert device_name == "device2"
## Update of second message should not change the device if still available ## Update of second message should not change the device if still available
new_msg = ScanHistoryMessage( new_msg = scan_history_msg_2
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,)},
)
scan_history_device_viewer.update_devices_from_scan_history(new_msg) 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.scan_history_msg == new_msg
assert scan_history_device_viewer.device_model.devices == [ 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): def scan_selected_callback(msg):
"""Callback to check if the scan_selected signal is emitted.""" """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) 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 def test_scan_history_browser(qtbot, scan_history_browser, scan_history_msg, scan_history_msg_2):
# qtbot.mouseClick(scan_history_device_viewer.device_combo, QtCore.Qt.LeftButton) """Test the initialization of ScanHistoryBrowser."""
# qtbot.waitUntil( assert isinstance(scan_history_browser.scan_history_view, ScanHistoryView)
# lambda: scan_history_device_viewer.device_combo.view().isVisible(), timeout=1000 assert isinstance(scan_history_browser.scan_history_metadata_viewer, ScanHistoryMetadataViewer)
# ) assert isinstance(scan_history_browser.scan_history_device_viewer, ScanHistoryDeviceViewer)
# # 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)
# 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): assert len(scan_history_browser.scan_history_view.scan_history) == 2
# """Callback to check if the device combo text is updated.""" # Click on first scan item history to select it
# device_name = scan_history_device_viewer.device_combo.model().data( qtbot.mouseClick(
# scan_history_device_viewer.device_combo.model().index(index, 0), QtCore.Qt.UserRole scan_history_browser.scan_history_view.viewport(),
# ) QtCore.Qt.LeftButton,
# if device_name == "device2": pos=scan_history_browser.scan_history_view.visualItemRect(
# index_changed = True 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( # Click on second scan item history to select it
# scan_history_device_viewer.device_combo.view().viewport(), qtbot.mouseClick(
# QtCore.Qt.LeftButton, scan_history_browser.scan_history_view.viewport(),
# pos=rect.center(), 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) # Both metadata and device viewers should be updated with the first scan
# assert scan_history_device_viewer.device_combo.currentText() == "device1" 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( def plotting_callback(device_name, msg):
# qtbot, scan_history_device_viewer, scan_history_msg """Callback to check if the request plotting signal is emitted."""
# ): # device_name should be the first device
# """Test requesting plotting from the device viewer.""" callback_args.append((device_name, msg))
# # Connect simple callback to request plotting signal
# data = []
# def plot_cb(device_name, msg): scan_history_browser.scan_history_device_viewer.request_history_plot.connect(plotting_callback)
# """Simple callback to check request plotting signal.""" # Test emit plotting request
# data.append((device_name, msg)) 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) assert len(scan_history_browser.scan_history_view.scan_history) == 0
# with qtbot.waitExposed(scan_history_device_viewer.request_plotting_button): assert scan_history_browser.scan_history_view.topLevelItemCount() == 0
# 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
# # Now change the scan_history_msg and check if the device combo updates qtbot.waitUntil(
# # But keep the same device name somewhere in the list lambda: scan_history_browser.scan_history_metadata_viewer.scan_history_msg is None,
# new_msg = ScanHistoryMessage( timeout=1000,
# scan_id="test_scan_2", )
# dataset_number=2, qtbot.waitUntil(
# scan_number=2, lambda: scan_history_browser.scan_history_device_viewer.scan_history_msg is None,
# scan_name="Test Scan 2", timeout=1000,
# 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