test: added tests for dynamic_pseudo

This commit is contained in:
wakonig_k 2024-03-08 15:29:51 +01:00
parent d9f09b0d86
commit c76e1a0b4e
2 changed files with 35 additions and 3 deletions

View File

@ -3,6 +3,7 @@ This module provides a class for creating a pseudo signal that is computed from
""" """
from functools import reduce from functools import reduce
from typing import Callable
from ophyd import SignalRO from ophyd import SignalRO
from ophyd.ophydobj import Kind from ophyd.ophydobj import Kind
@ -63,7 +64,7 @@ class ComputedSignal(SignalRO):
self._run_subs(sub_type=self.SUB_VALUE, old_value=None, value=self.get()) self._run_subs(sub_type=self.SUB_VALUE, old_value=None, value=self.get())
@property @property
def compute_method(self): def compute_method(self) -> Callable | None:
""" """
Set the compute method for the pseudo signal Set the compute method for the pseudo signal
@ -117,6 +118,7 @@ class ComputedSignal(SignalRO):
self._input_signals = signals self._input_signals = signals
def get(self): def get(self):
if self._compute_method: if self.compute_method:
return self._compute_method(*self._input_signals) # pylint: disable=not-callable
return self.compute_method(*self.input_signals)
return None return None

View File

@ -0,0 +1,30 @@
from unittest import mock
import pytest
from ophyd_devices.utils.dynamic_pseudo import ComputedSignal
from tests.utils import DMMock
@pytest.fixture
def device_manager_with_devices():
dm = DMMock()
dm.add_device("a")
dm.add_device("b")
device_mock = mock.MagicMock()
device_mock.obj.readback.get.return_value = 20
dm.devices["a"] = device_mock
dm.devices["b"] = device_mock
return dm
def test_computed_signal(device_manager_with_devices):
signal = ComputedSignal(name="test", device_manager=device_manager_with_devices)
assert signal.get() is None
signal.compute_method = "def test(a, b): return a.get() + b.get()"
signal.input_signals = ["a_readback", "b_readback"]
assert signal.get() == 40
assert callable(signal.compute_method)