diff --git a/bec_widgets/cli/client_utils.py b/bec_widgets/cli/client_utils.py index 60173dc3..c2097012 100644 --- a/bec_widgets/cli/client_utils.py +++ b/bec_widgets/cli/client_utils.py @@ -36,6 +36,17 @@ def rpc_call(func): @wraps(func) def wrapper(self, *args, **kwargs): + # we could rely on a strict type check here, but this is more flexible + # moreover, it would anyway crash for objects... + out = [] + for arg in args: + if hasattr(arg, "name"): + arg = arg.name + out.append(arg) + args = tuple(out) + for key, val in kwargs.items(): + if hasattr(val, "name"): + kwargs[key] = val.name if not self.gui_is_alive(): raise RuntimeError("GUI is not alive") return self._run_rpc(func.__name__, *args, **kwargs) diff --git a/tests/unit_tests/test_client_utils.py b/tests/unit_tests/test_client_utils.py new file mode 100644 index 00000000..61dc63ca --- /dev/null +++ b/tests/unit_tests/test_client_utils.py @@ -0,0 +1,29 @@ +from unittest import mock + +import pytest + +from bec_widgets.cli.client import BECFigure + +from .client_mocks import FakeDevice + + +@pytest.fixture +def cli_figure(): + fig = BECFigure(gui_id="test") + with mock.patch.object(fig, "_run_rpc") as mock_rpc_call: + with mock.patch.object(fig, "gui_is_alive", return_value=True): + yield fig, mock_rpc_call + + +def test_rpc_call_plot(cli_figure): + fig, mock_rpc_call = cli_figure + fig.plot("samx", "bpm4i") + mock_rpc_call.assert_called_with("plot", "samx", "bpm4i") + + +def test_rpc_call_accepts_device_as_input(cli_figure): + dev1 = FakeDevice("samx") + dev2 = FakeDevice("bpm4i") + fig, mock_rpc_call = cli_figure + fig.plot(dev1, dev2) + mock_rpc_call.assert_called_with("plot", "samx", "bpm4i")