|
| 1 | +"""Tests for the exception hierarchy in ``dbus_fast.errors``. |
| 2 | +
|
| 3 | +The hierarchy was introduced to let callers catch every dbus-fast error with |
| 4 | +``except DBusFastError`` without giving up the historical ability to catch |
| 5 | +specific built-in types like ``ValueError`` or ``TypeError`` for individual |
| 6 | +classes (see GH #507). |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from dbus_fast import ( |
| 14 | + AuthError, |
| 15 | + DBusError, |
| 16 | + DBusFastError, |
| 17 | + InterfaceNotFoundError, |
| 18 | + InvalidAddressError, |
| 19 | + InvalidBusNameError, |
| 20 | + InvalidInterfaceNameError, |
| 21 | + InvalidIntrospectionError, |
| 22 | + InvalidMemberNameError, |
| 23 | + InvalidMessageError, |
| 24 | + InvalidObjectPathError, |
| 25 | + InvalidSignatureError, |
| 26 | + SignalDisabledError, |
| 27 | + SignatureBodyMismatchError, |
| 28 | +) |
| 29 | + |
| 30 | +_ALL_ERRORS = [ |
| 31 | + AuthError, |
| 32 | + DBusError, |
| 33 | + InterfaceNotFoundError, |
| 34 | + InvalidAddressError, |
| 35 | + InvalidBusNameError, |
| 36 | + InvalidInterfaceNameError, |
| 37 | + InvalidIntrospectionError, |
| 38 | + InvalidMemberNameError, |
| 39 | + InvalidMessageError, |
| 40 | + InvalidObjectPathError, |
| 41 | + InvalidSignatureError, |
| 42 | + SignalDisabledError, |
| 43 | + SignatureBodyMismatchError, |
| 44 | +] |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.parametrize("err_cls", _ALL_ERRORS) |
| 48 | +def test_all_errors_share_dbus_fast_base(err_cls: type[BaseException]) -> None: |
| 49 | + assert issubclass(err_cls, DBusFastError) |
| 50 | + assert issubclass(err_cls, Exception) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.parametrize( |
| 54 | + "err_cls", |
| 55 | + [ |
| 56 | + SignatureBodyMismatchError, |
| 57 | + InvalidSignatureError, |
| 58 | + InvalidAddressError, |
| 59 | + InvalidMessageError, |
| 60 | + InvalidIntrospectionError, |
| 61 | + ], |
| 62 | +) |
| 63 | +def test_value_error_subclasses_preserved(err_cls: type[BaseException]) -> None: |
| 64 | + assert issubclass(err_cls, ValueError) |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.parametrize( |
| 68 | + "err_cls", |
| 69 | + [ |
| 70 | + InvalidBusNameError, |
| 71 | + InvalidObjectPathError, |
| 72 | + InvalidInterfaceNameError, |
| 73 | + InvalidMemberNameError, |
| 74 | + ], |
| 75 | +) |
| 76 | +def test_type_error_subclasses_preserved(err_cls: type[BaseException]) -> None: |
| 77 | + assert issubclass(err_cls, TypeError) |
| 78 | + |
| 79 | + |
| 80 | +def test_value_error_subclass_mro_order() -> None: |
| 81 | + """Built-in ``ValueError`` must precede ``DBusFastError`` in the MRO. |
| 82 | +
|
| 83 | + This ordering ensures ``super().__init__`` routes through |
| 84 | + ``ValueError.__init__`` rather than skipping straight to |
| 85 | + ``Exception.__init__``, preserving the behavior of the historical |
| 86 | + single-inheritance ``ValueError`` subclasses. |
| 87 | + """ |
| 88 | + mro = InvalidSignatureError.__mro__ |
| 89 | + assert mro.index(ValueError) < mro.index(DBusFastError) |
| 90 | + |
| 91 | + |
| 92 | +def test_type_error_subclass_mro_order() -> None: |
| 93 | + """Built-in ``TypeError`` must precede ``DBusFastError`` in the MRO. |
| 94 | +
|
| 95 | + Same rationale as the ``ValueError`` MRO test: keeps ``super().__init__`` |
| 96 | + routing through ``TypeError.__init__`` to match the original |
| 97 | + single-inheritance ``TypeError`` subclasses. |
| 98 | + """ |
| 99 | + mro = InvalidBusNameError.__mro__ |
| 100 | + assert mro.index(TypeError) < mro.index(DBusFastError) |
| 101 | + |
| 102 | + |
| 103 | +def test_dbus_fast_error_catches_value_error_subclass() -> None: |
| 104 | + with pytest.raises(DBusFastError): |
| 105 | + raise SignatureBodyMismatchError("body mismatch") |
| 106 | + |
| 107 | + |
| 108 | +def test_dbus_fast_error_catches_type_error_subclass() -> None: |
| 109 | + with pytest.raises(DBusFastError): |
| 110 | + raise InvalidBusNameError("not.a.valid.bus.name!") |
| 111 | + |
| 112 | + |
| 113 | +def test_value_error_still_catches_signature_errors() -> None: |
| 114 | + with pytest.raises(ValueError): |
| 115 | + raise InvalidSignatureError("bad signature") |
| 116 | + |
| 117 | + |
| 118 | +def test_type_error_still_catches_name_errors() -> None: |
| 119 | + with pytest.raises(TypeError): |
| 120 | + raise InvalidMemberNameError("1bad") |
| 121 | + |
| 122 | + |
| 123 | +def test_invalid_bus_name_error_message_unchanged() -> None: |
| 124 | + err = InvalidBusNameError("bad") |
| 125 | + assert str(err) == "invalid bus name: bad" |
| 126 | + |
| 127 | + |
| 128 | +def test_invalid_object_path_error_message_unchanged() -> None: |
| 129 | + err = InvalidObjectPathError("nope") |
| 130 | + assert str(err) == "invalid object path: nope" |
| 131 | + |
| 132 | + |
| 133 | +def test_invalid_interface_name_error_message_unchanged() -> None: |
| 134 | + err = InvalidInterfaceNameError("bad.iface") |
| 135 | + assert str(err) == "invalid interface name: bad.iface" |
| 136 | + |
| 137 | + |
| 138 | +def test_invalid_member_name_error_message_unchanged() -> None: |
| 139 | + err = InvalidMemberNameError("1bad") |
| 140 | + assert str(err) == "invalid member name: 1bad" |
| 141 | + |
| 142 | + |
| 143 | +def test_dbus_error_inherits_dbus_fast_error() -> None: |
| 144 | + err = DBusError("org.freedesktop.DBus.Error.Failed", "boom") |
| 145 | + assert isinstance(err, DBusFastError) |
| 146 | + assert err.type == "org.freedesktop.DBus.Error.Failed" |
| 147 | + assert err.text == "boom" |
| 148 | + |
| 149 | + |
| 150 | +def test_dbus_fast_error_directly_raisable() -> None: |
| 151 | + with pytest.raises(DBusFastError): |
| 152 | + raise DBusFastError("plain base class") |
0 commit comments