mirror of
https://github.com/bec-project/bec_widgets.git
synced 2025-07-13 03:01:50 +02:00
fix: fix bug in RPCReferenc prohibiting from executing properties, added test
This commit is contained in:
@ -99,6 +99,16 @@ class RPCReference:
|
||||
def __getitem__(self, key):
|
||||
return self._registry[self._gui_id].__getitem__(key)
|
||||
|
||||
def __setattr__(self, name, value):
|
||||
if name in ["_registry", "_gui_id"]:
|
||||
super().__setattr__(name, value)
|
||||
else:
|
||||
registry = super().__getattribute__("_registry")
|
||||
gui_id = super().__getattribute__("_gui_id")
|
||||
if gui_id not in registry:
|
||||
raise DeletedWidgetError(f"Widget with gui_id {gui_id} has been deleted")
|
||||
registry.__getitem__(gui_id).__setattr__(name, value)
|
||||
|
||||
def __repr__(self):
|
||||
if self._gui_id not in self._registry:
|
||||
return f"<Deleted widget with gui_id {self._gui_id}>"
|
||||
|
@ -1,5 +1,8 @@
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
from bec_widgets.cli.client import Waveform
|
||||
from bec_widgets.cli.rpc.rpc_base import DeletedWidgetError, RPCBase, RPCReference
|
||||
|
||||
|
||||
@ -8,6 +11,11 @@ def rpc_base():
|
||||
yield RPCBase(gui_id="rpc_base_test", name="test")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def rpc_waveform():
|
||||
yield Waveform(gui_id="rpc_waveform_test", name="test")
|
||||
|
||||
|
||||
def test_rpc_base(rpc_base):
|
||||
"""Test registry and reference creation"""
|
||||
registry = {rpc_base._gui_id: rpc_base}
|
||||
@ -26,3 +34,21 @@ def test_rpc_base(rpc_base):
|
||||
|
||||
with pytest.raises(DeletedWidgetError):
|
||||
ref.widget_name # Object no longer referenced in registry
|
||||
|
||||
|
||||
def test_rpc_reference_property(rpc_waveform):
|
||||
"""Test registry and reference creation"""
|
||||
waveform = rpc_waveform
|
||||
registry = {rpc_waveform._gui_id: rpc_waveform}
|
||||
ref = RPCReference(registry, rpc_waveform._gui_id)
|
||||
|
||||
with mock.patch.object(waveform, "_run_rpc") as mock_run_rpc:
|
||||
with mock.patch.object(waveform._root, "_gui_is_alive", return_value=True):
|
||||
# Call property
|
||||
ref.enable_fps_monitor = True
|
||||
assert mock_run_rpc.call_count == 1
|
||||
assert mock_run_rpc.call_args == mock.call("enable_fps_monitor", True)
|
||||
# Call method
|
||||
ref.set(title="test_title")
|
||||
assert mock_run_rpc.call_count == 2
|
||||
assert mock_run_rpc.call_args == mock.call("set", title="test_title")
|
||||
|
Reference in New Issue
Block a user