This commit is contained in:
+31
-41
@@ -4,50 +4,40 @@ import colorama
|
||||
from unittest.mock import MagicMock
|
||||
from slic.utils.pv import PV
|
||||
|
||||
import pytest
|
||||
from io import StringIO
|
||||
import colorama
|
||||
from slic.utils.pv import PV
|
||||
from epics import PV as EpicsPV
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def capture_stdout(monkeypatch):
|
||||
buf = StringIO()
|
||||
monkeypatch.setattr("sys.stdout", buf)
|
||||
return buf
|
||||
|
||||
# Fake PV
|
||||
class FakeEPICS(EpicsPV):
|
||||
def __init__(self, *args, **kwargs):
|
||||
|
||||
super().__init__(*args, **kwargs)
|
||||
self._value = 0.0
|
||||
self.units = "units"
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
return self._value
|
||||
|
||||
def put(self, value, *args, **kwargs):
|
||||
self._value = value
|
||||
return value
|
||||
|
||||
# Fixture that monkeypatch epics.PV → FakeEPICS
|
||||
@pytest.fixture
|
||||
def fake_epics_pv(monkeypatch):
|
||||
class FakeEPICS:
|
||||
def __init__(self, pvname, **kwargs):
|
||||
self.pvname = pvname
|
||||
self._value = 0.0 # Initialize with default value
|
||||
self.units = "units"
|
||||
self._callbacks = {}
|
||||
self.auto_monitor = kwargs.get('auto_monitor', False)
|
||||
|
||||
def get(self):
|
||||
print(">> FAKE GET =", self._value)
|
||||
return self._value
|
||||
|
||||
def put(self, value, **kwargs):
|
||||
old = self._value
|
||||
self._value = value
|
||||
for cb in self._callbacks.values():
|
||||
cb(value=value, old=old, **kwargs)
|
||||
monkeypatch.setattr("epics.PV", FakeEPICS)
|
||||
return PV("TEST:PV")
|
||||
|
||||
|
||||
def add_callback(self, callback):
|
||||
cb_id = len(self._callbacks) + 1
|
||||
self._callbacks[cb_id] = callback
|
||||
return cb_id
|
||||
|
||||
def remove_callback(self, cb_id):
|
||||
self._callbacks.pop(cb_id, None)
|
||||
|
||||
def __repr__(self):
|
||||
return f"<FakeEPICS {self.pvname}>"
|
||||
|
||||
# Create mock instance we can control
|
||||
mock_pv = FakeEPICS("TEST:PV")
|
||||
|
||||
# Monkey patch EPICS PV creation
|
||||
def mock_pv_creator(pvname, **kwargs):
|
||||
return mock_pv
|
||||
|
||||
monkeypatch.setattr('epics.PV', mock_pv_creator)
|
||||
|
||||
# Create our test PV
|
||||
pv = PV("TEST:PV")
|
||||
return pv
|
||||
|
||||
@pytest.fixture
|
||||
def capture_stdout(monkeypatch):
|
||||
|
||||
Reference in New Issue
Block a user