Update default parse_mode propagation.

This commit is contained in:
Alex Root Junior 2020-06-14 18:18:29 +03:00
parent 4d631a3069
commit f5684aef07
80 changed files with 552 additions and 185 deletions

View file

@ -25,9 +25,9 @@ class MockedSession(BaseSession):
pass
async def make_request(
self, token: str, method: TelegramMethod[T], timeout: Optional[int] = UNSET
self, bot: Bot, method: TelegramMethod[T], timeout: Optional[int] = UNSET
) -> T:
self.requests.append(method.build_request())
self.requests.append(method.build_request(bot))
response: Response[T] = self.responses.pop()
self.raise_for_status(response)
return response.result # type: ignore

View file

@ -42,12 +42,13 @@ class TestBot:
new_callable=CoroutineMock,
) as mocked_make_request:
await bot(method)
mocked_make_request.assert_awaited_with("42:TEST", method)
mocked_make_request.assert_awaited_with(bot, method, timeout=None)
@pytest.mark.asyncio
async def test_close(self):
bot = Bot("42:TEST", session=AiohttpSession())
await bot.session.create_session()
session = AiohttpSession()
bot = Bot("42:TEST", session=session)
await session.create_session()
with patch(
"aiogram.api.client.session.aiohttp.AiohttpSession.close", new_callable=CoroutineMock

View file

@ -5,9 +5,11 @@ import aiohttp_socks
import pytest
from aresponses import ResponsesMockServer
from aiogram import Bot
from aiogram.api.client.session.aiohttp import AiohttpSession
from aiogram.api.methods import Request, TelegramMethod
from aiogram.api.types import InputFile
from tests.mocked_bot import MockedBot
try:
from asynctest import CoroutineMock, patch
@ -147,7 +149,7 @@ class TestAiohttpSession:
assert isinstance(fields[1][2], BareInputFile)
@pytest.mark.asyncio
async def test_make_request(self, aresponses: ResponsesMockServer):
async def test_make_request(self, bot: MockedBot, aresponses: ResponsesMockServer):
aresponses.add(
aresponses.ANY,
"/bot42:TEST/method",
@ -164,14 +166,14 @@ class TestAiohttpSession:
class TestMethod(TelegramMethod[int]):
__returning__ = int
def build_request(self) -> Request:
def build_request(self, bot: Bot) -> Request:
return Request(method="method", data={})
call = TestMethod()
with patch(
"aiogram.api.client.session.base.BaseSession.raise_for_status"
) as patched_raise_for_status:
result = await session.make_request("42:TEST", call)
result = await session.make_request(bot, call)
assert isinstance(result, int)
assert result == 42

View file

@ -29,17 +29,14 @@ class TestAnswerInlineQuery:
assert request.method == "answerInlineQuery"
assert response == prepare_result.result
def test_parse_mode(self):
def test_parse_mode(self, bot: MockedBot):
query = AnswerInlineQuery(
inline_query_id="query id",
results=[InlineQueryResultPhoto(id="result id", photo_url="photo", thumb_url="thumb")],
)
request = query.build_request()
request = query.build_request(bot)
assert request.data["results"][0]["parse_mode"] is None
token = Bot.set_current(Bot(token="42:TEST", parse_mode="HTML"))
try:
request = query.build_request()
assert request.data["results"][0]["parse_mode"] == "HTML"
finally:
Bot.reset_current(token)
new_bot = Bot(token="42:TEST", parse_mode="HTML")
request = query.build_request(new_bot)
assert request.data["results"][0]["parse_mode"] == "HTML"

View file

@ -4,6 +4,7 @@ import pytest
from aiogram import Bot
from aiogram.api.methods.base import prepare_parse_mode
from tests.mocked_bot import MockedBot
class TestPrepareFile:
@ -35,19 +36,19 @@ class TestPrepareParseMode:
)
@pytest.mark.asyncio
async def test_default_parse_mode(
self, parse_mode: str, data: Dict[str, str], result: Optional[str]
self, bot: MockedBot, parse_mode: str, data: Dict[str, str], result: Optional[str]
):
async with Bot(token="42:TEST", parse_mode=parse_mode).context() as bot:
assert bot.parse_mode == parse_mode
prepare_parse_mode(data)
prepare_parse_mode(bot, data)
assert data.get("parse_mode") == result
@pytest.mark.asyncio
async def test_list(self):
data = [{}] * 2
data.append({"parse_mode": "HTML"})
async with Bot(token="42:TEST", parse_mode="Markdown").context():
prepare_parse_mode(data)
bot = Bot(token="42:TEST", parse_mode="Markdown")
prepare_parse_mode(bot, data)
assert isinstance(data, list)
assert len(data) == 3
@ -56,7 +57,7 @@ class TestPrepareParseMode:
assert data[1]["parse_mode"] == "Markdown"
assert data[2]["parse_mode"] == "HTML"
def test_bot_not_in_context(self):
def test_bot_not_in_context(self, bot: MockedBot):
data = {}
prepare_parse_mode(data)
prepare_parse_mode(bot, data)
assert data["parse_mode"] is None