Fixes + coverage

This commit is contained in:
JRoot Junior 2024-04-08 02:16:08 +03:00
parent c96a4fceca
commit 61d1bcdd96
No known key found for this signature in database
GPG key ID: 738964250D5FF6E2
14 changed files with 130 additions and 79 deletions

View file

@ -40,3 +40,4 @@ extract:
- business_connection_id
- sender_business_bot
- is_from_offline
- has_media_spoiler

View file

@ -5450,16 +5450,16 @@
"annotations": [
{
"type": "Integer",
"description": "The minute's sequence number in a week, starting on Monday, marking the start of the time interval during which the business is open; 0 - 7 24 60",
"html_description": "<td>The minute's sequence number in a week, starting on Monday, marking the start of the time interval during which the business is open; 0 - 7 <em> 24 </em> 60</td>",
"description": "The minute's sequence number in a week, starting on Monday, marking the start of the time interval during which the business is open; 0 - 7 * 24 * 60",
"html_description": "<td>The minute's sequence number in a week, starting on Monday, marking the start of the time interval during which the business is open; 0 - 7 * 24 * 60</td>",
"rst_description": "The minute's sequence number in a week, starting on Monday, marking the start of the time interval during which the business is open; 0 - 7 * 24 * 60\n",
"name": "opening_minute",
"required": true
},
{
"type": "Integer",
"description": "The minute's sequence number in a week, starting on Monday, marking the end of the time interval during which the business is open; 0 - 8 24 60",
"html_description": "<td>The minute's sequence number in a week, starting on Monday, marking the end of the time interval during which the business is open; 0 - 8 <em> 24 </em> 60</td>",
"description": "The minute's sequence number in a week, starting on Monday, marking the end of the time interval during which the business is open; 0 - 8 * 24 * 60",
"html_description": "<td>The minute's sequence number in a week, starting on Monday, marking the end of the time interval during which the business is open; 0 - 8 * 24 * 60</td>",
"rst_description": "The minute's sequence number in a week, starting on Monday, marking the end of the time interval during which the business is open; 0 - 8 * 24 * 60\n",
"name": "closing_minute",
"required": true

View file

@ -13,16 +13,16 @@
"annotations": [
{
"type": "Integer",
"description": "The minute's sequence number in a week, starting on Monday, marking the start of the time interval during which the business is open; 0 - 7 24 60",
"html_description": "<td>The minute's sequence number in a week, starting on Monday, marking the start of the time interval during which the business is open; 0 - 7 <em> 24 </em> 60</td>",
"description": "The minute's sequence number in a week, starting on Monday, marking the start of the time interval during which the business is open; 0 - 7 * 24 * 60",
"html_description": "<td>The minute's sequence number in a week, starting on Monday, marking the start of the time interval during which the business is open; 0 - 7 * 24 * 60</td>",
"rst_description": "The minute's sequence number in a week, starting on Monday, marking the start of the time interval during which the business is open; 0 - 7 * 24 * 60\n",
"name": "opening_minute",
"required": true
},
{
"type": "Integer",
"description": "The minute's sequence number in a week, starting on Monday, marking the end of the time interval during which the business is open; 0 - 8 24 60",
"html_description": "<td>The minute's sequence number in a week, starting on Monday, marking the end of the time interval during which the business is open; 0 - 8 <em> 24 </em> 60</td>",
"description": "The minute's sequence number in a week, starting on Monday, marking the end of the time interval during which the business is open; 0 - 8 * 24 * 60",
"html_description": "<td>The minute's sequence number in a week, starting on Monday, marking the end of the time interval during which the business is open; 0 - 8 * 24 * 60</td>",
"rst_description": "The minute's sequence number in a week, starting on Monday, marking the end of the time interval during which the business is open; 0 - 8 * 24 * 60\n",
"name": "closing_minute",
"required": true

View file

@ -1 +0,0 @@
Fixing tests and content types for Telegram Bot API 7.2 update

View file

@ -86,6 +86,10 @@ class UserContextMiddleware(BaseMiddleware):
return event.chat_boost.chat, None, None
if event.removed_chat_boost:
return event.removed_chat_boost.chat, None, None
if event.deleted_business_messages:
return event.deleted_business_messages.chat, None, None
if event.business_connection:
return None, event.business_connection.user, None
if event.business_message:
return (
event.business_message.chat,

View file

@ -18,7 +18,6 @@ class ContentType(str, Enum):
VIDEO = "video"
VIDEO_NOTE = "video_note"
VOICE = "voice"
HAS_MEDIA_SPOILER = "has_media_spoiler"
CONTACT = "contact"
DICE = "dice"
GAME = "game"
@ -62,7 +61,3 @@ class ContentType(str, Enum):
VIDEO_CHAT_PARTICIPANTS_INVITED = "video_chat_participants_invited"
WEB_APP_DATA = "web_app_data"
USER_SHARED = "user_shared"
SHARED_USER = "shared_user"
BUSINESS_CONNECTION_ID = "business_connection_id"
SENDER_BUSINESS_BOT = "sender_business_bot"
IS_FROM_OFFLINE = "is_from_offline"

View file

@ -3,7 +3,7 @@ from __future__ import annotations
import datetime
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
from pydantic import Field
from pydantic import Field, computed_field
from aiogram.utils.text_decorations import (
TextDecoration,
@ -96,13 +96,13 @@ if TYPE_CHECKING:
from .reply_keyboard_markup import ReplyKeyboardMarkup
from .reply_keyboard_remove import ReplyKeyboardRemove
from .reply_parameters import ReplyParameters
from .shared_user import SharedUser
from .sticker import Sticker
from .story import Story
from .successful_payment import SuccessfulPayment
from .text_quote import TextQuote
from .user import User
from .user_shared import UserShared
from .shared_user import SharedUser
from .users_shared import UsersShared
from .venue import Venue
from .video import Video
@ -239,8 +239,6 @@ class Message(MaybeInaccessibleMessage):
"""*Optional*. Message is an invoice for a `payment <https://core.telegram.org/bots/api#payments>`_, information about the invoice. `More about payments » <https://core.telegram.org/bots/api#payments>`_"""
successful_payment: Optional[SuccessfulPayment] = None
"""*Optional*. Message is a service message about a successful payment, information about the payment. `More about payments » <https://core.telegram.org/bots/api#payments>`_"""
shared_user: Optional[SharedUser] = None
"""*Optional*. A user that was shared with the bot"""
users_shared: Optional[UsersShared] = None
"""*Optional*. Service message: users were shared with the bot"""
chat_shared: Optional[ChatShared] = None
@ -623,22 +621,12 @@ class Message(MaybeInaccessibleMessage):
return ContentType.USER_SHARED
if self.chat_shared:
return ContentType.CHAT_SHARED
if self.shared_user:
return ContentType.SHARED_USER
if self.story:
return ContentType.STORY
if self.has_media_spoiler:
return ContentType.HAS_MEDIA_SPOILER
if self.write_access_allowed:
return ContentType.WRITE_ACCESS_ALLOWED
if self.boost_added:
return ContentType.BOOST_ADDED
if self.business_connection_id:
return ContentType.BUSINESS_CONNECTION_ID
if self.is_from_offline:
return ContentType.IS_FROM_OFFLINE
if self.sender_business_bot:
return ContentType.SENDER_BUSINESS_BOT
return ContentType.UNKNOWN

View file

@ -11,7 +11,9 @@ class TestAddStickerToSet:
response: bool = await bot.add_sticker_to_set(
user_id=42,
name="test stickers pack",
sticker=InputSticker(sticker="file id", format=StickerFormat.STATIC, emoji_list=[":)"]),
sticker=InputSticker(
sticker="file id", format=StickerFormat.STATIC, emoji_list=[":)"]
),
)
request = bot.get_request()
assert response == prepare_result.result

View file

@ -14,7 +14,9 @@ class TestCreateNewStickerSet:
title="title",
stickers=[
InputSticker(sticker="file id", format=StickerFormat.STATIC, emoji_list=[":)"]),
InputSticker(sticker=FSInputFile("file.png"), format=StickerFormat.STATIC, emoji_list=["=("]),
InputSticker(
sticker=FSInputFile("file.png"), format=StickerFormat.STATIC, emoji_list=["=("]
),
],
sticker_format=StickerFormat.STATIC,
)

View file

@ -0,0 +1,24 @@
from aiogram.methods import GetBusinessConnection
from aiogram.types import BusinessConnection, User
from tests.mocked_bot import MockedBot
class TestGetBusinessConnection:
async def test_bot_method(self, bot: MockedBot):
prepare_result = bot.add_result_for(
GetBusinessConnection,
ok=True,
result=BusinessConnection(
id="test",
user=User(id=42, is_bot=False, first_name="User"),
user_chat_id=42,
date=42,
can_reply=True,
is_enabled=True,
),
)
response: BusinessConnection = await bot.get_business_connection(
business_connection_id="test"
)
request = bot.get_request()
assert response == prepare_result.result

View file

@ -0,0 +1,21 @@
from aiogram.methods import ReplaceStickerInSet
from aiogram.types import InputSticker
from tests.mocked_bot import MockedBot
class TestReplaceStickerInSet:
async def test_bot_method(self, bot: MockedBot):
prepare_result = bot.add_result_for(ReplaceStickerInSet, ok=True, result=True)
response: bool = await bot.replace_sticker_in_set(
user_id=42,
name="test",
old_sticker="test",
sticker=InputSticker(
sticker="test",
format="static",
emoji_list=["test"],
),
)
request = bot.get_request()
assert response == prepare_result.result

View file

@ -7,6 +7,8 @@ class TestSetStickerSetThumbnail:
async def test_bot_method(self, bot: MockedBot):
prepare_result = bot.add_result_for(SetStickerSetThumbnail, ok=True, result=None)
response: bool = await bot.set_sticker_set_thumbnail(name="test", format=StickerFormat.STATIC, user_id=42)
response: bool = await bot.set_sticker_set_thumbnail(
name="test", format=StickerFormat.STATIC, user_id=42
)
request = bot.get_request()
assert response == prepare_result.result

View file

@ -463,13 +463,6 @@ TEST_MESSAGE_USER_SHARED = Message(
from_user=User(id=42, is_bot=False, first_name="Test"),
user_shared=UserShared(request_id=42, user_id=42),
)
TEST_MESSAGE_SHARED_USER = Message(
message_id=42,
date=datetime.datetime.now(),
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="User"),
shared_user=SharedUser(request_id=42, user_id=1),
)
TEST_MESSAGE_USERS_SHARED = Message(
message_id=42,
date=datetime.datetime.now(),
@ -493,8 +486,6 @@ TEST_MESSAGE_STORY = Message(
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
story=Story(chat=Chat(id=42, type="private"), id=42),
forward_signature="Test",
forward_date=datetime.datetime.now(),
)
TEST_MESSAGE_GIVEAWAY = Message(
@ -535,13 +526,6 @@ TEST_MESSAGE_GIVEAWAY_COMPLETED = Message(
from_user=None,
giveaway_completed=GiveawayCompleted(winner_count=10),
)
TEST_MESSAGE_HAS_MEDIA_SPOILER = Message(
message_id=42,
date=datetime.datetime.now(),
chat=Chat(id=42, type="private"),
from_user=None,
has_media_spoiler=True,
)
TEST_MESSAGE_GENERAL_FORUM_TOPIC_HIDDEN = Message(
message_id=42,
date=datetime.datetime.now(),
@ -560,7 +544,6 @@ TEST_MESSAGE_WRITE_ACCESS_ALLOWED = Message(
message_id=42,
date=datetime.datetime.now(),
chat=Chat(id=42, type="private"),
from_user=None,
write_access_allowed=WriteAccessAllowed(),
)
TEST_MESSAGE_BOOST_ADDED = Message(
@ -570,27 +553,6 @@ TEST_MESSAGE_BOOST_ADDED = Message(
from_user=User(id=42, is_bot=False, first_name="User"),
boost_added=ChatBoostAdded(boost_count=1),
)
TEST_MESSAGE_BUSINESS_CONNECTION_ID = Message(
message_id=42,
date=datetime.datetime.now(),
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="User"),
business_connection_id="idk"
)
TEST_MESSAGE_IS_FROM_OFFLINE = Message(
message_id=42,
date=datetime.datetime.now(),
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="User"),
is_from_offline=True
)
TEST_MESSAGE_SENDER_BUSINESS_BOT = Message(
message_id=42,
date=datetime.datetime.now(),
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=True, first_name="Bot"),
sender_business_bot=User(id=42, is_bot=True, first_name="Bot")
)
TEST_MESSAGE_UNKNOWN = Message(
message_id=42,
date=datetime.datetime.now(),
@ -649,20 +611,15 @@ MESSAGES_AND_CONTENT_TYPES = [
[TEST_MESSAGE_USER_SHARED, ContentType.USER_SHARED],
[TEST_MESSAGE_USERS_SHARED, ContentType.USERS_SHARED],
[TEST_CHAT_SHARED, ContentType.CHAT_SHARED],
[TEST_MESSAGE_SHARED_USER, ContentType.SHARED_USER],
[TEST_MESSAGE_STORY, ContentType.STORY],
[TEST_MESSAGE_GIVEAWAY, ContentType.GIVEAWAY],
[TEST_MESSAGE_GIVEAWAY_CREATED, ContentType.GIVEAWAY_CREATED],
[TEST_MESSAGE_GIVEAWAY_WINNERS, ContentType.GIVEAWAY_WINNERS],
[TEST_MESSAGE_GIVEAWAY_COMPLETED, ContentType.GIVEAWAY_COMPLETED],
[TEST_MESSAGE_HAS_MEDIA_SPOILER, ContentType.HAS_MEDIA_SPOILER],
[TEST_MESSAGE_GENERAL_FORUM_TOPIC_HIDDEN, ContentType.GENERAL_FORUM_TOPIC_HIDDEN],
[TEST_MESSAGE_GENERAL_FORUM_TOPIC_UNHIDDEN, ContentType.GENERAL_FORUM_TOPIC_UNHIDDEN],
[TEST_MESSAGE_WRITE_ACCESS_ALLOWED, ContentType.WRITE_ACCESS_ALLOWED],
[TEST_MESSAGE_BOOST_ADDED, ContentType.BOOST_ADDED],
[TEST_MESSAGE_BUSINESS_CONNECTION_ID, ContentType.BUSINESS_CONNECTION_ID],
[TEST_MESSAGE_IS_FROM_OFFLINE, ContentType.IS_FROM_OFFLINE],
[TEST_MESSAGE_SENDER_BUSINESS_BOT, ContentType.SENDER_BUSINESS_BOT],
[TEST_MESSAGE_UNKNOWN, ContentType.UNKNOWN],
]
@ -706,9 +663,7 @@ MESSAGES_AND_COPY_METHODS = [
[TEST_MESSAGE_DICE, SendDice],
[TEST_MESSAGE_USER_SHARED, None],
[TEST_CHAT_SHARED, None],
[TEST_MESSAGE_SHARED_USER, None],
[TEST_MESSAGE_GIVEAWAY_COMPLETED, None],
[TEST_MESSAGE_HAS_MEDIA_SPOILER, None],
[TEST_MESSAGE_WEB_APP_DATA, None],
[TEST_FORUM_TOPIC_CREATED, None],
[TEST_FORUM_TOPIC_EDITED, None],
@ -723,9 +678,6 @@ MESSAGES_AND_COPY_METHODS = [
[TEST_MESSAGE_GIVEAWAY, None],
[TEST_MESSAGE_GIVEAWAY_WINNERS, None],
[TEST_MESSAGE_BOOST_ADDED, None],
[TEST_MESSAGE_BUSINESS_CONNECTION_ID, None],
[TEST_MESSAGE_IS_FROM_OFFLINE, None],
[TEST_MESSAGE_SENDER_BUSINESS_BOT, None],
[TEST_MESSAGE_UNKNOWN, None],
]

View file

@ -16,6 +16,8 @@ from aiogram.dispatcher.event.bases import UNHANDLED, SkipHandler
from aiogram.dispatcher.router import Router
from aiogram.methods import GetMe, GetUpdates, SendMessage, TelegramMethod
from aiogram.types import (
BusinessConnection,
BusinessMessagesDeleted,
CallbackQuery,
Chat,
ChatBoost,
@ -525,6 +527,65 @@ class TestDispatcher:
True,
False,
),
pytest.param(
"deleted_business_messages",
Update(
update_id=42,
deleted_business_messages=BusinessMessagesDeleted(
chat=Chat(id=-42, type="private"),
business_connection_id="qwerty",
message_ids=[1, 2, 3],
),
),
True,
False,
),
pytest.param(
"business_connection",
Update(
update_id=42,
business_connection=BusinessConnection(
id="qwerty",
user=User(id=42, is_bot=False, first_name="Test"),
user_chat_id=42,
date=int(time.time()),
can_reply=True,
is_enabled=True,
),
),
False,
True,
),
pytest.param(
"edited_business_message",
Update(
update_id=42,
edited_business_message=Message(
message_id=42,
date=datetime.datetime.now(),
text="test",
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
),
),
True,
True,
),
pytest.param(
"business_message",
Update(
update_id=42,
business_message=Message(
message_id=42,
date=datetime.datetime.now(),
text="test",
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
),
),
True,
True,
),
],
)
async def test_listen_update(