From 624ba40ab011eab54c6bf0f2b7a4cf8950026623 Mon Sep 17 00:00:00 2001 From: JRoot Junior Date: Sun, 17 Nov 2024 22:58:31 +0200 Subject: [PATCH] Added tests --- .../methods/setUserEmojiStatus/replace.yml | 11 ++++ .../types/PreparedInlineMessage/replace.yml | 11 ++++ aiogram/client/bot.py | 4 +- .../methods/save_prepared_inline_message.py | 53 +++++++++---------- aiogram/methods/send_gift.py | 3 +- aiogram/methods/set_user_emoji_status.py | 11 ++-- aiogram/types/prepared_inline_message.py | 11 ++-- .../test_edit_user_star_subscription.py | 17 ++++++ .../test_methods/test_get_available_gifts.py | 33 ++++++++++++ .../test_save_prepared_inline_message.py | 36 +++++++++++++ tests/test_api/test_methods/test_send_gift.py | 13 +++++ .../test_set_user_emoji_status.py | 17 ++++++ 12 files changed, 184 insertions(+), 36 deletions(-) create mode 100644 .butcher/methods/setUserEmojiStatus/replace.yml create mode 100644 .butcher/types/PreparedInlineMessage/replace.yml create mode 100644 tests/test_api/test_methods/test_edit_user_star_subscription.py create mode 100644 tests/test_api/test_methods/test_get_available_gifts.py create mode 100644 tests/test_api/test_methods/test_save_prepared_inline_message.py create mode 100644 tests/test_api/test_methods/test_send_gift.py create mode 100644 tests/test_api/test_methods/test_set_user_emoji_status.py diff --git a/.butcher/methods/setUserEmojiStatus/replace.yml b/.butcher/methods/setUserEmojiStatus/replace.yml new file mode 100644 index 00000000..0507a6df --- /dev/null +++ b/.butcher/methods/setUserEmojiStatus/replace.yml @@ -0,0 +1,11 @@ +annotations: + emoji_status_expiration_date: + parsed_type: + type: union + items: + - type: std + name: datetime.datetime + - type: std + name: datetime.timedelta + - type: std + name: int diff --git a/.butcher/types/PreparedInlineMessage/replace.yml b/.butcher/types/PreparedInlineMessage/replace.yml new file mode 100644 index 00000000..3b8e5159 --- /dev/null +++ b/.butcher/types/PreparedInlineMessage/replace.yml @@ -0,0 +1,11 @@ +annotations: + expiration_date: + parsed_type: + type: union + items: + - type: std + name: datetime.datetime + - type: std + name: datetime.timedelta + - type: std + name: int diff --git a/aiogram/client/bot.py b/aiogram/client/bot.py index eba5f59e..b538485f 100644 --- a/aiogram/client/bot.py +++ b/aiogram/client/bot.py @@ -4968,7 +4968,9 @@ class Bot: self, user_id: int, emoji_status_custom_emoji_id: Optional[str] = None, - emoji_status_expiration_date: Optional[int] = None, + emoji_status_expiration_date: Optional[ + Union[datetime.datetime, datetime.timedelta, int] + ] = None, request_timeout: Optional[int] = None, ) -> bool: """ diff --git a/aiogram/methods/save_prepared_inline_message.py b/aiogram/methods/save_prepared_inline_message.py index be330a07..3ab6f28f 100644 --- a/aiogram/methods/save_prepared_inline_message.py +++ b/aiogram/methods/save_prepared_inline_message.py @@ -5,33 +5,32 @@ from typing import TYPE_CHECKING, Any, Optional, Union from ..types.prepared_inline_message import PreparedInlineMessage from .base import TelegramMethod -if TYPE_CHECKING: - from ..types.inline_query_result_article import InlineQueryResultArticle - from ..types.inline_query_result_audio import InlineQueryResultAudio - from ..types.inline_query_result_cached_audio import InlineQueryResultCachedAudio - from ..types.inline_query_result_cached_document import ( - InlineQueryResultCachedDocument, - ) - from ..types.inline_query_result_cached_gif import InlineQueryResultCachedGif - from ..types.inline_query_result_cached_mpeg4_gif import ( - InlineQueryResultCachedMpeg4Gif, - ) - from ..types.inline_query_result_cached_photo import InlineQueryResultCachedPhoto - from ..types.inline_query_result_cached_sticker import ( - InlineQueryResultCachedSticker, - ) - from ..types.inline_query_result_cached_video import InlineQueryResultCachedVideo - from ..types.inline_query_result_cached_voice import InlineQueryResultCachedVoice - from ..types.inline_query_result_contact import InlineQueryResultContact - from ..types.inline_query_result_document import InlineQueryResultDocument - from ..types.inline_query_result_game import InlineQueryResultGame - from ..types.inline_query_result_gif import InlineQueryResultGif - from ..types.inline_query_result_location import InlineQueryResultLocation - from ..types.inline_query_result_mpeg4_gif import InlineQueryResultMpeg4Gif - from ..types.inline_query_result_photo import InlineQueryResultPhoto - from ..types.inline_query_result_venue import InlineQueryResultVenue - from ..types.inline_query_result_video import InlineQueryResultVideo - from ..types.inline_query_result_voice import InlineQueryResultVoice +from ..types.inline_query_result_article import InlineQueryResultArticle +from ..types.inline_query_result_audio import InlineQueryResultAudio +from ..types.inline_query_result_cached_audio import InlineQueryResultCachedAudio +from ..types.inline_query_result_cached_document import ( + InlineQueryResultCachedDocument, +) +from ..types.inline_query_result_cached_gif import InlineQueryResultCachedGif +from ..types.inline_query_result_cached_mpeg4_gif import ( + InlineQueryResultCachedMpeg4Gif, +) +from ..types.inline_query_result_cached_photo import InlineQueryResultCachedPhoto +from ..types.inline_query_result_cached_sticker import ( + InlineQueryResultCachedSticker, +) +from ..types.inline_query_result_cached_video import InlineQueryResultCachedVideo +from ..types.inline_query_result_cached_voice import InlineQueryResultCachedVoice +from ..types.inline_query_result_contact import InlineQueryResultContact +from ..types.inline_query_result_document import InlineQueryResultDocument +from ..types.inline_query_result_game import InlineQueryResultGame +from ..types.inline_query_result_gif import InlineQueryResultGif +from ..types.inline_query_result_location import InlineQueryResultLocation +from ..types.inline_query_result_mpeg4_gif import InlineQueryResultMpeg4Gif +from ..types.inline_query_result_photo import InlineQueryResultPhoto +from ..types.inline_query_result_venue import InlineQueryResultVenue +from ..types.inline_query_result_video import InlineQueryResultVideo +from ..types.inline_query_result_voice import InlineQueryResultVoice class SavePreparedInlineMessage(TelegramMethod[PreparedInlineMessage]): diff --git a/aiogram/methods/send_gift.py b/aiogram/methods/send_gift.py index fa6ba797..150e085b 100644 --- a/aiogram/methods/send_gift.py +++ b/aiogram/methods/send_gift.py @@ -4,8 +4,7 @@ from typing import TYPE_CHECKING, Any, Optional from .base import TelegramMethod -if TYPE_CHECKING: - from ..types import MessageEntity +from ..types.message_entity import MessageEntity class SendGift(TelegramMethod[bool]): diff --git a/aiogram/methods/set_user_emoji_status.py b/aiogram/methods/set_user_emoji_status.py index 360e22f0..216ec787 100644 --- a/aiogram/methods/set_user_emoji_status.py +++ b/aiogram/methods/set_user_emoji_status.py @@ -1,6 +1,7 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, Optional +import datetime +from typing import TYPE_CHECKING, Any, Optional, Union from .base import TelegramMethod @@ -19,7 +20,9 @@ class SetUserEmojiStatus(TelegramMethod[bool]): """Unique identifier of the target user""" emoji_status_custom_emoji_id: Optional[str] = None """Custom emoji identifier of the emoji status to set. Pass an empty string to remove the status.""" - emoji_status_expiration_date: Optional[int] = None + emoji_status_expiration_date: Optional[Union[datetime.datetime, datetime.timedelta, int]] = ( + None + ) """Expiration date of the emoji status, if any""" if TYPE_CHECKING: @@ -31,7 +34,9 @@ class SetUserEmojiStatus(TelegramMethod[bool]): *, user_id: int, emoji_status_custom_emoji_id: Optional[str] = None, - emoji_status_expiration_date: Optional[int] = None, + emoji_status_expiration_date: Optional[ + Union[datetime.datetime, datetime.timedelta, int] + ] = None, **__pydantic_kwargs: Any, ) -> None: # DO NOT EDIT MANUALLY!!! diff --git a/aiogram/types/prepared_inline_message.py b/aiogram/types/prepared_inline_message.py index 73ce5ba9..3d91f5fe 100644 --- a/aiogram/types/prepared_inline_message.py +++ b/aiogram/types/prepared_inline_message.py @@ -1,6 +1,7 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any +import datetime +from typing import TYPE_CHECKING, Any, Union from .base import TelegramObject @@ -14,7 +15,7 @@ class PreparedInlineMessage(TelegramObject): id: str """Unique identifier of the prepared message""" - expiration_date: int + expiration_date: Union[datetime.datetime, datetime.timedelta, int] """Expiration date of the prepared message, in Unix time. Expired prepared messages can no longer be used""" if TYPE_CHECKING: @@ -22,7 +23,11 @@ class PreparedInlineMessage(TelegramObject): # This section was auto-generated via `butcher` def __init__( - __pydantic__self__, *, id: str, expiration_date: int, **__pydantic_kwargs: Any + __pydantic__self__, + *, + id: str, + expiration_date: Union[datetime.datetime, datetime.timedelta, int], + **__pydantic_kwargs: Any, ) -> None: # DO NOT EDIT MANUALLY!!! # This method was auto-generated via `butcher` diff --git a/tests/test_api/test_methods/test_edit_user_star_subscription.py b/tests/test_api/test_methods/test_edit_user_star_subscription.py new file mode 100644 index 00000000..74628d8f --- /dev/null +++ b/tests/test_api/test_methods/test_edit_user_star_subscription.py @@ -0,0 +1,17 @@ +from aiogram.enums import StickerFormat +from aiogram.methods import EditUserStarSubscription, Request, UploadStickerFile +from aiogram.types import BufferedInputFile, File +from tests.mocked_bot import MockedBot + + +class TestEditUserStarSubscription: + async def test_bot_method(self, bot: MockedBot): + prepare_result = bot.add_result_for(EditUserStarSubscription, ok=True, result=True) + + response: bool = await bot.edit_user_star_subscription( + user_id=42, + telegram_payment_charge_id="telegram_payment_charge_id", + is_canceled=False, + ) + request = bot.get_request() + assert response == prepare_result.result diff --git a/tests/test_api/test_methods/test_get_available_gifts.py b/tests/test_api/test_methods/test_get_available_gifts.py new file mode 100644 index 00000000..79e0d36a --- /dev/null +++ b/tests/test_api/test_methods/test_get_available_gifts.py @@ -0,0 +1,33 @@ +from aiogram.enums import StickerFormat +from aiogram.methods import GetAvailableGifts, Request, UploadStickerFile +from aiogram.types import BufferedInputFile, File, Gift, Gifts, Sticker +from tests.mocked_bot import MockedBot + + +class TestGetAvailableGifts: + async def test_bot_method(self, bot: MockedBot): + prepare_result = bot.add_result_for( + GetAvailableGifts, + ok=True, + result=Gifts( + gifts=[ + Gift( + id="gift_id", + sticker=Sticker( + file_id="file_id", + file_unique_id="file_id", + type="regular", + width=512, + height=512, + is_animated=False, + is_video=False, + ), + star_count=1, + ) + ] + ), + ) + + response: Gifts = await bot.get_available_gifts() + request = bot.get_request() + assert response == prepare_result.result diff --git a/tests/test_api/test_methods/test_save_prepared_inline_message.py b/tests/test_api/test_methods/test_save_prepared_inline_message.py new file mode 100644 index 00000000..b29dc94e --- /dev/null +++ b/tests/test_api/test_methods/test_save_prepared_inline_message.py @@ -0,0 +1,36 @@ +from datetime import datetime, timedelta + +from aiogram.methods import ( + SavePreparedInlineMessage, +) +from aiogram.types import ( + InlineQueryResultArticle, + InputTextMessageContent, + PreparedInlineMessage, +) +from tests.mocked_bot import MockedBot + + +class TestSavePreparedInlineMessage: + async def test_bot_method(self, bot: MockedBot): + prepare_result = bot.add_result_for( + SavePreparedInlineMessage, + ok=True, + result=PreparedInlineMessage( + id="id", + expiration_date=datetime.now() + timedelta(days=1), + ), + ) + + response: PreparedInlineMessage = await bot.save_prepared_inline_message( + user_id=42, + result=InlineQueryResultArticle( + id="id", + title="title", + input_message_content=InputTextMessageContent( + message_text="message_text", + ), + ), + ) + request = bot.get_request() + assert response == prepare_result.result diff --git a/tests/test_api/test_methods/test_send_gift.py b/tests/test_api/test_methods/test_send_gift.py new file mode 100644 index 00000000..07d9a41c --- /dev/null +++ b/tests/test_api/test_methods/test_send_gift.py @@ -0,0 +1,13 @@ +from aiogram.enums import StickerFormat +from aiogram.methods import GetAvailableGifts, Request, SendGift, UploadStickerFile +from aiogram.types import BufferedInputFile, File, Gift, Gifts, Sticker +from tests.mocked_bot import MockedBot + + +class TestSendGift: + async def test_bot_method(self, bot: MockedBot): + prepare_result = bot.add_result_for(SendGift, ok=True, result=True) + + response: bool = await bot.send_gift(user_id=42, gift_id="gift_id") + request = bot.get_request() + assert response == prepare_result.result diff --git a/tests/test_api/test_methods/test_set_user_emoji_status.py b/tests/test_api/test_methods/test_set_user_emoji_status.py new file mode 100644 index 00000000..4e661eaf --- /dev/null +++ b/tests/test_api/test_methods/test_set_user_emoji_status.py @@ -0,0 +1,17 @@ +from datetime import datetime, timedelta + +from aiogram.methods import SetUserEmojiStatus +from tests.mocked_bot import MockedBot + + +class TestSetUserEmojiStatus: + async def test_bot_method(self, bot: MockedBot): + prepare_result = bot.add_result_for(SetUserEmojiStatus, ok=True, result=True) + + response: bool = await bot.set_user_emoji_status( + user_id=42, + emoji_status_custom_emoji_id="emoji_status_custom_emoji_id", + emoji_status_expiration_date=datetime.now() + timedelta(days=1), + ) + request = bot.get_request() + assert response == prepare_result.result