mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-06 16:15:51 +00:00
Pytest part two
This commit is contained in:
parent
4ba810fe25
commit
c3e7728866
2 changed files with 134 additions and 217 deletions
|
|
@ -6,6 +6,33 @@ from aiogram import Bot, types
|
|||
TOKEN = '123456789:AABBCCDDEEFFaabbccddeeff-1234567890'
|
||||
|
||||
|
||||
class FakeTelegram(aresponses.ResponsesMockServer):
|
||||
def __init__(self, message_dict, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self._body, self._headers = self.parse_data(message_dict)
|
||||
|
||||
async def __aenter__(self):
|
||||
await super().__aenter__()
|
||||
_response = self.Response(text=self._body, headers=self._headers, status=200, reason='OK')
|
||||
self.add(self.ANY, response=_response)
|
||||
|
||||
@staticmethod
|
||||
def parse_data(message_dict):
|
||||
import json
|
||||
|
||||
_body = '{"ok":true,"result":' + json.dumps(message_dict) + '}'
|
||||
_headers = {'Server': 'nginx/1.12.2',
|
||||
'Date': 'Tue, 03 Apr 2018 16:59:54 GMT',
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': str(len(_body)),
|
||||
'Connection': 'keep-alive',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
|
||||
'Access-Control-Expose-Headers': 'Content-Length,Content-Type,Date,Server,Connection',
|
||||
'Strict-Transport-Security': 'max-age=31536000; includeSubdomains'}
|
||||
return _body, _headers
|
||||
|
||||
|
||||
@pytest.yield_fixture()
|
||||
@pytest.mark.asyncio
|
||||
async def bot(event_loop):
|
||||
|
|
@ -16,50 +43,35 @@ async def bot(event_loop):
|
|||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_bot(bot, event_loop):
|
||||
async def test_get_me(bot: Bot, event_loop):
|
||||
""" getMe method test """
|
||||
_body = '{"ok":true,"result":{"id":492189143,"is_bot":true,' \
|
||||
'"first_name":"Dev Tester","username":"MiscDevTesterBot"}}'
|
||||
_headers = {'Server': 'nginx/1.12.2',
|
||||
'Date': 'Tue, 03 Apr 2018 16:59:54 GMT',
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': str(len(_body)),
|
||||
'Connection': 'keep-alive',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
|
||||
'Access-Control-Expose-Headers': 'Content-Length,Content-Type,Date,Server,Connection',
|
||||
'Strict-Transport-Security': 'max-age=31536000; includeSubdomains'}
|
||||
async with aresponses.ResponsesMockServer(loop=event_loop) as server:
|
||||
server.add(server.ANY, response=server.Response(text=_body,
|
||||
status=200,
|
||||
reason='OK',
|
||||
headers=_headers))
|
||||
bot_user = await bot.me
|
||||
assert isinstance(bot_user, types.User)
|
||||
from .types.dataset import USER
|
||||
user = types.User(**USER)
|
||||
|
||||
async with FakeTelegram(message_dict=USER, loop=event_loop):
|
||||
result = await bot.get_me()
|
||||
assert result == user
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_message(bot, event_loop):
|
||||
""" SendMessage method test """
|
||||
message_text = 'Test message'
|
||||
chat_id = -1234567890
|
||||
_body = """{"ok":true,"result":{"message_id":74,"from":{"id":492189143,"is_bot":true,"first_name":"Dev Tester",
|
||||
"username":"MiscDevTesterBot"},"chat":{"id":66812456,"first_name":"O","username":"Oleg_Oleg_Oleg","type":"private"},
|
||||
"date":1522774794,"text":"Test message"}}"""
|
||||
_headers = {'Server': 'nginx/1.12.2',
|
||||
'Date': 'Tue, 03 Apr 2018 16:59:54 GMT',
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': str(len(_body)),
|
||||
'Connection': 'keep-alive',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
|
||||
'Access-Control-Expose-Headers': 'Content-Length,Content-Type,Date,Server,Connection',
|
||||
'Strict-Transport-Security': 'max-age=31536000; includeSubdomains'}
|
||||
async def test_send_message(bot: Bot, event_loop):
|
||||
""" sendMessage method test """
|
||||
from .types.dataset import MESSAGE
|
||||
msg = types.Message(**MESSAGE)
|
||||
|
||||
async with aresponses.ResponsesMockServer(loop=event_loop) as server:
|
||||
server.add(server.ANY, response=server.Response(text=_body,
|
||||
status=200,
|
||||
reason='OK',
|
||||
headers=_headers))
|
||||
msg: types.Message = await bot.send_message(chat_id=chat_id, text=message_text)
|
||||
assert msg.text == message_text
|
||||
async with FakeTelegram(message_dict=MESSAGE, loop=event_loop):
|
||||
result = await bot.send_message(chat_id=msg.chat.id, text=msg.text)
|
||||
assert result == msg
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_forward_message(bot: Bot, event_loop):
|
||||
""" forwardMessage method test """
|
||||
from .types.dataset import FORWARDED_MESSAGE
|
||||
msg = types.Message(**FORWARDED_MESSAGE)
|
||||
from_chat = -1234567890
|
||||
|
||||
async with FakeTelegram(message_dict=FORWARDED_MESSAGE, loop=event_loop):
|
||||
result = await bot.forward_message(chat_id=msg.chat.id, from_chat_id=from_chat,
|
||||
message_id=msg.forward_from_message_id)
|
||||
assert result == msg
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue