| 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 | """Tests for Python 3.9 Compatibility.""" |
| 29 | |
| 30 | from __future__ import annotations |
| 31 | |
| 32 | from unittest import mock |
| 33 | |
| 34 | import pytest |
| 35 | |
| 36 | from apprise.plugins.irc import protocol |
| 37 | |
| 38 | |
| 39 | def test_compat_dataclass_no_exception(): |
| 40 | """dataclass_compat() py39 returns dataclass result on success.""" |
| 41 | # protocol.dataclass is a compat wrapper around apprise.compat._dataclass. |
| 42 | # Patch the underlying dataclass binding to simulate older Python |
| 43 | # behaviour where slots= is unsupported. |
| 44 | with mock.patch("apprise.compat._dataclass") as m: |
| 45 | sentinel = object() |
| 46 | m.return_value = sentinel |
| 47 | |
| 48 | result = protocol.dataclass(frozen=True, slots=True) |
| 49 | |
| 50 | assert result is sentinel |
| 51 | m.assert_called_once_with(frozen=True, slots=True) |
| 52 | |
| 53 | |
| 54 | def test_compat_dataclass_strips_slots_on_typeerror(): |
| 55 | """dataclass_compat() py39 strips slots= and retries after TypeError.""" |
| 56 | with mock.patch("apprise.compat._dataclass") as m: |
| 57 | sentinel = object() |
| 58 | m.side_effect = [TypeError("unsupported"), sentinel] |
| 59 | |
| 60 | result = protocol.dataclass(frozen=True, slots=True) |
| 61 | |
| 62 | assert result is sentinel |
| 63 | assert m.call_count == 2 |
| 64 | |
| 65 | # First call includes slots |
| 66 | assert m.call_args_list[0].kwargs == {"frozen": True, "slots": True} |
| 67 | |
| 68 | # Second call must omit slots |
| 69 | assert m.call_args_list[1].kwargs == {"frozen": True} |
| 70 | |
| 71 | |
| 72 | def test_compat_dataclass_reraises_when_no_slots(): |
| 73 | """dataclass_compat() re-raises TypeError when slots is not present.""" |
| 74 | with mock.patch("apprise.compat._dataclass") as m: |
| 75 | m.side_effect = TypeError("boom") |
| 76 | |
| 77 | with pytest.raises(TypeError): |
| 78 | protocol.dataclass(frozen=True) |