File: test_pprint.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 (130 lines) | stat: -rw-r--r-- 3,590 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
# SPDX-FileCopyrightText: All Contributors to the PyTango project
# SPDX-License-Identifier: LGPL-3.0-or-later

import pytest
from tango import AttributeConfig, CmdArgType, DevError, DevFailed, Group, ClientAddr
from tango._tango import GroupAttrReply, GroupCmdReply, GroupReply
from tango.pytango_pprint import _SEQUENCE_TYPES, _STRUCT_TYPES
from tango.server import Device, attribute, command
from tango.test_context import DeviceTestContext


STRUCTS_NOT_INSTANTIABLE_FROM_PYTHON = (
    GroupAttrReply,
    GroupCmdReply,
    GroupReply,
)
TESTABLE_TYPES = set(_STRUCT_TYPES).difference(STRUCTS_NOT_INSTANTIABLE_FROM_PYTHON)


class GroupTestDevice(Device):
    @attribute
    def attr(self) -> float:
        return 12.3

    @attr.setter
    def attr(self, val: float) -> None:
        pass

    @attribute
    def attr_fail(self) -> float:
        raise RuntimeError("Fail for test")

    @attr_fail.setter
    def attr_fail(self, val: float) -> None:
        raise RuntimeError("Fail for test")

    @command
    def cmd_fail(self):
        raise RuntimeError("Fail for test")


@pytest.fixture
def group():
    with DeviceTestContext(GroupTestDevice, device_name="test/device/1"):
        group_client = Group("test")
        group_client.add("test/device/1")
        yield group_client


@pytest.mark.parametrize(
    "to_string, requires_newline",
    [
        (str, True),
        (repr, False),
    ],
)
def test_pprint_structs(to_string, requires_newline):
    for struct_type in TESTABLE_TYPES:
        struct = struct_type()
        s = to_string(struct)
        assert struct_type.__name__ in s
        assert ("\n" in s) == requires_newline


@pytest.mark.parametrize(
    "to_string, requires_newline",
    [
        (str, True),
        (repr, False),
    ],
)
def test_pprint_group_structs(to_string, requires_newline, group):
    group_cmd_reply = group.command_inout("State")[0]
    group_attr_reply = group.read_attribute("attr")[0]
    group_reply = group.write_attribute("attr", 45.6)[0]
    group_cmd_failed_reply = group.command_inout("cmd_fail")[0]
    group_attr_failed_reply = group.read_attribute("attr_fail")[0]
    group_failed_reply = group.write_attribute("attr_fail", 45.6)[0]
    for struct in [
        group_cmd_reply,
        group_attr_reply,
        group_reply,
        group_cmd_failed_reply,
        group_attr_failed_reply,
        group_failed_reply,
    ]:
        s = to_string(struct)
        assert type(struct).__name__ in s
        assert ("\n" in s) == requires_newline


@pytest.mark.parametrize(
    "to_string, requires_newline",
    [
        (str, True),
        (repr, False),
    ],
)
def test_pprint_dev_failed_dev_error(to_string, requires_newline):
    dev_failed = DevFailed(DevError(), DevError())
    s = to_string(dev_failed)
    assert "DevFailed" in s
    assert "DevError" in s
    assert ("\n" in s) == requires_newline


@pytest.mark.parametrize("to_string", [str, repr])
def test_pprint_sequences(to_string):
    for seq_type in _SEQUENCE_TYPES:
        struct = seq_type()
        s = to_string(struct)
        assert "[" in s
        assert "]" in s


def test_pprint_data_type_is_cmd_arg_type():
    config = AttributeConfig()
    for dtype_value in CmdArgType.values:
        config.data_type = dtype_value
        s = str(config)
        assert f"data_type = {CmdArgType(dtype_value)!r}" in s


def test_pprint_client_addr():
    addr = ClientAddr("127.0.0.1")
    s = str(addr)
    assert s == "Client identification not available"
    r = repr(addr)
    assert "ClientAddr" in r
    assert "127.0.0.1" in r