From 3016c8e21e7a0aa0e77f17b048e6efc64ed63fd2 Mon Sep 17 00:00:00 2001 From: Egor Date: Fri, 3 Jul 2020 16:08:31 +0500 Subject: [PATCH] feat: ChatFactory and private_chat fixture --- tests/conftest.py | 6 ++ tests/factories/chat.py | 28 ++++++++ .../test_methods/test_edit_message_caption.py | 13 ++-- .../test_methods/test_forward_message.py | 26 +++----- tests/test_api/test_methods/test_get_chat.py | 12 ++-- .../test_methods/test_send_animation.py | 12 ++-- .../test_api/test_methods/test_send_audio.py | 14 ++-- .../test_methods/test_send_contact.py | 12 ++-- .../test_methods/test_send_document.py | 12 ++-- tests/test_api/test_methods/test_send_game.py | 12 ++-- .../test_methods/test_send_invoice.py | 13 ++-- .../test_methods/test_send_location.py | 12 ++-- .../test_methods/test_send_media_group.py | 16 ++--- .../test_methods/test_send_sticker.py | 12 ++-- .../test_methods/test_send_video_note.py | 12 ++-- .../test_api/test_methods/test_send_voice.py | 7 +- tests/test_api/test_types/test_message.py | 64 +++++++++---------- .../test_handler/test_message.py | 9 +-- 18 files changed, 161 insertions(+), 131 deletions(-) create mode 100644 tests/factories/chat.py diff --git a/tests/conftest.py b/tests/conftest.py index 60d9d0fe..3bb41292 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,7 @@ import pytest from aiogram import Bot +from tests.factories.chat import ChatFactory from tests.mocked_bot import MockedBot @@ -11,3 +12,8 @@ def bot(): yield bot Bot.reset_current(token) bot.me.invalidate(bot) + + +@pytest.fixture() +def private_chat(): + return ChatFactory() diff --git a/tests/factories/chat.py b/tests/factories/chat.py new file mode 100644 index 00000000..85ce69ea --- /dev/null +++ b/tests/factories/chat.py @@ -0,0 +1,28 @@ +import factory + +from aiogram.api.types import Chat +from aiogram.api.types.chat import ChatType +from tests.factories import sequences + + +class ChatFactory(factory.Factory): + class Meta: + model = Chat + + id = None # lazy attribute + first_name = sequences.first_name + last_name = sequences.last_name + username = sequences.username + type = ChatType.PRIVATE + + @factory.lazy_attribute_sequence + def id(self, n): + _id = n + if self.type is ChatType.CHANNEL: + _id = -_id + return _id + + @factory.lazy_attribute_sequence + def title(self, n): + if self.type is ChatType.CHANNEL: + return f"Title #{n}" diff --git a/tests/test_api/test_methods/test_edit_message_caption.py b/tests/test_api/test_methods/test_edit_message_caption.py index 50fcf838..695e2287 100644 --- a/tests/test_api/test_methods/test_edit_message_caption.py +++ b/tests/test_api/test_methods/test_edit_message_caption.py @@ -4,7 +4,8 @@ from typing import Union import pytest from aiogram.api.methods import EditMessageCaption, Request -from aiogram.api.types import Chat, Message +from aiogram.api.types import Message +from tests.factories.chat import ChatFactory from tests.mocked_bot import MockedBot @@ -15,10 +16,7 @@ class TestEditMessageCaption: EditMessageCaption, ok=True, result=Message( - message_id=42, - date=datetime.datetime.now(), - text="text", - chat=Chat(id=42, type="private"), + message_id=42, date=datetime.datetime.now(), text="text", chat=ChatFactory(), ), ) @@ -33,10 +31,7 @@ class TestEditMessageCaption: EditMessageCaption, ok=True, result=Message( - message_id=42, - date=datetime.datetime.now(), - text="text", - chat=Chat(id=42, type="private"), + message_id=42, date=datetime.datetime.now(), text="text", chat=ChatFactory(), ), ) diff --git a/tests/test_api/test_methods/test_forward_message.py b/tests/test_api/test_methods/test_forward_message.py index 3e2ae0d9..6d788986 100644 --- a/tests/test_api/test_methods/test_forward_message.py +++ b/tests/test_api/test_methods/test_forward_message.py @@ -9,38 +9,32 @@ from tests.mocked_bot import MockedBot class TestForwardMessage: @pytest.mark.asyncio - async def test_method(self, bot: MockedBot): + async def test_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( ForwardMessage, ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), - chat=Chat(id=42, title="chat", type="private"), - text="text", - ), + result=Message(message_id=42, date=datetime.datetime.now(), chat=private_chat, text="text", ), ) - response: Message = await ForwardMessage(chat_id=42, from_chat_id=42, message_id=42) + response: Message = await ForwardMessage( + chat_id=private_chat.id, from_chat_id=private_chat.id, message_id=42 + ) request: Request = bot.get_request() assert request.method == "forwardMessage" # assert request.data == {} assert response == prepare_result.result @pytest.mark.asyncio - async def test_bot_method(self, bot: MockedBot): + async def test_bot_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( ForwardMessage, ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), - chat=Chat(id=42, title="chat", type="private"), - text="text", - ), + result=Message(message_id=42, date=datetime.datetime.now(), chat=private_chat, text="text", ), ) - response: Message = await bot.forward_message(chat_id=42, from_chat_id=42, message_id=42) + response: Message = await bot.forward_message( + chat_id=private_chat.id, from_chat_id=private_chat.id, message_id=42 + ) request: Request = bot.get_request() assert request.method == "forwardMessage" # assert request.data == {} diff --git a/tests/test_api/test_methods/test_get_chat.py b/tests/test_api/test_methods/test_get_chat.py index d88b5261..f79aa49c 100644 --- a/tests/test_api/test_methods/test_get_chat.py +++ b/tests/test_api/test_methods/test_get_chat.py @@ -2,28 +2,32 @@ import pytest from aiogram.api.methods import GetChat, Request from aiogram.api.types import Chat +from aiogram.api.types.chat import ChatType +from tests.factories.chat import ChatFactory from tests.mocked_bot import MockedBot class TestGetChat: @pytest.mark.asyncio async def test_method(self, bot: MockedBot): + channel = ChatFactory(type=ChatType.CHANNEL) prepare_result = bot.add_result_for( - GetChat, ok=True, result=Chat(id=-42, type="channel", title="chat") + GetChat, ok=True, result=channel ) - response: Chat = await GetChat(chat_id=-42) + response: Chat = await GetChat(chat_id=channel.id) request: Request = bot.get_request() assert request.method == "getChat" assert response == prepare_result.result @pytest.mark.asyncio async def test_bot_method(self, bot: MockedBot): + channel = ChatFactory(type=ChatType.CHANNEL) prepare_result = bot.add_result_for( - GetChat, ok=True, result=Chat(id=-42, type="channel", title="chat") + GetChat, ok=True, result=channel ) - response: Chat = await bot.get_chat(chat_id=-42) + response: Chat = await bot.get_chat(chat_id=channel.id) request: Request = bot.get_request() assert request.method == "getChat" assert response == prepare_result.result diff --git a/tests/test_api/test_methods/test_send_animation.py b/tests/test_api/test_methods/test_send_animation.py index b02bad5f..918959dc 100644 --- a/tests/test_api/test_methods/test_send_animation.py +++ b/tests/test_api/test_methods/test_send_animation.py @@ -9,7 +9,7 @@ from tests.mocked_bot import MockedBot class TestSendAnimation: @pytest.mark.asyncio - async def test_method(self, bot: MockedBot): + async def test_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendAnimation, ok=True, @@ -19,17 +19,17 @@ class TestSendAnimation: animation=Animation( file_id="file id", width=42, height=42, duration=0, file_unique_id="file id" ), - chat=Chat(id=42, type="private"), + chat=private_chat, ), ) - response: Message = await SendAnimation(chat_id=42, animation="file id") + response: Message = await SendAnimation(chat_id=private_chat.id, animation="file id") request: Request = bot.get_request() assert request.method == "sendAnimation" assert response == prepare_result.result @pytest.mark.asyncio - async def test_bot_method(self, bot: MockedBot): + async def test_bot_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendAnimation, ok=True, @@ -39,11 +39,11 @@ class TestSendAnimation: animation=Animation( file_id="file id", width=42, height=42, duration=0, file_unique_id="file id" ), - chat=Chat(id=42, type="private"), + chat=private_chat, ), ) - response: Message = await bot.send_animation(chat_id=42, animation="file id") + response: Message = await bot.send_animation(chat_id=private_chat.id, animation="file id") request: Request = bot.get_request() assert request.method == "sendAnimation" assert response == prepare_result.result diff --git a/tests/test_api/test_methods/test_send_audio.py b/tests/test_api/test_methods/test_send_audio.py index fdc06bdb..27752ede 100644 --- a/tests/test_api/test_methods/test_send_audio.py +++ b/tests/test_api/test_methods/test_send_audio.py @@ -3,13 +3,13 @@ import datetime import pytest from aiogram.api.methods import Request, SendAudio -from aiogram.api.types import Audio, Chat, File, Message +from aiogram.api.types import Audio, Chat, Message from tests.mocked_bot import MockedBot class TestSendAudio: @pytest.mark.asyncio - async def test_method(self, bot: MockedBot): + async def test_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendAudio, ok=True, @@ -17,17 +17,17 @@ class TestSendAudio: message_id=42, date=datetime.datetime.now(), audio=Audio(file_id="file id", duration=42, file_unique_id="file id"), - chat=Chat(id=42, type="private"), + chat=private_chat, ), ) - response: Message = await SendAudio(chat_id=42, audio="file id") + response: Message = await SendAudio(chat_id=private_chat.id, audio="file id") request: Request = bot.get_request() assert request.method == "sendAudio" assert response == prepare_result.result @pytest.mark.asyncio - async def test_bot_method(self, bot: MockedBot): + async def test_bot_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendAudio, ok=True, @@ -35,11 +35,11 @@ class TestSendAudio: message_id=42, date=datetime.datetime.now(), audio=Audio(file_id="file id", duration=42, file_unique_id="file id"), - chat=Chat(id=42, type="private"), + chat=private_chat, ), ) - response: Message = await bot.send_audio(chat_id=42, audio="file id") + response: Message = await bot.send_audio(chat_id=private_chat.id, audio="file id") request: Request = bot.get_request() assert request.method == "sendAudio" assert response == prepare_result.result diff --git a/tests/test_api/test_methods/test_send_contact.py b/tests/test_api/test_methods/test_send_contact.py index 44435d2d..d7e89d40 100644 --- a/tests/test_api/test_methods/test_send_contact.py +++ b/tests/test_api/test_methods/test_send_contact.py @@ -9,7 +9,7 @@ from tests.mocked_bot import MockedBot class TestSendContact: @pytest.mark.asyncio - async def test_method(self, bot: MockedBot): + async def test_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendContact, ok=True, @@ -17,17 +17,17 @@ class TestSendContact: message_id=42, date=datetime.datetime.now(), contact=Contact(phone_number="911", first_name="911"), - chat=Chat(id=42, type="private"), + chat=private_chat, ), ) - response: Message = await SendContact(chat_id=42, phone_number="911", first_name="911") + response: Message = await SendContact(chat_id=private_chat.id, phone_number="911", first_name="911") request: Request = bot.get_request() assert request.method == "sendContact" assert response == prepare_result.result @pytest.mark.asyncio - async def test_bot_method(self, bot: MockedBot): + async def test_bot_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendContact, ok=True, @@ -35,12 +35,12 @@ class TestSendContact: message_id=42, date=datetime.datetime.now(), contact=Contact(phone_number="911", first_name="911"), - chat=Chat(id=42, type="private"), + chat=private_chat, ), ) response: Message = await bot.send_contact( - chat_id=42, phone_number="911", first_name="911" + chat_id=private_chat.id, phone_number="911", first_name="911" ) request: Request = bot.get_request() assert request.method == "sendContact" diff --git a/tests/test_api/test_methods/test_send_document.py b/tests/test_api/test_methods/test_send_document.py index 5ddd2e6a..b2daaba1 100644 --- a/tests/test_api/test_methods/test_send_document.py +++ b/tests/test_api/test_methods/test_send_document.py @@ -9,7 +9,7 @@ from tests.mocked_bot import MockedBot class TestSendDocument: @pytest.mark.asyncio - async def test_method(self, bot: MockedBot): + async def test_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendDocument, ok=True, @@ -17,17 +17,17 @@ class TestSendDocument: message_id=42, date=datetime.datetime.now(), document=Document(file_id="file id", file_unique_id="file id"), - chat=Chat(id=42, type="private"), + chat=private_chat, ), ) - response: Message = await SendDocument(chat_id=42, document="file id") + response: Message = await SendDocument(chat_id=private_chat.id, document="file id") request: Request = bot.get_request() assert request.method == "sendDocument" assert response == prepare_result.result @pytest.mark.asyncio - async def test_bot_method(self, bot: MockedBot): + async def test_bot_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendDocument, ok=True, @@ -35,11 +35,11 @@ class TestSendDocument: message_id=42, date=datetime.datetime.now(), document=Document(file_id="file id", file_unique_id="file id"), - chat=Chat(id=42, type="private"), + chat=private_chat, ), ) - response: Message = await bot.send_document(chat_id=42, document="file id") + response: Message = await bot.send_document(chat_id=private_chat.id, document="file id") request: Request = bot.get_request() assert request.method == "sendDocument" assert response == prepare_result.result diff --git a/tests/test_api/test_methods/test_send_game.py b/tests/test_api/test_methods/test_send_game.py index 7728c079..05194990 100644 --- a/tests/test_api/test_methods/test_send_game.py +++ b/tests/test_api/test_methods/test_send_game.py @@ -9,7 +9,7 @@ from tests.mocked_bot import MockedBot class TestSendGame: @pytest.mark.asyncio - async def test_method(self, bot: MockedBot): + async def test_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendGame, ok=True, @@ -23,17 +23,17 @@ class TestSendGame: PhotoSize(file_id="file id", width=42, height=42, file_unique_id="file id") ], ), - chat=Chat(id=42, type="private"), + chat=private_chat, ), ) - response: Message = await SendGame(chat_id=42, game_short_name="game") + response: Message = await SendGame(chat_id=private_chat.id, game_short_name="game") request: Request = bot.get_request() assert request.method == "sendGame" assert response == prepare_result.result @pytest.mark.asyncio - async def test_bot_method(self, bot: MockedBot): + async def test_bot_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendGame, ok=True, @@ -47,11 +47,11 @@ class TestSendGame: PhotoSize(file_id="file id", width=42, height=42, file_unique_id="file id") ], ), - chat=Chat(id=42, type="private"), + chat=private_chat, ), ) - response: Message = await bot.send_game(chat_id=42, game_short_name="game") + response: Message = await bot.send_game(chat_id=private_chat.id, game_short_name="game") request: Request = bot.get_request() assert request.method == "sendGame" assert response == prepare_result.result diff --git a/tests/test_api/test_methods/test_send_invoice.py b/tests/test_api/test_methods/test_send_invoice.py index eb6c83b6..7d3b939f 100644 --- a/tests/test_api/test_methods/test_send_invoice.py +++ b/tests/test_api/test_methods/test_send_invoice.py @@ -4,12 +4,13 @@ import pytest from aiogram.api.methods import Request, SendInvoice from aiogram.api.types import Chat, Invoice, LabeledPrice, Message +from tests.factories.chat import ChatFactory from tests.mocked_bot import MockedBot class TestSendInvoice: @pytest.mark.asyncio - async def test_method(self, bot: MockedBot): + async def test_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendInvoice, ok=True, @@ -23,12 +24,12 @@ class TestSendInvoice: currency="BTC", total_amount=1, ), - chat=Chat(id=42, type="private"), + chat=private_chat, ), ) response: Message = await SendInvoice( - chat_id=42, + chat_id=private_chat.id, title="test", description="test", payload="payload", @@ -42,7 +43,7 @@ class TestSendInvoice: assert response == prepare_result.result @pytest.mark.asyncio - async def test_bot_method(self, bot: MockedBot): + async def test_bot_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendInvoice, ok=True, @@ -56,12 +57,12 @@ class TestSendInvoice: currency="BTC", total_amount=1, ), - chat=Chat(id=42, type="private"), + chat=private_chat, ), ) response: Message = await bot.send_invoice( - chat_id=42, + chat_id=private_chat.id, title="test", description="test", payload="payload", diff --git a/tests/test_api/test_methods/test_send_location.py b/tests/test_api/test_methods/test_send_location.py index d833733c..49a64d99 100644 --- a/tests/test_api/test_methods/test_send_location.py +++ b/tests/test_api/test_methods/test_send_location.py @@ -9,7 +9,7 @@ from tests.mocked_bot import MockedBot class TestSendLocation: @pytest.mark.asyncio - async def test_method(self, bot: MockedBot): + async def test_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendLocation, ok=True, @@ -17,17 +17,17 @@ class TestSendLocation: message_id=42, date=datetime.datetime.now(), location=Location(longitude=3.14, latitude=3.14), - chat=Chat(id=42, type="private"), + chat=private_chat, ), ) - response: Message = await SendLocation(chat_id=42, latitude=3.14, longitude=3.14) + response: Message = await SendLocation(chat_id=private_chat.id, latitude=3.14, longitude=3.14) request: Request = bot.get_request() assert request.method == "sendLocation" assert response == prepare_result.result @pytest.mark.asyncio - async def test_bot_method(self, bot: MockedBot): + async def test_bot_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendLocation, ok=True, @@ -35,11 +35,11 @@ class TestSendLocation: message_id=42, date=datetime.datetime.now(), location=Location(longitude=3.14, latitude=3.14), - chat=Chat(id=42, type="private"), + chat=private_chat, ), ) - response: Message = await bot.send_location(chat_id=42, latitude=3.14, longitude=3.14) + response: Message = await bot.send_location(chat_id=private_chat.id, latitude=3.14, longitude=3.14) request: Request = bot.get_request() assert request.method == "sendLocation" assert response == prepare_result.result diff --git a/tests/test_api/test_methods/test_send_media_group.py b/tests/test_api/test_methods/test_send_media_group.py index 14a30ccb..ba90e87a 100644 --- a/tests/test_api/test_methods/test_send_media_group.py +++ b/tests/test_api/test_methods/test_send_media_group.py @@ -18,7 +18,7 @@ from tests.mocked_bot import MockedBot class TestSendMediaGroup: @pytest.mark.asyncio - async def test_method(self, bot: MockedBot): + async def test_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendMediaGroup, ok=True, @@ -30,7 +30,7 @@ class TestSendMediaGroup: PhotoSize(file_id="file id", width=42, height=42, file_unique_id="file id") ], media_group_id="media group", - chat=Chat(id=42, type="private"), + chat=private_chat, ), Message( message_id=43, @@ -43,13 +43,13 @@ class TestSendMediaGroup: file_unique_id="file id", ), media_group_id="media group", - chat=Chat(id=42, type="private"), + chat=private_chat, ), ], ) response: List[Message] = await SendMediaGroup( - chat_id=42, + chat_id=private_chat.id, media=[ InputMediaPhoto(media="file id"), InputMediaVideo(media=BufferedInputFile(b"", "video.mp4")), @@ -60,7 +60,7 @@ class TestSendMediaGroup: assert response == prepare_result.result @pytest.mark.asyncio - async def test_bot_method(self, bot: MockedBot): + async def test_bot_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendMediaGroup, ok=True, @@ -72,7 +72,7 @@ class TestSendMediaGroup: PhotoSize(file_id="file id", width=42, height=42, file_unique_id="file id") ], media_group_id="media group", - chat=Chat(id=42, type="private"), + chat=private_chat, ), Message( message_id=43, @@ -85,13 +85,13 @@ class TestSendMediaGroup: file_unique_id="file id", ), media_group_id="media group", - chat=Chat(id=42, type="private"), + chat=private_chat, ), ], ) response: List[Message] = await bot.send_media_group( - chat_id=42, + chat_id=private_chat.id, media=[ InputMediaPhoto(media="file id"), InputMediaVideo(media=BufferedInputFile(b"", "video.mp4")), diff --git a/tests/test_api/test_methods/test_send_sticker.py b/tests/test_api/test_methods/test_send_sticker.py index 8475581e..e881aded 100644 --- a/tests/test_api/test_methods/test_send_sticker.py +++ b/tests/test_api/test_methods/test_send_sticker.py @@ -9,7 +9,7 @@ from tests.mocked_bot import MockedBot class TestSendSticker: @pytest.mark.asyncio - async def test_method(self, bot: MockedBot): + async def test_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendSticker, ok=True, @@ -23,17 +23,17 @@ class TestSendSticker: is_animated=False, file_unique_id="file id", ), - chat=Chat(id=42, type="private"), + chat=private_chat, ), ) - response: Message = await SendSticker(chat_id=42, sticker="file id") + response: Message = await SendSticker(chat_id=private_chat.id, sticker="file id") request: Request = bot.get_request() assert request.method == "sendSticker" assert response == prepare_result.result @pytest.mark.asyncio - async def test_bot_method(self, bot: MockedBot): + async def test_bot_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendSticker, ok=True, @@ -47,11 +47,11 @@ class TestSendSticker: is_animated=False, file_unique_id="file id", ), - chat=Chat(id=42, type="private"), + chat=private_chat, ), ) - response: Message = await bot.send_sticker(chat_id=42, sticker="file id") + response: Message = await bot.send_sticker(chat_id=private_chat.id, sticker="file id") request: Request = bot.get_request() assert request.method == "sendSticker" assert response == prepare_result.result diff --git a/tests/test_api/test_methods/test_send_video_note.py b/tests/test_api/test_methods/test_send_video_note.py index bb235bed..0dff6a30 100644 --- a/tests/test_api/test_methods/test_send_video_note.py +++ b/tests/test_api/test_methods/test_send_video_note.py @@ -9,7 +9,7 @@ from tests.mocked_bot import MockedBot class TestSendVideoNote: @pytest.mark.asyncio - async def test_method(self, bot: MockedBot): + async def test_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendVideoNote, ok=True, @@ -19,19 +19,19 @@ class TestSendVideoNote: video_note=VideoNote( file_id="file id", length=0, duration=0, file_unique_id="file id" ), - chat=Chat(id=42, type="private"), + chat=private_chat, ), ) response: Message = await SendVideoNote( - chat_id=42, video_note="file id", thumb=BufferedInputFile(b"", "file.png") + chat_id=private_chat.id, video_note="file id", thumb=BufferedInputFile(b"", "file.png") ) request: Request = bot.get_request() assert request.method == "sendVideoNote" assert response == prepare_result.result @pytest.mark.asyncio - async def test_bot_method(self, bot: MockedBot): + async def test_bot_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendVideoNote, ok=True, @@ -41,12 +41,12 @@ class TestSendVideoNote: video_note=VideoNote( file_id="file id", length=0, duration=0, file_unique_id="file id" ), - chat=Chat(id=42, type="private"), + chat=private_chat, ), ) response: Message = await bot.send_video_note( - chat_id=42, video_note="file id", thumb=BufferedInputFile(b"", "file.png") + chat_id=private_chat.id, video_note="file id", thumb=BufferedInputFile(b"", "file.png") ) request: Request = bot.get_request() assert request.method == "sendVideoNote" diff --git a/tests/test_api/test_methods/test_send_voice.py b/tests/test_api/test_methods/test_send_voice.py index 6dcab09d..22688bfe 100644 --- a/tests/test_api/test_methods/test_send_voice.py +++ b/tests/test_api/test_methods/test_send_voice.py @@ -3,7 +3,8 @@ import datetime import pytest from aiogram.api.methods import Request, SendVoice -from aiogram.api.types import Chat, Message, Voice +from aiogram.api.types import Message, Voice +from tests.factories.chat import ChatFactory from tests.mocked_bot import MockedBot @@ -17,7 +18,7 @@ class TestSendVoice: message_id=42, date=datetime.datetime.now(), voice=Voice(file_id="file id", duration=0, file_unique_id="file id"), - chat=Chat(id=42, type="private"), + chat=ChatFactory(), ), ) @@ -35,7 +36,7 @@ class TestSendVoice: message_id=42, date=datetime.datetime.now(), voice=Voice(file_id="file id", duration=0, file_unique_id="file id"), - chat=Chat(id=42, type="private"), + chat=ChatFactory(), ), ) diff --git a/tests/test_api/test_types/test_message.py b/tests/test_api/test_types/test_message.py index 7b77afe2..c8cb59a2 100644 --- a/tests/test_api/test_types/test_message.py +++ b/tests/test_api/test_types/test_message.py @@ -25,7 +25,6 @@ from aiogram.api.methods import ( from aiogram.api.types import ( Animation, Audio, - Chat, Contact, Dice, Document, @@ -45,6 +44,7 @@ from aiogram.api.types import ( Voice, ) from aiogram.api.types.message import ContentType, Message +from tests.factories.chat import ChatFactory from tests.factories.user import UserFactory @@ -57,7 +57,7 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), text="test", - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.TEXT, @@ -67,7 +67,7 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), audio=Audio(file_id="file id", file_unique_id="file id", duration=42), - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.AUDIO, @@ -83,7 +83,7 @@ class TestMessage: height=42, duration=0, ), - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.ANIMATION, @@ -93,7 +93,7 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), document=Document(file_id="file id", file_unique_id="file id"), - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.DOCUMENT, @@ -111,7 +111,7 @@ class TestMessage: ) ], ), - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.GAME, @@ -123,7 +123,7 @@ class TestMessage: photo=[ PhotoSize(file_id="file id", file_unique_id="file id", width=42, height=42) ], - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.PHOTO, @@ -139,7 +139,7 @@ class TestMessage: height=42, is_animated=False, ), - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.STICKER, @@ -155,7 +155,7 @@ class TestMessage: height=42, duration=0, ), - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.VIDEO, @@ -167,7 +167,7 @@ class TestMessage: video_note=VideoNote( file_id="file id", file_unique_id="file id", length=0, duration=0 ), - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.VIDEO_NOTE, @@ -177,7 +177,7 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), voice=Voice(file_id="file id", file_unique_id="file id", duration=0), - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.VOICE, @@ -187,7 +187,7 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), contact=Contact(phone_number="911", first_name="911"), - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.CONTACT, @@ -202,7 +202,7 @@ class TestMessage: address="Under the stairs, 4 Privet Drive, " "Little Whinging, Surrey, England, Great Britain", ), - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.VENUE, @@ -212,7 +212,7 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), location=Location(longitude=3.14, latitude=3.14), - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.LOCATION, @@ -222,7 +222,7 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), new_chat_members=[UserFactory()], - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.NEW_CHAT_MEMBERS, @@ -232,7 +232,7 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), left_chat_member=UserFactory(), - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.LEFT_CHAT_MEMBER, @@ -248,7 +248,7 @@ class TestMessage: currency="BTC", total_amount=1, ), - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.INVOICE, @@ -264,7 +264,7 @@ class TestMessage: telegram_payment_charge_id="charge", provider_payment_charge_id="payment", ), - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.SUCCESSFUL_PAYMENT, @@ -274,7 +274,7 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), connected_website="token", - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.CONNECTED_WEBSITE, @@ -284,7 +284,7 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), migrate_from_chat_id=42, - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.MIGRATE_FROM_CHAT_ID, @@ -294,7 +294,7 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), migrate_to_chat_id=42, - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.MIGRATE_TO_CHAT_ID, @@ -307,10 +307,10 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), text="pinned", - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.PINNED_MESSAGE, @@ -320,7 +320,7 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), new_chat_title="test", - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.NEW_CHAT_TITLE, @@ -332,7 +332,7 @@ class TestMessage: new_chat_photo=[ PhotoSize(file_id="file id", file_unique_id="file id", width=42, height=42) ], - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.NEW_CHAT_PHOTO, @@ -342,7 +342,7 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), delete_chat_photo=True, - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.DELETE_CHAT_PHOTO, @@ -352,7 +352,7 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), group_chat_created=True, - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.GROUP_CHAT_CREATED, @@ -365,7 +365,7 @@ class TestMessage: data=[], credentials=EncryptedCredentials(data="test", hash="test", secret="test"), ), - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.PASSPORT_DATA, @@ -388,7 +388,7 @@ class TestMessage: total_voter_count=0, correct_option_id=1, ), - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.POLL, @@ -397,7 +397,7 @@ class TestMessage: Message( message_id=42, date=datetime.datetime.now(), - chat=Chat(id=42, type="private"), + chat=ChatFactory(), dice=Dice(value=6, emoji="X"), from_user=UserFactory(), ), @@ -407,7 +407,7 @@ class TestMessage: Message( message_id=42, date=datetime.datetime.now(), - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=UserFactory(), ), ContentType.UNKNOWN, @@ -485,7 +485,7 @@ class TestMessage: ], ): message = Message( - message_id=42, chat=Chat(id=42, type="private"), date=datetime.datetime.now() + message_id=42, chat=ChatFactory(), date=datetime.datetime.now() ) alias_name = "_".join(item for item in [alias_type, alias_for_method] if item) diff --git a/tests/test_dispatcher/test_handler/test_message.py b/tests/test_dispatcher/test_handler/test_message.py index d4cc4818..90882738 100644 --- a/tests/test_dispatcher/test_handler/test_message.py +++ b/tests/test_dispatcher/test_handler/test_message.py @@ -3,9 +3,10 @@ from typing import Any import pytest -from aiogram.api.types import Chat, Message, User +from aiogram.api.types import Message, User from aiogram.dispatcher.filters import CommandObject from aiogram.dispatcher.handler.message import MessageHandler, MessageHandlerCommandMixin +from tests.factories.chat import ChatFactory class MyHandler(MessageHandler): @@ -20,7 +21,7 @@ class TestClassBasedMessageHandler: message_id=42, date=datetime.datetime.now(), text="test", - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=User(id=42, is_bot=False, first_name="Test"), ) handler = MyHandler(event=event) @@ -41,7 +42,7 @@ class TestBaseMessageHandlerCommandMixin: message_id=42, date=datetime.datetime.now(), text="/test args", - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=User(id=42, is_bot=False, first_name="Test"), ), command=CommandObject(prefix="/", command="command", args="args"), @@ -56,7 +57,7 @@ class TestBaseMessageHandlerCommandMixin: message_id=42, date=datetime.datetime.now(), text="test", - chat=Chat(id=42, type="private"), + chat=ChatFactory(), from_user=User(id=42, is_bot=False, first_name="Test"), ) )