1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2025
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
import datetime as dtm
import pickle
from collections import defaultdict
import pytest
from telegram.error import (
BadRequest,
ChatMigrated,
Conflict,
EndPointNotFound,
Forbidden,
InvalidToken,
NetworkError,
PassportDecryptionError,
RetryAfter,
TelegramError,
TimedOut,
)
from telegram.ext import InvalidCallbackData
from telegram.warnings import PTBDeprecationWarning
from tests.auxil.slots import mro_slots
class TestErrors:
def test_telegram_error(self):
with pytest.raises(TelegramError, match="^test message$"):
raise TelegramError("test message")
with pytest.raises(TelegramError, match="^Test message$"):
raise TelegramError("Error: test message")
with pytest.raises(TelegramError, match="^Test message$"):
raise TelegramError("[Error]: test message")
with pytest.raises(TelegramError, match="^Test message$"):
raise TelegramError("Bad Request: test message")
def test_unauthorized(self):
with pytest.raises(Forbidden, match="test message"):
raise Forbidden("test message")
with pytest.raises(Forbidden, match="^Test message$"):
raise Forbidden("Error: test message")
with pytest.raises(Forbidden, match="^Test message$"):
raise Forbidden("[Error]: test message")
with pytest.raises(Forbidden, match="^Test message$"):
raise Forbidden("Bad Request: test message")
def test_invalid_token(self):
with pytest.raises(InvalidToken, match="Invalid token"):
raise InvalidToken
def test_network_error(self):
with pytest.raises(NetworkError, match="test message"):
raise NetworkError("test message")
with pytest.raises(NetworkError, match="^Test message$"):
raise NetworkError("Error: test message")
with pytest.raises(NetworkError, match="^Test message$"):
raise NetworkError("[Error]: test message")
with pytest.raises(NetworkError, match="^Test message$"):
raise NetworkError("Bad Request: test message")
def test_bad_request(self):
with pytest.raises(BadRequest, match="test message"):
raise BadRequest("test message")
with pytest.raises(BadRequest, match="^Test message$"):
raise BadRequest("Error: test message")
with pytest.raises(BadRequest, match="^Test message$"):
raise BadRequest("[Error]: test message")
with pytest.raises(BadRequest, match="^Test message$"):
raise BadRequest("Bad Request: test message")
def test_timed_out(self):
with pytest.raises(TimedOut, match="^Timed out$"):
raise TimedOut
def test_chat_migrated(self):
with pytest.raises(ChatMigrated, match="New chat id: 1234") as e:
raise ChatMigrated(1234)
assert e.value.new_chat_id == 1234
@pytest.mark.parametrize("retry_after", [12, dtm.timedelta(seconds=12)])
def test_retry_after(self, PTB_TIMEDELTA, retry_after):
if PTB_TIMEDELTA:
with pytest.raises(RetryAfter, match="Flood control exceeded. Retry in 0:00:12"):
raise (exception := RetryAfter(retry_after))
assert type(exception.retry_after) is dtm.timedelta
else:
with pytest.raises(RetryAfter, match="Flood control exceeded. Retry in 12 seconds"):
raise (exception := RetryAfter(retry_after))
assert type(exception.retry_after) is int
def test_retry_after_int_deprecated(self, PTB_TIMEDELTA, recwarn):
retry_after = RetryAfter(12).retry_after
if PTB_TIMEDELTA:
assert len(recwarn) == 0
assert type(retry_after) is dtm.timedelta
else:
assert len(recwarn) == 1
assert "`retry_after` will be of type `datetime.timedelta`" in str(recwarn[0].message)
assert recwarn[0].category is PTBDeprecationWarning
assert type(retry_after) is int
def test_conflict(self):
with pytest.raises(Conflict, match="Something something."):
raise Conflict("Something something.")
@pytest.mark.parametrize(
("exception", "attributes"),
[
(TelegramError("test message"), ["message"]),
(Forbidden("test message"), ["message"]),
(InvalidToken(), ["message"]),
(NetworkError("test message"), ["message"]),
(BadRequest("test message"), ["message"]),
(TimedOut(), ["message"]),
(ChatMigrated(1234), ["message", "new_chat_id"]),
(RetryAfter(12), ["message", "retry_after"]),
(RetryAfter(dtm.timedelta(seconds=12)), ["message", "retry_after"]),
(Conflict("test message"), ["message"]),
(PassportDecryptionError("test message"), ["message"]),
(InvalidCallbackData("test data"), ["callback_data"]),
(EndPointNotFound("endPoint"), ["message"]),
],
)
def test_errors_pickling(self, exception, attributes):
pickled = pickle.dumps(exception)
unpickled = pickle.loads(pickled)
assert type(unpickled) is type(exception)
assert str(unpickled) == str(exception)
for attribute in attributes:
assert getattr(unpickled, attribute) == getattr(exception, attribute)
@pytest.mark.parametrize(
"inst",
[
(TelegramError("test message")),
(Forbidden("test message")),
(InvalidToken()),
(NetworkError("test message")),
(BadRequest("test message")),
(TimedOut()),
(ChatMigrated(1234)),
(RetryAfter(dtm.timedelta(seconds=12))),
(Conflict("test message")),
(PassportDecryptionError("test message")),
(InvalidCallbackData("test data")),
(EndPointNotFound("test message")),
],
)
def test_slot_behaviour(self, inst):
for attr in inst.__slots__:
assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'"
assert len(mro_slots(inst)) == len(set(mro_slots(inst))), "duplicate slot"
def test_coverage(self):
"""
This test is only here to make sure that new errors will override __reduce__ and set
__slots__ properly.
Add the new error class to the below covered_subclasses dict, if it's covered in the above
test_errors_pickling and test_slots_behavior tests.
"""
def make_assertion(cls):
assert set(cls.__subclasses__()) == covered_subclasses[cls]
for subcls in cls.__subclasses__():
make_assertion(subcls)
covered_subclasses = defaultdict(set)
covered_subclasses.update(
{
TelegramError: {
Forbidden,
InvalidToken,
NetworkError,
ChatMigrated,
RetryAfter,
Conflict,
PassportDecryptionError,
InvalidCallbackData,
EndPointNotFound,
},
NetworkError: {BadRequest, TimedOut},
}
)
make_assertion(TelegramError)
def test_string_representations(self, PTB_TIMEDELTA):
"""We just randomly test a few of the subclasses - should suffice"""
e = TelegramError("This is a message")
assert repr(e) == "TelegramError('This is a message')"
assert str(e) == "This is a message"
e = RetryAfter(dtm.timedelta(seconds=42))
if PTB_TIMEDELTA:
assert repr(e) == "RetryAfter('Flood control exceeded. Retry in 0:00:42')"
assert str(e) == "Flood control exceeded. Retry in 0:00:42"
else:
assert repr(e) == "RetryAfter('Flood control exceeded. Retry in 42 seconds')"
assert str(e) == "Flood control exceeded. Retry in 42 seconds"
e = BadRequest("This is a message")
assert repr(e) == "BadRequest('This is a message')"
assert str(e) == "This is a message"
|