mirror of
https://github.com/bec-project/bec_widgets.git
synced 2025-07-14 11:41:49 +02:00
tests WIP
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
from typing import Literal
|
from typing import Literal
|
||||||
|
|
||||||
from PyQt6.QtWidgets import (
|
from qtpy.QtWidgets import (
|
||||||
QCheckBox,
|
QCheckBox,
|
||||||
QComboBox,
|
QComboBox,
|
||||||
QDoubleSpinBox,
|
QDoubleSpinBox,
|
||||||
|
@ -1,184 +1,153 @@
|
|||||||
# # pylint: disable = no-name-in-module,missing-class-docstring, missing-module-docstring
|
# pylint: disable = no-name-in-module,missing-class-docstring, missing-module-docstring
|
||||||
# from unittest.mock import MagicMock
|
from unittest.mock import MagicMock
|
||||||
#
|
|
||||||
# import pytest
|
import pytest
|
||||||
# from qtpy.QtWidgets import QLineEdit
|
from qtpy.QtWidgets import QLineEdit
|
||||||
#
|
|
||||||
# from bec_widgets.utils.widget_io import WidgetIO
|
from bec_widgets.utils.widget_io import WidgetIO
|
||||||
# from bec_widgets.widgets import ScanControl
|
from bec_widgets.widgets import ScanControl
|
||||||
# from tests.unit_tests.test_msgs.available_scans_message import available_scans_message
|
from tests.unit_tests.test_msgs.available_scans_message import available_scans_message
|
||||||
#
|
|
||||||
#
|
|
||||||
# class FakePositioner:
|
from .client_mocks import mocked_client
|
||||||
# """Fake minimal positioner class for testing."""
|
|
||||||
#
|
|
||||||
# def __init__(self, name, enabled=True):
|
@pytest.fixture(scope="function")
|
||||||
# self.name = name
|
def scan_control(qtbot, mocked_client): # , mock_dev):
|
||||||
# self.enabled = enabled
|
widget = ScanControl(client=mocked_client)
|
||||||
#
|
qtbot.addWidget(widget)
|
||||||
# def __contains__(self, item):
|
qtbot.waitExposed(widget)
|
||||||
# return item == self.name
|
yield widget
|
||||||
#
|
|
||||||
#
|
|
||||||
# def get_mocked_device(device_name):
|
def test_populate_scans(scan_control, mocked_client):
|
||||||
# """Helper function to mock the devices"""
|
# The comboBox should be populated with all scan from the message right after initialization
|
||||||
# if device_name == "samx":
|
expected_scans = available_scans_message.resource.keys()
|
||||||
# return FakePositioner(name="samx", enabled=True)
|
assert scan_control.comboBox_scan_selection.count() == len(expected_scans)
|
||||||
#
|
for scan in expected_scans: # Each scan should be in the comboBox
|
||||||
#
|
assert scan_control.comboBox_scan_selection.findText(scan) != -1
|
||||||
# @pytest.fixture(scope="function")
|
|
||||||
# def mocked_client():
|
|
||||||
# # Create a MagicMock object
|
@pytest.mark.parametrize(
|
||||||
# client = MagicMock()
|
"scan_name", ["line_scan", "grid_scan"]
|
||||||
#
|
) # TODO now only for line_scan and grid_scan, later for all loaded scans
|
||||||
# # Mock the producer.get method to return the packed message
|
def test_on_scan_selected(scan_control, scan_name):
|
||||||
# client.producer.get.return_value = available_scans_message
|
# Expected scan info from the message signature
|
||||||
#
|
expected_scan_info = available_scans_message.resource[scan_name]
|
||||||
# # # Mock the device_manager.devices attribute to return a mock object for samx
|
|
||||||
# client.device_manager.devices = MagicMock()
|
# Select a scan from the comboBox
|
||||||
# client.device_manager.devices.__contains__.side_effect = lambda x: x == "samx"
|
scan_control.comboBox_scan_selection.setCurrentText(scan_name)
|
||||||
# client.device_manager.devices.samx = get_mocked_device("samx")
|
|
||||||
#
|
# Check labels and widgets in args table
|
||||||
# return client
|
for index, (arg_key, arg_value) in enumerate(expected_scan_info["arg_input"].items()):
|
||||||
#
|
label = scan_control.args_table.horizontalHeaderItem(index)
|
||||||
#
|
assert label.text().lower() == arg_key # labes
|
||||||
# @pytest.fixture(scope="function")
|
|
||||||
# def scan_control(qtbot, mocked_client): # , mock_dev):
|
for row in range(expected_scan_info["arg_bundle_size"]["min"]):
|
||||||
# widget = ScanControl(client=mocked_client)
|
widget = scan_control.args_table.cellWidget(row, index)
|
||||||
# # widget.dev.samx = MagicMock()
|
assert widget is not None # Confirm that a widget exists
|
||||||
# qtbot.addWidget(widget)
|
expected_widget_type = scan_control.WIDGET_HANDLER.get(arg_value, None)
|
||||||
# qtbot.waitExposed(widget)
|
assert isinstance(widget, expected_widget_type) # Confirm the widget type matches
|
||||||
# yield widget
|
|
||||||
#
|
# kwargs
|
||||||
#
|
kwargs_from_signature = [
|
||||||
# def test_populate_scans(scan_control, mocked_client):
|
param for param in expected_scan_info["signature"] if param["kind"] == "KEYWORD_ONLY"
|
||||||
# # The comboBox should be populated with all scan from the message right after initialization
|
]
|
||||||
# expected_scans = available_scans_message.resource.keys()
|
|
||||||
# assert scan_control.comboBox_scan_selection.count() == len(expected_scans)
|
# Check labels and widgets in kwargs grid layout
|
||||||
# for scan in expected_scans: # Each scan should be in the comboBox
|
for index, kwarg_info in enumerate(kwargs_from_signature):
|
||||||
# assert scan_control.comboBox_scan_selection.findText(scan) != -1
|
label_widget = scan_control.kwargs_layout.itemAtPosition(1, index).widget()
|
||||||
#
|
assert label_widget.text() == kwarg_info["name"].capitalize()
|
||||||
#
|
widget = scan_control.kwargs_layout.itemAtPosition(2, index).widget()
|
||||||
# @pytest.mark.parametrize(
|
expected_widget_type = scan_control.WIDGET_HANDLER.get(kwarg_info["annotation"], QLineEdit)
|
||||||
# "scan_name", ["line_scan", "grid_scan"]
|
assert isinstance(widget, expected_widget_type)
|
||||||
# ) # TODO now only for line_scan and grid_scan, later for all loaded scans
|
|
||||||
# def test_on_scan_selected(scan_control, scan_name):
|
|
||||||
# # Expected scan info from the message signature
|
@pytest.mark.parametrize("scan_name", ["line_scan", "grid_scan"])
|
||||||
# expected_scan_info = available_scans_message.resource[scan_name]
|
def test_add_remove_bundle(scan_control, scan_name):
|
||||||
#
|
# Expected scan info from the message signature
|
||||||
# # Select a scan from the comboBox
|
expected_scan_info = available_scans_message.resource[scan_name]
|
||||||
# scan_control.comboBox_scan_selection.setCurrentText(scan_name)
|
|
||||||
#
|
# Select a scan from the comboBox
|
||||||
# # Check labels and widgets in args table
|
scan_control.comboBox_scan_selection.setCurrentText(scan_name)
|
||||||
# for index, (arg_key, arg_value) in enumerate(expected_scan_info["arg_input"].items()):
|
|
||||||
# label = scan_control.args_table.horizontalHeaderItem(index)
|
# Initial number of args row
|
||||||
# assert label.text().lower() == arg_key # labes
|
initial_num_of_rows = scan_control.args_table.rowCount()
|
||||||
#
|
|
||||||
# for row in range(expected_scan_info["arg_bundle_size"]["min"]):
|
# Check initial row count of args table
|
||||||
# widget = scan_control.args_table.cellWidget(row, index)
|
assert scan_control.args_table.rowCount() == expected_scan_info["arg_bundle_size"]["min"]
|
||||||
# assert widget is not None # Confirm that a widget exists
|
|
||||||
# expected_widget_type = scan_control.WIDGET_HANDLER.get(arg_value, None)
|
# Try to remove default number of args row
|
||||||
# assert isinstance(widget, expected_widget_type) # Confirm the widget type matches
|
scan_control.pushButton_remove_bundle.click()
|
||||||
#
|
assert scan_control.args_table.rowCount() == expected_scan_info["arg_bundle_size"]["min"]
|
||||||
# # kwargs
|
|
||||||
# kwargs_from_signature = [
|
# Try to add two bundles
|
||||||
# param for param in expected_scan_info["signature"] if param["kind"] == "KEYWORD_ONLY"
|
scan_control.pushButton_add_bundle.click()
|
||||||
# ]
|
scan_control.pushButton_add_bundle.click()
|
||||||
#
|
|
||||||
# # Check labels and widgets in kwargs grid layout
|
# check the case where no max number of args are defined
|
||||||
# for index, kwarg_info in enumerate(kwargs_from_signature):
|
# TODO do check also for the case where max number of args are defined
|
||||||
# label_widget = scan_control.kwargs_layout.itemAtPosition(1, index).widget()
|
if expected_scan_info["arg_bundle_size"]["max"] is None:
|
||||||
# assert label_widget.text() == kwarg_info["name"].capitalize()
|
assert scan_control.args_table.rowCount() == initial_num_of_rows + 2
|
||||||
# widget = scan_control.kwargs_layout.itemAtPosition(2, index).widget()
|
|
||||||
# expected_widget_type = scan_control.WIDGET_HANDLER.get(kwarg_info["annotation"], QLineEdit)
|
# Remove one bundle
|
||||||
# assert isinstance(widget, expected_widget_type)
|
scan_control.pushButton_remove_bundle.click()
|
||||||
#
|
|
||||||
#
|
# check the case where no max number of args are defined
|
||||||
# @pytest.mark.parametrize("scan_name", ["line_scan", "grid_scan"])
|
if expected_scan_info["arg_bundle_size"]["max"] is None:
|
||||||
# def test_add_remove_bundle(scan_control, scan_name):
|
assert scan_control.args_table.rowCount() == initial_num_of_rows + 1
|
||||||
# # Expected scan info from the message signature
|
|
||||||
# expected_scan_info = available_scans_message.resource[scan_name]
|
|
||||||
#
|
def test_run_line_scan_with_parameters(scan_control, mocked_client):
|
||||||
# # Select a scan from the comboBox
|
scan_name = "line_scan"
|
||||||
# scan_control.comboBox_scan_selection.setCurrentText(scan_name)
|
kwargs = {"exp_time": 0.1, "steps": 10, "relative": True, "burst_at_each_point": 1}
|
||||||
#
|
args = {"device": "samx", "start": -5, "stop": 5}
|
||||||
# # Initial number of args row
|
|
||||||
# initial_num_of_rows = scan_control.args_table.rowCount()
|
# Select a scan from the comboBox
|
||||||
#
|
scan_control.comboBox_scan_selection.setCurrentText(scan_name)
|
||||||
# # Check initial row count of args table
|
|
||||||
# assert scan_control.args_table.rowCount() == expected_scan_info["arg_bundle_size"]["min"]
|
# Set kwargs in the UI
|
||||||
#
|
for label_index in range(
|
||||||
# # Try to remove default number of args row
|
scan_control.kwargs_layout.rowCount() + 1
|
||||||
# scan_control.pushButton_remove_bundle.click()
|
): # from some reason rowCount() returns 1 less than the actual number of rows
|
||||||
# assert scan_control.args_table.rowCount() == expected_scan_info["arg_bundle_size"]["min"]
|
label_item = scan_control.kwargs_layout.itemAtPosition(1, label_index)
|
||||||
#
|
if label_item:
|
||||||
# # Try to add two bundles
|
label_widget = label_item.widget()
|
||||||
# scan_control.pushButton_add_bundle.click()
|
kwarg_key = WidgetIO.get_value(label_widget).lower()
|
||||||
# scan_control.pushButton_add_bundle.click()
|
if kwarg_key in kwargs:
|
||||||
#
|
widget_item = scan_control.kwargs_layout.itemAtPosition(2, label_index)
|
||||||
# # check the case where no max number of args are defined
|
if widget_item:
|
||||||
# # TODO do check also for the case where max number of args are defined
|
widget = widget_item.widget()
|
||||||
# if expected_scan_info["arg_bundle_size"]["max"] is None:
|
WidgetIO.set_value(widget, kwargs[kwarg_key])
|
||||||
# assert scan_control.args_table.rowCount() == initial_num_of_rows + 2
|
|
||||||
#
|
# Set args in the UI
|
||||||
# # Remove one bundle
|
for col_index in range(scan_control.args_table.columnCount()):
|
||||||
# scan_control.pushButton_remove_bundle.click()
|
header_item = scan_control.args_table.horizontalHeaderItem(col_index)
|
||||||
#
|
if header_item:
|
||||||
# # check the case where no max number of args are defined
|
arg_key = header_item.text().lower()
|
||||||
# if expected_scan_info["arg_bundle_size"]["max"] is None:
|
if arg_key in args:
|
||||||
# assert scan_control.args_table.rowCount() == initial_num_of_rows + 1
|
for row_index in range(scan_control.args_table.rowCount()):
|
||||||
#
|
widget = scan_control.args_table.cellWidget(row_index, col_index)
|
||||||
#
|
WidgetIO.set_value(widget, args[arg_key])
|
||||||
# def test_run_line_scan_with_parameters(scan_control, mocked_client):
|
|
||||||
# scan_name = "line_scan"
|
# Mock the scan function
|
||||||
# kwargs = {"exp_time": 0.1, "steps": 10, "relative": True, "burst_at_each_point": 1}
|
mocked_scan_function = MagicMock()
|
||||||
# args = {"device": "samx", "start": -5, "stop": 5}
|
setattr(mocked_client.scans, scan_name, mocked_scan_function)
|
||||||
#
|
|
||||||
# # Select a scan from the comboBox
|
# Run the scan
|
||||||
# scan_control.comboBox_scan_selection.setCurrentText(scan_name)
|
scan_control.button_run_scan.click()
|
||||||
#
|
|
||||||
# # Set kwargs in the UI
|
# Retrieve the actual arguments passed to the mock
|
||||||
# for label_index in range(
|
called_args, called_kwargs = mocked_scan_function.call_args
|
||||||
# scan_control.kwargs_layout.rowCount() + 1
|
|
||||||
# ): # from some reason rowCount() returns 1 less than the actual number of rows
|
# Check if the scan function was called correctly
|
||||||
# label_item = scan_control.kwargs_layout.itemAtPosition(1, label_index)
|
expected_device = (
|
||||||
# if label_item:
|
mocked_client.device_manager.devices.samx
|
||||||
# label_widget = label_item.widget()
|
) # This is the FakePositioner instance
|
||||||
# kwarg_key = WidgetIO.get_value(label_widget).lower()
|
expected_args_list = [expected_device, args["start"], args["stop"]]
|
||||||
# if kwarg_key in kwargs:
|
assert called_args == tuple(
|
||||||
# widget_item = scan_control.kwargs_layout.itemAtPosition(2, label_index)
|
expected_args_list
|
||||||
# if widget_item:
|
), "The positional arguments passed to the scan function do not match expected values."
|
||||||
# widget = widget_item.widget()
|
assert (
|
||||||
# WidgetIO.set_value(widget, kwargs[kwarg_key])
|
called_kwargs == kwargs
|
||||||
#
|
), "The keyword arguments passed to the scan function do not match expected values."
|
||||||
# # Set args in the UI
|
|
||||||
# for col_index in range(scan_control.args_table.columnCount()):
|
|
||||||
# header_item = scan_control.args_table.horizontalHeaderItem(col_index)
|
|
||||||
# if header_item:
|
|
||||||
# arg_key = header_item.text().lower()
|
|
||||||
# if arg_key in args:
|
|
||||||
# for row_index in range(scan_control.args_table.rowCount()):
|
|
||||||
# widget = scan_control.args_table.cellWidget(row_index, col_index)
|
|
||||||
# WidgetIO.set_value(widget, args[arg_key])
|
|
||||||
#
|
|
||||||
# # Mock the scan function
|
|
||||||
# mocked_scan_function = MagicMock()
|
|
||||||
# setattr(mocked_client.scans, scan_name, mocked_scan_function)
|
|
||||||
#
|
|
||||||
# # Run the scan
|
|
||||||
# scan_control.button_run_scan.click()
|
|
||||||
#
|
|
||||||
# # Retrieve the actual arguments passed to the mock
|
|
||||||
# called_args, called_kwargs = mocked_scan_function.call_args
|
|
||||||
#
|
|
||||||
# # Check if the scan function was called correctly
|
|
||||||
# expected_device = (
|
|
||||||
# mocked_client.device_manager.devices.samx
|
|
||||||
# ) # This is the FakePositioner instance
|
|
||||||
# expected_args_list = [expected_device, args["start"], args["stop"]]
|
|
||||||
# assert called_args == tuple(
|
|
||||||
# expected_args_list
|
|
||||||
# ), "The positional arguments passed to the scan function do not match expected values."
|
|
||||||
# assert (
|
|
||||||
# called_kwargs == kwargs
|
|
||||||
# ), "The keyword arguments passed to the scan function do not match expected values."
|
|
||||||
|
Reference in New Issue
Block a user