|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# A library that provides a Python interface to the Telegram Bot API |
| 4 | +# Copyright (C) 2015-2017 |
| 5 | +# Leandro Toledo de Souza <devs@python-telegram-bot.org> |
| 6 | +# |
| 7 | +# This program is free software: you can redistribute it and/or modify |
| 8 | +# it under the terms of the GNU Lesser Public License as published by |
| 9 | +# the Free Software Foundation, either version 3 of the License, or |
| 10 | +# (at your option) any later version. |
| 11 | +# |
| 12 | +# This program is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +# GNU Lesser Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU Lesser Public License |
| 18 | +# along with this program. If not, see [http://www.gnu.org/licenses/]. |
| 19 | +import pytest |
| 20 | + |
| 21 | +from telegram import TelegramError |
| 22 | +from telegram.error import Unauthorized, InvalidToken, NetworkError, BadRequest, TimedOut, \ |
| 23 | + ChatMigrated, RetryAfter |
| 24 | + |
| 25 | + |
| 26 | +class TestErrors(object): |
| 27 | + def test_telegram_error(self): |
| 28 | + with pytest.raises(TelegramError, match="^test message$"): |
| 29 | + raise TelegramError("test message") |
| 30 | + with pytest.raises(TelegramError, match="^Test message$"): |
| 31 | + raise TelegramError("Error: test message") |
| 32 | + with pytest.raises(TelegramError, match="^Test message$"): |
| 33 | + raise TelegramError("[Error]: test message") |
| 34 | + with pytest.raises(TelegramError, match="^Test message$"): |
| 35 | + raise TelegramError("Bad Request: test message") |
| 36 | + |
| 37 | + def test_unauthorized(self): |
| 38 | + with pytest.raises(Unauthorized, match="test message"): |
| 39 | + raise Unauthorized("test message") |
| 40 | + with pytest.raises(Unauthorized, match="^Test message$"): |
| 41 | + raise Unauthorized("Error: test message") |
| 42 | + with pytest.raises(Unauthorized, match="^Test message$"): |
| 43 | + raise Unauthorized("[Error]: test message") |
| 44 | + with pytest.raises(Unauthorized, match="^Test message$"): |
| 45 | + raise Unauthorized("Bad Request: test message") |
| 46 | + |
| 47 | + def test_invalid_token(self): |
| 48 | + with pytest.raises(InvalidToken, match="Invalid token"): |
| 49 | + raise InvalidToken |
| 50 | + |
| 51 | + def test_network_error(self): |
| 52 | + with pytest.raises(NetworkError, match="test message"): |
| 53 | + raise NetworkError("test message") |
| 54 | + with pytest.raises(NetworkError, match="^Test message$"): |
| 55 | + raise NetworkError("Error: test message") |
| 56 | + with pytest.raises(NetworkError, match="^Test message$"): |
| 57 | + raise NetworkError("[Error]: test message") |
| 58 | + with pytest.raises(NetworkError, match="^Test message$"): |
| 59 | + raise NetworkError("Bad Request: test message") |
| 60 | + |
| 61 | + def test_bad_request(self): |
| 62 | + with pytest.raises(BadRequest, match="test message"): |
| 63 | + raise BadRequest("test message") |
| 64 | + with pytest.raises(BadRequest, match="^Test message$"): |
| 65 | + raise BadRequest("Error: test message") |
| 66 | + with pytest.raises(BadRequest, match="^Test message$"): |
| 67 | + raise BadRequest("[Error]: test message") |
| 68 | + with pytest.raises(BadRequest, match="^Test message$"): |
| 69 | + raise BadRequest("Bad Request: test message") |
| 70 | + |
| 71 | + def test_timed_out(self): |
| 72 | + with pytest.raises(TimedOut, match="^Timed out$"): |
| 73 | + raise TimedOut |
| 74 | + |
| 75 | + def test_chat_migrated(self): |
| 76 | + with pytest.raises(ChatMigrated, match="Group migrated to supergroup. New chat id: 1234"): |
| 77 | + raise ChatMigrated(1234) |
| 78 | + try: |
| 79 | + raise ChatMigrated(1234) |
| 80 | + except ChatMigrated as e: |
| 81 | + assert e.new_chat_id == 1234 |
| 82 | + |
| 83 | + def test_retry_after(self): |
| 84 | + with pytest.raises(RetryAfter, match="Flood control exceeded. Retry in 12 seconds"): |
| 85 | + raise RetryAfter(12) |
0 commit comments