Skip to content

Commit d254dd3

Browse files
committed
Add tests
1 parent e2d4636 commit d254dd3

File tree

1 file changed

+77
-2
lines changed

1 file changed

+77
-2
lines changed

tests/backends/epics/test_ioc.py

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,96 @@
1+
from typing import Any
2+
13
import pytest
24
from pytest_mock import MockerFixture
35

4-
from fastcs.attributes import AttrR, AttrRW
6+
from fastcs.attributes import AttrR, AttrRW, AttrW
57
from fastcs.backends.epics.ioc import (
68
EPICS_MAX_NAME_LENGTH,
79
EpicsIOC,
810
_add_attr_pvi_info,
911
_add_pvi_info,
1012
_add_sub_controller_pvi_info,
13+
_get_input_record,
14+
_get_output_record,
1115
)
1216
from fastcs.controller import Controller
1317
from fastcs.cs_methods import Command
14-
from fastcs.datatypes import Int
18+
from fastcs.datatypes import Int, String
19+
from fastcs.exceptions import FastCSException
1520
from fastcs.mapping import Mapping
1621

1722
DEVICE = "DEVICE"
1823

24+
SEVENTEEN_VALUES = [str(i) for i in range(1, 18)]
25+
ONOFF_STATES = {"ZRST": "disabled", "ONST": "enabled"}
26+
27+
28+
@pytest.mark.parametrize(
29+
"attribute,record_type,kwargs",
30+
(
31+
(AttrR(String()), "longStringIn", {}),
32+
(
33+
AttrR(String(), allowed_values=list(ONOFF_STATES.values())),
34+
"mbbIn",
35+
ONOFF_STATES,
36+
),
37+
(AttrR(String(), allowed_values=SEVENTEEN_VALUES), "longStringIn", {}),
38+
),
39+
)
40+
def test_get_input_record(
41+
attribute: AttrR,
42+
record_type: str,
43+
kwargs: dict[str, Any],
44+
mocker: MockerFixture,
45+
):
46+
builder = mocker.patch("fastcs.backends.epics.ioc.builder")
47+
48+
pv = "PV"
49+
_get_input_record(pv, attribute)
50+
51+
getattr(builder, record_type).assert_called_once_with(pv, **kwargs)
52+
53+
54+
def test_get_input_record_raises(mocker: MockerFixture):
55+
# Pass a mock as attribute to provoke the fallback case matching on datatype
56+
with pytest.raises(FastCSException):
57+
_get_input_record("PV", mocker.MagicMock())
58+
59+
60+
@pytest.mark.parametrize(
61+
"attribute,record_type,kwargs",
62+
(
63+
(AttrR(String()), "longStringOut", {}),
64+
(
65+
AttrR(String(), allowed_values=list(ONOFF_STATES.values())),
66+
"mbbOut",
67+
ONOFF_STATES,
68+
),
69+
(AttrR(String(), allowed_values=SEVENTEEN_VALUES), "longStringOut", {}),
70+
),
71+
)
72+
def test_get_output_record(
73+
attribute: AttrW,
74+
record_type: str,
75+
kwargs: dict[str, Any],
76+
mocker: MockerFixture,
77+
):
78+
builder = mocker.patch("fastcs.backends.epics.ioc.builder")
79+
update = mocker.MagicMock()
80+
81+
pv = "PV"
82+
_get_output_record(pv, attribute, on_update=update)
83+
84+
getattr(builder, record_type).assert_called_once_with(
85+
pv, always_update=True, on_update=update, **kwargs
86+
)
87+
88+
89+
def test_get_output_record_raises(mocker: MockerFixture):
90+
# Pass a mock as attribute to provoke the fallback case matching on datatype
91+
with pytest.raises(FastCSException):
92+
_get_output_record("PV", mocker.MagicMock(), on_update=mocker.MagicMock())
93+
1994

2095
def test_ioc(mocker: MockerFixture, mapping: Mapping):
2196
builder = mocker.patch("fastcs.backends.epics.ioc.builder")

0 commit comments

Comments
 (0)