File: test_sdbus_async_bad_class.py

package info (click to toggle)
python-sdbus 0.14.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 996 kB
  • sloc: python: 7,911; ansic: 2,507; makefile: 9; sh: 4
file content (250 lines) | stat: -rw-r--r-- 7,435 bytes parent folder | download | duplicates (4)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# SPDX-License-Identifier: LGPL-2.1-or-later

# Copyright (C) 2023 igo95862

# This file is part of python-sdbus

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
from __future__ import annotations

from gc import collect
from unittest import TestCase
from unittest import main as unittest_main

from sdbus.dbus_common_funcs import PROPERTY_FLAGS_MASK, count_bits

from sdbus import (
    DbusDeprecatedFlag,
    DbusInterfaceCommonAsync,
    DbusPropertyConstFlag,
    DbusPropertyEmitsChangeFlag,
    dbus_method_async,
    dbus_method_async_override,
    dbus_property_async,
    dbus_signal_async,
)

from .common_test_util import skip_if_no_asserts, skip_if_no_name_validations


class TestInterface(
    DbusInterfaceCommonAsync,
    interface_name="org.example.good",
):
    @dbus_method_async(result_signature="i")
    async def test_int(self) -> int:
        return 1


class TestBadAsyncDbusClass(TestCase):
    def test_name_validations(self) -> None:
        skip_if_no_name_validations()

        with self.assertRaisesRegex(
            AssertionError,
            "^Invalid interface name",
        ):

            class BadInterfaceName(
                DbusInterfaceCommonAsync,
                interface_name="0.test",
            ):
                ...

        with self.assertRaisesRegex(
            AssertionError,
            "^Invalid method name",
        ):

            class BadMethodName(
                DbusInterfaceCommonAsync,
                interface_name="org.example",
            ):
                @dbus_method_async(
                    result_signature="s",
                    method_name="🤫",
                )
                async def test(self) -> str:
                    return "test"

        with self.assertRaisesRegex(
            AssertionError,
            "^Invalid property name",
        ):

            class BadPropertyName(
                DbusInterfaceCommonAsync,
                interface_name="org.example",
            ):
                @dbus_property_async(
                    property_signature="s",
                    property_name="🤫",
                )
                def test(self) -> str:
                    return "test"

        with self.assertRaisesRegex(
            AssertionError,
            "^Invalid signal name",
        ):

            class BadSignalName(
                DbusInterfaceCommonAsync,
                interface_name="org.example",
            ):
                @dbus_signal_async(
                    signal_signature="s",
                    signal_name="🤫",
                )
                def test(self) -> str:
                    raise NotImplementedError

    def test_property_flags(self) -> None:
        self.assertEqual(0, PROPERTY_FLAGS_MASK & DbusDeprecatedFlag)
        self.assertEqual(
            1,
            count_bits(
                PROPERTY_FLAGS_MASK
                & (DbusDeprecatedFlag | DbusPropertyEmitsChangeFlag)
            ),
        )
        self.assertEqual(
            2,
            count_bits(
                PROPERTY_FLAGS_MASK
                & (
                    DbusDeprecatedFlag
                    | DbusPropertyConstFlag
                    | DbusPropertyEmitsChangeFlag
                )
            ),
        )

        with self.subTest("Test incorrect flags"), self.assertRaisesRegex(
            AssertionError,
            "^Incorrect number of Property flags",
        ):
            skip_if_no_asserts()

            class InvalidPropertiesFlags(
                DbusInterfaceCommonAsync, interface_name="org.test.invalidprop"
            ):
                @dbus_property_async(
                    "s",
                    flags=DbusPropertyConstFlag | DbusPropertyEmitsChangeFlag,
                )
                def test_constant(self) -> str:
                    return "a"

        with self.subTest("Valid properties flags"):

            class ValidPropertiesFlags(
                DbusInterfaceCommonAsync, interface_name="org.test.validprop"
            ):
                @dbus_property_async(
                    "s",
                    flags=DbusDeprecatedFlag | DbusPropertyEmitsChangeFlag,
                )
                def test_constant(self) -> str:
                    return "a"

    def test_bad_subclass(self) -> None:
        with self.assertRaises(ValueError):

            class TestInheritence(TestInterface):
                async def test_int(self) -> int:
                    return 2

        with self.assertRaises(ValueError):

            class TestInheritence2(TestInterface):
                @dbus_method_async_override()
                async def test_unrelated(self) -> int:
                    return 2

    def test_dbus_elements_without_interface_name(self) -> None:
        with self.assertRaisesRegex(TypeError, "without interface name"):

            class NoInterfaceName(DbusInterfaceCommonAsync):
                @dbus_method_async()
                async def example(self) -> None:
                    ...

    def test_dbus_elements_without_interface_name_subclass(self) -> None:
        with self.assertRaisesRegex(TypeError, "without interface name"):

            class NoInterfaceName(TestInterface):
                @dbus_method_async()
                async def example(self) -> None:
                    ...

    def test_shared_parent_class(self) -> None:
        class One(TestInterface):
            ...

        class Two(TestInterface):
            ...

        class Shared(One, Two):
            ...

    def test_combined_collision(self) -> None:

        class One(
            DbusInterfaceCommonAsync,
            interface_name="org.example.foo",
        ):
            @dbus_method_async()
            async def example(self) -> None:
                ...

        class Two(
            DbusInterfaceCommonAsync,
            interface_name="org.example.bar",
        ):
            @dbus_method_async()
            async def example(self) -> None:
                ...

        with self.assertRaisesRegex(ValueError, "collision"):
            class Combined(One, Two):
                ...

    def test_class_cleanup(self) -> None:
        class One(
            DbusInterfaceCommonAsync,
            interface_name="org.example.foo1",
        ):
            ...

        with self.assertRaises(ValueError):
            class Two(
                DbusInterfaceCommonAsync,
                interface_name="org.example.foo1",
            ):
                ...

        del One
        collect()  # Let weak refs be processed

        class After(
            DbusInterfaceCommonAsync,
            interface_name="org.example.foo1",
        ):
            ...


if __name__ == "__main__":
    unittest_main()