Add tests/test_utils_lazypv.py
Run CI Tests / test (push) Successful in 29s

This commit is contained in:
2025-07-25 17:31:25 +02:00
parent e16bc21a81
commit 03a4209ad6
+78
View File
@@ -0,0 +1,78 @@
import pytest
import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from slic.utils.lazypv import *
import pytest
import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from slic.utils.lazypv import PV
def test_getattr():
# Only valid kwargs accepted by epics.PV
pv = PV(
"ca://TEST:FLOAT",
form='time',
auto_monitor=True,
verbose=True,
connection_timeout=3.0
)
# Pv not yet instantiated
assert pv._pv is None
# Trigger instantiation by accessing a real attribute
pvname = pv.__getattr__('pvname')
assert isinstance(pvname, str)
assert pvname == "ca://TEST:FLOAT"
assert pv._pv is not None
# access all known epics.PV kwargs via __getattr__
form = pv.__getattr__('form')
assert form == 'time'
auto_monitor = pv.__getattr__('auto_monitor')
assert auto_monitor is True
verbose = pv.__getattr__('verbose')
assert verbose is True
timeout = pv.__getattr__('connection_timeout')
assert isinstance(timeout, (float, int))
assert timeout == 3.0
# Have the get() method from (__getattr__) to get all the parameters in a dict
get_method = pv.__getattr__('get')
assert callable(get_method) # Check if it's really a function
result = get_method()
assert isinstance(result, dict), "Expected dict because form='time'"
# Full check of expected fields
assert 'value' in result
assert 'timestamp' in result
assert 'status' in result
assert 'severity' in result
value = result['value']
timestamp = result['timestamp']
status = result['status']
severity = result['severity']
assert isinstance(value, (int, float)), "Value should be numeric"
assert isinstance(timestamp, (int, float)), "Timestamp should be numeric"
assert isinstance(status, int), "Status should be int"
assert isinstance(severity, int), "Severity should be int"
# Confirm PV not re-instantiated on second getattr
second_get = pv.__getattr__('get')
assert get_method is second_get
# Remove the instantiated PV
del pv._pv
pv._pv = None
assert pv._pv is None