Skip to content

Commit f787546

Browse files
committed
Work on tango tests
1 parent ae978a9 commit f787546

File tree

3 files changed

+90
-7
lines changed

3 files changed

+90
-7
lines changed

src/fastcs/backends/tango/dsr.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,10 @@ def _collect_dsr_args(options: TangoDSROptions) -> list[str]:
155155
class TangoDSR:
156156
def __init__(self, mapping: Mapping):
157157
self._mapping = mapping
158+
self.dev_class = self._mapping.controller.__class__.__name__
159+
self._device = self._create_device()
158160

159-
def run(self, options: TangoDSROptions | None = None) -> None:
160-
if options is None:
161-
options = TangoDSROptions()
162-
161+
def _create_device(self):
163162
class_dict: dict = {
164163
**_collect_dev_attributes(self._mapping),
165164
**_collect_dev_commands(self._mapping),
@@ -169,12 +168,17 @@ def run(self, options: TangoDSROptions | None = None) -> None:
169168
}
170169

171170
class_bases = (server.Device,)
172-
pytango_class = type(options.dev_class, class_bases, class_dict)
171+
pytango_class = type(self.dev_class, class_bases, class_dict)
172+
return pytango_class
173+
174+
def run(self, options: TangoDSROptions | None = None) -> None:
175+
if options is None:
176+
options = TangoDSROptions()
173177

174178
dsr_args = _collect_dsr_args(options)
175179

176180
server.run(
177-
(pytango_class,),
181+
(self.dev_class,),
178182
[options.dev_class, options.dsr_instance, *dsr_args],
179183
green_mode=server.GreenMode.Asyncio,
180184
)

tests/backends/tango/test_dsr.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import pytest
22
from pytest_mock import MockerFixture
3+
from tango import DevState
34
from tango._tango import AttrWriteType, CmdArgType
45

6+
from fastcs.attributes import AttrR
57
from fastcs.backends.tango.dsr import _collect_dev_attributes, _collect_dev_commands
8+
from fastcs.datatypes import Bool, Int
69

710

811
def test_collect_attributes(mapping):
@@ -35,3 +38,70 @@ async def test_collect_commands(mapping, mocker: MockerFixture):
3538
# Check that command is created and it can be called
3639
assert list(commands.keys()) == ["Go"]
3740
await commands["Go"](mocker.MagicMock())
41+
42+
43+
class TestTangoDevice:
44+
def test_list_attributes(self, tango_context):
45+
assert list(tango_context.get_attribute_list()) == [
46+
"BigEnum",
47+
"ReadBool",
48+
"ReadInt",
49+
"ReadWriteFloat",
50+
"ReadWriteInt",
51+
"StringEnum",
52+
"WriteBool",
53+
"State",
54+
"Status",
55+
]
56+
57+
def test_list_commands(self, tango_context):
58+
assert list(tango_context.get_command_list()) == [
59+
"Go",
60+
"Init",
61+
"State",
62+
"Status",
63+
]
64+
65+
def test_read_int(self, tango_context):
66+
result = tango_context.read_attribute("ReadInt")
67+
assert result.value == AttrR(Int())._value
68+
69+
def test_read_write_int(self, tango_context):
70+
expected = 42
71+
tango_context.write_attribute("ReadWriteInt", expected)
72+
result = tango_context.read_attribute("ReadWriteInt")
73+
assert result.w_value == expected
74+
75+
def test_read_write_float(self, tango_context):
76+
expected = 23.1
77+
tango_context.write_attribute("ReadWriteFloat", expected)
78+
result = tango_context.read_attribute("ReadWriteFloat")
79+
assert result.w_value == expected
80+
81+
def test_read_bool(self, tango_context):
82+
result = tango_context.read_attribute("ReadBool")
83+
assert result.value == AttrR(Bool())._value
84+
85+
def test_write_bool(self, tango_context):
86+
tango_context.write_attribute("WriteBool", AttrR(Bool())._value)
87+
88+
# # We need to discuss enums
89+
# def test_string_enum(self, tango_context):
90+
# expected = 1
91+
# tango_context.write_attribute("StringEnum", "green")
92+
# result = tango_context.read_attribute("StringEnum")
93+
# assert result.w_value == expected
94+
95+
def test_big_enum(self, tango_context):
96+
result = tango_context.read_attribute("BigEnum")
97+
assert result.value == AttrR(Int(), allowed_values=list(range(1, 18)))._value
98+
99+
def test_go(self, tango_context):
100+
tango_context.command_inout("go")
101+
102+
def test_state(self, tango_context):
103+
assert tango_context.command_inout("State") == DevState.ON
104+
105+
def test_status(self, tango_context):
106+
expected = "The device is in ON state."
107+
assert tango_context.command_inout("Status") == expected

tests/conftest.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
import pytest
1010
from aioca import purge_channel_caches
11+
from tango.test_context import DeviceTestContext
1112

1213
from fastcs.attributes import AttrR, AttrRW, AttrW, Handler, Sender, Updater
14+
from fastcs.backends.tango.backend import TangoBackend
1315
from fastcs.controller import Controller
1416
from fastcs.datatypes import Bool, Float, Int, String
1517
from fastcs.mapping import Mapping
@@ -58,7 +60,7 @@ class TestController(Controller):
5860
string_enum: AttrRW = AttrRW(String(), allowed_values=["red", "green", "blue"])
5961
big_enum: AttrR = AttrR(
6062
Int(),
61-
allowed_values=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17],
63+
allowed_values=list(range(1, 18)),
6264
)
6365

6466
initialised = False
@@ -120,3 +122,10 @@ def ioc():
120122
except ValueError:
121123
# Someone else already called communicate
122124
pass
125+
126+
127+
@pytest.fixture(scope="class")
128+
def tango_context():
129+
device = TangoBackend(TestController())._dsr._device
130+
with DeviceTestContext(device) as proxy:
131+
yield proxy

0 commit comments

Comments
 (0)