This commit is contained in:
Egor 2021-01-25 01:04:52 +02:00 committed by GitHub
commit f98c23689e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
50 changed files with 518 additions and 523 deletions

View file

@ -1,26 +1,17 @@
import datetime
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.message import MessageFactory
from tests.mocked_bot import MockedBot
class TestEditMessageCaption:
@pytest.mark.asyncio
async def test_method(self, bot: MockedBot):
prepare_result = bot.add_result_for(
EditMessageCaption,
ok=True,
result=Message(
message_id=42,
date=datetime.datetime.now(),
text="text",
chat=Chat(id=42, type="private"),
),
)
prepare_result = bot.add_result_for(EditMessageCaption, ok=True, result=MessageFactory())
response: Union[Message, bool] = await EditMessageCaption()
request: Request = bot.get_request()
@ -29,16 +20,7 @@ class TestEditMessageCaption:
@pytest.mark.asyncio
async def test_bot_method(self, bot: MockedBot):
prepare_result = bot.add_result_for(
EditMessageCaption,
ok=True,
result=Message(
message_id=42,
date=datetime.datetime.now(),
text="text",
chat=Chat(id=42, type="private"),
),
)
prepare_result = bot.add_result_for(EditMessageCaption, ok=True, result=MessageFactory())
response: Union[Message, bool] = await bot.edit_message_caption()
request: Request = bot.get_request()

View file

@ -3,7 +3,7 @@ from typing import Union
import pytest
from aiogram.api.methods import EditMessageMedia, Request
from aiogram.api.types import BufferedInputFile, InputMedia, InputMediaPhoto, Message
from aiogram.api.types import BufferedInputFile, InputMediaPhoto, Message
from tests.mocked_bot import MockedBot

View file

@ -4,43 +4,30 @@ 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
class TestForwardMessage:
@pytest.mark.asyncio
async def test_method(self, bot: MockedBot):
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",
),
)
async def test_method(self, bot: MockedBot, private_chat: Chat):
prepare_result = bot.add_result_for(ForwardMessage, ok=True, result=MessageFactory())
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):
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",
),
)
async def test_bot_method(self, bot: MockedBot, private_chat: Chat):
prepare_result = bot.add_result_for(ForwardMessage, ok=True, result=MessageFactory())
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 == {}

View file

@ -2,28 +2,28 @@ 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):
prepare_result = bot.add_result_for(
GetChat, ok=True, result=Chat(id=-42, type="channel", title="chat")
)
channel = ChatFactory(type=ChatType.CHANNEL)
prepare_result = bot.add_result_for(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):
prepare_result = bot.add_result_for(
GetChat, ok=True, result=Chat(id=-42, type="channel", title="chat")
)
channel = ChatFactory(type=ChatType.CHANNEL)
prepare_result = bot.add_result_for(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

View file

@ -3,7 +3,8 @@ from typing import List
import pytest
from aiogram.api.methods import GetChatAdministrators, Request
from aiogram.api.types import ChatMember, User
from aiogram.api.types import ChatMember
from tests.factories.chat_member import ChatMemberFactory
from tests.mocked_bot import MockedBot
@ -11,11 +12,7 @@ class TestGetChatAdministrators:
@pytest.mark.asyncio
async def test_method(self, bot: MockedBot):
prepare_result = bot.add_result_for(
GetChatAdministrators,
ok=True,
result=[
ChatMember(user=User(id=42, is_bot=False, first_name="User"), status="creator")
],
GetChatAdministrators, ok=True, result=[ChatMemberFactory(status="creator")]
)
response: List[ChatMember] = await GetChatAdministrators(chat_id=-42)
@ -26,11 +23,7 @@ class TestGetChatAdministrators:
@pytest.mark.asyncio
async def test_bot_method(self, bot: MockedBot):
prepare_result = bot.add_result_for(
GetChatAdministrators,
ok=True,
result=[
ChatMember(user=User(id=42, is_bot=False, first_name="User"), status="creator")
],
GetChatAdministrators, ok=True, result=[ChatMemberFactory(status="creator")]
)
response: List[ChatMember] = await bot.get_chat_administrators(chat_id=-42)
request: Request = bot.get_request()

View file

@ -1,7 +1,8 @@
import pytest
from aiogram.api.methods import GetChatMember, Request
from aiogram.api.types import ChatMember, User
from aiogram.api.types import ChatMember
from tests.factories.chat_member import ChatMemberFactory
from tests.mocked_bot import MockedBot
@ -9,9 +10,7 @@ class TestGetChatMember:
@pytest.mark.asyncio
async def test_method(self, bot: MockedBot):
prepare_result = bot.add_result_for(
GetChatMember,
ok=True,
result=ChatMember(user=User(id=42, is_bot=False, first_name="User"), status="creator"),
GetChatMember, ok=True, result=ChatMemberFactory(status="creator")
)
response: ChatMember = await GetChatMember(chat_id=-42, user_id=42)
@ -22,9 +21,7 @@ class TestGetChatMember:
@pytest.mark.asyncio
async def test_bot_method(self, bot: MockedBot):
prepare_result = bot.add_result_for(
GetChatMember,
ok=True,
result=ChatMember(user=User(id=42, is_bot=False, first_name="User"), status="creator"),
GetChatMember, ok=True, result=ChatMemberFactory(status="creator")
)
response: ChatMember = await bot.get_chat_member(chat_id=-42, user_id=42)

View file

@ -4,46 +4,41 @@ 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
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,
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=Chat(id=42, type="private"),
)
),
)
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,
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=Chat(id=42, type="private"),
)
),
)
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

