From 5e6cc254a4ea4f48df357b97273f481ae4acdbd5 Mon Sep 17 00:00:00 2001 From: Hinrich mahler Date: Sat, 26 Oct 2019 06:49:27 +0000 Subject: [PATCH] Make @log preserve signature, add bots with defaults to tests --- requirements.txt | 1 + telegram/bot.py | 8 ++-- telegram/utils/helpers.py | 11 +++++ tests/conftest.py | 27 +++++++++++- tests/test_animation.py | 31 ++++++-------- tests/test_audio.py | 44 +++++--------------- tests/test_bot.py | 78 +++++++++++++++++------------------ tests/test_callbackquery.py | 8 ++-- tests/test_chat.py | 28 ++++++------- tests/test_document.py | 31 ++++++-------- tests/test_message.py | 82 ++++++++++++++++++------------------- tests/test_photo.py | 47 +++++---------------- tests/test_user.py | 36 ++++++++-------- tests/test_video.py | 31 ++++++-------- tests/test_voice.py | 31 ++++++-------- 15 files changed, 225 insertions(+), 269 deletions(-) diff --git a/requirements.txt b/requirements.txt index c004d5fc7..6f926578e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ future>=0.16.0 certifi tornado>=5.1 cryptography +decorator>=4.4.0 diff --git a/telegram/bot.py b/telegram/bot.py index caa0a335e..fae7a4a22 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -22,6 +22,7 @@ import functools import inspect +from decorator import decorate try: import ujson as json @@ -58,18 +59,17 @@ def info(func): return decorator -def log(func): +def log(func, *args, **kwargs): logger = logging.getLogger(func.__module__) - @functools.wraps(func) def decorator(self, *args, **kwargs): logger.debug('Entering: %s', func.__name__) - result = func(self, *args, **kwargs) + result = func(*args, **kwargs) logger.debug(result) logger.debug('Exiting: %s', func.__name__) return result - return decorator + return decorate(func, decorator) class Bot(TelegramObject): diff --git a/telegram/utils/helpers.py b/telegram/utils/helpers.py index 0bed738a9..58800e165 100644 --- a/telegram/utils/helpers.py +++ b/telegram/utils/helpers.py @@ -272,6 +272,17 @@ class Defaults: def __init__(self, parse_mode=None): self.parse_mode = parse_mode + def __hash__(self): + return hash((self.parse_mode)) + + def __eq__(self, other): + if isinstance(other, Defaults): + return self.__dict__ == other.__dict__ + return False + + def __ne__(self, other): + return not (self == other) + class DefaultValue: """Wrapper for immutable default arguments that allows to check, if the default value was set diff --git a/tests/conftest.py b/tests/conftest.py index c702b0f63..d428130e5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -30,6 +30,7 @@ import pytest from telegram import Bot, Message, User, Chat, MessageEntity, Update, \ InlineQuery, CallbackQuery, ShippingQuery, PreCheckoutQuery, ChosenInlineResult from telegram.ext import Dispatcher, JobQueue, Updater, BaseFilter +from telegram.utils.helpers import Defaults from tests.bots import get_bot TRAVIS = os.getenv('TRAVIS', False) @@ -52,6 +53,28 @@ def bot(bot_info): return make_bot(bot_info) +DEFAULT_BOTS = {} +@pytest.fixture(scope='function') +def default_bot(request, bot_info): + param = request.param if hasattr(request, 'param') else {} + + # allow both `default_parse_mode` and `parse_mode` + for kwarg in param.keys(): + if kwarg.startswith('default_'): + value = param.pop(kwarg) + param[kwarg[8:]] = value + def_param = {'default_' + k: v for (k, v) in param.items()} + + defaults = Defaults(**param) + default_bot = DEFAULT_BOTS.get(defaults) + if default_bot: + return default_bot + else: + default_bot = make_bot(bot_info, **def_param) + DEFAULT_BOTS[defaults] = default_bot + return default_bot + + @pytest.fixture(scope='session') def chat_id(bot_info): return bot_info['chat_id'] @@ -151,8 +174,8 @@ def pytest_configure(config): # TODO: Write so good code that we don't need to ignore ResourceWarnings anymore -def make_bot(bot_info): - return Bot(bot_info['token'], private_key=PRIVATE_KEY) +def make_bot(bot_info, **kwargs): + return Bot(bot_info['token'], private_key=PRIVATE_KEY, **kwargs) CMD_PATTERN = re.compile(r'/[\da-z_]{1,32}(?:@\w{1,32})?') diff --git a/tests/test_animation.py b/tests/test_animation.py index 03cabec17..ac82934f5 100644 --- a/tests/test_animation.py +++ b/tests/test_animation.py @@ -19,7 +19,6 @@ import os import pytest -import functools from flaky import flaky from telegram import PhotoSize, Animation, Voice, TelegramError @@ -109,42 +108,36 @@ class TestAnimation(object): assert message.animation.mime_type == animation.mime_type assert message.animation.file_size == animation.file_size - flaky(3, 1) + @flaky(3, 1) @pytest.mark.timeout(10) - def test_send_animation_default_parse_mode_1(self, monkeypatch, bot, chat_id, animation_file): - monkeypatch.setattr('telegram.Bot.send_animation', functools.partial(bot.send_animation, - **{'parse_mode': 'Markdown'})) - + @pytest.mark.parametrize('default_bot', [{'default_parse_mode': 'Markdown'}], indirect=True) + def test_send_animation_default_parse_mode_1(self, default_bot, chat_id, animation_file): test_string = 'Italic Bold Code' test_markdown_string = '_Italic_ *Bold* `Code`' - message = bot.send_animation(chat_id, animation_file, caption=test_markdown_string) + message = default_bot.send_animation(chat_id, animation_file, caption=test_markdown_string) assert message.caption_markdown == test_markdown_string assert message.caption == test_string @flaky(3, 1) @pytest.mark.timeout(10) - def test_send_animation_default_parse_mode_2(self, monkeypatch, bot, chat_id, animation_file): - monkeypatch.setattr('telegram.Bot.send_animation', functools.partial(bot.send_animation, - **{'parse_mode': 'Markdown'})) - + @pytest.mark.parametrize('default_bot', [{'default_parse_mode': 'Markdown'}], indirect=True) + def test_send_animation_default_parse_mode_2(self, default_bot, chat_id, animation_file): test_markdown_string = '_Italic_ *Bold* `Code`' - message = bot.send_animation(chat_id, animation_file, caption=test_markdown_string, - parse_mode=None) + message = default_bot.send_animation(chat_id, animation_file, caption=test_markdown_string, + parse_mode=None) assert message.caption == test_markdown_string assert message.caption_markdown == escape_markdown(test_markdown_string) @flaky(3, 1) @pytest.mark.timeout(10) - def test_send_animation_default_parse_mode_3(self, monkeypatch, bot, chat_id, animation_file): - monkeypatch.setattr('telegram.Bot.send_animation', functools.partial(bot.send_animation, - **{'parse_mode': 'Markdown'})) - + @pytest.mark.parametrize('default_bot', [{'default_parse_mode': 'Markdown'}], indirect=True) + def test_send_animation_default_parse_mode_3(self, default_bot, chat_id, animation_file): test_markdown_string = '_Italic_ *Bold* `Code`' - message = bot.send_animation(chat_id, animation_file, caption=test_markdown_string, - parse_mode='HTML') + message = default_bot.send_animation(chat_id, animation_file, caption=test_markdown_string, + parse_mode='HTML') assert message.caption == test_markdown_string assert message.caption_markdown == escape_markdown(test_markdown_string) diff --git a/tests/test_audio.py b/tests/test_audio.py index 8e7d27fdf..a189dff5f 100644 --- a/tests/test_audio.py +++ b/tests/test_audio.py @@ -17,7 +17,6 @@ # 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 os -import functools import pytest from flaky import flaky @@ -136,55 +135,34 @@ class TestAudio(object): @flaky(3, 1) @pytest.mark.timeout(10) - def test_send_audio_default_parse_mode_1(self, - monkeypatch, - bot, - chat_id, - audio_file, - thumb_file): - monkeypatch.setattr('telegram.Bot.send_audio', functools.partial(bot.send_audio, - **{'parse_mode': 'Markdown'})) - + @pytest.mark.parametrize('default_bot', [{'default_parse_mode': 'Markdown'}], indirect=True) + def test_send_audio_default_parse_mode_1(self, default_bot, chat_id, audio_file, thumb_file): test_string = 'Italic Bold Code' test_markdown_string = '_Italic_ *Bold* `Code`' - message = bot.send_audio(chat_id, audio_file, caption=test_markdown_string) + message = default_bot.send_audio(chat_id, audio_file, caption=test_markdown_string) assert message.caption_markdown == test_markdown_string assert message.caption == test_string @flaky(3, 1) @pytest.mark.timeout(10) - def test_send_audio_default_parse_mode_2(self, - monkeypatch, - bot, - chat_id, - audio_file, - thumb_file): - monkeypatch.setattr('telegram.Bot.send_audio', functools.partial(bot.send_audio, - **{'parse_mode': 'Markdown'})) - + @pytest.mark.parametrize('default_bot', [{'default_parse_mode': 'Markdown'}], indirect=True) + def test_send_audio_default_parse_mode_2(self, default_bot, chat_id, audio_file, thumb_file): test_markdown_string = '_Italic_ *Bold* `Code`' - message = bot.send_audio(chat_id, audio_file, caption=test_markdown_string, - parse_mode=None) + message = default_bot.send_audio(chat_id, audio_file, caption=test_markdown_string, + parse_mode=None) assert message.caption == test_markdown_string assert message.caption_markdown == escape_markdown(test_markdown_string) @flaky(3, 1) @pytest.mark.timeout(10) - def test_send_audio_default_parse_mode_3(self, - monkeypatch, - bot, - chat_id, - audio_file, - thumb_file): - monkeypatch.setattr('telegram.Bot.send_audio', functools.partial(bot.send_audio, - **{'parse_mode': 'Markdown'})) - + @pytest.mark.parametrize('default_bot', [{'default_parse_mode': 'Markdown'}], indirect=True) + def test_send_audio_default_parse_mode_3(self, default_bot, chat_id, audio_file, thumb_file): test_markdown_string = '_Italic_ *Bold* `Code`' - message = bot.send_audio(chat_id, audio_file, caption=test_markdown_string, - parse_mode='HTML') + message = default_bot.send_audio(chat_id, audio_file, caption=test_markdown_string, + parse_mode='HTML') assert message.caption == test_markdown_string assert message.caption_markdown == escape_markdown(test_markdown_string) diff --git a/tests/test_bot.py b/tests/test_bot.py index 937f72a44..a45b71a18 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -19,7 +19,6 @@ import os import sys import time -import functools from datetime import datetime from platform import python_implementation @@ -311,31 +310,29 @@ class TestBot(object): @flaky(3, 1) @pytest.mark.timeout(10) - def test_edit_message_text_default_parse_mode(self, monkeypatch, bot, message): - monkeypatch.setattr('telegram.Bot.edit_message_text', - functools.partial(bot.edit_message_text, **{'parse_mode': 'Markdown'})) - + @pytest.mark.parametrize('default_bot', [{'default_parse_mode': 'Markdown'}], indirect=True) + def test_edit_message_text_default_parse_mode(self, default_bot, message): test_string = 'Italic Bold Code' test_markdown_string = '_Italic_ *Bold* `Code`' - message = bot.edit_message_text(text=test_markdown_string, chat_id=message.chat_id, - message_id=message.message_id, - disable_web_page_preview=True) + message = default_bot.edit_message_text(text=test_markdown_string, chat_id=message.chat_id, + message_id=message.message_id, + disable_web_page_preview=True) assert message.text_markdown == test_markdown_string assert message.text == test_string - message = bot.edit_message_text(text=test_markdown_string, chat_id=message.chat_id, - message_id=message.message_id, parse_mode=None, - disable_web_page_preview=True) + message = default_bot.edit_message_text(text=test_markdown_string, chat_id=message.chat_id, + message_id=message.message_id, parse_mode=None, + disable_web_page_preview=True) assert message.text == test_markdown_string assert message.text_markdown == escape_markdown(test_markdown_string) - message = bot.edit_message_text(text=test_markdown_string, chat_id=message.chat_id, - message_id=message.message_id, - disable_web_page_preview=True) - message = bot.edit_message_text(text=test_markdown_string, chat_id=message.chat_id, - message_id=message.message_id, parse_mode='HTML', - disable_web_page_preview=True) + message = default_bot.edit_message_text(text=test_markdown_string, chat_id=message.chat_id, + message_id=message.message_id, + disable_web_page_preview=True) + message = default_bot.edit_message_text(text=test_markdown_string, chat_id=message.chat_id, + message_id=message.message_id, parse_mode='HTML', + disable_web_page_preview=True) assert message.text == test_markdown_string assert message.text_markdown == escape_markdown(test_markdown_string) @@ -355,32 +352,31 @@ class TestBot(object): @flaky(3, 1) @pytest.mark.timeout(10) - def test_edit_message_caption_default_parse_mode(self, monkeypatch, bot, media_message): - monkeypatch.setattr('telegram.Bot.edit_message_caption', - functools.partial(bot.edit_message_caption, - **{'parse_mode': 'Markdown'})) - + @pytest.mark.parametrize('default_bot', [{'default_parse_mode': 'Markdown'}], indirect=True) + def test_edit_message_caption_default_parse_mode(self, default_bot, media_message): test_string = 'Italic Bold Code' test_markdown_string = '_Italic_ *Bold* `Code`' - message = bot.edit_message_caption(caption=test_markdown_string, - chat_id=media_message.chat_id, - message_id=media_message.message_id) + message = default_bot.edit_message_caption(caption=test_markdown_string, + chat_id=media_message.chat_id, + message_id=media_message.message_id) assert message.caption_markdown == test_markdown_string assert message.caption == test_string - message = bot.edit_message_caption(caption=test_markdown_string, - chat_id=media_message.chat_id, - message_id=media_message.message_id, parse_mode=None) + message = default_bot.edit_message_caption(caption=test_markdown_string, + chat_id=media_message.chat_id, + message_id=media_message.message_id, + parse_mode=None) assert message.caption == test_markdown_string assert message.caption_markdown == escape_markdown(test_markdown_string) - message = bot.edit_message_caption(caption=test_markdown_string, - chat_id=media_message.chat_id, - message_id=media_message.message_id) - message = bot.edit_message_caption(caption=test_markdown_string, - chat_id=media_message.chat_id, - message_id=media_message.message_id, parse_mode='HTML') + message = default_bot.edit_message_caption(caption=test_markdown_string, + chat_id=media_message.chat_id, + message_id=media_message.message_id) + message = default_bot.edit_message_caption(caption=test_markdown_string, + chat_id=media_message.chat_id, + message_id=media_message.message_id, + parse_mode='HTML') assert message.caption == test_markdown_string assert message.caption_markdown == escape_markdown(test_markdown_string) @@ -784,21 +780,21 @@ class TestBot(object): with pytest.raises(OkException): bot.send_photo(chat_id, open('tests/data/telegram.jpg', 'rb')) - def test_send_message_default_parse_mode(self, monkeypatch, bot, chat_id): - monkeypatch.setattr('telegram.Bot.send_message', functools.partial(bot.send_message, - **{'parse_mode': 'Markdown'})) - + @flaky(3, 1) + @pytest.mark.timeout(10) + @pytest.mark.parametrize('default_bot', [{'default_parse_mode': 'Markdown'}], indirect=True) + def test_send_message_default_parse_mode(self, default_bot, chat_id): test_string = 'Italic Bold Code' test_markdown_string = '_Italic_ *Bold* `Code`' - message = bot.send_message(chat_id, test_markdown_string) + message = default_bot.send_message(chat_id, test_markdown_string) assert message.text_markdown == test_markdown_string assert message.text == test_string - message = bot.send_message(chat_id, test_markdown_string, parse_mode=None) + message = default_bot.send_message(chat_id, test_markdown_string, parse_mode=None) assert message.text == test_markdown_string assert message.text_markdown == escape_markdown(test_markdown_string) - message = bot.send_message(chat_id, test_markdown_string, parse_mode='HTML') + message = default_bot.send_message(chat_id, test_markdown_string, parse_mode='HTML') assert message.text == test_markdown_string assert message.text_markdown == escape_markdown(test_markdown_string) diff --git a/tests/test_callbackquery.py b/tests/test_callbackquery.py index 842a8bc18..1d4eebf57 100644 --- a/tests/test_callbackquery.py +++ b/tests/test_callbackquery.py @@ -88,7 +88,7 @@ class TestCallbackQuery(object): def test_edit_message_text(self, monkeypatch, callback_query): def test(*args, **kwargs): - text = args[1] == 'test' + text = args[0] == 'test' try: id = kwargs['inline_message_id'] == callback_query.inline_message_id return id and text @@ -97,7 +97,7 @@ class TestCallbackQuery(object): message_id = kwargs['message_id'] == callback_query.message.message_id return chat_id and message_id and text - monkeypatch.setattr('telegram.Bot.edit_message_text', test) + monkeypatch.setattr(callback_query.bot, 'edit_message_text', test) assert callback_query.edit_message_text(text='test') assert callback_query.edit_message_text('test') @@ -112,7 +112,7 @@ class TestCallbackQuery(object): message = kwargs['message_id'] == callback_query.message.message_id return id and message and caption - monkeypatch.setattr('telegram.Bot.edit_message_caption', test) + monkeypatch.setattr(callback_query.bot, 'edit_message_caption', test) assert callback_query.edit_message_caption(caption='new caption') assert callback_query.edit_message_caption('new caption') @@ -127,7 +127,7 @@ class TestCallbackQuery(object): message = kwargs['message_id'] == callback_query.message.message_id return id and message and reply_markup - monkeypatch.setattr('telegram.Bot.edit_message_reply_markup', test) + monkeypatch.setattr(callback_query.bot, 'edit_message_reply_markup', test) assert callback_query.edit_message_reply_markup(reply_markup=[['1', '2']]) assert callback_query.edit_message_reply_markup([['1', '2']]) diff --git a/tests/test_chat.py b/tests/test_chat.py index 23e95806a..b3b693e3f 100644 --- a/tests/test_chat.py +++ b/tests/test_chat.py @@ -153,30 +153,30 @@ class TestChat(object): def test_instance_method_send_message(self, monkeypatch, chat): def test(*args, **kwargs): - return args[1] == chat.id and args[2] == 'test' + return args[0] == chat.id and args[1] == 'test' - monkeypatch.setattr('telegram.Bot.send_message', test) + monkeypatch.setattr(chat.bot, 'send_message', test) assert chat.send_message('test') def test_instance_method_send_photo(self, monkeypatch, chat): def test(*args, **kwargs): - return args[1] == chat.id and args[2] == 'test_photo' + return args[0] == chat.id and args[1] == 'test_photo' - monkeypatch.setattr('telegram.Bot.send_photo', test) + monkeypatch.setattr(chat.bot, 'send_photo', test) assert chat.send_photo('test_photo') def test_instance_method_send_audio(self, monkeypatch, chat): def test(*args, **kwargs): - return args[1] == chat.id and args[2] == 'test_audio' + return args[0] == chat.id and args[1] == 'test_audio' - monkeypatch.setattr('telegram.Bot.send_audio', test) + monkeypatch.setattr(chat.bot, 'send_audio', test) assert chat.send_audio('test_audio') def test_instance_method_send_document(self, monkeypatch, chat): def test(*args, **kwargs): - return args[1] == chat.id and args[2] == 'test_document' + return args[0] == chat.id and args[1] == 'test_document' - monkeypatch.setattr('telegram.Bot.send_document', test) + monkeypatch.setattr(chat.bot, 'send_document', test) assert chat.send_document('test_document') def test_instance_method_send_sticker(self, monkeypatch, chat): @@ -188,9 +188,9 @@ class TestChat(object): def test_instance_method_send_video(self, monkeypatch, chat): def test(*args, **kwargs): - return args[1] == chat.id and args[2] == 'test_video' + return args[0] == chat.id and args[1] == 'test_video' - monkeypatch.setattr('telegram.Bot.send_video', test) + monkeypatch.setattr(chat.bot, 'send_video', test) assert chat.send_video('test_video') def test_instance_method_send_video_note(self, monkeypatch, chat): @@ -202,16 +202,16 @@ class TestChat(object): def test_instance_method_send_voice(self, monkeypatch, chat): def test(*args, **kwargs): - return args[1] == chat.id and args[2] == 'test_voice' + return args[0] == chat.id and args[1] == 'test_voice' - monkeypatch.setattr('telegram.Bot.send_voice', test) + monkeypatch.setattr(chat.bot, 'send_voice', test) assert chat.send_voice('test_voice') def test_instance_method_send_animation(self, monkeypatch, chat): def test(*args, **kwargs): - return args[1] == chat.id and args[2] == 'test_animation' + return args[0] == chat.id and args[1] == 'test_animation' - monkeypatch.setattr('telegram.Bot.send_animation', test) + monkeypatch.setattr(chat.bot, 'send_animation', test) assert chat.send_animation('test_animation') def test_instance_method_send_poll(self, monkeypatch, chat): diff --git a/tests/test_document.py b/tests/test_document.py index 8a36f37af..c74c58fa7 100644 --- a/tests/test_document.py +++ b/tests/test_document.py @@ -17,7 +17,6 @@ # 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 os -import functools import pytest from flaky import flaky @@ -125,42 +124,36 @@ class TestDocument(object): assert message - flaky(3, 1) + @flaky(3, 1) @pytest.mark.timeout(10) - def test_send_document_default_parse_mode_1(self, monkeypatch, bot, chat_id, document): - monkeypatch.setattr('telegram.Bot.send_document', functools.partial(bot.send_document, - **{'parse_mode': 'Markdown'})) - + @pytest.mark.parametrize('default_bot', [{'default_parse_mode': 'Markdown'}], indirect=True) + def test_send_document_default_parse_mode_1(self, default_bot, chat_id, document): test_string = 'Italic Bold Code' test_markdown_string = '_Italic_ *Bold* `Code`' - message = bot.send_document(chat_id, document, caption=test_markdown_string) + message = default_bot.send_document(chat_id, document, caption=test_markdown_string) assert message.caption_markdown == test_markdown_string assert message.caption == test_string @flaky(3, 1) @pytest.mark.timeout(10) - def test_send_document_default_parse_mode_2(self, monkeypatch, bot, chat_id, document): - monkeypatch.setattr('telegram.Bot.send_document', functools.partial(bot.send_document, - **{'parse_mode': 'Markdown'})) - + @pytest.mark.parametrize('default_bot', [{'default_parse_mode': 'Markdown'}], indirect=True) + def test_send_document_default_parse_mode_2(self, default_bot, chat_id, document): test_markdown_string = '_Italic_ *Bold* `Code`' - message = bot.send_document(chat_id, document, caption=test_markdown_string, - parse_mode=None) + message = default_bot.send_document(chat_id, document, caption=test_markdown_string, + parse_mode=None) assert message.caption == test_markdown_string assert message.caption_markdown == escape_markdown(test_markdown_string) @flaky(3, 1) @pytest.mark.timeout(10) - def test_send_document_default_parse_mode_3(self, monkeypatch, bot, chat_id, document): - monkeypatch.setattr('telegram.Bot.send_document', functools.partial(bot.send_document, - **{'parse_mode': 'Markdown'})) - + @pytest.mark.parametrize('default_bot', [{'default_parse_mode': 'Markdown'}], indirect=True) + def test_send_document_default_parse_mode_3(self, default_bot, chat_id, document): test_markdown_string = '_Italic_ *Bold* `Code`' - message = bot.send_document(chat_id, document, caption=test_markdown_string, - parse_mode='HTML') + message = default_bot.send_document(chat_id, document, caption=test_markdown_string, + parse_mode='HTML') assert message.caption == test_markdown_string assert message.caption_markdown == escape_markdown(test_markdown_string) diff --git a/tests/test_message.py b/tests/test_message.py index 6aaf63a9e..4af714302 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -328,15 +328,15 @@ class TestMessage(object): def test_reply_text(self, monkeypatch, message): def test(*args, **kwargs): - id = args[1] == message.chat_id - text = args[2] == 'test' + id = args[0] == message.chat_id + text = args[1] == 'test' if kwargs.get('reply_to_message_id'): reply = kwargs['reply_to_message_id'] == message.message_id else: reply = True return id and text and reply - monkeypatch.setattr('telegram.Bot.send_message', test) + monkeypatch.setattr(message.bot, 'send_message', test) assert message.reply_text('test') assert message.reply_text('test', quote=True) assert message.reply_text('test', reply_to_message_id=message.message_id, quote=True) @@ -347,8 +347,8 @@ class TestMessage(object): 'http://google.com') def test(*args, **kwargs): - cid = args[1] == message.chat_id - markdown_text = args[2] == test_md_string + cid = args[0] == message.chat_id + markdown_text = args[1] == test_md_string markdown_enabled = kwargs['parse_mode'] == ParseMode.MARKDOWN if kwargs.get('reply_to_message_id'): reply = kwargs['reply_to_message_id'] == message.message_id @@ -359,7 +359,7 @@ class TestMessage(object): text_markdown = self.test_message.text_markdown assert text_markdown == test_md_string - monkeypatch.setattr('telegram.Bot.send_message', test) + monkeypatch.setattr(message.bot, 'send_message', test) assert message.reply_markdown(self.test_message.text_markdown) assert message.reply_markdown(self.test_message.text_markdown, quote=True) assert message.reply_markdown(self.test_message.text_markdown, @@ -373,8 +373,8 @@ class TestMessage(object): '
pre
. http://google.com') def test(*args, **kwargs): - cid = args[1] == message.chat_id - html_text = args[2] == test_html_string + cid = args[0] == message.chat_id + html_text = args[1] == test_html_string html_enabled = kwargs['parse_mode'] == ParseMode.HTML if kwargs.get('reply_to_message_id'): reply = kwargs['reply_to_message_id'] == message.message_id @@ -385,7 +385,7 @@ class TestMessage(object): text_html = self.test_message.text_html assert text_html == test_html_string - monkeypatch.setattr('telegram.Bot.send_message', test) + monkeypatch.setattr(message.bot, 'send_message', test) assert message.reply_html(self.test_message.text_html) assert message.reply_html(self.test_message.text_html, quote=True) assert message.reply_html(self.test_message.text_html, @@ -394,7 +394,7 @@ class TestMessage(object): def test_reply_media_group(self, monkeypatch, message): def test(*args, **kwargs): - id = args[1] == message.chat_id + id = args[0] == message.chat_id media = kwargs['media'] == 'reply_media_group' if kwargs.get('reply_to_message_id'): reply = kwargs['reply_to_message_id'] == message.message_id @@ -402,13 +402,13 @@ class TestMessage(object): reply = True return id and media and reply - monkeypatch.setattr('telegram.Bot.send_media_group', test) + monkeypatch.setattr(message.bot, 'send_media_group', test) assert message.reply_media_group(media='reply_media_group') assert message.reply_media_group(media='reply_media_group', quote=True) def test_reply_photo(self, monkeypatch, message): def test(*args, **kwargs): - id = args[1] == message.chat_id + id = args[0] == message.chat_id photo = kwargs['photo'] == 'test_photo' if kwargs.get('reply_to_message_id'): reply = kwargs['reply_to_message_id'] == message.message_id @@ -416,13 +416,13 @@ class TestMessage(object): reply = True return id and photo and reply - monkeypatch.setattr('telegram.Bot.send_photo', test) + monkeypatch.setattr(message.bot, 'send_photo', test) assert message.reply_photo(photo='test_photo') assert message.reply_photo(photo='test_photo', quote=True) def test_reply_audio(self, monkeypatch, message): def test(*args, **kwargs): - id = args[1] == message.chat_id + id = args[0] == message.chat_id audio = kwargs['audio'] == 'test_audio' if kwargs.get('reply_to_message_id'): reply = kwargs['reply_to_message_id'] == message.message_id @@ -430,13 +430,13 @@ class TestMessage(object): reply = True return id and audio and reply - monkeypatch.setattr('telegram.Bot.send_audio', test) + monkeypatch.setattr(message.bot, 'send_audio', test) assert message.reply_audio(audio='test_audio') assert message.reply_audio(audio='test_audio', quote=True) def test_reply_document(self, monkeypatch, message): def test(*args, **kwargs): - id = args[1] == message.chat_id + id = args[0] == message.chat_id document = kwargs['document'] == 'test_document' if kwargs.get('reply_to_message_id'): reply = kwargs['reply_to_message_id'] == message.message_id @@ -444,13 +444,13 @@ class TestMessage(object): reply = True return id and document and reply - monkeypatch.setattr('telegram.Bot.send_document', test) + monkeypatch.setattr(message.bot, 'send_document', test) assert message.reply_document(document='test_document') assert message.reply_document(document='test_document', quote=True) def test_reply_animation(self, monkeypatch, message): def test(*args, **kwargs): - id = args[1] == message.chat_id + id = args[0] == message.chat_id animation = kwargs['animation'] == 'test_animation' if kwargs.get('reply_to_message_id'): reply = kwargs['reply_to_message_id'] == message.message_id @@ -458,13 +458,13 @@ class TestMessage(object): reply = True return id and animation and reply - monkeypatch.setattr('telegram.Bot.send_animation', test) + monkeypatch.setattr(message.bot, 'send_animation', test) assert message.reply_animation(animation='test_animation') assert message.reply_animation(animation='test_animation', quote=True) def test_reply_sticker(self, monkeypatch, message): def test(*args, **kwargs): - id = args[1] == message.chat_id + id = args[0] == message.chat_id sticker = kwargs['sticker'] == 'test_sticker' if kwargs.get('reply_to_message_id'): reply = kwargs['reply_to_message_id'] == message.message_id @@ -472,13 +472,13 @@ class TestMessage(object): reply = True return id and sticker and reply - monkeypatch.setattr('telegram.Bot.send_sticker', test) + monkeypatch.setattr(message.bot, 'send_sticker', test) assert message.reply_sticker(sticker='test_sticker') assert message.reply_sticker(sticker='test_sticker', quote=True) def test_reply_video(self, monkeypatch, message): def test(*args, **kwargs): - id = args[1] == message.chat_id + id = args[0] == message.chat_id video = kwargs['video'] == 'test_video' if kwargs.get('reply_to_message_id'): reply = kwargs['reply_to_message_id'] == message.message_id @@ -486,13 +486,13 @@ class TestMessage(object): reply = True return id and video and reply - monkeypatch.setattr('telegram.Bot.send_video', test) + monkeypatch.setattr(message.bot, 'send_video', test) assert message.reply_video(video='test_video') assert message.reply_video(video='test_video', quote=True) def test_reply_video_note(self, monkeypatch, message): def test(*args, **kwargs): - id = args[1] == message.chat_id + id = args[0] == message.chat_id video_note = kwargs['video_note'] == 'test_video_note' if kwargs.get('reply_to_message_id'): reply = kwargs['reply_to_message_id'] == message.message_id @@ -500,13 +500,13 @@ class TestMessage(object): reply = True return id and video_note and reply - monkeypatch.setattr('telegram.Bot.send_video_note', test) + monkeypatch.setattr(message.bot, 'send_video_note', test) assert message.reply_video_note(video_note='test_video_note') assert message.reply_video_note(video_note='test_video_note', quote=True) def test_reply_voice(self, monkeypatch, message): def test(*args, **kwargs): - id = args[1] == message.chat_id + id = args[0] == message.chat_id voice = kwargs['voice'] == 'test_voice' if kwargs.get('reply_to_message_id'): reply = kwargs['reply_to_message_id'] == message.message_id @@ -514,13 +514,13 @@ class TestMessage(object): reply = True return id and voice and reply - monkeypatch.setattr('telegram.Bot.send_voice', test) + monkeypatch.setattr(message.bot, 'send_voice', test) assert message.reply_voice(voice='test_voice') assert message.reply_voice(voice='test_voice', quote=True) def test_reply_location(self, monkeypatch, message): def test(*args, **kwargs): - id = args[1] == message.chat_id + id = args[0] == message.chat_id location = kwargs['location'] == 'test_location' if kwargs.get('reply_to_message_id'): reply = kwargs['reply_to_message_id'] == message.message_id @@ -528,13 +528,13 @@ class TestMessage(object): reply = True return id and location and reply - monkeypatch.setattr('telegram.Bot.send_location', test) + monkeypatch.setattr(message.bot, 'send_location', test) assert message.reply_location(location='test_location') assert message.reply_location(location='test_location', quote=True) def test_reply_venue(self, monkeypatch, message): def test(*args, **kwargs): - id = args[1] == message.chat_id + id = args[0] == message.chat_id venue = kwargs['venue'] == 'test_venue' if kwargs.get('reply_to_message_id'): reply = kwargs['reply_to_message_id'] == message.message_id @@ -542,13 +542,13 @@ class TestMessage(object): reply = True return id and venue and reply - monkeypatch.setattr('telegram.Bot.send_venue', test) + monkeypatch.setattr(message.bot, 'send_venue', test) assert message.reply_venue(venue='test_venue') assert message.reply_venue(venue='test_venue', quote=True) def test_reply_contact(self, monkeypatch, message): def test(*args, **kwargs): - id = args[1] == message.chat_id + id = args[0] == message.chat_id contact = kwargs['contact'] == 'test_contact' if kwargs.get('reply_to_message_id'): reply = kwargs['reply_to_message_id'] == message.message_id @@ -556,13 +556,13 @@ class TestMessage(object): reply = True return id and contact and reply - monkeypatch.setattr('telegram.Bot.send_contact', test) + monkeypatch.setattr(message.bot, 'send_contact', test) assert message.reply_contact(contact='test_contact') assert message.reply_contact(contact='test_contact', quote=True) def test_reply_poll(self, monkeypatch, message): def test(*args, **kwargs): - id = args[1] == message.chat_id + id = args[0] == message.chat_id contact = kwargs['contact'] == 'test_poll' if kwargs.get('reply_to_message_id'): reply = kwargs['reply_to_message_id'] == message.message_id @@ -570,7 +570,7 @@ class TestMessage(object): reply = True return id and contact and reply - monkeypatch.setattr('telegram.Bot.send_poll', test) + monkeypatch.setattr(message.bot, 'send_poll', test) assert message.reply_poll(contact='test_poll') assert message.reply_poll(contact='test_poll', quote=True) @@ -585,7 +585,7 @@ class TestMessage(object): notification = True return chat_id and from_chat and message_id and notification - monkeypatch.setattr('telegram.Bot.forward_message', test) + monkeypatch.setattr(message.bot, 'forward_message', test) assert message.forward(123456) assert message.forward(123456, disable_notification=True) assert not message.forward(635241) @@ -597,7 +597,7 @@ class TestMessage(object): text = kwargs['text'] == 'test' return chat_id and message_id and text - monkeypatch.setattr('telegram.Bot.edit_message_text', test) + monkeypatch.setattr(message.bot, 'edit_message_text', test) assert message.edit_text(text='test') def test_edit_caption(self, monkeypatch, message): @@ -607,7 +607,7 @@ class TestMessage(object): caption = kwargs['caption'] == 'new caption' return chat_id and message_id and caption - monkeypatch.setattr('telegram.Bot.edit_message_caption', test) + monkeypatch.setattr(message.bot, 'edit_message_caption', test) assert message.edit_caption(caption='new caption') def test_edit_media(self, monkeypatch, message): @@ -617,7 +617,7 @@ class TestMessage(object): media = kwargs['media'] == 'my_media' return chat_id and message_id and media - monkeypatch.setattr('telegram.Bot.edit_message_media', test) + monkeypatch.setattr(message.bot, 'edit_message_media', test) assert message.edit_media('my_media') def test_edit_reply_markup(self, monkeypatch, message): @@ -627,7 +627,7 @@ class TestMessage(object): reply_markup = kwargs['reply_markup'] == [['1', '2']] return chat_id and message_id and reply_markup - monkeypatch.setattr('telegram.Bot.edit_message_reply_markup', test) + monkeypatch.setattr(message.bot, 'edit_message_reply_markup', test) assert message.edit_reply_markup(reply_markup=[['1', '2']]) def test_delete(self, monkeypatch, message): @@ -636,7 +636,7 @@ class TestMessage(object): message_id = kwargs['message_id'] == message.message_id return chat_id and message_id - monkeypatch.setattr('telegram.Bot.delete_message', test) + monkeypatch.setattr(message.bot, 'delete_message', test) assert message.delete() def test_equality(self): diff --git a/tests/test_photo.py b/tests/test_photo.py index ecf407618..a7d6bbede 100644 --- a/tests/test_photo.py +++ b/tests/test_photo.py @@ -17,7 +17,6 @@ # 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 os -import functools from io import BytesIO import pytest @@ -143,58 +142,34 @@ class TestPhoto(object): @flaky(3, 1) @pytest.mark.timeout(10) - def test_send_photo_default_parse_mode_1(self, - monkeypatch, - bot, - chat_id, - photo_file, - thumb, - photo): - monkeypatch.setattr('telegram.Bot.send_photo', functools.partial(bot.send_photo, - **{'parse_mode': 'Markdown'})) - + @pytest.mark.parametrize('default_bot', [{'default_parse_mode': 'Markdown'}], indirect=True) + def test_send_photo_default_parse_mode_1(self, default_bot, chat_id, photo_file, thumb, photo): test_string = 'Italic Bold Code' test_markdown_string = '_Italic_ *Bold* `Code`' - message = bot.send_photo(chat_id, photo_file, caption=test_markdown_string) + message = default_bot.send_photo(chat_id, photo_file, caption=test_markdown_string) assert message.caption_markdown == test_markdown_string assert message.caption == test_string @flaky(3, 1) @pytest.mark.timeout(10) - def test_send_photo_default_parse_mode_2(self, - monkeypatch, - bot, - chat_id, - photo_file, - thumb, - photo): - monkeypatch.setattr('telegram.Bot.send_photo', functools.partial(bot.send_photo, - **{'parse_mode': 'Markdown'})) - + @pytest.mark.parametrize('default_bot', [{'default_parse_mode': 'Markdown'}], indirect=True) + def test_send_photo_default_parse_mode_2(self, default_bot, chat_id, photo_file, thumb, photo): test_markdown_string = '_Italic_ *Bold* `Code`' - message = bot.send_photo(chat_id, photo_file, caption=test_markdown_string, - parse_mode=None) + message = default_bot.send_photo(chat_id, photo_file, caption=test_markdown_string, + parse_mode=None) assert message.caption == test_markdown_string assert message.caption_markdown == escape_markdown(test_markdown_string) @flaky(3, 1) @pytest.mark.timeout(10) - def test_send_photo_default_parse_mode_3(self, - monkeypatch, - bot, - chat_id, - photo_file, - thumb, - photo): - monkeypatch.setattr('telegram.Bot.send_photo', functools.partial(bot.send_photo, - **{'parse_mode': 'Markdown'})) - + @pytest.mark.parametrize('default_bot', [{'default_parse_mode': 'Markdown'}], indirect=True) + def test_send_photo_default_parse_mode_3(self, default_bot, chat_id, photo_file, thumb, photo): test_markdown_string = '_Italic_ *Bold* `Code`' - message = bot.send_photo(chat_id, photo_file, caption=test_markdown_string, - parse_mode='HTML') + message = default_bot.send_photo(chat_id, photo_file, caption=test_markdown_string, + parse_mode='HTML') assert message.caption == test_markdown_string assert message.caption_markdown == escape_markdown(test_markdown_string) diff --git a/tests/test_user.py b/tests/test_user.py index cdcdea50f..fc138cb28 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -111,65 +111,65 @@ class TestUser(object): def test_instance_method_send_message(self, monkeypatch, user): def test(*args, **kwargs): - return args[1] == user.id and args[2] == 'test' + return args[0] == user.id and args[1] == 'test' - monkeypatch.setattr('telegram.Bot.send_message', test) + monkeypatch.setattr(user.bot, 'send_message', test) assert user.send_message('test') def test_instance_method_send_photo(self, monkeypatch, user): def test(*args, **kwargs): - return args[1] == user.id and args[2] == 'test_photo' + return args[0] == user.id and args[1] == 'test_photo' - monkeypatch.setattr('telegram.Bot.send_photo', test) + monkeypatch.setattr(user.bot, 'send_photo', test) assert user.send_photo('test_photo') def test_instance_method_send_audio(self, monkeypatch, user): def test(*args, **kwargs): - return args[1] == user.id and args[2] == 'test_audio' + return args[0] == user.id and args[1] == 'test_audio' - monkeypatch.setattr('telegram.Bot.send_audio', test) + monkeypatch.setattr(user.bot, 'send_audio', test) assert user.send_audio('test_audio') def test_instance_method_send_document(self, monkeypatch, user): def test(*args, **kwargs): - return args[1] == user.id and args[2] == 'test_document' + return args[0] == user.id and args[1] == 'test_document' - monkeypatch.setattr('telegram.Bot.send_document', test) + monkeypatch.setattr(user.bot, 'send_document', test) assert user.send_document('test_document') def test_instance_method_send_sticker(self, monkeypatch, user): def test(*args, **kwargs): - return args[1] == user.id and args[2] == 'test_sticker' + return args[0] == user.id and args[1] == 'test_sticker' - monkeypatch.setattr('telegram.Bot.send_sticker', test) + monkeypatch.setattr(user.bot, 'send_sticker', test) assert user.send_sticker('test_sticker') def test_instance_method_send_video(self, monkeypatch, user): def test(*args, **kwargs): - return args[1] == user.id and args[2] == 'test_video' + return args[0] == user.id and args[1] == 'test_video' - monkeypatch.setattr('telegram.Bot.send_video', test) + monkeypatch.setattr(user.bot, 'send_video', test) assert user.send_video('test_video') def test_instance_method_send_video_note(self, monkeypatch, user): def test(*args, **kwargs): - return args[1] == user.id and args[2] == 'test_video_note' + return args[0] == user.id and args[1] == 'test_video_note' - monkeypatch.setattr('telegram.Bot.send_video_note', test) + monkeypatch.setattr(user.bot, 'send_video_note', test) assert user.send_video_note('test_video_note') def test_instance_method_send_voice(self, monkeypatch, user): def test(*args, **kwargs): - return args[1] == user.id and args[2] == 'test_voice' + return args[0] == user.id and args[1] == 'test_voice' - monkeypatch.setattr('telegram.Bot.send_voice', test) + monkeypatch.setattr(user.bot, 'send_voice', test) assert user.send_voice('test_voice') def test_instance_method_send_animation(self, monkeypatch, user): def test(*args, **kwargs): - return args[1] == user.id and args[2] == 'test_animation' + return args[0] == user.id and args[1] == 'test_animation' - monkeypatch.setattr('telegram.Bot.send_animation', test) + monkeypatch.setattr(user.bot, 'send_animation', test) assert user.send_animation('test_animation') def test_mention_html(self, user): diff --git a/tests/test_video.py b/tests/test_video.py index dd921eee8..e5922715b 100644 --- a/tests/test_video.py +++ b/tests/test_video.py @@ -17,7 +17,6 @@ # 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 os -import functools import pytest from flaky import flaky @@ -143,42 +142,36 @@ class TestVideo(object): message = bot.send_video(chat_id, video=video) assert message - flaky(3, 1) + @flaky(3, 1) @pytest.mark.timeout(10) - def test_send_video_default_parse_mode_1(self, monkeypatch, bot, chat_id, video): - monkeypatch.setattr('telegram.Bot.send_video', functools.partial(bot.send_video, - **{'parse_mode': 'Markdown'})) - + @pytest.mark.parametrize('default_bot', [{'default_parse_mode': 'Markdown'}], indirect=True) + def test_send_video_default_parse_mode_1(self, default_bot, chat_id, video): test_string = 'Italic Bold Code' test_markdown_string = '_Italic_ *Bold* `Code`' - message = bot.send_video(chat_id, video, caption=test_markdown_string) + message = default_bot.send_video(chat_id, video, caption=test_markdown_string) assert message.caption_markdown == test_markdown_string assert message.caption == test_string @flaky(3, 1) @pytest.mark.timeout(10) - def test_send_video_default_parse_mode_2(self, monkeypatch, bot, chat_id, video): - monkeypatch.setattr('telegram.Bot.send_video', functools.partial(bot.send_video, - **{'parse_mode': 'Markdown'})) - + @pytest.mark.parametrize('default_bot', [{'default_parse_mode': 'Markdown'}], indirect=True) + def test_send_video_default_parse_mode_2(self, default_bot, chat_id, video): test_markdown_string = '_Italic_ *Bold* `Code`' - message = bot.send_video(chat_id, video, caption=test_markdown_string, - parse_mode=None) + message = default_bot.send_video(chat_id, video, caption=test_markdown_string, + parse_mode=None) assert message.caption == test_markdown_string assert message.caption_markdown == escape_markdown(test_markdown_string) @flaky(3, 1) @pytest.mark.timeout(10) - def test_send_video_default_parse_mode_3(self, monkeypatch, bot, chat_id, video): - monkeypatch.setattr('telegram.Bot.send_video', functools.partial(bot.send_video, - **{'parse_mode': 'Markdown'})) - + @pytest.mark.parametrize('default_bot', [{'default_parse_mode': 'Markdown'}], indirect=True) + def test_send_video_default_parse_mode_3(self, default_bot, chat_id, video): test_markdown_string = '_Italic_ *Bold* `Code`' - message = bot.send_video(chat_id, video, caption=test_markdown_string, - parse_mode='HTML') + message = default_bot.send_video(chat_id, video, caption=test_markdown_string, + parse_mode='HTML') assert message.caption == test_markdown_string assert message.caption_markdown == escape_markdown(test_markdown_string) diff --git a/tests/test_voice.py b/tests/test_voice.py index 40369d560..d52fea6b0 100644 --- a/tests/test_voice.py +++ b/tests/test_voice.py @@ -17,7 +17,6 @@ # 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 os -import functools import pytest from flaky import flaky @@ -113,42 +112,36 @@ class TestVoice(object): message = bot.send_voice(chat_id, voice=voice) assert message - flaky(3, 1) + @flaky(3, 1) @pytest.mark.timeout(10) - def test_send_voice_default_parse_mode_1(self, monkeypatch, bot, chat_id, voice): - monkeypatch.setattr('telegram.Bot.send_voice', functools.partial(bot.send_voice, - **{'parse_mode': 'Markdown'})) - + @pytest.mark.parametrize('default_bot', [{'default_parse_mode': 'Markdown'}], indirect=True) + def test_send_voice_default_parse_mode_1(self, default_bot, chat_id, voice): test_string = 'Italic Bold Code' test_markdown_string = '_Italic_ *Bold* `Code`' - message = bot.send_voice(chat_id, voice, caption=test_markdown_string) + message = default_bot.send_voice(chat_id, voice, caption=test_markdown_string) assert message.caption_markdown == test_markdown_string assert message.caption == test_string @flaky(3, 1) @pytest.mark.timeout(10) - def test_send_voice_default_parse_mode_2(self, monkeypatch, bot, chat_id, voice): - monkeypatch.setattr('telegram.Bot.send_voice', functools.partial(bot.send_voice, - **{'parse_mode': 'Markdown'})) - + @pytest.mark.parametrize('default_bot', [{'default_parse_mode': 'Markdown'}], indirect=True) + def test_send_voice_default_parse_mode_2(self, default_bot, chat_id, voice): test_markdown_string = '_Italic_ *Bold* `Code`' - message = bot.send_voice(chat_id, voice, caption=test_markdown_string, - parse_mode=None) + message = default_bot.send_voice(chat_id, voice, caption=test_markdown_string, + parse_mode=None) assert message.caption == test_markdown_string assert message.caption_markdown == escape_markdown(test_markdown_string) @flaky(3, 1) @pytest.mark.timeout(10) - def test_send_voice_default_parse_mode_3(self, monkeypatch, bot, chat_id, voice): - monkeypatch.setattr('telegram.Bot.send_voice', functools.partial(bot.send_voice, - **{'parse_mode': 'Markdown'})) - + @pytest.mark.parametrize('default_bot', [{'default_parse_mode': 'Markdown'}], indirect=True) + def test_send_voice_default_parse_mode_3(self, default_bot, chat_id, voice): test_markdown_string = '_Italic_ *Bold* `Code`' - message = bot.send_voice(chat_id, voice, caption=test_markdown_string, - parse_mode='HTML') + message = default_bot.send_voice(chat_id, voice, caption=test_markdown_string, + parse_mode='HTML') assert message.caption == test_markdown_string assert message.caption_markdown == escape_markdown(test_markdown_string)