| 1 |
# BSD 2-Clause License |
| 2 |
# |
| 3 |
# Apprise - Push Notification Library. |
| 4 |
# Copyright (c) 2026, Chris Caron <[email protected]> |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted provided that the following conditions are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright notice, |
| 10 |
# this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 |
# this list of conditions and the following disclaimer in the documentation |
| 14 |
# and/or other materials provided with the distribution. |
| 15 |
# |
| 16 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
# POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
import errno |
| 29 |
import logging |
| 30 |
|
| 31 |
import pytest |
| 32 |
|
| 33 |
from apprise.exception import ( |
| 34 |
AppriseDiskIOError, |
| 35 |
AppriseException, |
| 36 |
AppriseFileNotFound, |
| 37 |
AppriseImproperlyConfigured, |
| 38 |
AppriseInvalidData, |
| 39 |
ApprisePluginException, |
| 40 |
) |
| 41 |
|
| 42 |
# Disable logging for a cleaner testing output |
| 43 |
logging.disable(logging.CRITICAL) |
| 44 |
|
| 45 |
|
| 46 |
def test_appriseexception_base_behavior(): |
| 47 |
"""AppriseException stores the message and defaults error_code to 0.""" |
| 48 |
exc = AppriseException("something went wrong") |
| 49 |
assert str(exc) == "something went wrong" |
| 50 |
assert exc.error_code == 0 |
| 51 |
|
| 52 |
exc = AppriseException("with a code", error_code=42) |
| 53 |
assert exc.error_code == 42 |
| 54 |
|
| 55 |
|
| 56 |
def test_appriseplugin_exception_defaults(): |
| 57 |
"""ApprisePluginException defaults its error_code to 600.""" |
| 58 |
exc = ApprisePluginException("plugin failure") |
| 59 |
assert isinstance(exc, AppriseException) |
| 60 |
assert exc.error_code == 600 |
| 61 |
|
| 62 |
exc = ApprisePluginException("plugin failure", error_code=503) |
| 63 |
assert exc.error_code == 503 |
| 64 |
|
| 65 |
|
| 66 |
def test_appriseimproperlyconfigured_is_apprise_exception(): |
| 67 |
"""AppriseImproperlyConfigured is a first-class AppriseException.""" |
| 68 |
exc = AppriseImproperlyConfigured("bad config") |
| 69 |
assert isinstance(exc, AppriseException) |
| 70 |
assert exc.error_code == errno.EINVAL |
| 71 |
|
| 72 |
|
| 73 |
def test_appriseimproperlyconfigured_builtin_compatibility(): |
| 74 |
"""The new exception remains compatible with historical built-ins.""" |
| 75 |
exc = AppriseImproperlyConfigured("bad config") |
| 76 |
assert isinstance(exc, TypeError) |
| 77 |
assert isinstance(exc, ValueError) |
| 78 |
assert isinstance(exc, AttributeError) |
| 79 |
|
| 80 |
for builtin in (TypeError, ValueError, AttributeError): |
| 81 |
with pytest.raises(builtin): |
| 82 |
raise AppriseImproperlyConfigured("bad config") |
| 83 |
|
| 84 |
with pytest.raises(AppriseException): |
| 85 |
raise AppriseImproperlyConfigured("bad config") |
| 86 |
|
| 87 |
|
| 88 |
def test_appriseinvaliddata_defaults(): |
| 89 |
"""AppriseInvalidData defaults its error_code to EINVAL.""" |
| 90 |
exc = AppriseInvalidData("bad data") |
| 91 |
assert isinstance(exc, AppriseException) |
| 92 |
assert exc.error_code == errno.EINVAL |
| 93 |
|
| 94 |
|
| 95 |
def test_apprisediskioerror_is_oserror(): |
| 96 |
"""AppriseDiskIOError supplies standard and Apprise error details.""" |
| 97 |
exc = AppriseDiskIOError("disk failure") |
| 98 |
assert isinstance(exc, AppriseException) |
| 99 |
assert isinstance(exc, OSError) |
| 100 |
assert exc.error_code == errno.EIO |
| 101 |
assert exc.errno == errno.EIO |
| 102 |
assert exc.strerror == "disk failure" |
| 103 |
assert str(exc) == "disk failure" |
| 104 |
|
| 105 |
with pytest.raises(OSError): |
| 106 |
raise AppriseDiskIOError("disk failure") |
| 107 |
|
| 108 |
custom = AppriseDiskIOError("disk full", error_code=errno.ENOSPC) |
| 109 |
assert custom.error_code == errno.ENOSPC |
| 110 |
assert custom.errno == errno.ENOSPC |
| 111 |
assert custom.strerror == "disk full" |
| 112 |
|
| 113 |
|
| 114 |
def test_apprisefilenotfound_is_filenotfounderror(): |
| 115 |
"""AppriseFileNotFound remains catchable as FileNotFoundError/OSError.""" |
| 116 |
exc = AppriseFileNotFound("missing attachment") |
| 117 |
assert isinstance(exc, AppriseDiskIOError) |
| 118 |
assert isinstance(exc, FileNotFoundError) |
| 119 |
assert isinstance(exc, OSError) |
| 120 |
assert exc.error_code == errno.ENOENT |
| 121 |
assert exc.errno == errno.ENOENT |
| 122 |
assert exc.strerror == "missing attachment" |
| 123 |
assert str(exc) == "missing attachment" |