From a5f9373381d5cd4bdea458fe8f2e0a3f9244f047 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Sun, 5 Apr 2020 17:02:43 +0300 Subject: [PATCH] refactored message_dict to message_data in FakeTelegram --- tests/__init__.py | 6 ++-- tests/test_bot.py | 84 +++++++++++++++++++++---------------------- tests/test_message.py | 2 +- 3 files changed, 47 insertions(+), 45 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index 262c9395..71ed023b 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -6,9 +6,11 @@ TOKEN = '123456789:AABBCCDDEEFFaabbccddeeff-1234567890' class FakeTelegram(aresponses.ResponsesMockServer): - def __init__(self, message_dict, bot=None, **kwargs): + def __init__(self, message_data, bot=None, **kwargs): + from aiogram.utils.payload import _normalize super().__init__(**kwargs) - self._body, self._headers = self.parse_data(message_dict) + message_data = _normalize(message_data) + self._body, self._headers = self.parse_data(message_data) if isinstance(bot, Bot): Bot.set_current(bot) diff --git a/tests/test_bot.py b/tests/test_bot.py index b2289c8f..c33b07d7 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -19,7 +19,7 @@ async def test_get_me(bot: Bot, event_loop): from .types.dataset import USER user = types.User(**USER) - async with FakeTelegram(message_dict=USER, loop=event_loop): + async with FakeTelegram(message_data=USER, loop=event_loop): result = await bot.me assert result == user @@ -29,7 +29,7 @@ async def test_send_message(bot: Bot, event_loop): from .types.dataset import MESSAGE msg = types.Message(**MESSAGE) - async with FakeTelegram(message_dict=MESSAGE, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE, loop=event_loop): result = await bot.send_message(chat_id=msg.chat.id, text=msg.text) assert result == msg @@ -39,7 +39,7 @@ async def test_forward_message(bot: Bot, event_loop): from .types.dataset import FORWARDED_MESSAGE msg = types.Message(**FORWARDED_MESSAGE) - async with FakeTelegram(message_dict=FORWARDED_MESSAGE, loop=event_loop): + async with FakeTelegram(message_data=FORWARDED_MESSAGE, loop=event_loop): result = await bot.forward_message(chat_id=msg.chat.id, from_chat_id=msg.forward_from_chat.id, message_id=msg.forward_from_message_id) assert result == msg @@ -51,7 +51,7 @@ async def test_send_photo(bot: Bot, event_loop): msg = types.Message(**MESSAGE_WITH_PHOTO) photo = types.PhotoSize(**PHOTO) - async with FakeTelegram(message_dict=MESSAGE_WITH_PHOTO, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE_WITH_PHOTO, loop=event_loop): result = await bot.send_photo(msg.chat.id, photo=photo.file_id, caption=msg.caption, parse_mode=types.ParseMode.HTML, disable_notification=False) assert result == msg @@ -62,7 +62,7 @@ async def test_send_audio(bot: Bot, event_loop): from .types.dataset import MESSAGE_WITH_AUDIO msg = types.Message(**MESSAGE_WITH_AUDIO) - async with FakeTelegram(message_dict=MESSAGE_WITH_AUDIO, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE_WITH_AUDIO, loop=event_loop): result = await bot.send_audio(chat_id=msg.chat.id, audio=msg.audio.file_id, caption=msg.caption, parse_mode=types.ParseMode.HTML, duration=msg.audio.duration, performer=msg.audio.performer, title=msg.audio.title, disable_notification=False) @@ -74,7 +74,7 @@ async def test_send_document(bot: Bot, event_loop): from .types.dataset import MESSAGE_WITH_DOCUMENT msg = types.Message(**MESSAGE_WITH_DOCUMENT) - async with FakeTelegram(message_dict=MESSAGE_WITH_DOCUMENT, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE_WITH_DOCUMENT, loop=event_loop): result = await bot.send_document(chat_id=msg.chat.id, document=msg.document.file_id, caption=msg.caption, parse_mode=types.ParseMode.HTML, disable_notification=False) assert result == msg @@ -86,7 +86,7 @@ async def test_send_video(bot: Bot, event_loop): msg = types.Message(**MESSAGE_WITH_VIDEO) video = types.Video(**VIDEO) - async with FakeTelegram(message_dict=MESSAGE_WITH_VIDEO, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE_WITH_VIDEO, loop=event_loop): result = await bot.send_video(chat_id=msg.chat.id, video=video.file_id, duration=video.duration, width=video.width, height=video.height, caption=msg.caption, parse_mode=types.ParseMode.HTML, supports_streaming=True, @@ -100,7 +100,7 @@ async def test_send_voice(bot: Bot, event_loop): msg = types.Message(**MESSAGE_WITH_VOICE) voice = types.Voice(**VOICE) - async with FakeTelegram(message_dict=MESSAGE_WITH_VOICE, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE_WITH_VOICE, loop=event_loop): result = await bot.send_voice(chat_id=msg.chat.id, voice=voice.file_id, caption=msg.caption, parse_mode=types.ParseMode.HTML, duration=voice.duration, disable_notification=False) @@ -113,7 +113,7 @@ async def test_send_video_note(bot: Bot, event_loop): msg = types.Message(**MESSAGE_WITH_VIDEO_NOTE) video_note = types.VideoNote(**VIDEO_NOTE) - async with FakeTelegram(message_dict=MESSAGE_WITH_VIDEO_NOTE, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE_WITH_VIDEO_NOTE, loop=event_loop): result = await bot.send_video_note(chat_id=msg.chat.id, video_note=video_note.file_id, duration=video_note.duration, length=video_note.length, disable_notification=False) @@ -127,7 +127,7 @@ async def test_send_media_group(bot: Bot, event_loop): photo = types.PhotoSize(**PHOTO) media = [types.InputMediaPhoto(media=photo.file_id), types.InputMediaPhoto(media=photo.file_id)] - async with FakeTelegram(message_dict=[MESSAGE_WITH_MEDIA_GROUP, MESSAGE_WITH_MEDIA_GROUP], loop=event_loop): + async with FakeTelegram(message_data=[MESSAGE_WITH_MEDIA_GROUP, MESSAGE_WITH_MEDIA_GROUP], loop=event_loop): result = await bot.send_media_group(msg.chat.id, media=media, disable_notification=False) assert len(result) == len(media) assert result.pop().media_group_id @@ -139,7 +139,7 @@ async def test_send_location(bot: Bot, event_loop): msg = types.Message(**MESSAGE_WITH_LOCATION) location = types.Location(**LOCATION) - async with FakeTelegram(message_dict=MESSAGE_WITH_LOCATION, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE_WITH_LOCATION, loop=event_loop): result = await bot.send_location(msg.chat.id, latitude=location.latitude, longitude=location.longitude, live_period=10, disable_notification=False) assert result == msg @@ -152,7 +152,7 @@ async def test_edit_message_live_location_by_bot(bot: Bot, event_loop): location = types.Location(**LOCATION) # editing bot message - async with FakeTelegram(message_dict=MESSAGE_WITH_LOCATION, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE_WITH_LOCATION, loop=event_loop): result = await bot.edit_message_live_location(chat_id=msg.chat.id, message_id=msg.message_id, latitude=location.latitude, longitude=location.longitude) assert result == msg @@ -165,7 +165,7 @@ async def test_edit_message_live_location_by_user(bot: Bot, event_loop): location = types.Location(**LOCATION) # editing user's message - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.edit_message_live_location(chat_id=msg.chat.id, message_id=msg.message_id, latitude=location.latitude, longitude=location.longitude) assert isinstance(result, bool) and result is True @@ -177,7 +177,7 @@ async def test_stop_message_live_location_by_bot(bot: Bot, event_loop): msg = types.Message(**MESSAGE_WITH_LOCATION) # stopping bot message - async with FakeTelegram(message_dict=MESSAGE_WITH_LOCATION, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE_WITH_LOCATION, loop=event_loop): result = await bot.stop_message_live_location(chat_id=msg.chat.id, message_id=msg.message_id) assert result == msg @@ -188,7 +188,7 @@ async def test_stop_message_live_location_by_user(bot: Bot, event_loop): msg = types.Message(**MESSAGE_WITH_LOCATION) # stopping user's message - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.stop_message_live_location(chat_id=msg.chat.id, message_id=msg.message_id) assert isinstance(result, bool) assert result is True @@ -201,7 +201,7 @@ async def test_send_venue(bot: Bot, event_loop): location = types.Location(**LOCATION) venue = types.Venue(**VENUE) - async with FakeTelegram(message_dict=MESSAGE_WITH_VENUE, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE_WITH_VENUE, loop=event_loop): result = await bot.send_venue(msg.chat.id, latitude=location.latitude, longitude=location.longitude, title=venue.title, address=venue.address, foursquare_id=venue.foursquare_id, disable_notification=False) @@ -214,7 +214,7 @@ async def test_send_contact(bot: Bot, event_loop): msg = types.Message(**MESSAGE_WITH_CONTACT) contact = types.Contact(**CONTACT) - async with FakeTelegram(message_dict=MESSAGE_WITH_CONTACT, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE_WITH_CONTACT, loop=event_loop): result = await bot.send_contact(msg.chat.id, phone_number=contact.phone_number, first_name=contact.first_name, last_name=contact.last_name, disable_notification=False) assert result == msg @@ -225,7 +225,7 @@ async def test_send_dice(bot: Bot, event_loop): from .types.dataset import MESSAGE_WITH_DICE msg = types.Message(**MESSAGE_WITH_DICE) - async with FakeTelegram(message_dict=MESSAGE_WITH_DICE, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE_WITH_DICE, loop=event_loop): result = await bot.send_dice(msg.chat.id, disable_notification=False) assert result == msg @@ -235,7 +235,7 @@ async def test_send_chat_action(bot: Bot, event_loop): from .types.dataset import CHAT chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.send_chat_action(chat_id=chat.id, action=types.ChatActions.TYPING) assert isinstance(result, bool) assert result is True @@ -246,7 +246,7 @@ async def test_get_user_profile_photo(bot: Bot, event_loop): from .types.dataset import USER_PROFILE_PHOTOS, USER user = types.User(**USER) - async with FakeTelegram(message_dict=USER_PROFILE_PHOTOS, loop=event_loop): + async with FakeTelegram(message_data=USER_PROFILE_PHOTOS, loop=event_loop): result = await bot.get_user_profile_photos(user_id=user.id, offset=1, limit=1) assert isinstance(result, types.UserProfilePhotos) @@ -256,7 +256,7 @@ async def test_get_file(bot: Bot, event_loop): from .types.dataset import FILE file = types.File(**FILE) - async with FakeTelegram(message_dict=FILE, loop=event_loop): + async with FakeTelegram(message_data=FILE, loop=event_loop): result = await bot.get_file(file_id=file.file_id) assert isinstance(result, types.File) @@ -267,7 +267,7 @@ async def test_kick_chat_member(bot: Bot, event_loop): user = types.User(**USER) chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.kick_chat_member(chat_id=chat.id, user_id=user.id, until_date=123) assert isinstance(result, bool) assert result is True @@ -279,7 +279,7 @@ async def test_unban_chat_member(bot: Bot, event_loop): user = types.User(**USER) chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.unban_chat_member(chat_id=chat.id, user_id=user.id) assert isinstance(result, bool) assert result is True @@ -291,7 +291,7 @@ async def test_restrict_chat_member(bot: Bot, event_loop): user = types.User(**USER) chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.restrict_chat_member( chat_id=chat.id, user_id=user.id, @@ -311,7 +311,7 @@ async def test_promote_chat_member(bot: Bot, event_loop): user = types.User(**USER) chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.promote_chat_member(chat_id=chat.id, user_id=user.id, can_change_info=True, can_delete_messages=True, can_edit_messages=True, can_invite_users=True, can_pin_messages=True, can_post_messages=True, @@ -325,7 +325,7 @@ async def test_export_chat_invite_link(bot: Bot, event_loop): from .types.dataset import CHAT, INVITE_LINK chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=INVITE_LINK, loop=event_loop): + async with FakeTelegram(message_data=INVITE_LINK, loop=event_loop): result = await bot.export_chat_invite_link(chat_id=chat.id) assert result == INVITE_LINK @@ -335,7 +335,7 @@ async def test_delete_chat_photo(bot: Bot, event_loop): from .types.dataset import CHAT chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.delete_chat_photo(chat_id=chat.id) assert isinstance(result, bool) assert result is True @@ -346,7 +346,7 @@ async def test_set_chat_title(bot: Bot, event_loop): from .types.dataset import CHAT chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.set_chat_title(chat_id=chat.id, title='Test title') assert isinstance(result, bool) assert result is True @@ -357,7 +357,7 @@ async def test_set_chat_description(bot: Bot, event_loop): from .types.dataset import CHAT chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.set_chat_description(chat_id=chat.id, description='Test description') assert isinstance(result, bool) assert result is True @@ -368,7 +368,7 @@ async def test_pin_chat_message(bot: Bot, event_loop): from .types.dataset import MESSAGE message = types.Message(**MESSAGE) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.pin_chat_message(chat_id=message.chat.id, message_id=message.message_id, disable_notification=False) assert isinstance(result, bool) @@ -380,7 +380,7 @@ async def test_unpin_chat_message(bot: Bot, event_loop): from .types.dataset import CHAT chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.unpin_chat_message(chat_id=chat.id) assert isinstance(result, bool) assert result is True @@ -391,7 +391,7 @@ async def test_leave_chat(bot: Bot, event_loop): from .types.dataset import CHAT chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.leave_chat(chat_id=chat.id) assert isinstance(result, bool) assert result is True @@ -402,7 +402,7 @@ async def test_get_chat(bot: Bot, event_loop): from .types.dataset import CHAT chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=CHAT, loop=event_loop): + async with FakeTelegram(message_data=CHAT, loop=event_loop): result = await bot.get_chat(chat_id=chat.id) assert result == chat @@ -413,7 +413,7 @@ async def test_get_chat_administrators(bot: Bot, event_loop): chat = types.Chat(**CHAT) member = types.ChatMember(**CHAT_MEMBER) - async with FakeTelegram(message_dict=[CHAT_MEMBER, CHAT_MEMBER], loop=event_loop): + async with FakeTelegram(message_data=[CHAT_MEMBER, CHAT_MEMBER], loop=event_loop): result = await bot.get_chat_administrators(chat_id=chat.id) assert result[0] == member assert len(result) == 2 @@ -425,7 +425,7 @@ async def test_get_chat_members_count(bot: Bot, event_loop): chat = types.Chat(**CHAT) count = 5 - async with FakeTelegram(message_dict=count, loop=event_loop): + async with FakeTelegram(message_data=count, loop=event_loop): result = await bot.get_chat_members_count(chat_id=chat.id) assert result == count @@ -436,7 +436,7 @@ async def test_get_chat_member(bot: Bot, event_loop): chat = types.Chat(**CHAT) member = types.ChatMember(**CHAT_MEMBER) - async with FakeTelegram(message_dict=CHAT_MEMBER, loop=event_loop): + async with FakeTelegram(message_data=CHAT_MEMBER, loop=event_loop): result = await bot.get_chat_member(chat_id=chat.id, user_id=member.user.id) assert isinstance(result, types.ChatMember) assert result == member @@ -447,7 +447,7 @@ async def test_set_chat_sticker_set(bot: Bot, event_loop): from .types.dataset import CHAT chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.set_chat_sticker_set(chat_id=chat.id, sticker_set_name='aiogram_stickers') assert isinstance(result, bool) assert result is True @@ -458,7 +458,7 @@ async def test_delete_chat_sticker_set(bot: Bot, event_loop): from .types.dataset import CHAT chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.delete_chat_sticker_set(chat_id=chat.id) assert isinstance(result, bool) assert result is True @@ -467,7 +467,7 @@ async def test_delete_chat_sticker_set(bot: Bot, event_loop): async def test_answer_callback_query(bot: Bot, event_loop): """ answerCallbackQuery method test """ - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.answer_callback_query(callback_query_id='QuERyId', text='Test Answer') assert isinstance(result, bool) assert result is True @@ -477,7 +477,7 @@ async def test_set_my_commands(bot: Bot, event_loop): """ setMyCommands method test """ from .types.dataset import BOT_COMMAND - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): commands = [types.BotCommand(**BOT_COMMAND), types.BotCommand(**BOT_COMMAND)] result = await bot.set_my_commands(commands) assert isinstance(result, bool) @@ -490,7 +490,7 @@ async def test_edit_message_text_by_bot(bot: Bot, event_loop): msg = types.Message(**EDITED_MESSAGE) # message by bot - async with FakeTelegram(message_dict=EDITED_MESSAGE, loop=event_loop): + async with FakeTelegram(message_data=EDITED_MESSAGE, loop=event_loop): result = await bot.edit_message_text(text=msg.text, chat_id=msg.chat.id, message_id=msg.message_id) assert result == msg @@ -501,7 +501,7 @@ async def test_edit_message_text_by_user(bot: Bot, event_loop): msg = types.Message(**EDITED_MESSAGE) # message by user - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.edit_message_text(text=msg.text, chat_id=msg.chat.id, message_id=msg.message_id) assert isinstance(result, bool) assert result is True diff --git a/tests/test_message.py b/tests/test_message.py index 996529f3..32168d57 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -28,7 +28,7 @@ async def message(bot, event_loop): from .types.dataset import MESSAGE msg = types.Message(**MESSAGE) - async with FakeTelegram(message_dict=MESSAGE, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE, loop=event_loop): _message = await bot.send_message(chat_id=msg.chat.id, text=msg.text) yield _message