Reworked bot-wide defaults (#1392)

* Reworked defaults

* Added changelog and partial docs
This commit is contained in:
Alex Root Junior 2024-01-27 17:19:45 +02:00 committed by GitHub
parent 1281bf551a
commit 24f59da70d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
140 changed files with 608 additions and 530 deletions

View file

@ -12,6 +12,7 @@ from aiogram.client.session.aiohttp import AiohttpSession
from aiogram.client.telegram import TelegramAPIServer
from aiogram.methods import GetFile, GetMe
from aiogram.types import File, PhotoSize
from tests.deprecated import check_deprecated
from tests.mocked_bot import MockedBot
@ -41,6 +42,13 @@ class TestBot:
assert isinstance(bot.session, AiohttpSession)
assert bot.id == 42
def test_init_default(self):
with check_deprecated(
max_version="3.5.0",
exception=TypeError,
):
bot = Bot(token="42:Test", parse_mode="HTML")
def test_hashable(self):
bot = Bot("42:TEST")
assert hash(bot) == hash("42:TEST")

View file

@ -1,5 +1,13 @@
import asyncio
from typing import Any, AsyncContextManager, AsyncGenerator, AsyncIterable, Dict, List
from typing import (
Any,
AsyncContextManager,
AsyncGenerator,
AsyncIterable,
Dict,
List,
Union,
)
from unittest.mock import AsyncMock, patch
import aiohttp_socks
@ -8,6 +16,7 @@ from aiohttp import ClientError
from aresponses import ResponsesMockServer
from aiogram import Bot
from aiogram.client.default import Default
from aiogram.client.session import aiohttp
from aiogram.client.session.aiohttp import AiohttpSession
from aiogram.exceptions import TelegramNetworkError
@ -106,7 +115,7 @@ class TestAiohttpSession:
str_: str
int_: int
bool_: bool
unset_: str = UNSET_PARSE_MODE
unset_: Union[str, Default] = Default("parse_mode")
null_: None
list_: List[str]
dict_: Dict[str, Any]
@ -118,7 +127,7 @@ class TestAiohttpSession:
str_="value",
int_=42,
bool_=True,
unset_=UNSET_PARSE_MODE,
unset_=Default("parse_mode"),
null_=None,
list_=["foo"],
dict_={"bar": "baz"},

View file

@ -7,6 +7,7 @@ import pytest
from pytz import utc
from aiogram import Bot
from aiogram.client.default import Default, DefaultBotProperties
from aiogram.client.session.base import BaseSession, TelegramType
from aiogram.client.telegram import PRODUCTION, TelegramAPIServer
from aiogram.enums import ChatType, ParseMode, TopicIconColor
@ -125,15 +126,18 @@ class TestBaseSession:
def test_prepare_value_defaults_replace(self):
bot = MockedBot(
parse_mode=ParseMode.HTML,
protect_content=True,
disable_web_page_preview=True,
default=DefaultBotProperties(
parse_mode=ParseMode.HTML,
protect_content=True,
link_preview_is_disabled=True,
)
)
assert bot.session.prepare_value(UNSET_PARSE_MODE, bot=bot, files={}) == "HTML"
assert bot.session.prepare_value(Default("parse_mode"), bot=bot, files={}) == "HTML"
assert (
bot.session.prepare_value(UNSET_DISABLE_WEB_PAGE_PREVIEW, bot=bot, files={}) == "true"
bot.session.prepare_value(Default("link_preview_is_disabled"), bot=bot, files={})
== "true"
)
assert bot.session.prepare_value(UNSET_PROTECT_CONTENT, bot=bot, files={}) == "true"
assert bot.session.prepare_value(Default("protect_content"), bot=bot, files={}) == "true"
def test_prepare_value_defaults_unset(self):
bot = MockedBot()