File: test_group.py

package info (click to toggle)
pytango 10.1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,304 kB
  • sloc: python: 27,795; cpp: 16,150; sql: 252; sh: 152; makefile: 43
file content (227 lines) | stat: -rw-r--r-- 6,197 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# SPDX-FileCopyrightText: All Contributors to the PyTango project
# SPDX-License-Identifier: LGPL-3.0-or-later
import pytest

from tango import (
    AttrWriteType,
    DevFailed,
    DeviceData,
    DeviceDataList,
    DevState,
    Group,
    GroupAttrReply,
    GroupCmdReply,
    GroupReply,
    StdStringVector,
)
from tango.server import Device, attribute, command
from tango.test_context import MultiDeviceTestContext
from tango.utils import TO_TANGO_TYPE

_is_allowed = [True]


class SimpleDevice(Device):
    _attr = None

    def init_device(self):
        self._attr = int(self.get_name()[-1])
        super().init_device()
        self.set_state(DevState.ON)

    @attribute(access=AttrWriteType.READ_WRITE)
    def attr(self) -> int:
        return self._attr

    @attr.setter
    def set_attr(self, new_value):
        self._attr = new_value

    @attr.is_allowed
    def is_allowed(self, req) -> bool:
        return _is_allowed[0]

    @command()
    def indent(self, v_in: int) -> int:
        return v_in

    def is_indent_allowed(self) -> bool:
        return _is_allowed[0]


devices_info = (
    {
        "class": SimpleDevice,
        "devices": [{"name": "test/device/1"}, {"name": "test/device/2"}],
    },
)


@pytest.fixture(scope="module")
def context():
    with MultiDeviceTestContext(devices_info) as ctx:
        yield ctx


def test_wrong_device_name(context):
    group = Group("test_group")
    group.add("test/device/1")

    with pytest.raises(KeyError, match="some/wrong/device"):
        group.get_device("some/wrong/device")

    with pytest.raises(KeyError, match="some/wrong/device"):
        group["some/wrong/device"]


def test_nested_multi_group(context):
    group_singles = Group("add-one-at-a-time")
    group_singles.add("test/device/1")
    group_singles.add("test/device/2")
    group_multiples_list = Group("add-multiple-via-list")
    group_multiples_list.add(["test/device/1", "test/device/2"])
    group_multiples_vector = Group("add-multiple-via-std-vector")
    vector = StdStringVector()
    vector.append("test/device/1")
    vector.append("test/device/2")
    group_multiples_vector.add(vector)
    group_in_group = Group("add-sub-group")
    group_in_group.add(group_singles)

    groups = [
        group_singles,
        group_multiples_list,
        group_multiples_vector,
        group_in_group,
    ]

    device1_fqdn = context.get_device_access("test/device/1")
    device2_fqdn = context.get_device_access("test/device/2")
    for group in groups:
        assert device1_fqdn in group
        assert device2_fqdn in group
        reply = group.read_attribute("attr")
        assert reply[0].dev_name() == device1_fqdn
        assert reply[1].dev_name() == device2_fqdn
        assert not reply[0].has_failed()
        assert not reply[1].has_failed()
        assert reply[0].get_data().value == 1
        assert reply[1].get_data().value == 2

    # patterns are not supported via DeviceTestContext
    with pytest.raises(DevFailed):
        group_multiples_pattern = Group("add-multiple-via-pattern")
        group_multiples_pattern.add("test/device/*")


def test_read_write_attribute(context):

    _is_allowed[0] = True

    group = Group("test_group")
    group.add("test/device/1")
    group.add("test/device/2")

    seq = group.write_attribute("attr", 3)

    for ret in seq:
        assert not ret.has_failed()
        assert isinstance(ret, GroupReply)

    seq = group.read_attribute("attr")

    for ret in seq:
        assert not ret.has_failed()
        assert ret.get_data().value == 3
        # we check, that we can read value for the second time
        assert ret.get_data().value == 3
        assert isinstance(ret, GroupAttrReply)

    # now we check that we can iterate over GroupReplyList for the second time
    for ret in seq:
        assert not ret.has_failed()
        assert ret.get_data().value == 3
        assert ret.get_data().value == 3
        assert isinstance(ret, GroupAttrReply)

    dev = group.get_device(1)
    attr_info = dev.get_attribute_config("attr")
    seq = group.write_attribute(attr_info, 4)

    for ret in seq:
        assert not ret.has_failed()
        assert isinstance(ret, GroupReply)

    seq = group.read_attribute("attr")

    for ret in seq:
        assert not ret.has_failed()
        assert ret.get_data().value == 4

    _is_allowed[0] = False

    for ret in group.write_attribute("attr", 5):
        assert ret.has_failed()
        assert (
            ret.get_err_stack()[0].desc
            == "It is currently not allowed to write attribute attr. The device state is ON"
        )

    for ret in group.read_attribute("attr"):
        assert ret.has_failed()
        assert (
            ret.get_err_stack()[0].desc
            == "It is currently not allowed to read attribute attr"
        )


def test_command(context):

    _is_allowed[0] = True

    group = Group("test_group")
    group.add("test/device/1")
    group.add("test/device/2")

    seq = group.command_inout("indent", 1)

    for ret in seq:
        assert not ret.has_failed()
        assert ret.get_data() == 1
        # we check, that we can read value for the second time
        assert ret.get_data() == 1
        assert isinstance(ret, GroupCmdReply)

    # now we check that we can iterate over GroupReplyList for the second time
    for ret in seq:
        assert not ret.has_failed()
        assert ret.get_data() == 1
        assert ret.get_data() == 1
        assert isinstance(ret, GroupCmdReply)

    param = DeviceData()
    param.insert(TO_TANGO_TYPE[int], 1)

    seq = group.command_inout("indent", param)

    for ret in seq:
        assert not ret.has_failed()
        assert ret.get_data() == 1

    param_list = DeviceDataList()
    param_list.extend([param, param])

    seq = group.command_inout("indent", param_list)

    for ret in seq:
        assert not ret.has_failed()
        assert ret.get_data() == 1

    _is_allowed[0] = False

    for ret in group.command_inout("indent", 2):
        assert ret.has_failed()
        assert (
            ret.get_err_stack()[0].desc
            == "Command indent not allowed when the device is in ON state"
        )