Refactor handling of kwargs in Bot methods (#1924)

* Unify kwargs handling in Bot methods

* Remove Request.get, make api_kwargs an explicit argument, move note to head of Bot class

* Fix test_official

* Update get_file methods
This commit is contained in:
Bibo-Joshi 2020-06-30 22:07:38 +02:00
parent 5555582b72
commit 3930072659
28 changed files with 474 additions and 557 deletions

File diff suppressed because it is too large Load diff

View file

@ -94,14 +94,15 @@ class Animation(TelegramObject):
return cls(bot=bot, **data)
def get_file(self, timeout=None, **kwargs):
def get_file(self, timeout=None, api_kwargs=None):
"""Convenience wrapper over :attr:`telegram.Bot.get_file`
Args:
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
the read timeout from the server (instead of the one specified during creation of
the connection pool).
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to be passed to the
Telegram API.
Returns:
:class:`telegram.File`
@ -110,4 +111,4 @@ class Animation(TelegramObject):
:class:`telegram.TelegramError`
"""
return self.bot.get_file(self.file_id, timeout=timeout, **kwargs)
return self.bot.get_file(self.file_id, timeout=timeout, api_kwargs=api_kwargs)

View file

@ -91,14 +91,15 @@ class Audio(TelegramObject):
return cls(bot=bot, **data)
def get_file(self, timeout=None, **kwargs):
def get_file(self, timeout=None, api_kwargs=None):
"""Convenience wrapper over :attr:`telegram.Bot.get_file`
Args:
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
the read timeout from the server (instead of the one specified during creation of
the connection pool).
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to be passed to the
Telegram API.
Returns:
:class:`telegram.File`
@ -107,4 +108,4 @@ class Audio(TelegramObject):
:class:`telegram.TelegramError`
"""
return self.bot.get_file(self.file_id, timeout=timeout, **kwargs)
return self.bot.get_file(self.file_id, timeout=timeout, api_kwargs=api_kwargs)

View file

@ -83,7 +83,8 @@ class ChatPhoto(TelegramObject):
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
the read timeout from the server (instead of the one specified during creation of
the connection pool).
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to be passed to the
Telegram API.
Returns:
:class:`telegram.File`
@ -102,7 +103,8 @@ class ChatPhoto(TelegramObject):
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
the read timeout from the server (instead of the one specified during creation of
the connection pool).
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to be passed to the
Telegram API.
Returns:
:class:`telegram.File`

View file

@ -83,14 +83,15 @@ class Document(TelegramObject):
return cls(bot=bot, **data)
def get_file(self, timeout=None, **kwargs):
def get_file(self, timeout=None, api_kwargs=None):
"""Convenience wrapper over :attr:`telegram.Bot.get_file`
Args:
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
the read timeout from the server (instead of the one specified during creation of
the connection pool).
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to be passed to the
Telegram API.
Returns:
:class:`telegram.File`
@ -99,4 +100,4 @@ class Document(TelegramObject):
:class:`telegram.TelegramError`
"""
return self.bot.get_file(self.file_id, timeout=timeout, **kwargs)
return self.bot.get_file(self.file_id, timeout=timeout, api_kwargs=api_kwargs)

View file

@ -85,14 +85,15 @@ class PhotoSize(TelegramObject):
return photos
def get_file(self, timeout=None, **kwargs):
def get_file(self, timeout=None, api_kwargs=None):
"""Convenience wrapper over :attr:`telegram.Bot.get_file`
Args:
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
the read timeout from the server (instead of the one specified during creation of
the connection pool).
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to be passed to the
Telegram API.
Returns:
:class:`telegram.File`
@ -101,4 +102,4 @@ class PhotoSize(TelegramObject):
:class:`telegram.TelegramError`
"""
return self.bot.get_file(self.file_id, timeout=timeout, **kwargs)
return self.bot.get_file(self.file_id, timeout=timeout, api_kwargs=api_kwargs)

View file

@ -112,14 +112,15 @@ class Sticker(TelegramObject):
return [cls.de_json(d, bot) for d in data]
def get_file(self, timeout=None, **kwargs):
def get_file(self, timeout=None, api_kwargs=None):
"""Convenience wrapper over :attr:`telegram.Bot.get_file`
Args:
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
the read timeout from the server (instead of the one specified during creation of
the connection pool).
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to be passed to the
Telegram API.
Returns:
:class:`telegram.File`
@ -128,7 +129,7 @@ class Sticker(TelegramObject):
:class:`telegram.TelegramError`
"""
return self.bot.get_file(self.file_id, timeout=timeout, **kwargs)
return self.bot.get_file(self.file_id, timeout=timeout, api_kwargs=api_kwargs)
class StickerSet(TelegramObject):

View file

@ -90,14 +90,15 @@ class Video(TelegramObject):
return cls(bot=bot, **data)
def get_file(self, timeout=None, **kwargs):
def get_file(self, timeout=None, api_kwargs=None):
"""Convenience wrapper over :attr:`telegram.Bot.get_file`
Args:
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
the read timeout from the server (instead of the one specified during creation of
the connection pool).
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to be passed to the
Telegram API.
Returns:
:class:`telegram.File`
@ -106,4 +107,4 @@ class Video(TelegramObject):
:class:`telegram.TelegramError`
"""
return self.bot.get_file(self.file_id, timeout=timeout, **kwargs)
return self.bot.get_file(self.file_id, timeout=timeout, api_kwargs=api_kwargs)

View file

@ -83,14 +83,15 @@ class VideoNote(TelegramObject):
return cls(bot=bot, **data)
def get_file(self, timeout=None, **kwargs):
def get_file(self, timeout=None, api_kwargs=None):
"""Convenience wrapper over :attr:`telegram.Bot.get_file`
Args:
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
the read timeout from the server (instead of the one specified during creation of
the connection pool).
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to be passed to the
Telegram API.
Returns:
:class:`telegram.File`
@ -99,4 +100,4 @@ class VideoNote(TelegramObject):
:class:`telegram.TelegramError`
"""
return self.bot.get_file(self.file_id, timeout=timeout, **kwargs)
return self.bot.get_file(self.file_id, timeout=timeout, api_kwargs=api_kwargs)

View file

@ -76,14 +76,15 @@ class Voice(TelegramObject):
return cls(bot=bot, **data)
def get_file(self, timeout=None, **kwargs):
def get_file(self, timeout=None, api_kwargs=None):
"""Convenience wrapper over :attr:`telegram.Bot.get_file`
Args:
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
the read timeout from the server (instead of the one specified during creation of
the connection pool).
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to be passed to the
Telegram API.
Returns:
:class:`telegram.File`
@ -92,4 +93,4 @@ class Voice(TelegramObject):
:class:`telegram.TelegramError`
"""
return self.bot.get_file(self.file_id, timeout=timeout, **kwargs)
return self.bot.get_file(self.file_id, timeout=timeout, api_kwargs=api_kwargs)

View file

@ -102,7 +102,7 @@ class PassportFile(TelegramObject):
return [cls.de_json_decrypted(passport_file, bot, credentials[i])
for i, passport_file in enumerate(data)]
def get_file(self, timeout=None, **kwargs):
def get_file(self, timeout=None, api_kwargs=None):
"""
Wrapper over :attr:`telegram.Bot.get_file`. Will automatically assign the correct
credentials to the returned :class:`telegram.File` if originating from
@ -112,7 +112,8 @@ class PassportFile(TelegramObject):
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
the read timeout from the server (instead of the one specified during creation of
the connection pool).
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to be passed to the
Telegram API.
Returns:
:class:`telegram.File`
@ -121,6 +122,6 @@ class PassportFile(TelegramObject):
:class:`telegram.TelegramError`
"""
file = self.bot.get_file(self.file_id, timeout=timeout, **kwargs)
file = self.bot.get_file(self.file_id, timeout=timeout, api_kwargs=api_kwargs)
file.set_credentials(self._credentials)
return file

View file

@ -255,14 +255,15 @@ class Request:
else:
raise NetworkError('{} ({})'.format(message, resp.status))
def get(self, url, timeout=None):
def post(self, url, data=None, timeout=None):
"""Request an URL.
Args:
url (:obj:`str`): The web location we want to retrieve.
timeout (:obj:`int` | :obj:`float`): If this value is specified, use it as the read
timeout from the server (instead of the one specified during creation of the
connection pool).
data (dict[str, str|int], optional): A dict of key/value pairs.
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
the read timeout from the server (instead of the one specified during creation of
the connection pool).
Returns:
A JSON object.
@ -273,27 +274,8 @@ class Request:
if timeout is not None:
urlopen_kwargs['timeout'] = Timeout(read=timeout, connect=self._connect_timeout)
result = self._request_wrapper('GET', url, **urlopen_kwargs)
return self._parse(result)
def post(self, url, data, timeout=None):
"""Request an URL.
Args:
url (:obj:`str`): The web location we want to retrieve.
data (dict[str, str|int]): A dict of key/value pairs.
timeout (:obj:`int` | :obj:`float`): If this value is specified, use it as the read
timeout from the server (instead of the one specified during creation of the
connection pool).
Returns:
A JSON object.
"""
urlopen_kwargs = {}
if timeout is not None:
urlopen_kwargs['timeout'] = Timeout(read=timeout, connect=self._connect_timeout)
if data is None:
data = {}
# Are we uploading files?
files = False

View file

@ -72,7 +72,7 @@ class TestAnimation:
message = bot.send_animation(chat_id, animation_file, duration=self.duration,
width=self.width, height=self.height, caption=self.caption,
parse_mode='Markdown', disable_notification=False,
filename=self.file_name, thumb=thumb_file)
thumb=thumb_file)
assert isinstance(message.animation, Animation)
assert isinstance(message.animation.file_id, str)
@ -158,10 +158,10 @@ class TestAnimation:
assert message.animation == animation
def test_send_with_animation(self, monkeypatch, bot, chat_id, animation):
def test(_, url, data, **kwargs):
def test(url, data, **kwargs):
return data['animation'] == animation.file_id
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
message = bot.send_animation(animation=animation, chat_id=chat_id)
assert message

View file

@ -135,10 +135,10 @@ class TestAudio:
assert message.audio == audio
def test_send_with_audio(self, monkeypatch, bot, chat_id, audio):
def test(_, url, data, **kwargs):
def test(url, data, **kwargs):
return data['audio'] == audio.file_id
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
message = bot.send_audio(audio=audio, chat_id=chat_id)
assert message

View file

@ -91,6 +91,14 @@ class TestBot:
with pytest.raises(InvalidToken):
bot.get_me()
def test_unknown_kwargs(self, bot, monkeypatch):
def post(url, data, timeout):
assert data['unknown_kwarg_1'] == 7
assert data['unknown_kwarg_2'] == 5
monkeypatch.setattr(bot.request, 'post', post)
bot.send_message(123, 'text', api_kwargs={'unknown_kwarg_1': 7, 'unknown_kwarg_2': 5})
@flaky(3, 1)
@pytest.mark.timeout(10)
def test_get_me_and_properties(self, bot):
@ -317,7 +325,7 @@ class TestBot:
# TODO: Needs improvement. We need incoming inline query to test answer.
def test_answer_inline_query(self, monkeypatch, bot):
# For now just test that our internals pass the correct data
def test(_, url, data, *args, **kwargs):
def test(url, data, *args, **kwargs):
return data == {'cache_time': 300,
'results': [{'title': 'first', 'id': '11', 'type': 'article',
'input_message_content': {'message_text': 'first'}},
@ -327,7 +335,7 @@ class TestBot:
'inline_query_id': 1234, 'is_personal': True,
'switch_pm_text': 'switch pm'}
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
results = [InlineQueryResultArticle('11', 'first', InputTextMessageContent('first')),
InlineQueryResultArticle('12', 'second', InputTextMessageContent('second'))]
@ -340,7 +348,7 @@ class TestBot:
switch_pm_parameter='start_pm')
def test_answer_inline_query_no_default_parse_mode(self, monkeypatch, bot):
def test(_, url, data, *args, **kwargs):
def test(url, data, *args, **kwargs):
return data == {'cache_time': 300,
'results': [{'title': 'test_result', 'id': '123', 'type': 'document',
'document_url': 'https://raw.githubusercontent.com/'
@ -351,7 +359,7 @@ class TestBot:
'inline_query_id': 1234, 'is_personal': True,
'switch_pm_text': 'switch pm'}
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
results = [InlineQueryResultDocument(
id='123',
document_url='https://raw.githubusercontent.com/python-telegram-bot/logos/master/'
@ -371,7 +379,7 @@ class TestBot:
@pytest.mark.parametrize('default_bot', [{'parse_mode': 'Markdown'}], indirect=True)
def test_answer_inline_query_default_parse_mode(self, monkeypatch, default_bot):
def test(_, url, data, *args, **kwargs):
def test(url, data, *args, **kwargs):
return data == {'cache_time': 300,
'results': [{'title': 'test_result', 'id': '123', 'type': 'document',
'document_url': 'https://raw.githubusercontent.com/'
@ -382,7 +390,7 @@ class TestBot:
'inline_query_id': 1234, 'is_personal': True,
'switch_pm_text': 'switch pm'}
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(default_bot.request, 'post', test)
results = [InlineQueryResultDocument(
id='123',
document_url='https://raw.githubusercontent.com/python-telegram-bot/logos/master/'
@ -420,63 +428,63 @@ class TestBot:
id_offset,
expected_next_offset):
# For now just test that our internals pass the correct data
def make_assertion(_, url, data, *args, **kwargs):
def make_assertion(url, data, *args, **kwargs):
results = data['results']
length_matches = len(results) == num_results
ids_match = all([int(res['id']) == id_offset + i for i, res in enumerate(results)])
next_offset_matches = data['next_offset'] == expected_next_offset
return length_matches and ids_match and next_offset_matches
monkeypatch.setattr('telegram.utils.request.Request.post', make_assertion)
monkeypatch.setattr(bot.request, 'post', make_assertion)
assert bot.answer_inline_query(1234, results=inline_results, current_offset=current_offset)
def test_answer_inline_query_current_offset_2(self, monkeypatch, bot, inline_results):
# For now just test that our internals pass the correct data
def make_assertion(_, url, data, *args, **kwargs):
def make_assertion(url, data, *args, **kwargs):
results = data['results']
length_matches = len(results) == MAX_INLINE_QUERY_RESULTS
ids_match = all([int(res['id']) == 1 + i for i, res in enumerate(results)])
next_offset_matches = data['next_offset'] == 1
return length_matches and ids_match and next_offset_matches
monkeypatch.setattr('telegram.utils.request.Request.post', make_assertion)
monkeypatch.setattr(bot.request, 'post', make_assertion)
assert bot.answer_inline_query(1234, results=inline_results, current_offset=0)
inline_results = inline_results[:30]
def make_assertion(_, url, data, *args, **kwargs):
def make_assertion(url, data, *args, **kwargs):
results = data['results']
length_matches = len(results) == 30
ids_match = all([int(res['id']) == 1 + i for i, res in enumerate(results)])
next_offset_matches = data['next_offset'] == ''
return length_matches and ids_match and next_offset_matches
monkeypatch.setattr('telegram.utils.request.Request.post', make_assertion)
monkeypatch.setattr(bot.request, 'post', make_assertion)
assert bot.answer_inline_query(1234, results=inline_results, current_offset=0)
def test_answer_inline_query_current_offset_callback(self, monkeypatch, bot, caplog):
# For now just test that our internals pass the correct data
def test(_, url, data, *args, **kwargs):
def make_assertion(url, data, *args, **kwargs):
results = data['results']
length = len(results) == 5
ids = all([int(res['id']) == 6 + i for i, res in enumerate(results)])
next_offset = data['next_offset'] == 2
return length and ids and next_offset
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', make_assertion)
assert bot.answer_inline_query(1234, results=inline_results_callback, current_offset=1)
def test(_, url, data, *args, **kwargs):
def make_assertion(url, data, *args, **kwargs):
results = data['results']
length = results == []
next_offset = data['next_offset'] == ''
return length and next_offset
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', make_assertion)
assert bot.answer_inline_query(1234, results=inline_results_callback, current_offset=6)
@ -497,13 +505,13 @@ class TestBot:
# TODO: Needs improvement. No feasable way to test until bots can add members.
def test_kick_chat_member(self, monkeypatch, bot):
def test(_, url, data, *args, **kwargs):
def test(url, data, *args, **kwargs):
chat_id = data['chat_id'] == 2
user_id = data['user_id'] == 32
until_date = data.get('until_date', 1577887200) == 1577887200
return chat_id and user_id and until_date
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
until = from_timestamp(1577887200)
assert bot.kick_chat_member(2, 32)
@ -512,43 +520,43 @@ class TestBot:
# TODO: Needs improvement.
def test_unban_chat_member(self, monkeypatch, bot):
def test(_, url, data, *args, **kwargs):
def test(url, data, *args, **kwargs):
chat_id = data['chat_id'] == 2
user_id = data['user_id'] == 32
return chat_id and user_id
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
assert bot.unban_chat_member(2, 32)
def test_set_chat_permissions(self, monkeypatch, bot, chat_permissions):
def test(_, url, data, *args, **kwargs):
def test(url, data, *args, **kwargs):
chat_id = data['chat_id'] == 2
permissions = data['permissions'] == chat_permissions.to_dict()
return chat_id and permissions
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
assert bot.set_chat_permissions(2, chat_permissions)
def test_set_chat_administrator_custom_title(self, monkeypatch, bot):
def test(_, url, data, *args, **kwargs):
def test(url, data, *args, **kwargs):
chat_id = data['chat_id'] == 2
user_id = data['user_id'] == 32
custom_title = data['custom_title'] == 'custom_title'
return chat_id and user_id and custom_title
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
assert bot.set_chat_administrator_custom_title(2, 32, 'custom_title')
# TODO: Needs improvement. Need an incoming callbackquery to test
def test_answer_callback_query(self, monkeypatch, bot):
# For now just test that our internals pass the correct data
def test(_, url, data, *args, **kwargs):
def test(url, data, *args, **kwargs):
return data == {'callback_query_id': 23, 'show_alert': True, 'url': 'no_url',
'cache_time': 1, 'text': 'answer'}
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
assert bot.answer_callback_query(23, text='answer', show_alert=True, url='no_url',
cache_time=1)
@ -888,23 +896,23 @@ class TestBot:
# TODO: Needs improvement. Need incoming shippping queries to test
def test_answer_shipping_query_ok(self, monkeypatch, bot):
# For now just test that our internals pass the correct data
def test(_, url, data, *args, **kwargs):
def test(url, data, *args, **kwargs):
return data == {'shipping_query_id': 1, 'ok': True,
'shipping_options': [{'title': 'option1',
'prices': [{'label': 'price', 'amount': 100}],
'id': 1}]}
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
shipping_options = ShippingOption(1, 'option1', [LabeledPrice('price', 100)])
assert bot.answer_shipping_query(1, True, shipping_options=[shipping_options])
def test_answer_shipping_query_error_message(self, monkeypatch, bot):
# For now just test that our internals pass the correct data
def test(_, url, data, *args, **kwargs):
def test(url, data, *args, **kwargs):
return data == {'shipping_query_id': 1, 'error_message': 'Not enough fish',
'ok': False}
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
assert bot.answer_shipping_query(1, False, error_message='Not enough fish')
def test_answer_shipping_query_errors(self, monkeypatch, bot):
@ -925,19 +933,19 @@ class TestBot:
# TODO: Needs improvement. Need incoming pre checkout queries to test
def test_answer_pre_checkout_query_ok(self, monkeypatch, bot):
# For now just test that our internals pass the correct data
def test(_, url, data, *args, **kwargs):
def test(url, data, *args, **kwargs):
return data == {'pre_checkout_query_id': 1, 'ok': True}
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
assert bot.answer_pre_checkout_query(1, True)
def test_answer_pre_checkout_query_error_message(self, monkeypatch, bot):
# For now just test that our internals pass the correct data
def test(_, url, data, *args, **kwargs):
def test(url, data, *args, **kwargs):
return data == {'pre_checkout_query_id': 1, 'error_message': 'Not enough fish',
'ok': False}
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
assert bot.answer_pre_checkout_query(1, False, error_message='Not enough fish')
def test_answer_pre_checkout_query_errors(self, monkeypatch, bot):

View file

@ -77,10 +77,10 @@ class TestChatPhoto:
assert os.path.isfile('telegram.jpg')
def test_send_with_chat_photo(self, monkeypatch, bot, super_group_id, chat_photo):
def test(_, url, data, **kwargs):
def test(url, data, **kwargs):
return data['photo'] == chat_photo
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
message = bot.set_chat_photo(photo=chat_photo, chat_id=super_group_id)
assert message

View file

@ -52,13 +52,13 @@ class TestContact:
assert contact.user_id == self.user_id
def test_send_with_contact(self, monkeypatch, bot, chat_id, contact):
def test(_, url, data, **kwargs):
def test(url, data, **kwargs):
phone = data['phone_number'] == contact.phone_number
first = data['first_name'] == contact.first_name
last = data['last_name'] == contact.last_name
return phone and first and last
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
message = bot.send_contact(contact=contact, chat_id=chat_id)
assert message

View file

@ -124,10 +124,10 @@ class TestDocument:
assert message.document == document
def test_send_with_document(self, monkeypatch, bot, chat_id, document):
def test(_, url, data, **kwargs):
def test(url, data, **kwargs):
return data['document'] == document.file_id
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
message = bot.send_document(document=document, chat_id=chat_id)

View file

@ -111,11 +111,11 @@ class TestInvoice:
assert message.invoice.total_amount == self.total_amount
def test_send_object_as_provider_data(self, monkeypatch, bot, chat_id, provider_token):
def test(_, url, data, **kwargs):
def test(url, data, **kwargs):
return (data['provider_data'] == '{"test_data": 123456789}' # Depends if using
or data['provider_data'] == '{"test_data":123456789}') # ujson or not
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
assert bot.send_invoice(chat_id, self.title, self.description, self.payload,
provider_token, self.start_parameter, self.currency,

View file

@ -64,40 +64,40 @@ class TestLocation:
# TODO: Needs improvement with in inline sent live location.
def test_edit_live_inline_message(self, monkeypatch, bot, location):
def test(_, url, data, **kwargs):
def test(url, data, **kwargs):
lat = data['latitude'] == location.latitude
lon = data['longitude'] == location.longitude
id_ = data['inline_message_id'] == 1234
return lat and lon and id_
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
assert bot.edit_message_live_location(inline_message_id=1234, location=location)
# TODO: Needs improvement with in inline sent live location.
def test_stop_live_inline_message(self, monkeypatch, bot):
def test(_, url, data, **kwargs):
def test(url, data, **kwargs):
id_ = data['inline_message_id'] == 1234
return id_
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
assert bot.stop_message_live_location(inline_message_id=1234)
def test_send_with_location(self, monkeypatch, bot, chat_id, location):
def test(_, url, data, **kwargs):
def test(url, data, **kwargs):
lat = data['latitude'] == location.latitude
lon = data['longitude'] == location.longitude
return lat and lon
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
assert bot.send_location(location=location, chat_id=chat_id)
def test_edit_live_location_with_location(self, monkeypatch, bot, location):
def test(_, url, data, **kwargs):
def test(url, data, **kwargs):
lat = data['latitude'] == location.latitude
lon = data['longitude'] == location.longitude
return lat and lon
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
assert bot.edit_message_live_location(None, None, location=location)
def test_send_location_without_required(self, bot, chat_id):

View file

@ -27,7 +27,8 @@ from telegram.vendor.ptb_urllib3 import urllib3
import telegram
IGNORED_OBJECTS = ('ResponseParameters', 'CallbackGame')
IGNORED_PARAMETERS = {'self', 'args', 'kwargs', 'read_latency', 'network_delay', 'timeout', 'bot'}
IGNORED_PARAMETERS = {'self', 'args', 'kwargs', 'read_latency', 'network_delay', 'timeout', 'bot',
'api_kwargs'}
def find_next_sibling_until(tag, name, until):

View file

@ -349,7 +349,7 @@ class TestPassport:
assert file._credentials.secret == self.driver_license_selfie_credentials_secret
def test_mocked_set_passport_data_errors(self, monkeypatch, bot, chat_id, passport_data):
def test(_, url, data, **kwargs):
def test(url, data, **kwargs):
return (data['user_id'] == chat_id
and data['errors'][0]['file_hash'] == (passport_data.decrypted_credentials
.secure_data.driver_license
@ -358,7 +358,7 @@ class TestPassport:
.secure_data.driver_license
.data.data_hash))
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
message = bot.set_passport_data_errors(chat_id, [
PassportElementErrorSelfie('driver_license',
(passport_data.decrypted_credentials

View file

@ -304,10 +304,10 @@ class TestPhoto:
assert photo.file_size == 33372
def test_send_with_photosize(self, monkeypatch, bot, chat_id, photo):
def test(_, url, data, **kwargs):
def test(url, data, **kwargs):
return data['photo'] == photo.file_id
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
message = bot.send_photo(photo=photo, chat_id=chat_id)
assert message

View file

@ -194,10 +194,10 @@ class TestSticker:
assert json_sticker.thumb == sticker.thumb
def test_send_with_sticker(self, monkeypatch, bot, chat_id, sticker):
def test(_, url, data, **kwargs):
def test(url, data, **kwargs):
return data['sticker'] == sticker.file_id
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
message = bot.send_sticker(sticker=sticker, chat_id=chat_id)
assert message

View file

@ -55,7 +55,7 @@ class TestVenue:
assert venue.foursquare_type == self.foursquare_type
def test_send_with_venue(self, monkeypatch, bot, chat_id, venue):
def test(_, url, data, **kwargs):
def test(url, data, **kwargs):
return (data['longitude'] == self.location.longitude
and data['latitude'] == self.location.latitude
and data['title'] == self.title
@ -63,7 +63,7 @@ class TestVenue:
and data['foursquare_id'] == self.foursquare_id
and data['foursquare_type'] == self.foursquare_type)
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
message = bot.send_venue(chat_id, venue=venue)
assert message

View file

@ -149,10 +149,10 @@ class TestVideo:
assert message.video == video
def test_send_with_video(self, monkeypatch, bot, chat_id, video):
def test(_, url, data, **kwargs):
def test(url, data, **kwargs):
return data['video'] == video.file_id
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
message = bot.send_video(chat_id, video=video)
assert message

View file

@ -111,10 +111,10 @@ class TestVideoNote:
assert message.video_note == video_note
def test_send_with_video_note(self, monkeypatch, bot, chat_id, video_note):
def test(_, url, data, **kwargs):
def test(url, data, **kwargs):
return data['video_note'] == video_note.file_id
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
message = bot.send_video_note(chat_id, video_note=video_note)
assert message

View file

@ -115,10 +115,10 @@ class TestVoice:
assert message.voice == voice
def test_send_with_voice(self, monkeypatch, bot, chat_id, voice):
def test(_, url, data, **kwargs):
def test(url, data, **kwargs):
return data['voice'] == voice.file_id
monkeypatch.setattr('telegram.utils.request.Request.post', test)
monkeypatch.setattr(bot.request, 'post', test)
message = bot.send_voice(chat_id, voice=voice)
assert message