View file

@ -3,43 +3,38 @@ 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.factories.message import MessageFactory
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,
result=Message(
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"),
result=MessageFactory(
audio=Audio(file_id="file id", duration=42, file_unique_id="file id")
),
)
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,
result=Message(
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"),
result=MessageFactory(
audio=Audio(file_id="file id", duration=42, file_unique_id="file id")
),
)
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

View file

@ -1,46 +1,37 @@
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
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,
result=Message(
message_id=42,
date=datetime.datetime.now(),
contact=Contact(phone_number="911", first_name="911"),
chat=Chat(id=42, type="private"),
),
result=MessageFactory(contact=Contact(phone_number="911", first_name="911")),
)
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,
result=Message(
message_id=42,
date=datetime.datetime.now(),
contact=Contact(phone_number="911", first_name="911"),
chat=Chat(id=42, type="private"),
),
result=MessageFactory(contact=Contact(phone_number="911", first_name="911")),
)
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"

View file

@ -4,42 +4,33 @@ 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
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,
result=Message(
message_id=42,
date=datetime.datetime.now(),
document=Document(file_id="file id", file_unique_id="file id"),
chat=Chat(id=42, type="private"),
),
result=MessageFactory(document=Document(file_id="file id", file_unique_id="file id")),
)
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,
result=Message(
message_id=42,
date=datetime.datetime.now(),
document=Document(file_id="file id", file_unique_id="file id"),
chat=Chat(id=42, type="private"),
),
result=MessageFactory(document=Document(file_id="file id", file_unique_id="file id")),
)
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

View file

@ -4,54 +4,49 @@ 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
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,
result=Message(
message_id=42,
date=datetime.datetime.now(),
result=MessageFactory(
game=Game(
title="title",
description="description",
photo=[
PhotoSize(file_id="file id", width=42, height=42, file_unique_id="file id")
],
),
chat=Chat(id=42, type="private"),
)
),
)
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,
result=Message(
message_id=42,
date=datetime.datetime.now(),
result=MessageFactory(
game=Game(
title="title",
description="description",
photo=[
PhotoSize(file_id="file id", width=42, height=42, file_unique_id="file id")
],
),
chat=Chat(id=42, type="private"),
)
),
)
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

View file

@ -1,34 +1,30 @@
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
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,
result=Message(
message_id=42,
date=datetime.datetime.now(),
result=MessageFactory(
invoice=Invoice(
title="test",
description="test",
start_parameter="brilliant",
currency="BTC",
total_amount=1,
),
chat=Chat(id=42, type="private"),
)
),
)
response: Message = await SendInvoice(
chat_id=42,
chat_id=private_chat.id,
title="test",
description="test",
payload="payload",
@ -42,26 +38,23 @@ 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,
result=Message(
message_id=42,
date=datetime.datetime.now(),
result=MessageFactory(
invoice=Invoice(
title="test",
description="test",
start_parameter="brilliant",
currency="BTC",
total_amount=1,
),
chat=Chat(id=42, type="private"),
)
),
)
response: Message = await bot.send_invoice(
chat_id=42,
chat_id=private_chat.id,
title="test",
description="test",
payload="payload",

View file

@ -4,42 +4,37 @@ 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
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,
result=Message(
message_id=42,
date=datetime.datetime.now(),
location=Location(longitude=3.14, latitude=3.14),
chat=Chat(id=42, type="private"),
),
result=MessageFactory(location=Location(longitude=3.14, latitude=3.14)),
)
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,
result=Message(
message_id=42,
date=datetime.datetime.now(),
location=Location(longitude=3.14, latitude=3.14),
chat=Chat(id=42, type="private"),
),
result=MessageFactory(location=Location(longitude=3.14, latitude=3.14)),
)
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

View file

@ -13,28 +13,24 @@ from aiogram.api.types import (
PhotoSize,
Video,
)
from tests.factories.message import MessageFactory
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,
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=Chat(id=42, type="private"),
),
Message(
message_id=43,
date=datetime.datetime.now(),
MessageFactory(
video=Video(
file_id="file id",
width=42,
@ -43,13 +39,12 @@ class TestSendMediaGroup:
file_unique_id="file id",
),
media_group_id="media group",
chat=Chat(id=42, type="private"),
),
],
)
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,23 +55,18 @@ 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,
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=Chat(id=42, type="private"),
),
Message(
message_id=43,
date=datetime.datetime.now(),
MessageFactory(
video=Video(
file_id="file id",
width=42,
@ -85,13 +75,12 @@ class TestSendMediaGroup:
file_unique_id="file id",
),
media_group_id="media group",
chat=Chat(id=42, type="private"),
),
],
)
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")),

View file

@ -4,22 +4,14 @@ 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
class TestSendMessage:
@pytest.mark.asyncio
async def test_method(self, bot: MockedBot):
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"),
),
)
prepare_result = bot.add_result_for(SendMessage, ok=True, result=MessageFactory())
response: Message = await SendMessage(chat_id=42, text="test")
request: Request = bot.get_request()
@ -28,16 +20,7 @@ class TestSendMessage:
@pytest.mark.asyncio
async def test_bot_method(self, bot: MockedBot):
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"),
),
)
prepare_result = bot.add_result_for(SendMessage, ok=True, result=MessageFactory())
response: Message = await bot.send_message(chat_id=42, text="test")
request: Request = bot.get_request()

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,8 @@ class TestSendPhoto:
prepare_result = bot.add_result_for(
SendPhoto,
ok=True,
result=Message(
message_id=42,
date=datetime.datetime.now(),
photo=[
PhotoSize(file_id="file id", width=42, height=42, file_unique_id="file id")
],
chat=Chat(id=42, type="private"),
result=MessageFactory(
photo=[PhotoSize(file_id="file id", width=42, height=42, file_unique_id="file id")]
),
)
@ -33,13 +27,8 @@ class TestSendPhoto:
prepare_result = bot.add_result_for(
SendPhoto,
ok=True,
result=Message(
message_id=42,
date=datetime.datetime.now(),
photo=[
PhotoSize(file_id="file id", width=42, height=42, file_unique_id="file id")
],
chat=Chat(id=42, type="private"),
result=MessageFactory(
photo=[PhotoSize(file_id="file id", width=42, height=42, file_unique_id="file id")]
),
)

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,27 @@ 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 +65,16 @@ 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"

View file

@ -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

View file

@ -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"

View file

@ -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(),
),
)

View file

@ -1,6 +1,6 @@
import pytest
from aiogram.api.methods import Request, SetChatAdministratorCustomTitle, SetChatTitle
from aiogram.api.methods import Request, SetChatAdministratorCustomTitle
from tests.mocked_bot import MockedBot

View file

@ -1,7 +1,7 @@
import pytest
from aiogram.api.methods import Request, SetChatPhoto
from aiogram.api.types import BufferedInputFile, InputFile
from aiogram.api.types import BufferedInputFile
from tests.mocked_bot import MockedBot

View file

@ -1,12 +1,11 @@
from aiogram.api.methods import AnswerCallbackQuery
from aiogram.api.types import CallbackQuery, User
from aiogram.api.types import CallbackQuery
from tests.factories.user import UserFactory
class TestCallbackQuery:
def test_answer_alias(self):
callback_query = CallbackQuery(
id="id", from_user=User(id=42, is_bot=False, first_name="name"), chat_instance="chat"
)
callback_query = CallbackQuery(id="id", from_user=UserFactory(), chat_instance="chat")
kwargs = dict(text="foo", show_alert=True, url="https://foo.bar/", cache_time=123)

View file

@ -1,8 +1,6 @@
import pytest
from aiogram.api.types import ChatMember, User
user = User(id=42, is_bot=False, first_name="User", last_name=None)
from tests.factories.chat_member import ChatMemberFactory
class TestChatMember:
@ -10,7 +8,7 @@ class TestChatMember:
"status,result", [["administrator", True], ["creator", True], ["member", False]]
)
def test_is_chat_admin(self, status: str, result: bool):
chat_member = ChatMember(user=user, status=status)
chat_member = ChatMemberFactory(status=status)
assert chat_member.is_chat_admin == result
@pytest.mark.parametrize(
@ -25,5 +23,5 @@ class TestChatMember:
],
)
def test_is_chat_member(self, status: str, result: bool):
chat_member = ChatMember(user=user, status=status)
chat_member = ChatMemberFactory(status=status)
assert chat_member.is_chat_member == result

View file

@ -1,15 +1,11 @@
from aiogram.api.methods import AnswerInlineQuery
from aiogram.api.types import InlineQuery, User
from aiogram.api.types import InlineQuery
from tests.factories.user import UserFactory
class TestInlineQuery:
def test_answer_alias(self):
inline_query = InlineQuery(
id="id",
from_user=User(id=42, is_bot=False, first_name="name"),
query="query",
offset="",
)
inline_query = InlineQuery(id="id", from_user=UserFactory(), query="query", offset="",)
kwargs = dict(
results=[],

View file

@ -25,7 +25,6 @@ from aiogram.api.methods import (
from aiogram.api.types import (
Animation,
Audio,
Chat,
Contact,
Dice,
Document,
@ -39,13 +38,15 @@ from aiogram.api.types import (
PollOption,
Sticker,
SuccessfulPayment,
User,
Venue,
Video,
VideoNote,
Voice,
)
from aiogram.api.types.message import ContentType, Message
from tests.factories.chat import ChatFactory
from tests.factories.message import MessageFactory
from tests.factories.user import UserFactory
class TestMessage:
@ -53,12 +54,7 @@ class TestMessage:
"message,content_type",
[
[
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"),
MessageFactory(
),
ContentType.TEXT,
],
@ -67,8 +63,8 @@ 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"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.AUDIO,
],
@ -83,8 +79,8 @@ class TestMessage:
height=42,
duration=0,
),
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.ANIMATION,
],
@ -93,8 +89,8 @@ 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"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.DOCUMENT,
],
@ -111,8 +107,8 @@ class TestMessage:
)
],
),
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.GAME,
],
@ -123,8 +119,8 @@ class TestMessage:
photo=[
PhotoSize(file_id="file id", file_unique_id="file id", width=42, height=42)
],
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.PHOTO,
],
@ -139,8 +135,8 @@ class TestMessage:
height=42,
is_animated=False,
),
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.STICKER,
],
@ -155,8 +151,8 @@ class TestMessage:
height=42,
duration=0,
),
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.VIDEO,
],
@ -167,8 +163,8 @@ class TestMessage:
video_note=VideoNote(
file_id="file id", file_unique_id="file id", length=0, duration=0
),
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.VIDEO_NOTE,
],
@ -177,8 +173,8 @@ 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"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.VOICE,
],
@ -187,8 +183,8 @@ class TestMessage:
message_id=42,
date=datetime.datetime.now(),
contact=Contact(phone_number="911", first_name="911"),
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.CONTACT,
],
@ -202,8 +198,8 @@ class TestMessage:
address="Under the stairs, 4 Privet Drive, "
"Little Whinging, Surrey, England, Great Britain",
),
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.VENUE,
],
@ -212,8 +208,8 @@ class TestMessage:
message_id=42,
date=datetime.datetime.now(),
location=Location(longitude=3.14, latitude=3.14),
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.LOCATION,
],
@ -221,9 +217,9 @@ class TestMessage:
Message(
message_id=42,
date=datetime.datetime.now(),
new_chat_members=[User(id=42, is_bot=False, first_name="Test")],
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
new_chat_members=[UserFactory()],
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.NEW_CHAT_MEMBERS,
],
@ -231,9 +227,9 @@ class TestMessage:
Message(
message_id=42,
date=datetime.datetime.now(),
left_chat_member=User(id=42, is_bot=False, first_name="Test"),
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
left_chat_member=UserFactory(),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.LEFT_CHAT_MEMBER,
],
@ -248,8 +244,8 @@ class TestMessage:
currency="BTC",
total_amount=1,
),
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.INVOICE,
],
@ -264,8 +260,8 @@ class TestMessage:
telegram_payment_charge_id="charge",
provider_payment_charge_id="payment",
),
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.SUCCESSFUL_PAYMENT,
],
@ -274,8 +270,8 @@ class TestMessage:
message_id=42,
date=datetime.datetime.now(),
connected_website="token",
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.CONNECTED_WEBSITE,
],
@ -284,8 +280,8 @@ class TestMessage:
message_id=42,
date=datetime.datetime.now(),
migrate_from_chat_id=42,
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.MIGRATE_FROM_CHAT_ID,
],
@ -294,8 +290,8 @@ class TestMessage:
message_id=42,
date=datetime.datetime.now(),
migrate_to_chat_id=42,
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.MIGRATE_TO_CHAT_ID,
],
@ -307,11 +303,11 @@ class TestMessage:
message_id=42,
date=datetime.datetime.now(),
text="pinned",
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.PINNED_MESSAGE,
],
@ -320,8 +316,8 @@ class TestMessage:
message_id=42,
date=datetime.datetime.now(),
new_chat_title="test",
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.NEW_CHAT_TITLE,
],
@ -332,8 +328,8 @@ 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"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.NEW_CHAT_PHOTO,
],
@ -342,8 +338,8 @@ class TestMessage:
message_id=42,
date=datetime.datetime.now(),
delete_chat_photo=True,
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.DELETE_CHAT_PHOTO,
],
@ -352,8 +348,8 @@ class TestMessage:
message_id=42,
date=datetime.datetime.now(),
group_chat_created=True,
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.GROUP_CHAT_CREATED,
],
@ -365,8 +361,8 @@ class TestMessage:
data=[],
credentials=EncryptedCredentials(data="test", hash="test", secret="test"),
),
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.PASSPORT_DATA,
],
@ -388,8 +384,8 @@ class TestMessage:
total_voter_count=0,
correct_option_id=1,
),
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.POLL,
],
@ -397,9 +393,9 @@ 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=User(id=42, is_bot=False, first_name="Test"),
from_user=UserFactory(),
),
ContentType.DICE,
],
@ -407,8 +403,8 @@ class TestMessage:
Message(
message_id=42,
date=datetime.datetime.now(),
chat=Chat(id=42, type="private"),
from_user=User(id=42, is_bot=False, first_name="Test"),
chat=ChatFactory(),
from_user=UserFactory(),
),
ContentType.UNKNOWN,
],
@ -484,9 +480,7 @@ class TestMessage:
]
],
):
message = Message(
message_id=42, chat=Chat(id=42, type="private"), date=datetime.datetime.now()
)
message = Message(message_id=42, chat=ChatFactory(), date=datetime.datetime.now())
alias_name = "_".join(item for item in [alias_type, alias_for_method] if item)
alias = getattr(message, alias_name)

View file

@ -1,12 +1,13 @@
from aiogram.api.methods import AnswerPreCheckoutQuery
from aiogram.api.types import PreCheckoutQuery, User
from aiogram.api.types import PreCheckoutQuery
from tests.factories.user import UserFactory
class TestPreCheckoutQuery:
def test_answer_alias(self):
pre_checkout_query = PreCheckoutQuery(
id="id",
from_user=User(id=42, is_bot=False, first_name="name"),
from_user=UserFactory(),
currency="currency",
total_amount=123,
invoice_payload="payload",

View file

@ -1,12 +1,13 @@
from aiogram.api.methods import AnswerShippingQuery
from aiogram.api.types import LabeledPrice, ShippingAddress, ShippingOption, ShippingQuery, User
from aiogram.api.types import LabeledPrice, ShippingAddress, ShippingOption, ShippingQuery
from tests.factories.user import UserFactory
class TestInlineQuery:
def test_answer_alias(self):
shipping_query = ShippingQuery(
id="id",
from_user=User(id=42, is_bot=False, first_name="name"),
from_user=UserFactory(),
invoice_payload="payload",
shipping_address=ShippingAddress(
country_code="foo",

View file

@ -1,11 +1,11 @@
import pytest
from aiogram.api.types import User
from tests.factories.user import UserFactory
class TestUser:
@pytest.mark.parametrize(
"first,last,result",
"first_name,last_name,result",
[
["User", None, "User"],
["", None, ""],
@ -15,6 +15,6 @@ class TestUser:
[" ", " ", " "],
],
)
def test_full_name(self, first: str, last: str, result: bool):
user = User(id=42, is_bot=False, first_name=first, last_name=last)
def test_full_name(self, first_name: str, last_name: str, result: bool):
user = UserFactory(first_name=first_name, last_name=last_name)
assert user.full_name == result