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

test(scan_history): add initial tests

This commit is contained in:
2025-07-08 09:17:29 +02:00
parent 9b270af676
commit f2a47d9df4

View File

@ -0,0 +1,76 @@
from unittest import mock
import pytest
from bec_lib.messages import ScanHistoryMessage
from pytestqt import qtbot
from qtpy import QtCore
from bec_widgets.widgets.services.scan_history_browser.components import (
ScanHistoryDeviceViewer,
ScanHistoryMetadataViewer,
ScanHistoryView,
)
from .client_mocks import mocked_client
@pytest.fixture
def scan_history_msg():
"""Fixture to create a mock ScanHistoryMessage."""
yield ScanHistoryMessage(
scan_id="test_scan",
dataset_number=1,
scan_number=1,
scan_name="Test Scan",
file_path="/path/to/scan",
start_time=1751957906.3310962,
end_time=1751957907.3310962, # 1s later
exit_status="closed",
num_points=10,
request_inputs={"some_input": "value"},
device_data_info={"device1": 10, "device2": 20, "device3": 1},
)
@pytest.fixture
def scan_history_device_viewer(qtbot, mocked_client):
widget = ScanHistoryDeviceViewer(client=mocked_client)
qtbot.addWidget(widget)
qtbot.waitExposed(widget)
yield widget
def test_scan_history_device_viewer_init(scan_history_device_viewer):
"""Test the initialization of ScanHistoryDeviceViewer."""
assert scan_history_device_viewer.scan_history_msg is None
assert scan_history_device_viewer._selected_device == ""
assert scan_history_device_viewer.device_model.devices == []
assert scan_history_device_viewer.device_model.rowCount() == 0
def test_scan_history_device_viewer_update_logic(
qtbot, scan_history_device_viewer, scan_history_msg
):
"""Test updating devices from scan history."""
# Update with first scan history message
assert scan_history_device_viewer.scan_history_msg is None
scan_history_device_viewer.update_devices_from_scan_history(scan_history_msg)
assert scan_history_device_viewer.scan_history_msg == scan_history_msg
assert scan_history_device_viewer.device_model.devices == [
("device2", 20),
("device1", 10),
("device3", 1),
] # sorted by number of points
with qtbot.waitExposed(scan_history_device_viewer.device_combo.view()):
qtbot.mouseClick(scan_history_device_viewer.device_combo, qtbot.MouseButton.LeftButton)
# 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)
with qtbot.waitExposed(scan_history_device_viewer.device_combo.currentIndexChanged):
qtbot.mouseClick(
scan_history_device_viewer.device_combo.view().viewport(),
QtCore.Qt.LeftButton,
pos=rect.center(),
)
# Now