From 08ab59440da6bafa951f1d9a3a7a107388745c08 Mon Sep 17 00:00:00 2001 From: wyzula-jan Date: Wed, 11 Nov 2026 12:14:33 +0100 Subject: [PATCH] fix(entry_validator): name validate_signal parameters device and signal --- bec_widgets/utils/entry_validator.py | 36 +++++++++++++------------ tests/unit_tests/test_curve_settings.py | 24 +++++++++++++++++ 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/bec_widgets/utils/entry_validator.py b/bec_widgets/utils/entry_validator.py index 2c8e6d79..31171bf7 100644 --- a/bec_widgets/utils/entry_validator.py +++ b/bec_widgets/utils/entry_validator.py @@ -2,45 +2,47 @@ class EntryValidator: def __init__(self, devices): self.devices = devices - def validate_signal(self, name: str, entry: str = None) -> str: + def validate_signal(self, device: str, signal: str | None = None) -> str: """ - Validate a signal entry for a given device. If the entry is not provided, the first signal entry will be used from the device hints. + Validate a signal for a given device. If the signal is not provided, the first signal will be used from the device hints. Args: - name(str): Device name - entry(str): Signal entry + device(str): Device name + signal(str): Signal name Returns: - str: Signal entry + str: Signal name """ - if name not in self.devices: - raise ValueError(f"Device '{name}' not found in current BEC session") + if device not in self.devices: + raise ValueError(f"Device '{device}' not found in current BEC session") - device = self.devices[name] + device_obj = self.devices[device] # Build list of available signal entries from device._info['signals'] - signals_dict = getattr(device, "_info", {}).get("signals", {}) + signals_dict = getattr(device_obj, "_info", {}).get("signals", {}) available_entries = [ sig.get("obj_name") for sig in signals_dict.values() if sig.get("obj_name") ] # If no signals are found, means device is a signal, use the device name as the entry if not available_entries: - available_entries = [name] + available_entries = [device] # edge case for if name is passed instead of full_name, should not happen - if entry in signals_dict: - entry = signals_dict[entry].get("obj_name", entry) + if signal in signals_dict: + signal = signals_dict[signal].get("obj_name", signal) - if entry is None or entry == "": - entry = next(iter(device._hints), name) if hasattr(device, "_hints") else name - if entry not in available_entries: + if signal is None or signal == "": + signal = ( + next(iter(device_obj._hints), device) if hasattr(device_obj, "_hints") else device + ) + if signal not in available_entries: raise ValueError( - f"Entry '{entry}' not found in device '{name}' signals. " + f"Entry '{signal}' not found in device '{device}' signals. " f"Available signals: '{available_entries}'" ) - return entry + return signal def validate_monitor(self, monitor: str) -> str: """ diff --git a/tests/unit_tests/test_curve_settings.py b/tests/unit_tests/test_curve_settings.py index 477eef9d..cae0e76e 100644 --- a/tests/unit_tests/test_curve_settings.py +++ b/tests/unit_tests/test_curve_settings.py @@ -477,6 +477,30 @@ def test_export_data_history_curve(curve_tree_fixture, scan_history_factory): assert exported["label"] == "bpm4i-bpm4i-scan-2" +def test_export_data_validates_a_typed_signal(curve_tree_fixture): + """ + A signal typed into the row that the device does not have is reported as such. + """ + curve_tree, _ = curve_tree_fixture + device_row = curve_tree.add_new_curve(device="bpm4i", signal="bpm4i") + device_row.entry_edit.setEditText("not_a_signal") + + with pytest.raises(ValueError, match="'not_a_signal' not found in device 'bpm4i'"): + device_row.export_data() + + +def test_entry_validator_takes_device_and_signal_keywords(curve_tree_fixture): + """ + The validator names its parameters like its callers do: device and signal. + """ + curve_tree, _ = curve_tree_fixture + validator = curve_tree.add_new_curve(device="bpm4i", signal="bpm4i").entry_validator + + assert validator.validate_signal(device="bpm4i", signal="bpm4i") == "bpm4i" + assert validator.validate_signal(device="bpm4i") == "bpm4i" + assert validator.validate_signal("bpm4i", "bpm4i") == "bpm4i" + + ################################################## # Staging of edits (issue #1314) ##################################################