Skip to content

Commit dcf0bf0

Browse files
committed
test: Separate bad async subclass testing in to a file
Subclass do not really need a D-Bus to run.
1 parent 44d84f0 commit dcf0bf0

File tree

4 files changed

+125
-87
lines changed

4 files changed

+125
-87
lines changed

test/common_test_util.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2020
from __future__ import annotations
2121

22-
from unittest import main
22+
from unittest import SkipTest, main
2323

2424

2525
def mem_test() -> None:
@@ -35,3 +35,23 @@ def mem_test_single(test_class: type, test_name: str) -> None:
3535
t = test_class()
3636
t.setUp()
3737
getattr(t, test_name)()
38+
39+
40+
def skip_if_no_asserts() -> None:
41+
try:
42+
assert False
43+
except AssertionError:
44+
return
45+
46+
raise SkipTest("Assertions are not enabled")
47+
48+
49+
def skip_if_no_name_validations() -> None:
50+
skip_if_no_asserts()
51+
52+
from sdbus.sd_bus_internals import is_interface_name_valid
53+
54+
try:
55+
is_interface_name_valid("org.test")
56+
except NotImplementedError:
57+
raise SkipTest("Validation functions not available")

test/test_sdbus_async.py

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
DbusDeprecatedFlag,
4141
DbusPropertyConstFlag,
4242
DbusPropertyEmitsChangeFlag,
43-
is_interface_name_valid,
4443
)
4544
from sdbus.unittest import IsolatedDbusTestCase
4645
from sdbus.utils import parse_properties_changed
@@ -754,82 +753,6 @@ def hello_world(self) -> str:
754753
test_object = EnumedInterfaceAsync()
755754
test_object.export_to_dbus(ObjectPathEnum.FOO)
756755

757-
async def test_name_validations(self) -> None:
758-
if not __debug__:
759-
raise SkipTest('Assertions are not enabled')
760-
761-
try:
762-
is_interface_name_valid('org.test')
763-
except NotImplementedError:
764-
raise SkipTest('Validation functions not available')
765-
766-
def test_bad_interface_name() -> None:
767-
class BadInterfaceName(
768-
DbusInterfaceCommonAsync,
769-
interface_name='0.test',
770-
):
771-
...
772-
773-
self.assertRaisesRegex(
774-
AssertionError,
775-
'^Invalid interface name',
776-
test_bad_interface_name,
777-
)
778-
779-
def test_bad_method_name() -> None:
780-
class BadMethodName(
781-
DbusInterfaceCommonAsync,
782-
interface_name='org.example',
783-
):
784-
@dbus_method_async(
785-
result_signature='s',
786-
method_name='🤫',
787-
)
788-
async def test(self) -> str:
789-
return 'test'
790-
791-
self.assertRaisesRegex(
792-
AssertionError,
793-
'^Invalid method name',
794-
test_bad_method_name,
795-
)
796-
797-
def test_bad_property_name() -> None:
798-
class BadPropertyName(
799-
DbusInterfaceCommonAsync,
800-
interface_name='org.example',
801-
):
802-
@dbus_property_async(
803-
property_signature='s',
804-
property_name='🤫',
805-
)
806-
def test(self) -> str:
807-
return 'test'
808-
809-
self.assertRaisesRegex(
810-
AssertionError,
811-
'^Invalid property name',
812-
test_bad_property_name,
813-
)
814-
815-
def test_bad_signal_name() -> None:
816-
class BadSignalName(
817-
DbusInterfaceCommonAsync,
818-
interface_name='org.example',
819-
):
820-
@dbus_signal_async(
821-
signal_signature='s',
822-
signal_name='🤫',
823-
)
824-
def test(self) -> str:
825-
raise NotImplementedError
826-
827-
self.assertRaisesRegex(
828-
AssertionError,
829-
'^Invalid signal name',
830-
test_bad_signal_name,
831-
)
832-
833756
async def test_properties_get_all_dict(self) -> None:
834757
test_object, test_object_connection = initialize_object()
835758

test/test_sdbus_async_bad_class.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# SPDX-License-Identifier: LGPL-2.1-or-later
2+
3+
# Copyright (C) 2023 igo95862
4+
5+
# This file is part of python-sdbus
6+
7+
# This library is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU Lesser General Public
9+
# License as published by the Free Software Foundation; either
10+
# version 2.1 of the License, or (at your option) any later version.
11+
12+
# This library is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
# Lesser General Public License for more details.
16+
17+
# You should have received a copy of the GNU Lesser General Public
18+
# License along with this library; if not, write to the Free Software
19+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
from __future__ import annotations
21+
22+
from unittest import TestCase
23+
from unittest import main as unittest_main
24+
25+
from sdbus import (
26+
DbusInterfaceCommonAsync,
27+
dbus_method_async,
28+
dbus_property_async,
29+
dbus_signal_async,
30+
)
31+
32+
from .common_test_util import skip_if_no_name_validations
33+
34+
35+
class TestBadAsyncDbusClass(TestCase):
36+
def test_name_validations(self) -> None:
37+
skip_if_no_name_validations()
38+
39+
with self.assertRaisesRegex(
40+
AssertionError,
41+
"^Invalid interface name",
42+
):
43+
44+
class BadInterfaceName(
45+
DbusInterfaceCommonAsync,
46+
interface_name="0.test",
47+
):
48+
...
49+
50+
with self.assertRaisesRegex(
51+
AssertionError,
52+
"^Invalid method name",
53+
):
54+
55+
class BadMethodName(
56+
DbusInterfaceCommonAsync,
57+
interface_name="org.example",
58+
):
59+
@dbus_method_async(
60+
result_signature="s",
61+
method_name="🤫",
62+
)
63+
async def test(self) -> str:
64+
return "test"
65+
66+
with self.assertRaisesRegex(
67+
AssertionError,
68+
"^Invalid property name",
69+
):
70+
71+
class BadPropertyName(
72+
DbusInterfaceCommonAsync,
73+
interface_name="org.example",
74+
):
75+
@dbus_property_async(
76+
property_signature="s",
77+
property_name="🤫",
78+
)
79+
def test(self) -> str:
80+
return "test"
81+
82+
with self.assertRaisesRegex(
83+
AssertionError,
84+
"^Invalid signal name",
85+
):
86+
87+
class BadSignalName(
88+
DbusInterfaceCommonAsync,
89+
interface_name="org.example",
90+
):
91+
@dbus_signal_async(
92+
signal_signature="s",
93+
signal_name="🤫",
94+
)
95+
def test(self) -> str:
96+
raise NotImplementedError
97+
98+
99+
if __name__ == "__main__":
100+
unittest_main()

test/test_sdbus_block_bad_class.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2020
from __future__ import annotations
2121

22-
from unittest import SkipTest, TestCase
22+
from unittest import TestCase
2323
from unittest import main as unittest_main
2424

2525
from sdbus import DbusInterfaceCommon, dbus_method, dbus_property
26-
from sdbus.sd_bus_internals import is_interface_name_valid
26+
27+
from .common_test_util import skip_if_no_name_validations
2728

2829

2930
class GoodDbusInterface(DbusInterfaceCommon):
@@ -75,13 +76,7 @@ def new_method(self) -> int:
7576
return 1
7677

7778
def test_bad_class_names(self) -> None:
78-
if not __debug__:
79-
raise SkipTest("Assertions are not enabled")
80-
81-
try:
82-
is_interface_name_valid("org.test")
83-
except NotImplementedError:
84-
raise SkipTest("Validation functions not available")
79+
skip_if_no_name_validations()
8580

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

0 commit comments

Comments
 (0)