2017-08-11 23:58:41 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2024-02-19 20:06:25 +01:00
|
|
|
# Copyright (C) 2015-2024
|
2017-08-11 23:58:41 +02:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
|
|
|
|
2022-11-13 21:28:41 +01:00
|
|
|
from datetime import datetime
|
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
import pytest
|
|
|
|
|
2024-02-08 17:12:00 +01:00
|
|
|
from telegram import Audio, Bot, CallbackQuery, Chat, InaccessibleMessage, Message, User
|
2022-12-12 10:51:33 +01:00
|
|
|
from tests.auxil.bot_method_checks import (
|
|
|
|
check_defaults_handling,
|
|
|
|
check_shortcut_call,
|
|
|
|
check_shortcut_signature,
|
|
|
|
)
|
2023-02-22 20:19:46 +01:00
|
|
|
from tests.auxil.slots import mro_slots
|
2017-08-11 23:58:41 +02:00
|
|
|
|
|
|
|
|
2024-02-08 17:12:00 +01:00
|
|
|
@pytest.fixture(params=["message", "inline", "inaccessible_message"])
|
2017-08-11 23:58:41 +02:00
|
|
|
def callback_query(bot, request):
|
2020-02-23 22:03:58 +01:00
|
|
|
cbq = CallbackQuery(
|
2023-02-11 10:45:17 +01:00
|
|
|
TestCallbackQueryBase.id_,
|
|
|
|
TestCallbackQueryBase.from_user,
|
|
|
|
TestCallbackQueryBase.chat_instance,
|
|
|
|
data=TestCallbackQueryBase.data,
|
|
|
|
game_short_name=TestCallbackQueryBase.game_short_name,
|
2017-08-11 23:58:41 +02:00
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
cbq.set_bot(bot)
|
2022-12-15 15:00:36 +01:00
|
|
|
cbq._unfreeze()
|
2017-08-11 23:58:41 +02:00
|
|
|
if request.param == "message":
|
2023-02-11 10:45:17 +01:00
|
|
|
cbq.message = TestCallbackQueryBase.message
|
2021-10-21 11:17:12 +02:00
|
|
|
cbq.message.set_bot(bot)
|
2024-02-08 17:12:00 +01:00
|
|
|
elif request.param == "inline":
|
2023-02-11 10:45:17 +01:00
|
|
|
cbq.inline_message_id = TestCallbackQueryBase.inline_message_id
|
2024-02-08 17:12:00 +01:00
|
|
|
elif request.param == "inaccessible_message":
|
|
|
|
cbq.message = InaccessibleMessage(
|
|
|
|
chat=TestCallbackQueryBase.message.chat,
|
|
|
|
message_id=TestCallbackQueryBase.message.message_id,
|
|
|
|
)
|
2017-08-11 23:58:41 +02:00
|
|
|
return cbq
|
|
|
|
|
|
|
|
|
2023-02-11 10:45:17 +01:00
|
|
|
class TestCallbackQueryBase:
|
2020-02-23 22:03:58 +01:00
|
|
|
id_ = "id"
|
2017-09-01 08:40:05 +02:00
|
|
|
from_user = User(1, "test_user", False)
|
2017-08-11 23:58:41 +02:00
|
|
|
chat_instance = "chat_instance"
|
2022-11-13 21:28:41 +01:00
|
|
|
message = Message(3, datetime.utcnow(), Chat(4, "private"), from_user=User(5, "bot", False))
|
2017-08-11 23:58:41 +02:00
|
|
|
data = "data"
|
|
|
|
inline_message_id = "inline_message_id"
|
|
|
|
game_short_name = "the_game"
|
|
|
|
|
2023-02-11 10:45:17 +01:00
|
|
|
|
|
|
|
class TestCallbackQueryWithoutRequest(TestCallbackQueryBase):
|
2021-02-19 19:07:48 +01:00
|
|
|
@staticmethod
|
|
|
|
def skip_params(callback_query: CallbackQuery):
|
|
|
|
if callback_query.inline_message_id:
|
2024-07-01 21:45:37 +02:00
|
|
|
return {"message_id", "chat_id", "business_connection_id"}
|
|
|
|
return {"inline_message_id", "business_connection_id"}
|
2021-02-19 19:07:48 +01:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def shortcut_kwargs(callback_query: CallbackQuery):
|
|
|
|
if not callback_query.inline_message_id:
|
|
|
|
return {"message_id", "chat_id"}
|
|
|
|
return {"inline_message_id"}
|
|
|
|
|
2020-12-30 13:41:07 +01:00
|
|
|
@staticmethod
|
|
|
|
def check_passed_ids(callback_query: CallbackQuery, kwargs):
|
|
|
|
if callback_query.inline_message_id:
|
|
|
|
id_ = kwargs["inline_message_id"] == callback_query.inline_message_id
|
|
|
|
chat_id = kwargs["chat_id"] is None
|
|
|
|
message_id = kwargs["message_id"] is None
|
|
|
|
else:
|
|
|
|
id_ = kwargs["inline_message_id"] is None
|
|
|
|
chat_id = kwargs["chat_id"] == callback_query.message.chat_id
|
|
|
|
message_id = kwargs["message_id"] == callback_query.message.message_id
|
|
|
|
return id_ and chat_id and message_id
|
|
|
|
|
2023-02-22 20:19:46 +01:00
|
|
|
def test_slot_behaviour(self, callback_query):
|
2022-04-24 12:38:09 +02:00
|
|
|
for attr in callback_query.__slots__:
|
|
|
|
assert getattr(callback_query, attr, "err") != "err", f"got extra slot '{attr}'"
|
|
|
|
assert len(mro_slots(callback_query)) == len(set(mro_slots(callback_query))), "same slot"
|
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
def test_de_json(self, bot):
|
2020-02-23 22:03:58 +01:00
|
|
|
json_dict = {
|
|
|
|
"id": self.id_,
|
2017-08-11 23:58:41 +02:00
|
|
|
"from": self.from_user.to_dict(),
|
|
|
|
"chat_instance": self.chat_instance,
|
|
|
|
"message": self.message.to_dict(),
|
|
|
|
"data": self.data,
|
|
|
|
"inline_message_id": self.inline_message_id,
|
2020-07-19 17:47:26 +02:00
|
|
|
"game_short_name": self.game_short_name,
|
|
|
|
}
|
2017-08-11 23:58:41 +02:00
|
|
|
callback_query = CallbackQuery.de_json(json_dict, bot)
|
2022-10-07 11:51:53 +02:00
|
|
|
assert callback_query.api_kwargs == {}
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2020-02-23 22:03:58 +01:00
|
|
|
assert callback_query.id == self.id_
|
2017-08-11 23:58:41 +02:00
|
|
|
assert callback_query.from_user == self.from_user
|
|
|
|
assert callback_query.chat_instance == self.chat_instance
|
|
|
|
assert callback_query.message == self.message
|
|
|
|
assert callback_query.data == self.data
|
|
|
|
assert callback_query.inline_message_id == self.inline_message_id
|
|
|
|
assert callback_query.game_short_name == self.game_short_name
|
|
|
|
|
|
|
|
def test_to_dict(self, callback_query):
|
|
|
|
callback_query_dict = callback_query.to_dict()
|
|
|
|
|
|
|
|
assert isinstance(callback_query_dict, dict)
|
|
|
|
assert callback_query_dict["id"] == callback_query.id
|
|
|
|
assert callback_query_dict["from"] == callback_query.from_user.to_dict()
|
|
|
|
assert callback_query_dict["chat_instance"] == callback_query.chat_instance
|
2024-02-08 17:12:00 +01:00
|
|
|
if callback_query.message is not None:
|
2017-08-11 23:58:41 +02:00
|
|
|
assert callback_query_dict["message"] == callback_query.message.to_dict()
|
2024-02-08 17:12:00 +01:00
|
|
|
elif callback_query.inline_message_id:
|
2017-08-11 23:58:41 +02:00
|
|
|
assert callback_query_dict["inline_message_id"] == callback_query.inline_message_id
|
|
|
|
assert callback_query_dict["data"] == callback_query.data
|
|
|
|
assert callback_query_dict["game_short_name"] == callback_query.game_short_name
|
|
|
|
|
2023-02-11 10:45:17 +01:00
|
|
|
def test_equality(self):
|
|
|
|
a = CallbackQuery(self.id_, self.from_user, "chat")
|
|
|
|
b = CallbackQuery(self.id_, self.from_user, "chat")
|
|
|
|
c = CallbackQuery(self.id_, None, "")
|
|
|
|
d = CallbackQuery("", None, "chat")
|
|
|
|
e = Audio(self.id_, "unique_id", 1)
|
|
|
|
|
|
|
|
assert a == b
|
|
|
|
assert hash(a) == hash(b)
|
|
|
|
assert a is not b
|
|
|
|
|
|
|
|
assert a == c
|
|
|
|
assert hash(a) == hash(c)
|
|
|
|
|
|
|
|
assert a != d
|
|
|
|
assert hash(a) != hash(d)
|
|
|
|
|
|
|
|
assert a != e
|
|
|
|
assert hash(a) != hash(e)
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_answer(self, monkeypatch, callback_query):
|
|
|
|
async def make_assertion(*_, **kwargs):
|
2021-02-19 19:07:48 +01:00
|
|
|
return kwargs["callback_query_id"] == callback_query.id
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2020-12-30 13:41:07 +01:00
|
|
|
assert check_shortcut_signature(
|
|
|
|
CallbackQuery.answer, Bot.answer_callback_query, ["callback_query_id"], []
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_shortcut_call(
|
2021-10-21 11:17:12 +02:00
|
|
|
callback_query.answer, callback_query.get_bot(), "answer_callback_query"
|
2021-02-19 19:07:48 +01:00
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_defaults_handling(callback_query.answer, callback_query.get_bot())
|
2020-12-30 13:41:07 +01:00
|
|
|
|
2021-10-21 11:17:12 +02:00
|
|
|
monkeypatch.setattr(callback_query.get_bot(), "answer_callback_query", make_assertion)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await callback_query.answer()
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_edit_message_text(self, monkeypatch, callback_query):
|
2024-02-08 17:12:00 +01:00
|
|
|
if isinstance(callback_query.message, InaccessibleMessage):
|
|
|
|
with pytest.raises(TypeError, match="inaccessible message"):
|
|
|
|
await callback_query.edit_message_text("test")
|
|
|
|
return
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def make_assertion(*_, **kwargs):
|
2020-12-30 13:41:07 +01:00
|
|
|
text = kwargs["text"] == "test"
|
|
|
|
ids = self.check_passed_ids(callback_query, kwargs)
|
2021-02-19 19:07:48 +01:00
|
|
|
return ids and text
|
2020-12-30 13:41:07 +01:00
|
|
|
|
|
|
|
assert check_shortcut_signature(
|
|
|
|
CallbackQuery.edit_message_text,
|
|
|
|
Bot.edit_message_text,
|
2024-07-01 21:45:37 +02:00
|
|
|
["inline_message_id", "message_id", "chat_id", "business_connection_id"],
|
2020-12-30 13:41:07 +01:00
|
|
|
[],
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_shortcut_call(
|
2021-02-19 19:07:48 +01:00
|
|
|
callback_query.edit_message_text,
|
2021-10-21 11:17:12 +02:00
|
|
|
callback_query.get_bot(),
|
2021-02-19 19:07:48 +01:00
|
|
|
"edit_message_text",
|
|
|
|
skip_params=self.skip_params(callback_query),
|
|
|
|
shortcut_kwargs=self.shortcut_kwargs(callback_query),
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_defaults_handling(
|
|
|
|
callback_query.edit_message_text, callback_query.get_bot()
|
|
|
|
)
|
2020-12-30 13:41:07 +01:00
|
|
|
|
2021-10-21 11:17:12 +02:00
|
|
|
monkeypatch.setattr(callback_query.get_bot(), "edit_message_text", make_assertion)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await callback_query.edit_message_text(text="test")
|
|
|
|
assert await callback_query.edit_message_text("test")
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_edit_message_caption(self, monkeypatch, callback_query):
|
2024-02-08 17:12:00 +01:00
|
|
|
if isinstance(callback_query.message, InaccessibleMessage):
|
|
|
|
with pytest.raises(TypeError, match="inaccessible message"):
|
|
|
|
await callback_query.edit_message_caption("test")
|
|
|
|
return
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def make_assertion(*_, **kwargs):
|
2019-02-13 11:37:13 +01:00
|
|
|
caption = kwargs["caption"] == "new caption"
|
2020-12-30 13:41:07 +01:00
|
|
|
ids = self.check_passed_ids(callback_query, kwargs)
|
2021-02-19 19:07:48 +01:00
|
|
|
return ids and caption
|
2020-12-30 13:41:07 +01:00
|
|
|
|
|
|
|
assert check_shortcut_signature(
|
|
|
|
CallbackQuery.edit_message_caption,
|
|
|
|
Bot.edit_message_caption,
|
2024-07-01 21:45:37 +02:00
|
|
|
["inline_message_id", "message_id", "chat_id", "business_connection_id"],
|
2020-12-30 13:41:07 +01:00
|
|
|
[],
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_shortcut_call(
|
2021-02-19 19:07:48 +01:00
|
|
|
callback_query.edit_message_caption,
|
2021-10-21 11:17:12 +02:00
|
|
|
callback_query.get_bot(),
|
2021-02-19 19:07:48 +01:00
|
|
|
"edit_message_caption",
|
|
|
|
skip_params=self.skip_params(callback_query),
|
|
|
|
shortcut_kwargs=self.shortcut_kwargs(callback_query),
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_defaults_handling(
|
2021-10-21 11:17:12 +02:00
|
|
|
callback_query.edit_message_caption, callback_query.get_bot()
|
|
|
|
)
|
2020-12-30 13:41:07 +01:00
|
|
|
|
2021-10-21 11:17:12 +02:00
|
|
|
monkeypatch.setattr(callback_query.get_bot(), "edit_message_caption", make_assertion)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await callback_query.edit_message_caption(caption="new caption")
|
|
|
|
assert await callback_query.edit_message_caption("new caption")
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_edit_message_reply_markup(self, monkeypatch, callback_query):
|
2024-02-08 17:12:00 +01:00
|
|
|
if isinstance(callback_query.message, InaccessibleMessage):
|
|
|
|
with pytest.raises(TypeError, match="inaccessible message"):
|
|
|
|
await callback_query.edit_message_reply_markup("test")
|
|
|
|
return
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def make_assertion(*_, **kwargs):
|
2019-02-13 11:37:13 +01:00
|
|
|
reply_markup = kwargs["reply_markup"] == [["1", "2"]]
|
2020-12-30 13:41:07 +01:00
|
|
|
ids = self.check_passed_ids(callback_query, kwargs)
|
2021-02-19 19:07:48 +01:00
|
|
|
return ids and reply_markup
|
2020-12-30 13:41:07 +01:00
|
|
|
|
|
|
|
assert check_shortcut_signature(
|
|
|
|
CallbackQuery.edit_message_reply_markup,
|
|
|
|
Bot.edit_message_reply_markup,
|
2024-07-01 21:45:37 +02:00
|
|
|
["inline_message_id", "message_id", "chat_id", "business_connection_id"],
|
2020-12-30 13:41:07 +01:00
|
|
|
[],
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_shortcut_call(
|
2021-02-19 19:07:48 +01:00
|
|
|
callback_query.edit_message_reply_markup,
|
2021-10-21 11:17:12 +02:00
|
|
|
callback_query.get_bot(),
|
2021-02-19 19:07:48 +01:00
|
|
|
"edit_message_reply_markup",
|
|
|
|
skip_params=self.skip_params(callback_query),
|
|
|
|
shortcut_kwargs=self.shortcut_kwargs(callback_query),
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_defaults_handling(
|
2021-10-21 11:17:12 +02:00
|
|
|
callback_query.edit_message_reply_markup, callback_query.get_bot()
|
2021-02-19 19:07:48 +01:00
|
|
|
)
|
2020-12-30 13:41:07 +01:00
|
|
|
|
2021-10-21 11:17:12 +02:00
|
|
|
monkeypatch.setattr(callback_query.get_bot(), "edit_message_reply_markup", make_assertion)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await callback_query.edit_message_reply_markup(reply_markup=[["1", "2"]])
|
|
|
|
assert await callback_query.edit_message_reply_markup([["1", "2"]])
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_edit_message_media(self, monkeypatch, callback_query):
|
2024-02-08 17:12:00 +01:00
|
|
|
if isinstance(callback_query.message, InaccessibleMessage):
|
|
|
|
with pytest.raises(TypeError, match="inaccessible message"):
|
|
|
|
await callback_query.edit_message_media("test")
|
|
|
|
return
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def make_assertion(*_, **kwargs):
|
2020-12-30 13:41:07 +01:00
|
|
|
message_media = kwargs.get("media") == [["1", "2"]]
|
|
|
|
ids = self.check_passed_ids(callback_query, kwargs)
|
2021-02-19 19:07:48 +01:00
|
|
|
return ids and message_media
|
2020-12-30 13:41:07 +01:00
|
|
|
|
|
|
|
assert check_shortcut_signature(
|
|
|
|
CallbackQuery.edit_message_media,
|
|
|
|
Bot.edit_message_media,
|
2024-07-01 21:45:37 +02:00
|
|
|
["inline_message_id", "message_id", "chat_id", "business_connection_id"],
|
2020-12-30 13:41:07 +01:00
|
|
|
[],
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_shortcut_call(
|
2021-02-19 19:07:48 +01:00
|
|
|
callback_query.edit_message_media,
|
2021-10-21 11:17:12 +02:00
|
|
|
callback_query.get_bot(),
|
2021-02-19 19:07:48 +01:00
|
|
|
"edit_message_media",
|
|
|
|
skip_params=self.skip_params(callback_query),
|
|
|
|
shortcut_kwargs=self.shortcut_kwargs(callback_query),
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_defaults_handling(
|
|
|
|
callback_query.edit_message_media, callback_query.get_bot()
|
|
|
|
)
|
2020-12-30 13:41:07 +01:00
|
|
|
|
2021-10-21 11:17:12 +02:00
|
|
|
monkeypatch.setattr(callback_query.get_bot(), "edit_message_media", make_assertion)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await callback_query.edit_message_media(media=[["1", "2"]])
|
|
|
|
assert await callback_query.edit_message_media([["1", "2"]])
|
2020-08-13 13:39:43 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_edit_message_live_location(self, monkeypatch, callback_query):
|
2024-02-08 17:12:00 +01:00
|
|
|
if isinstance(callback_query.message, InaccessibleMessage):
|
|
|
|
with pytest.raises(TypeError, match="inaccessible message"):
|
|
|
|
await callback_query.edit_message_live_location("test")
|
|
|
|
return
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def make_assertion(*_, **kwargs):
|
2020-12-30 13:41:07 +01:00
|
|
|
latitude = kwargs.get("latitude") == 1
|
|
|
|
longitude = kwargs.get("longitude") == 2
|
2024-05-20 15:25:25 +02:00
|
|
|
live = kwargs.get("live_period") == 900
|
2020-12-30 13:41:07 +01:00
|
|
|
ids = self.check_passed_ids(callback_query, kwargs)
|
2024-05-20 15:25:25 +02:00
|
|
|
return ids and latitude and longitude and live
|
2020-12-30 13:41:07 +01:00
|
|
|
|
|
|
|
assert check_shortcut_signature(
|
|
|
|
CallbackQuery.edit_message_live_location,
|
|
|
|
Bot.edit_message_live_location,
|
2024-07-01 21:45:37 +02:00
|
|
|
["inline_message_id", "message_id", "chat_id", "business_connection_id"],
|
2020-12-30 13:41:07 +01:00
|
|
|
[],
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_shortcut_call(
|
2021-02-19 19:07:48 +01:00
|
|
|
callback_query.edit_message_live_location,
|
2021-10-21 11:17:12 +02:00
|
|
|
callback_query.get_bot(),
|
2021-02-19 19:07:48 +01:00
|
|
|
"edit_message_live_location",
|
|
|
|
skip_params=self.skip_params(callback_query),
|
|
|
|
shortcut_kwargs=self.shortcut_kwargs(callback_query),
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_defaults_handling(
|
2021-10-21 11:17:12 +02:00
|
|
|
callback_query.edit_message_live_location, callback_query.get_bot()
|
2021-02-19 19:07:48 +01:00
|
|
|
)
|
2020-12-30 13:41:07 +01:00
|
|
|
|
2021-10-21 11:17:12 +02:00
|
|
|
monkeypatch.setattr(callback_query.get_bot(), "edit_message_live_location", make_assertion)
|
2024-05-20 15:25:25 +02:00
|
|
|
assert await callback_query.edit_message_live_location(
|
|
|
|
latitude=1, longitude=2, live_period=900
|
|
|
|
)
|
|
|
|
assert await callback_query.edit_message_live_location(1, 2, live_period=900)
|
2020-08-13 13:39:43 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_stop_message_live_location(self, monkeypatch, callback_query):
|
2024-02-08 17:12:00 +01:00
|
|
|
if isinstance(callback_query.message, InaccessibleMessage):
|
|
|
|
with pytest.raises(TypeError, match="inaccessible message"):
|
|
|
|
await callback_query.stop_message_live_location("test")
|
|
|
|
return
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def make_assertion(*_, **kwargs):
|
2023-03-25 19:18:04 +01:00
|
|
|
return self.check_passed_ids(callback_query, kwargs)
|
2020-12-30 13:41:07 +01:00
|
|
|
|
|
|
|
assert check_shortcut_signature(
|
|
|
|
CallbackQuery.stop_message_live_location,
|
|
|
|
Bot.stop_message_live_location,
|
2024-07-01 21:45:37 +02:00
|
|
|
["inline_message_id", "message_id", "chat_id", "business_connection_id"],
|
2020-12-30 13:41:07 +01:00
|
|
|
[],
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_shortcut_call(
|
2021-02-19 19:07:48 +01:00
|
|
|
callback_query.stop_message_live_location,
|
2021-10-21 11:17:12 +02:00
|
|
|
callback_query.get_bot(),
|
2021-02-19 19:07:48 +01:00
|
|
|
"stop_message_live_location",
|
|
|
|
skip_params=self.skip_params(callback_query),
|
|
|
|
shortcut_kwargs=self.shortcut_kwargs(callback_query),
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_defaults_handling(
|
2021-10-21 11:17:12 +02:00
|
|
|
callback_query.stop_message_live_location, callback_query.get_bot()
|
2021-02-19 19:07:48 +01:00
|
|
|
)
|
2020-12-30 13:41:07 +01:00
|
|
|
|
2021-10-21 11:17:12 +02:00
|
|
|
monkeypatch.setattr(callback_query.get_bot(), "stop_message_live_location", make_assertion)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await callback_query.stop_message_live_location()
|
2020-08-13 13:39:43 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_set_game_score(self, monkeypatch, callback_query):
|
2024-02-08 17:12:00 +01:00
|
|
|
if isinstance(callback_query.message, InaccessibleMessage):
|
|
|
|
with pytest.raises(TypeError, match="inaccessible message"):
|
|
|
|
await callback_query.set_game_score(user_id=1, score=2)
|
|
|
|
return
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def make_assertion(*_, **kwargs):
|
2020-12-30 13:41:07 +01:00
|
|
|
user_id = kwargs.get("user_id") == 1
|
|
|
|
score = kwargs.get("score") == 2
|
|
|
|
ids = self.check_passed_ids(callback_query, kwargs)
|
2021-02-19 19:07:48 +01:00
|
|
|
return ids and user_id and score
|
2020-12-30 13:41:07 +01:00
|
|
|
|
|
|
|
assert check_shortcut_signature(
|
|
|
|
CallbackQuery.set_game_score,
|
|
|
|
Bot.set_game_score,
|
|
|
|
["inline_message_id", "message_id", "chat_id"],
|
|
|
|
[],
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_shortcut_call(
|
2021-02-19 19:07:48 +01:00
|
|
|
callback_query.set_game_score,
|
2021-10-21 11:17:12 +02:00
|
|
|
callback_query.get_bot(),
|
2021-02-19 19:07:48 +01:00
|
|
|
"set_game_score",
|
|
|
|
skip_params=self.skip_params(callback_query),
|
|
|
|
shortcut_kwargs=self.shortcut_kwargs(callback_query),
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_defaults_handling(
|
|
|
|
callback_query.set_game_score, callback_query.get_bot()
|
|
|
|
)
|
2020-12-30 13:41:07 +01:00
|
|
|
|
2021-10-21 11:17:12 +02:00
|
|
|
monkeypatch.setattr(callback_query.get_bot(), "set_game_score", make_assertion)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await callback_query.set_game_score(user_id=1, score=2)
|
|
|
|
assert await callback_query.set_game_score(1, 2)
|
2020-08-13 13:39:43 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_get_game_high_scores(self, monkeypatch, callback_query):
|
2024-02-08 17:12:00 +01:00
|
|
|
if isinstance(callback_query.message, InaccessibleMessage):
|
|
|
|
with pytest.raises(TypeError, match="inaccessible message"):
|
|
|
|
await callback_query.get_game_high_scores("test")
|
|
|
|
return
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def make_assertion(*_, **kwargs):
|
2020-12-30 13:41:07 +01:00
|
|
|
user_id = kwargs.get("user_id") == 1
|
|
|
|
ids = self.check_passed_ids(callback_query, kwargs)
|
2021-02-19 19:07:48 +01:00
|
|
|
return ids and user_id
|
2020-12-30 13:41:07 +01:00
|
|
|
|
|
|
|
assert check_shortcut_signature(
|
|
|
|
CallbackQuery.get_game_high_scores,
|
|
|
|
Bot.get_game_high_scores,
|
|
|
|
["inline_message_id", "message_id", "chat_id"],
|
|
|
|
[],
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_shortcut_call(
|
2021-02-19 19:07:48 +01:00
|
|
|
callback_query.get_game_high_scores,
|
2021-10-21 11:17:12 +02:00
|
|
|
callback_query.get_bot(),
|
2021-02-19 19:07:48 +01:00
|
|
|
"get_game_high_scores",
|
|
|
|
skip_params=self.skip_params(callback_query),
|
|
|
|
shortcut_kwargs=self.shortcut_kwargs(callback_query),
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_defaults_handling(
|
2021-10-21 11:17:12 +02:00
|
|
|
callback_query.get_game_high_scores, callback_query.get_bot()
|
|
|
|
)
|
2020-12-30 13:41:07 +01:00
|
|
|
|
2021-10-21 11:17:12 +02:00
|
|
|
monkeypatch.setattr(callback_query.get_bot(), "get_game_high_scores", make_assertion)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await callback_query.get_game_high_scores(user_id=1)
|
|
|
|
assert await callback_query.get_game_high_scores(1)
|
2020-08-13 13:39:43 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_delete_message(self, monkeypatch, callback_query):
|
2024-02-08 17:12:00 +01:00
|
|
|
if isinstance(callback_query.message, InaccessibleMessage):
|
|
|
|
with pytest.raises(TypeError, match="inaccessible message"):
|
|
|
|
await callback_query.delete_message()
|
|
|
|
return
|
2020-11-05 17:11:35 +01:00
|
|
|
if callback_query.inline_message_id:
|
|
|
|
pytest.skip("Can't delete inline messages")
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def make_assertion(*args, **kwargs):
|
2020-11-05 17:11:35 +01:00
|
|
|
id_ = kwargs["chat_id"] == callback_query.message.chat_id
|
|
|
|
message = kwargs["message_id"] == callback_query.message.message_id
|
2021-02-19 19:07:48 +01:00
|
|
|
return id_ and message
|
2020-12-30 13:41:07 +01:00
|
|
|
|
|
|
|
assert check_shortcut_signature(
|
|
|
|
CallbackQuery.delete_message,
|
|
|
|
Bot.delete_message,
|
|
|
|
["message_id", "chat_id"],
|
|
|
|
[],
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_shortcut_call(
|
2021-10-21 11:17:12 +02:00
|
|
|
callback_query.delete_message, callback_query.get_bot(), "delete_message"
|
2021-02-19 19:07:48 +01:00
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_defaults_handling(
|
|
|
|
callback_query.delete_message, callback_query.get_bot()
|
|
|
|
)
|
2020-11-05 17:11:35 +01:00
|
|
|
|
2021-10-21 11:17:12 +02:00
|
|
|
monkeypatch.setattr(callback_query.get_bot(), "delete_message", make_assertion)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await callback_query.delete_message()
|
2020-11-05 17:11:35 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_pin_message(self, monkeypatch, callback_query):
|
2024-02-08 17:12:00 +01:00
|
|
|
if isinstance(callback_query.message, InaccessibleMessage):
|
|
|
|
with pytest.raises(TypeError, match="inaccessible message"):
|
|
|
|
await callback_query.pin_message()
|
|
|
|
return
|
2020-11-29 16:20:46 +01:00
|
|
|
if callback_query.inline_message_id:
|
|
|
|
pytest.skip("Can't pin inline messages")
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def make_assertion(*args, **kwargs):
|
2021-02-19 19:07:48 +01:00
|
|
|
return kwargs["chat_id"] == callback_query.message.chat_id
|
2020-12-30 13:41:07 +01:00
|
|
|
|
|
|
|
assert check_shortcut_signature(
|
|
|
|
CallbackQuery.pin_message,
|
|
|
|
Bot.pin_chat_message,
|
2024-08-02 13:43:27 +02:00
|
|
|
["message_id", "chat_id", "business_connection_id"],
|
2020-12-30 13:41:07 +01:00
|
|
|
[],
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_shortcut_call(
|
2024-08-02 13:43:27 +02:00
|
|
|
callback_query.pin_message,
|
|
|
|
callback_query.get_bot(),
|
|
|
|
"pin_chat_message",
|
|
|
|
["business_connection_id"],
|
2021-02-19 19:07:48 +01:00
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_defaults_handling(callback_query.pin_message, callback_query.get_bot())
|
2020-11-29 16:20:46 +01:00
|
|
|
|
2021-10-21 11:17:12 +02:00
|
|
|
monkeypatch.setattr(callback_query.get_bot(), "pin_chat_message", make_assertion)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await callback_query.pin_message()
|
2020-11-29 16:20:46 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_unpin_message(self, monkeypatch, callback_query):
|
2024-02-08 17:12:00 +01:00
|
|
|
if isinstance(callback_query.message, InaccessibleMessage):
|
|
|
|
with pytest.raises(TypeError, match="inaccessible message"):
|
|
|
|
await callback_query.unpin_message()
|
|
|
|
return
|
2020-11-29 16:20:46 +01:00
|
|
|
if callback_query.inline_message_id:
|
|
|
|
pytest.skip("Can't unpin inline messages")
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def make_assertion(*args, **kwargs):
|
2021-02-19 19:07:48 +01:00
|
|
|
return kwargs["chat_id"] == callback_query.message.chat_id
|
2020-12-30 13:41:07 +01:00
|
|
|
|
|
|
|
assert check_shortcut_signature(
|
|
|
|
CallbackQuery.unpin_message,
|
|
|
|
Bot.unpin_chat_message,
|
2024-08-02 13:43:27 +02:00
|
|
|
["message_id", "chat_id", "business_connection_id"],
|
2020-12-30 13:41:07 +01:00
|
|
|
[],
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_shortcut_call(
|
2021-02-19 19:07:48 +01:00
|
|
|
callback_query.unpin_message,
|
2021-10-21 11:17:12 +02:00
|
|
|
callback_query.get_bot(),
|
2021-02-19 19:07:48 +01:00
|
|
|
"unpin_chat_message",
|
2024-08-02 13:43:27 +02:00
|
|
|
shortcut_kwargs=["message_id"],
|
|
|
|
skip_params=["business_connection_id"],
|
2021-02-19 19:07:48 +01:00
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_defaults_handling(
|
|
|
|
callback_query.unpin_message, callback_query.get_bot()
|
|
|
|
)
|
2020-11-29 16:20:46 +01:00
|
|
|
|
2021-10-21 11:17:12 +02:00
|
|
|
monkeypatch.setattr(callback_query.get_bot(), "unpin_chat_message", make_assertion)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await callback_query.unpin_message()
|
2020-11-29 16:20:46 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_copy_message(self, monkeypatch, callback_query):
|
2024-02-08 17:12:00 +01:00
|
|
|
if isinstance(callback_query.message, InaccessibleMessage):
|
|
|
|
with pytest.raises(TypeError, match="inaccessible message"):
|
|
|
|
await callback_query.copy_message(1)
|
|
|
|
return
|
2020-11-29 16:20:46 +01:00
|
|
|
if callback_query.inline_message_id:
|
|
|
|
pytest.skip("Can't copy inline messages")
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def make_assertion(*args, **kwargs):
|
2020-11-29 16:20:46 +01:00
|
|
|
id_ = kwargs["from_chat_id"] == callback_query.message.chat_id
|
|
|
|
chat_id = kwargs["chat_id"] == 1
|
|
|
|
message = kwargs["message_id"] == callback_query.message.message_id
|
2021-02-19 19:07:48 +01:00
|
|
|
return id_ and message and chat_id
|
2020-12-30 13:41:07 +01:00
|
|
|
|
|
|
|
assert check_shortcut_signature(
|
|
|
|
CallbackQuery.copy_message,
|
|
|
|
Bot.copy_message,
|
|
|
|
["message_id", "from_chat_id"],
|
|
|
|
[],
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_shortcut_call(
|
2021-10-21 11:17:12 +02:00
|
|
|
callback_query.copy_message, callback_query.get_bot(), "copy_message"
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_defaults_handling(callback_query.copy_message, callback_query.get_bot())
|
2020-11-29 16:20:46 +01:00
|
|
|
|
2021-10-21 11:17:12 +02:00
|
|
|
monkeypatch.setattr(callback_query.get_bot(), "copy_message", make_assertion)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await callback_query.copy_message(1)
|