mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-03-17 04:39:55 +01:00
Stabilize CI (#1931)
This commit is contained in:
parent
2bd3f2a65a
commit
76567ba635
7 changed files with 85 additions and 12 deletions
|
@ -21,6 +21,9 @@ import json
|
|||
import base64
|
||||
import os
|
||||
import random
|
||||
import pytest
|
||||
from telegram.utils.request import Request
|
||||
from telegram.error import RetryAfter
|
||||
|
||||
# Provide some public fallbacks so it's easy for contributors to run tests on their local machine
|
||||
# These bots are only able to talk in our test chats, so they are quite useless for other
|
||||
|
@ -73,3 +76,17 @@ def get(name, fallback):
|
|||
|
||||
def get_bot():
|
||||
return {k: get(k, v) for k, v in random.choice(FALLBACKS).items()}
|
||||
|
||||
|
||||
# Patch request to xfail on flood control errors
|
||||
original_request_wrapper = Request._request_wrapper
|
||||
|
||||
|
||||
def patient_request_wrapper(*args, **kwargs):
|
||||
try:
|
||||
return original_request_wrapper(*args, **kwargs)
|
||||
except RetryAfter as e:
|
||||
pytest.xfail('Not waiting for flood control: {}'.format(e))
|
||||
|
||||
|
||||
Request._request_wrapper = patient_request_wrapper
|
||||
|
|
|
@ -32,6 +32,7 @@ from telegram import (Bot, Message, User, Chat, MessageEntity, Update,
|
|||
ChosenInlineResult)
|
||||
from telegram.ext import Dispatcher, JobQueue, Updater, BaseFilter, Defaults
|
||||
from telegram.utils.helpers import _UtcOffsetTimezone
|
||||
from telegram.error import BadRequest
|
||||
from tests.bots import get_bot
|
||||
|
||||
GITHUB_ACTION = os.getenv('GITHUB_ACTION', False)
|
||||
|
@ -281,3 +282,26 @@ def utc_offset(request):
|
|||
@pytest.fixture()
|
||||
def timezone(utc_offset):
|
||||
return _UtcOffsetTimezone(utc_offset)
|
||||
|
||||
|
||||
def expect_bad_request(func, message, reason):
|
||||
"""
|
||||
Wrapper for testing bot functions expected to result in an :class:`telegram.error.BadRequest`.
|
||||
Makes it XFAIL, if the specified error message is present.
|
||||
|
||||
Args:
|
||||
func: The callable to be executed.
|
||||
message: The expected message of the bad request error. If another message is present,
|
||||
the error will be reraised.
|
||||
reason: Explanation for the XFAIL.
|
||||
|
||||
Returns:
|
||||
On success, returns the return value of :attr:`func`
|
||||
"""
|
||||
try:
|
||||
return func()
|
||||
except BadRequest as e:
|
||||
if message in str(e):
|
||||
pytest.xfail('{}. {}'.format(reason, e))
|
||||
else:
|
||||
raise e
|
||||
|
|
|
@ -30,6 +30,7 @@ from telegram import (Bot, Update, ChatAction, TelegramError, User, InlineKeyboa
|
|||
InlineQueryResultDocument)
|
||||
from telegram.error import BadRequest, InvalidToken, NetworkError, RetryAfter
|
||||
from telegram.utils.helpers import from_timestamp, escape_markdown
|
||||
from tests.conftest import expect_bad_request
|
||||
|
||||
BASE_TIME = time.time()
|
||||
HIGHSCORE_DELTA = 1450000000
|
||||
|
@ -816,13 +817,19 @@ class TestBot(object):
|
|||
@flaky(3, 1)
|
||||
@pytest.mark.timeout(10)
|
||||
def test_set_chat_photo(self, bot, channel_id):
|
||||
with open('tests/data/telegram_test_channel.jpg', 'rb') as f:
|
||||
def func():
|
||||
assert bot.set_chat_photo(channel_id, f)
|
||||
|
||||
with open('tests/data/telegram_test_channel.jpg', 'rb') as f:
|
||||
expect_bad_request(func, 'Type of file mismatch', 'Telegram did not accept the file.')
|
||||
|
||||
@flaky(3, 1)
|
||||
@pytest.mark.timeout(10)
|
||||
def test_delete_chat_photo(self, bot, channel_id):
|
||||
assert bot.delete_chat_photo(channel_id)
|
||||
def func():
|
||||
assert bot.delete_chat_photo(channel_id)
|
||||
|
||||
expect_bad_request(func, 'Chat_not_modified', 'Chat photo was not set.')
|
||||
|
||||
@flaky(3, 1)
|
||||
@pytest.mark.timeout(10)
|
||||
|
|
|
@ -22,6 +22,7 @@ import pytest
|
|||
from flaky import flaky
|
||||
|
||||
from telegram import ChatPhoto, Voice, TelegramError
|
||||
from tests.conftest import expect_bad_request
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
|
@ -33,7 +34,10 @@ def chatphoto_file():
|
|||
|
||||
@pytest.fixture(scope='function')
|
||||
def chat_photo(bot, super_group_id):
|
||||
return bot.get_chat(super_group_id, timeout=50).photo
|
||||
def func():
|
||||
return bot.get_chat(super_group_id, timeout=50).photo
|
||||
|
||||
return expect_bad_request(func, 'Type of file mismatch', 'Telegram did not accept the file.')
|
||||
|
||||
|
||||
class TestChatPhoto(object):
|
||||
|
@ -46,7 +50,10 @@ class TestChatPhoto(object):
|
|||
@flaky(3, 1)
|
||||
@pytest.mark.timeout(10)
|
||||
def test_send_all_args(self, bot, super_group_id, chatphoto_file, chat_photo, thumb_file):
|
||||
assert bot.set_chat_photo(super_group_id, chatphoto_file)
|
||||
def func():
|
||||
assert bot.set_chat_photo(super_group_id, chatphoto_file)
|
||||
|
||||
expect_bad_request(func, 'Type of file mismatch', 'Telegram did not accept the file.')
|
||||
|
||||
@flaky(3, 1)
|
||||
@pytest.mark.timeout(10)
|
||||
|
|
|
@ -31,6 +31,7 @@ from .test_document import document, document_file # noqa: F401
|
|||
from .test_photo import _photo, photo_file, photo, thumb # noqa: F401
|
||||
# noinspection PyUnresolvedReferences
|
||||
from .test_video import video, video_file # noqa: F401
|
||||
from tests.conftest import expect_bad_request
|
||||
|
||||
|
||||
@pytest.fixture(scope='class')
|
||||
|
@ -320,10 +321,14 @@ class TestSendMediaGroup(object):
|
|||
@pytest.mark.timeout(10) # noqa: F811
|
||||
def test_send_media_group_new_files(self, bot, chat_id, video_file, photo_file, # noqa: F811
|
||||
animation_file): # noqa: F811
|
||||
messages = bot.send_media_group(chat_id, [
|
||||
InputMediaVideo(video_file),
|
||||
InputMediaPhoto(photo_file)
|
||||
])
|
||||
def func():
|
||||
return bot.send_media_group(chat_id, [
|
||||
InputMediaVideo(video_file),
|
||||
InputMediaPhoto(photo_file)
|
||||
])
|
||||
messages = expect_bad_request(func, 'Type of file mismatch',
|
||||
'Telegram did not accept the file.')
|
||||
|
||||
assert isinstance(messages, list)
|
||||
assert len(messages) == 2
|
||||
assert all([isinstance(mes, Message) for mes in messages])
|
||||
|
|
|
@ -24,6 +24,7 @@ from flaky import flaky
|
|||
|
||||
from telegram import Sticker, TelegramError, PhotoSize, InputFile
|
||||
from telegram.utils.helpers import escape_markdown
|
||||
from tests.conftest import expect_bad_request
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
|
@ -35,8 +36,11 @@ def photo_file():
|
|||
|
||||
@pytest.fixture(scope='class')
|
||||
def _photo(bot, chat_id):
|
||||
with open('tests/data/telegram.jpg', 'rb') as f:
|
||||
return bot.send_photo(chat_id, photo=f, timeout=50).photo
|
||||
def func():
|
||||
with open('tests/data/telegram.jpg', 'rb') as f:
|
||||
return bot.send_photo(chat_id, photo=f, timeout=50).photo
|
||||
|
||||
return expect_bad_request(func, 'Type of file mismatch', 'Telegram did not accept the file.')
|
||||
|
||||
|
||||
@pytest.fixture(scope='class')
|
||||
|
|
|
@ -25,6 +25,7 @@ from flaky import flaky
|
|||
from future.utils import PY2
|
||||
|
||||
from telegram import Sticker, PhotoSize, TelegramError, StickerSet, Audio, MaskPosition
|
||||
from telegram.error import BadRequest
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
|
@ -260,7 +261,11 @@ class TestSticker(object):
|
|||
def sticker_set(bot):
|
||||
ss = bot.get_sticker_set('test_by_{}'.format(bot.username))
|
||||
if len(ss.stickers) > 100:
|
||||
raise Exception('stickerset is growing too large.')
|
||||
try:
|
||||
for i in range(1, 50):
|
||||
bot.delete_sticker_from_set(ss.stickers[-i].file_id)
|
||||
except BadRequest:
|
||||
raise Exception('stickerset is growing too large.')
|
||||
return ss
|
||||
|
||||
|
||||
|
@ -268,7 +273,11 @@ def sticker_set(bot):
|
|||
def animated_sticker_set(bot):
|
||||
ss = bot.get_sticker_set('animated_test_by_{}'.format(bot.username))
|
||||
if len(ss.stickers) > 100:
|
||||
raise Exception('stickerset is growing too large.')
|
||||
try:
|
||||
for i in range(1, 50):
|
||||
bot.delete_sticker_from_set(ss.stickers[-i].file_id)
|
||||
except BadRequest:
|
||||
raise Exception('stickerset is growing too large.')
|
||||
return ss
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue