From f2a47d9df4923788150333b6b1d05c86454a5540 Mon Sep 17 00:00:00 2001 From: appel_c Date: Tue, 8 Jul 2025 09:17:29 +0200 Subject: [PATCH] test(scan_history): add initial tests --- tests/unit_tests/test_scan_history_browser.py | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 tests/unit_tests/test_scan_history_browser.py diff --git a/tests/unit_tests/test_scan_history_browser.py b/tests/unit_tests/test_scan_history_browser.py new file mode 100644 index 00000000..4a62cfdb --- /dev/null +++ b/tests/unit_tests/test_scan_history_browser.py @@ -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