mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
AIOG-T-51. TelegramObject property bot added
* property added * unit tests added * local import used to avoid circular reference
This commit is contained in:
parent
53db44101f
commit
b6ff48d8fc
2 changed files with 40 additions and 0 deletions
|
|
@ -1,9 +1,15 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
|
import typing
|
||||||
|
|
||||||
from pydantic import BaseModel, Extra
|
from pydantic import BaseModel, Extra
|
||||||
|
|
||||||
from aiogram.utils.mixins import ContextInstanceMixin
|
from aiogram.utils.mixins import ContextInstanceMixin
|
||||||
|
|
||||||
|
if typing.TYPE_CHECKING:
|
||||||
|
from ..client.bot import Bot # pragma: no cover
|
||||||
|
|
||||||
|
|
||||||
class TelegramObject(ContextInstanceMixin["TelegramObject"], BaseModel):
|
class TelegramObject(ContextInstanceMixin["TelegramObject"], BaseModel):
|
||||||
class Config:
|
class Config:
|
||||||
|
|
@ -15,6 +21,19 @@ class TelegramObject(ContextInstanceMixin["TelegramObject"], BaseModel):
|
||||||
allow_population_by_field_name = True
|
allow_population_by_field_name = True
|
||||||
json_encoders = {datetime.datetime: lambda dt: int(dt.timestamp())}
|
json_encoders = {datetime.datetime: lambda dt: int(dt.timestamp())}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def bot(self) -> Bot:
|
||||||
|
from ..client.bot import Bot
|
||||||
|
|
||||||
|
bot = Bot.get_current()
|
||||||
|
if bot is None:
|
||||||
|
raise RuntimeError(
|
||||||
|
"Can't get bot instance from context. "
|
||||||
|
"You can fix it with setting current instance: "
|
||||||
|
"'Bot.set_current(bot_instance)'"
|
||||||
|
)
|
||||||
|
return bot
|
||||||
|
|
||||||
|
|
||||||
class MutableTelegramObject(TelegramObject):
|
class MutableTelegramObject(TelegramObject):
|
||||||
class Config:
|
class Config:
|
||||||
|
|
|
||||||
21
tests/test_api/test_types/test_telegram_object.py
Normal file
21
tests/test_api/test_types/test_telegram_object.py
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from aiogram import Bot
|
||||||
|
from aiogram.api.types import TelegramObject
|
||||||
|
|
||||||
|
|
||||||
|
class TestTelegramObject:
|
||||||
|
@pytest.fixture()
|
||||||
|
def context_cleanup(self):
|
||||||
|
yield
|
||||||
|
Bot.reset_current(self.__token)
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("context_cleanup")
|
||||||
|
def test_bot_property_positive(self):
|
||||||
|
self.__token = Bot.set_current(Bot("42:TEST"))
|
||||||
|
|
||||||
|
assert isinstance(TelegramObject().bot, Bot)
|
||||||
|
|
||||||
|
def test_bot_property_negative(self):
|
||||||
|
with pytest.raises(RuntimeError, match=r"Bot.set_current\(bot_instance\)"):
|
||||||
|
isinstance(TelegramObject().bot, Bot)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue