92 lines
2.5 KiB
Python
92 lines
2.5 KiB
Python
# test_utils_pv.py
|
||
|
||
import asyncio
|
||
import threading
|
||
import time
|
||
import pytest
|
||
import colorama
|
||
from io import StringIO
|
||
from contextlib import contextmanager
|
||
|
||
from caproto.server import pvproperty, PVGroup
|
||
|
||
# --- IOC simulé avec caproto ---
|
||
class TestIOC(PVGroup):
|
||
PV = pvproperty(value=0.0, name='TEST:PV', units='units')
|
||
|
||
|
||
# --- Démarrage automatique de l'IOC ---
|
||
@pytest.fixture(scope="module", autouse=True)
|
||
def run_test_ioc():
|
||
ioc = TestIOC()
|
||
|
||
def run():
|
||
asyncio.run(ioc.serve(async_lib='asyncio'))
|
||
|
||
thread = threading.Thread(target=run, daemon=True)
|
||
thread.start()
|
||
time.sleep(1.5)
|
||
yield
|
||
|
||
|
||
# --- Import de TA CLASSE PV depuis slic.utils.pv ---
|
||
from slic.utils.pv import PV
|
||
|
||
# --- Capture stdout ---
|
||
@pytest.fixture
|
||
def capture_stdout(monkeypatch):
|
||
buf = StringIO()
|
||
monkeypatch.setattr("sys.stdout", buf)
|
||
return buf
|
||
|
||
|
||
# --- PV instancié sur l’IOC Caproto en live ---
|
||
@pytest.fixture
|
||
def real_pv():
|
||
pv = PV("TEST:PV")
|
||
assert pv.get() == 0.0
|
||
return pv
|
||
|
||
|
||
@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'),
|
||
(75.0, "███████▌ ", colorama.Fore.GREEN, 'PV "TEST:PV" at 75.0 units'),
|
||
(100.0, "██████████", colorama.Fore.GREEN, 'PV "TEST:PV" at 100.0 units'),
|
||
(150.0, ">>>>>>>>>>", colorama.Fore.RED, 'PV "TEST:PV" at 150.0 units'),
|
||
(-50.0, "<<<<<<<<<<", colorama.Fore.RED, 'PV "TEST:PV" at -50.0 units'),
|
||
])
|
||
def test_put_with_progress(real_pv, capture_stdout, value, expected_bar, expected_color, expected_repr):
|
||
pv = real_pv
|
||
pv.put(value, show_progress=True)
|
||
|
||
output = capture_stdout.getvalue()
|
||
assert f"|{expected_bar}|" in output
|
||
assert expected_color in output
|
||
assert str(value) in output
|
||
|
||
assert pv.get() == pytest.approx(value)
|
||
assert repr(pv) == expected_repr
|
||
|
||
|
||
def test_use_callback_context_manager(real_pv):
|
||
pv = real_pv
|
||
callback_values = []
|
||
|
||
def cb(value=None, **kwargs):
|
||
callback_values.append(value)
|
||
|
||
# Le callback est ajouté temporairement
|
||
with pv.use_callback(cb):
|
||
pv.put(42.0, wait=True)
|
||
time.sleep(0.1)
|
||
|
||
# Il a bien été appelé
|
||
assert any(val == 42.0 for val in callback_values)
|
||
|
||
# Hors du contexte : pas de rappel
|
||
callback_values.clear()
|
||
pv.put(50.0, wait=True)
|
||
time.sleep(0.1)
|
||
assert callback_values == []
|