diff --git a/tests/test_utils_pv.py b/tests/test_utils_pv.py index 1ba84e39d..142246e11 100644 --- a/tests/test_utils_pv.py +++ b/tests/test_utils_pv.py @@ -4,25 +4,25 @@ import colorama from slic.utils.pv import PV from epics import PV as EpicsPV - class FakeEPICS(EpicsPV): - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self._value = 0.0 + def __init__(self, pvname, **kwargs): + super().__init__(pvname, **kwargs) + self._value = 0.0 # Initial value set to 0.0 self.units = "units" self._callbacks = {} + self.pvname = pvname # Important pour le repr def get(self, *args, **kwargs): return self._value def put(self, value, *args, **kwargs): - old_value = self._value self._value = value - for cb in self._callbacks.values(): - cb(value=value) + # Trigger all callbacks + for callback in self._callbacks.values(): + callback(value=value, **kwargs) return value - def add_callback(self, callback): + def add_callback(self, callback, **kwargs): cb_index = len(self._callbacks) + 1 self._callbacks[cb_index] = callback return cb_index @@ -30,12 +30,16 @@ class FakeEPICS(EpicsPV): def remove_callback(self, cb_index): self._callbacks.pop(cb_index, None) + def __repr__(self): + return f"PV {self.pvname} at {self._value} {self.units}" @pytest.fixture def fake_epics_pv(monkeypatch): monkeypatch.setattr("epics.PV", FakeEPICS) - return PV("TEST:PV") - + pv = PV("TEST:PV") + # Force initial value + pv.put(0.0, wait=True) + return pv @pytest.fixture def capture_stdout(monkeypatch): @@ -43,7 +47,6 @@ def capture_stdout(monkeypatch): monkeypatch.setattr("sys.stdout", buf) return buf - @pytest.mark.parametrize("value, expected_bar, expected_color, expected_repr", [ (25.0, "██▌ ", colorama.Fore.GREEN, 'PV "TEST:PV" at 25.0 units'), (50.0, "█████ ", colorama.Fore.GREEN, 'PV "TEST:PV" at 50.0 units'), @@ -55,10 +58,10 @@ def capture_stdout(monkeypatch): def test_progress_and_repr(fake_epics_pv, capture_stdout, value, expected_bar, expected_color, expected_repr): pv = fake_epics_pv - # Verify initial value + # Verify initial value is properly set to 0.0 assert pv.get() == 0.0 - # Test put() and progress bar + # Test put() with progress pv.put(value, show_progress=True) output = capture_stdout.getvalue() @@ -67,18 +70,15 @@ def test_progress_and_repr(fake_epics_pv, capture_stdout, value, expected_bar, e assert expected_color in output assert str(value) in output - # Verify value update + # Verify value was updated assert pv.get() == pytest.approx(value) - - # Test repr - assert repr(pv) == expected_repr - # Test original repr + # Test string representation + assert repr(pv) == expected_repr assert pv.orig_repr() == f"PV TEST:PV at {value} units" capture_stdout.truncate(0) - def test_use_callback_context_manager(fake_epics_pv): pv = fake_epics_pv callback_log = [] @@ -89,10 +89,9 @@ def test_use_callback_context_manager(fake_epics_pv): # Verify no callbacks initially assert len(pv._pv._callbacks) == 0 - with pv.use_callback(test_callback) as callback_id: + with pv.use_callback(test_callback): # Callback should be added assert len(pv._pv._callbacks) == 1 - assert callback_id in pv._pv._callbacks # Trigger callback pv.put(10.0)