refator: use MessageFactory instead of Message, just a part

This commit is contained in:
Egor 2020-07-03 17:08:37 +05:00
parent e883366dc6
commit 0e005f661c
15 changed files with 54 additions and 143 deletions

View file

@ -1,11 +1,10 @@
import datetime
from typing import Union
import pytest
from aiogram.api.methods import EditMessageCaption, Request
from aiogram.api.types import Message
from tests.factories.chat import ChatFactory
from tests.factories.message import MessageFactory
from tests.mocked_bot import MockedBot
@ -15,9 +14,7 @@ class TestEditMessageCaption:
prepare_result = bot.add_result_for(
EditMessageCaption,
ok=True,
result=Message(
message_id=42, date=datetime.datetime.now(), text="text", chat=ChatFactory(),
),
result=MessageFactory(),
)
response: Union[Message, bool] = await EditMessageCaption()
@ -30,9 +27,7 @@ class TestEditMessageCaption:
prepare_result = bot.add_result_for(
EditMessageCaption,
ok=True,
result=Message(
message_id=42, date=datetime.datetime.now(), text="text", chat=ChatFactory(),
),
result=MessageFactory(),
)
response: Union[Message, bool] = await bot.edit_message_caption()

View file

@ -4,6 +4,7 @@ import pytest
from aiogram.api.methods import ForwardMessage, Request
from aiogram.api.types import Chat, Message
from tests.factories.message import MessageFactory
from tests.mocked_bot import MockedBot
@ -13,8 +14,7 @@ class TestForwardMessage:
prepare_result = bot.add_result_for(
ForwardMessage,
ok=True,
result=Message(
message_id=42, date=datetime.datetime.now(), chat=private_chat, text="text",
result=MessageFactory(
),
)
@ -31,8 +31,7 @@ class TestForwardMessage:
prepare_result = bot.add_result_for(
ForwardMessage,
ok=True,
result=Message(
message_id=42, date=datetime.datetime.now(), chat=private_chat, text="text",
result=MessageFactory(
),
)

View file

@ -4,6 +4,7 @@ import pytest
from aiogram.api.methods import Request, SendAnimation
from aiogram.api.types import Animation, Chat, Message
from tests.factories.message import MessageFactory
from tests.mocked_bot import MockedBot
@ -13,13 +14,10 @@ class TestSendAnimation:
prepare_result = bot.add_result_for(
SendAnimation,
ok=True,
result=Message(
message_id=42,
date=datetime.datetime.now(),
result=MessageFactory(
animation=Animation(
file_id="file id", width=42, height=42, duration=0, file_unique_id="file id"
),
chat=private_chat,
),
)
@ -33,13 +31,10 @@ class TestSendAnimation:
prepare_result = bot.add_result_for(
SendAnimation,
ok=True,
result=Message(
message_id=42,
date=datetime.datetime.now(),
result=MessageFactory(
animation=Animation(
file_id="file id", width=42, height=42, duration=0, file_unique_id="file id"
),
chat=private_chat,
),
)

View file

@ -4,6 +4,7 @@ import pytest
from aiogram.api.methods import Request, SendAudio
from aiogram.api.types import Audio, Chat, Message
from tests.factories.message import MessageFactory
from tests.mocked_bot import MockedBot
@ -13,11 +14,8 @@ class TestSendAudio:
prepare_result = bot.add_result_for(
SendAudio,
ok=True,
result=Message(
message_id=42,
date=datetime.datetime.now(),
result=MessageFactory(
audio=Audio(file_id="file id", duration=42, file_unique_id="file id"),
chat=private_chat,
),
)
@ -31,11 +29,8 @@ class TestSendAudio:
prepare_result = bot.add_result_for(
SendAudio,
ok=True,
result=Message(
message_id=42,
date=datetime.datetime.now(),
result=MessageFactory(
audio=Audio(file_id="file id", duration=42, file_unique_id="file id"),
chat=private_chat,
),
)

View file

@ -1,9 +1,8 @@
import datetime
import pytest
from aiogram.api.methods import Request, SendContact
from aiogram.api.types import Chat, Contact, Message
from tests.factories.message import MessageFactory
from tests.mocked_bot import MockedBot
@ -13,11 +12,8 @@ class TestSendContact:
prepare_result = bot.add_result_for(
SendContact,
ok=True,
result=Message(
message_id=42,
date=datetime.datetime.now(),
result=MessageFactory(
contact=Contact(phone_number="911", first_name="911"),
chat=private_chat,
),
)
@ -33,11 +29,8 @@ class TestSendContact:
prepare_result = bot.add_result_for(
SendContact,
ok=True,
result=Message(
message_id=42,
date=datetime.datetime.now(),
result=MessageFactory(
contact=Contact(phone_number="911", first_name="911"),
chat=private_chat,
),
)

View file

@ -4,6 +4,7 @@ import pytest
from aiogram.api.methods import Request, SendDocument
from aiogram.api.types import Chat, Document, Message
from tests.factories.message import MessageFactory
from tests.mocked_bot import MockedBot
@ -13,11 +14,8 @@ class TestSendDocument:
prepare_result = bot.add_result_for(
SendDocument,
ok=True,
result=Message(
message_id=42,
date=datetime.datetime.now(),
result=MessageFactory(
document=Document(file_id="file id", file_unique_id="file id"),
chat=private_chat,
),
)
@ -31,11 +29,8 @@ class TestSendDocument:
prepare_result = bot.add_result_for(
SendDocument,
ok=True,
result=Message(
message_id=42,
date=datetime.datetime.now(),
result=MessageFactory(
document=Document(file_id="file id", file_unique_id="file id"),
chat=private_chat,
),
)

View file

@ -4,6 +4,7 @@ import pytest
from aiogram.api.methods import Request, SendGame
from aiogram.api.types import Chat, Game, Message, PhotoSize
from tests.factories.message import MessageFactory
from tests.mocked_bot import MockedBot
@ -13,9 +14,7 @@ class TestSendGame:
prepare_result = bot.add_result_for(
SendGame,
ok=True,
result=Message(
message_id=42,
date=datetime.datetime.now(),
result=MessageFactory(
game=Game(
title="title",
description="description",
@ -23,7 +22,6 @@ class TestSendGame:
PhotoSize(file_id="file id", width=42, height=42, file_unique_id="file id")
],
),
chat=private_chat,
),
)
@ -37,9 +35,7 @@ class TestSendGame:
prepare_result = bot.add_result_for(
SendGame,
ok=True,
result=Message(
message_id=42,
date=datetime.datetime.now(),
result=MessageFactory(
game=Game(
title="title",
description="description",
@ -47,7 +43,6 @@ class TestSendGame:
PhotoSize(file_id="file id", width=42, height=42, file_unique_id="file id")
],
),
chat=private_chat,
),
)

View file

@ -1,9 +1,8 @@
import datetime
import pytest
from aiogram.api.methods import Request, SendInvoice
from aiogram.api.types import Chat, Invoice, LabeledPrice, Message
from tests.factories.message import MessageFactory
from tests.mocked_bot import MockedBot
@ -13,9 +12,7 @@ class TestSendInvoice:
prepare_result = bot.add_result_for(
SendInvoice,
ok=True,
result=Message(
message_id=42,
date=datetime.datetime.now(),
result=MessageFactory(
invoice=Invoice(
title="test",
description="test",
@ -23,7 +20,6 @@ class TestSendInvoice:
currency="BTC",
total_amount=1,
),
chat=private_chat,
),
)
@ -46,9 +42,7 @@ class TestSendInvoice:
prepare_result = bot.add_result_for(
SendInvoice,
ok=True,
result=Message(
message_id=42,
date=datetime.datetime.now(),
result=MessageFactory(
invoice=Invoice(
title="test",
description="test",
@ -56,7 +50,6 @@ class TestSendInvoice:
currency="BTC",
total_amount=1,
),
chat=private_chat,
),
)

View file

@ -4,6 +4,7 @@ import pytest
from aiogram.api.methods import Request, SendLocation
from aiogram.api.types import Chat, Location, Message
from tests.factories.message import MessageFactory
from tests.mocked_bot import MockedBot
@ -13,11 +14,8 @@ class TestSendLocation:
prepare_result = bot.add_result_for(
SendLocation,
ok=True,
result=Message(
message_id=42,
date=datetime.datetime.now(),
result=MessageFactory(
location=Location(longitude=3.14, latitude=3.14),
chat=private_chat,
),
)
@ -33,11 +31,8 @@ class TestSendLocation:
prepare_result = bot.add_result_for(
SendLocation,
ok=True,
result=Message(
message_id=42,
date=datetime.datetime.now(),
result=MessageFactory(
location=Location(longitude=3.14, latitude=3.14),
chat=private_chat,
),
)

View file

@ -13,6 +13,7 @@ from aiogram.api.types import (
PhotoSize,
Video,
)
from tests.factories.message import MessageFactory
from tests.mocked_bot import MockedBot
@ -23,18 +24,13 @@ class TestSendMediaGroup:
SendMediaGroup,
ok=True,
result=[
Message(
message_id=42,
date=datetime.datetime.now(),
MessageFactory(
photo=[
PhotoSize(file_id="file id", width=42, height=42, file_unique_id="file id")
],
media_group_id="media group",
chat=private_chat,
),
Message(
message_id=43,
date=datetime.datetime.now(),
MessageFactory(
video=Video(
file_id="file id",
width=42,
@ -43,7 +39,6 @@ class TestSendMediaGroup:
file_unique_id="file id",
),
media_group_id="media group",
chat=private_chat,
),
],
)
@ -65,18 +60,13 @@ class TestSendMediaGroup:
SendMediaGroup,
ok=True,
result=[
Message(
message_id=42,
date=datetime.datetime.now(),
MessageFactory(
photo=[
PhotoSize(file_id="file id", width=42, height=42, file_unique_id="file id")
],
media_group_id="media group",
chat=private_chat,
),
Message(
message_id=43,
date=datetime.datetime.now(),
MessageFactory(
video=Video(
file_id="file id",
width=42,
@ -85,7 +75,6 @@ class TestSendMediaGroup:
file_unique_id="file id",
),
media_group_id="media group",
chat=private_chat,
),
],
)

View file

@ -4,6 +4,7 @@ import pytest
from aiogram.api.methods import Request, SendMessage
from aiogram.api.types import Chat, Message
from tests.factories.message import MessageFactory
from tests.mocked_bot import MockedBot
@ -13,11 +14,7 @@ class TestSendMessage:
prepare_result = bot.add_result_for(
SendMessage,
ok=True,
result=Message(
message_id=42,
date=datetime.datetime.now(),
text="test",
chat=Chat(id=42, type="private"),
result=MessageFactory(
),
)
@ -31,11 +28,7 @@ class TestSendMessage:
prepare_result = bot.add_result_for(
SendMessage,
ok=True,
result=Message(
message_id=42,
date=datetime.datetime.now(),
text="test",
chat=Chat(id=42, type="private"),
result=MessageFactory(
),
)

View file

@ -1,9 +1,8 @@
import datetime
import pytest
from aiogram.api.methods import Request, SendPhoto
from aiogram.api.types import Chat, Message, PhotoSize
from aiogram.api.types import Message, PhotoSize
from tests.factories.message import MessageFactory
from tests.mocked_bot import MockedBot
@ -13,13 +12,10 @@ class TestSendPhoto:
prepare_result = bot.add_result_for(
SendPhoto,
ok=True,
result=Message(
message_id=42,
date=datetime.datetime.now(),
result=MessageFactory(
photo=[
PhotoSize(file_id="file id", width=42, height=42, file_unique_id="file id")
],
chat=Chat(id=42, type="private"),
),
)
@ -33,13 +29,10 @@ class TestSendPhoto:
prepare_result = bot.add_result_for(
SendPhoto,
ok=True,
result=Message(
message_id=42,
date=datetime.datetime.now(),
result=MessageFactory(
photo=[
PhotoSize(file_id="file id", width=42, height=42, file_unique_id="file id")
],
chat=Chat(id=42, type="private"),
),
)

View file

@ -4,18 +4,18 @@ import pytest
from aiogram.api.methods import Request, SendPoll
from aiogram.api.types import Chat, Message, Poll, PollOption
from tests.conftest import private_chat
from tests.factories.message import MessageFactory
from tests.mocked_bot import MockedBot
class TestSendPoll:
@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(
SendPoll,
ok=True,
result=Message(
message_id=42,
date=datetime.datetime.now(),
result=MessageFactory(
poll=Poll(
id="QA",
question="Q",
@ -30,25 +30,23 @@ class TestSendPoll:
total_voter_count=0,
correct_option_id=0,
),
chat=Chat(id=42, type="private"),
chat=private_chat
),
)
response: Message = await SendPoll(
chat_id=42, question="Q?", options=["A", "B"], correct_option_id=0, type="quiz"
chat_id=private_chat.id, question="Q?", options=["A", "B"], correct_option_id=0, type="quiz"
)
request: Request = bot.get_request()
assert request.method == "sendPoll"
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(
SendPoll,
ok=True,
result=Message(
message_id=42,
date=datetime.datetime.now(),
result=MessageFactory(
poll=Poll(
id="QA",
question="Q",
@ -63,12 +61,12 @@ class TestSendPoll:
total_voter_count=0,
correct_option_id=0,
),
chat=Chat(id=42, type="private"),
chat=private_chat
),
)
response: Message = await bot.send_poll(
chat_id=42, question="Q?", options=["A", "B"], correct_option_id=0, type="quiz"
chat_id=private_chat.id, question="Q?", options=["A", "B"], correct_option_id=0, type="quiz"
)
request: Request = bot.get_request()
assert request.method == "sendPoll"