|
1 | 1 | import pytest |
2 | | -from pytest_mock import MockerFixture |
3 | | -from tango._tango import AttrWriteType, CmdArgType |
4 | | - |
5 | | -from fastcs.backends.tango.dsr import _collect_dev_attributes, _collect_dev_commands |
6 | | - |
7 | | - |
8 | | -def test_collect_attributes(mapping): |
9 | | - attributes = _collect_dev_attributes(mapping) |
10 | | - |
11 | | - # Check that attributes are created and of expected type |
12 | | - assert list(attributes.keys()) == [ |
13 | | - "BigEnum", |
14 | | - "ReadBool", |
15 | | - "ReadInt", |
16 | | - "ReadWriteFloat", |
17 | | - "ReadWriteInt", |
18 | | - "StringEnum", |
19 | | - "WriteBool", |
20 | | - ] |
21 | | - assert attributes["ReadInt"].attr_write == AttrWriteType.READ |
22 | | - assert attributes["ReadInt"].attr_type == CmdArgType.DevLong64 |
23 | | - assert attributes["StringEnum"].attr_write == AttrWriteType.READ_WRITE |
24 | | - assert attributes["StringEnum"].attr_type == CmdArgType.DevString |
25 | | - assert attributes["ReadWriteFloat"].attr_write == AttrWriteType.READ_WRITE |
26 | | - assert attributes["ReadWriteFloat"].attr_type == CmdArgType.DevDouble |
27 | | - assert attributes["WriteBool"].attr_write == AttrWriteType.WRITE |
28 | | - assert attributes["WriteBool"].attr_type == CmdArgType.DevBoolean |
29 | | - |
30 | | - |
31 | | -@pytest.mark.asyncio |
32 | | -async def test_collect_commands(mapping, mocker: MockerFixture): |
33 | | - commands = _collect_dev_commands(mapping) |
34 | | - |
35 | | - # Check that command is created and it can be called |
36 | | - assert list(commands.keys()) == ["Go"] |
37 | | - await commands["Go"](mocker.MagicMock()) |
| 2 | +from tango import DevState |
| 3 | +from tango.test_context import DeviceTestContext |
| 4 | + |
| 5 | +from fastcs.backends.tango.backend import TangoBackend |
| 6 | + |
| 7 | + |
| 8 | +class TestTangoDevice: |
| 9 | + @pytest.fixture(scope="class") |
| 10 | + def tango_context(self, assertable_controller): |
| 11 | + # https://tango-controls.readthedocs.io/projects/pytango/en/v9.5.1/testing/test_context.html |
| 12 | + device = TangoBackend(assertable_controller)._dsr._device |
| 13 | + with DeviceTestContext(device) as proxy: |
| 14 | + yield proxy |
| 15 | + |
| 16 | + def test_list_attributes(self, tango_context): |
| 17 | + assert list(tango_context.get_attribute_list()) == [ |
| 18 | + "BigEnum", |
| 19 | + "ReadBool", |
| 20 | + "ReadInt", |
| 21 | + "ReadWriteFloat", |
| 22 | + "ReadWriteInt", |
| 23 | + "StringEnum", |
| 24 | + "WriteBool", |
| 25 | + "SubController01_ReadInt", |
| 26 | + "SubController02_ReadInt", |
| 27 | + "State", |
| 28 | + "Status", |
| 29 | + ] |
| 30 | + |
| 31 | + def test_list_commands(self, tango_context): |
| 32 | + assert list(tango_context.get_command_list()) == [ |
| 33 | + "Go", |
| 34 | + "Init", |
| 35 | + "State", |
| 36 | + "Status", |
| 37 | + ] |
| 38 | + |
| 39 | + def test_state(self, tango_context): |
| 40 | + assert tango_context.command_inout("State") == DevState.ON |
| 41 | + |
| 42 | + def test_status(self, tango_context): |
| 43 | + expect = "The device is in ON state." |
| 44 | + assert tango_context.command_inout("Status") == expect |
| 45 | + |
| 46 | + def test_read_int(self, assertable_controller, tango_context): |
| 47 | + expect = 0 |
| 48 | + with assertable_controller.assert_read_here(["read_int"]): |
| 49 | + result = tango_context.read_attribute("ReadInt").value |
| 50 | + assert result == expect |
| 51 | + |
| 52 | + def test_read_write_int(self, assertable_controller, tango_context): |
| 53 | + expect = 0 |
| 54 | + with assertable_controller.assert_read_here(["read_write_int"]): |
| 55 | + result = tango_context.read_attribute("ReadWriteInt").value |
| 56 | + assert result == expect |
| 57 | + new = 9 |
| 58 | + with assertable_controller.assert_write_here(["read_write_int"]): |
| 59 | + tango_context.write_attribute("ReadWriteInt", new) |
| 60 | + assert tango_context.read_attribute("ReadWriteInt").value == new |
| 61 | + |
| 62 | + def test_read_write_float(self, assertable_controller, tango_context): |
| 63 | + expect = 0.0 |
| 64 | + with assertable_controller.assert_read_here(["read_write_float"]): |
| 65 | + result = tango_context.read_attribute("ReadWriteFloat").value |
| 66 | + assert result == expect |
| 67 | + new = 0.5 |
| 68 | + with assertable_controller.assert_write_here(["read_write_float"]): |
| 69 | + tango_context.write_attribute("ReadWriteFloat", new) |
| 70 | + assert tango_context.read_attribute("ReadWriteFloat").value == new |
| 71 | + |
| 72 | + def test_read_bool(self, assertable_controller, tango_context): |
| 73 | + expect = False |
| 74 | + with assertable_controller.assert_read_here(["read_bool"]): |
| 75 | + result = tango_context.read_attribute("ReadBool").value |
| 76 | + assert result == expect |
| 77 | + |
| 78 | + def test_write_bool(self, assertable_controller, tango_context): |
| 79 | + with assertable_controller.assert_write_here(["write_bool"]): |
| 80 | + tango_context.write_attribute("WriteBool", True) |
| 81 | + |
| 82 | + def test_string_enum(self, assertable_controller, tango_context): |
| 83 | + expect = "" |
| 84 | + with assertable_controller.assert_read_here(["string_enum"]): |
| 85 | + result = tango_context.read_attribute("StringEnum").value |
| 86 | + assert result == expect |
| 87 | + new = "new" |
| 88 | + with assertable_controller.assert_write_here(["string_enum"]): |
| 89 | + tango_context.write_attribute("StringEnum", new) |
| 90 | + assert tango_context.read_attribute("StringEnum").value == new |
| 91 | + |
| 92 | + def test_big_enum(self, assertable_controller, tango_context): |
| 93 | + expect = 0 |
| 94 | + with assertable_controller.assert_read_here(["big_enum"]): |
| 95 | + result = tango_context.read_attribute("BigEnum").value |
| 96 | + assert result == expect |
| 97 | + |
| 98 | + def test_go(self, assertable_controller, tango_context): |
| 99 | + with assertable_controller.assert_execute_here(["go"]): |
| 100 | + tango_context.command_inout("Go") |
| 101 | + |
| 102 | + def test_read_child1(self, assertable_controller, tango_context): |
| 103 | + expect = 0 |
| 104 | + with assertable_controller.assert_read_here(["SubController01", "read_int"]): |
| 105 | + result = tango_context.read_attribute("SubController01_ReadInt").value |
| 106 | + assert result == expect |
| 107 | + |
| 108 | + def test_read_child2(self, assertable_controller, tango_context): |
| 109 | + expect = 0 |
| 110 | + with assertable_controller.assert_read_here(["SubController02", "read_int"]): |
| 111 | + result = tango_context.read_attribute("SubController02_ReadInt").value |
| 112 | + assert result == expect |
0 commit comments