Skip to content

Commit d75e0f6

Browse files
authored
Stabilize Coverage (python-telegram-bot#846)
* test_error added * ignore unstables for coverage
1 parent dbb3b16 commit d75e0f6

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

telegram/ext/updater.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def start_webhook(self,
260260
return self.update_queue
261261

262262
def _start_polling(self, poll_interval, timeout, read_latency, bootstrap_retries, clean,
263-
allowed_updates):
263+
allowed_updates): # pragma: no cover
264264
# """
265265
# Thread target of thread 'updater'. Runs in background, pulls
266266
# updates from Telegram and inserts them in the update queue of the

telegram/utils/request.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import json
3030

3131
import certifi
32+
3233
try:
3334
import telegram.vendor.ptb_urllib3.urllib3 as urllib3
3435
import telegram.vendor.ptb_urllib3.urllib3.contrib.appengine as appengine
@@ -150,7 +151,7 @@ def _parse(json_data):
150151
except ValueError:
151152
raise TelegramError('Invalid server response')
152153

153-
if not data.get('ok'):
154+
if not data.get('ok'): # pragma: no cover
154155
description = data.get('description')
155156
parameters = data.get('parameters')
156157
if parameters:

tests/test_error.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)