Skip to content

Commit

Permalink
test: Separate bad async subclass testing in to a file
Browse files Browse the repository at this point in the history
Subclass do not really need a D-Bus to run.
  • Loading branch information
igo95862 committed Nov 5, 2023
1 parent 44d84f0 commit dcf0bf0
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 87 deletions.
22 changes: 21 additions & 1 deletion test/common_test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
from __future__ import annotations

from unittest import main
from unittest import SkipTest, main


def mem_test() -> None:
Expand All @@ -35,3 +35,23 @@ def mem_test_single(test_class: type, test_name: str) -> None:
t = test_class()
t.setUp()
getattr(t, test_name)()


def skip_if_no_asserts() -> None:
try:
assert False
except AssertionError:
return

raise SkipTest("Assertions are not enabled")


def skip_if_no_name_validations() -> None:
skip_if_no_asserts()

from sdbus.sd_bus_internals import is_interface_name_valid

try:
is_interface_name_valid("org.test")
except NotImplementedError:
raise SkipTest("Validation functions not available")
77 changes: 0 additions & 77 deletions test/test_sdbus_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
DbusDeprecatedFlag,
DbusPropertyConstFlag,
DbusPropertyEmitsChangeFlag,
is_interface_name_valid,
)
from sdbus.unittest import IsolatedDbusTestCase
from sdbus.utils import parse_properties_changed
Expand Down Expand Up @@ -754,82 +753,6 @@ def hello_world(self) -> str:
test_object = EnumedInterfaceAsync()
test_object.export_to_dbus(ObjectPathEnum.FOO)

async def test_name_validations(self) -> None:
if not __debug__:
raise SkipTest('Assertions are not enabled')

try:
is_interface_name_valid('org.test')
except NotImplementedError:
raise SkipTest('Validation functions not available')

def test_bad_interface_name() -> None:
class BadInterfaceName(
DbusInterfaceCommonAsync,
interface_name='0.test',
):
...

self.assertRaisesRegex(
AssertionError,
'^Invalid interface name',
test_bad_interface_name,
)

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

self.assertRaisesRegex(
AssertionError,
'^Invalid method name',
test_bad_method_name,
)

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

self.assertRaisesRegex(
AssertionError,
'^Invalid property name',
test_bad_property_name,
)

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

self.assertRaisesRegex(
AssertionError,
'^Invalid signal name',
test_bad_signal_name,
)

async def test_properties_get_all_dict(self) -> None:
test_object, test_object_connection = initialize_object()

Expand Down
100 changes: 100 additions & 0 deletions test/test_sdbus_async_bad_class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# 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 unittest import TestCase
from unittest import main as unittest_main

from sdbus import (
DbusInterfaceCommonAsync,
dbus_method_async,
dbus_property_async,
dbus_signal_async,
)

from .common_test_util import skip_if_no_name_validations


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


if __name__ == "__main__":
unittest_main()
13 changes: 4 additions & 9 deletions test/test_sdbus_block_bad_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
from __future__ import annotations

from unittest import SkipTest, TestCase
from unittest import TestCase
from unittest import main as unittest_main

from sdbus import DbusInterfaceCommon, dbus_method, dbus_property
from sdbus.sd_bus_internals import is_interface_name_valid

from .common_test_util import skip_if_no_name_validations


class GoodDbusInterface(DbusInterfaceCommon):
Expand Down Expand Up @@ -75,13 +76,7 @@ def new_method(self) -> int:
return 1

def test_bad_class_names(self) -> None:
if not __debug__:
raise SkipTest("Assertions are not enabled")

try:
is_interface_name_valid("org.test")
except NotImplementedError:
raise SkipTest("Validation functions not available")
skip_if_no_name_validations()

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

Expand Down

0 comments on commit dcf0bf0

Please sign in to comment.