From 24e933bdde4f1eb1532aabcf860111ea4a9b79d8 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Tue, 8 Feb 2022 03:30:41 +0300 Subject: [PATCH] Fix: default parent for `__setitem__` (#806) * fix: self is default parent * chore: mv bot fixture * chore: add update_chat test * fix: string CHAT_PHOTO data --- aiogram/types/base.py | 2 +- tests/conftest.py | 16 +++++++++++++++ tests/test_bot.py | 8 -------- tests/test_dispatcher.py | 8 -------- tests/test_message.py | 10 +--------- tests/types/dataset.py | 42 ++++++++++++++++++++++++++++++++++++++++ tests/types/test_chat.py | 16 +++++++++++++-- 7 files changed, 74 insertions(+), 28 deletions(-) diff --git a/aiogram/types/base.py b/aiogram/types/base.py index 5bb29472..cca3d5f1 100644 --- a/aiogram/types/base.py +++ b/aiogram/types/base.py @@ -240,7 +240,7 @@ class TelegramObject(ContextInstanceMixin, metaclass=MetaTelegramObject): :return: """ if key in self.props: - return self.props[key].set_value(self, value, self.conf.get('parent', None)) + return self.props[key].set_value(self, value, self.conf.get('parent', self)) self.values[key] = value # Log warning when Telegram silently adds new Fields diff --git a/tests/conftest.py b/tests/conftest.py index b56c7b77..455f8b0d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,12 @@ +import asyncio + import aioredis import pytest from _pytest.config import UsageError +from aiogram import Bot +from . import TOKEN + try: import aioredis.util except ImportError: @@ -72,3 +77,14 @@ def redis_options(request): raise UsageError(f"Invalid redis URI {redis_uri!r}: {e}") raise UsageError("Unsupported aioredis version") + + +@pytest.fixture(name='bot') +async def bot_fixture(): + """Bot fixture.""" + bot = Bot(TOKEN) + yield bot + session = await bot.get_session() + if session and not session.closed: + await session.close() + await asyncio.sleep(0.2) diff --git a/tests/test_bot.py b/tests/test_bot.py index b2da7952..eea20b44 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -6,14 +6,6 @@ from . import FakeTelegram, TOKEN, BOT_ID pytestmark = pytest.mark.asyncio -@pytest.fixture(name='bot') -async def bot_fixture(): - """ Bot fixture """ - _bot = Bot(TOKEN, parse_mode=types.ParseMode.MARKDOWN_V2) - yield _bot - await _bot.close() - - async def test_get_me(bot: Bot): """ getMe method test """ from .types.dataset import USER diff --git a/tests/test_dispatcher.py b/tests/test_dispatcher.py index 81ae565c..e38d7c4a 100644 --- a/tests/test_dispatcher.py +++ b/tests/test_dispatcher.py @@ -5,14 +5,6 @@ from aiogram import Dispatcher, Bot pytestmark = pytest.mark.asyncio -@pytest.fixture(name='bot') -async def bot_fixture(): - """ Bot fixture """ - _bot = Bot(token='123456789:AABBCCDDEEFFaabbccddeeff-1234567890') - yield _bot - await _bot.close() - - class TestDispatcherInit: async def test_successful_init(self, bot): """ diff --git a/tests/test_message.py b/tests/test_message.py index 6fca789f..bc53dfbc 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -8,16 +8,8 @@ from . import FakeTelegram, TOKEN pytestmark = pytest.mark.asyncio -@pytest.fixture(name='bot') -async def bot_fixture(): - """ Bot fixture """ - _bot = Bot(TOKEN, parse_mode=types.ParseMode.HTML) - yield _bot - await _bot.close() - - @pytest.fixture() -async def message(bot): +async def message(bot: Bot): """ Message fixture :param bot: Telegram bot fixture diff --git a/tests/types/dataset.py b/tests/types/dataset.py index be23da9e..bd73f377 100644 --- a/tests/types/dataset.py +++ b/tests/types/dataset.py @@ -19,6 +19,14 @@ CHAT = { "type": "private", } +CHAT_PHOTO = { + "small_file_id": "small_file_id", + "small_file_unique_id": "small_file_unique_id", + "big_file_id": "big_file_id", + "big_file_unique_id": "big_file_unique_id", +} + + PHOTO = { "file_id": "AgADBAADFak0G88YZAf8OAug7bHyS9x2ZxkABHVfpJywcloRAAGAAQABAg", "file_size": 1101, @@ -485,3 +493,37 @@ REPLY_KEYBOARD_MARKUP = { "keyboard": [[{"text": "something here"}]], "resize_keyboard": True, } + +CHAT_PERMISSIONS = { + "can_send_messages": True, + "can_send_media_messages": True, + "can_send_polls": True, + "can_send_other_messages": True, + "can_add_web_page_previews": True, + "can_change_info": True, + "can_invite_users": True, + "can_pin_messages": True, +} + +CHAT_LOCATION = { + "location": LOCATION, + "address": "address", +} + +FULL_CHAT = { + **CHAT, + "photo": CHAT_PHOTO, + "bio": "bio", + "has_private_forwards": False, + "description": "description", + "invite_link": "invite_link", + "pinned_message": MESSAGE, + "permissions": CHAT_PERMISSIONS, + "slow_mode_delay": 10, + "message_auto_delete_time": 60, + "has_protected_content": True, + "sticker_set_name": "sticker_set_name", + "can_set_sticker_set": True, + "linked_chat_id": -1234567890, + "location": CHAT_LOCATION, +} diff --git a/tests/types/test_chat.py b/tests/types/test_chat.py index 1caa228d..c8e20146 100644 --- a/tests/types/test_chat.py +++ b/tests/types/test_chat.py @@ -1,5 +1,10 @@ -from aiogram import types -from .dataset import CHAT +import pytest + +from aiogram import Bot, types +from .dataset import CHAT, FULL_CHAT +from .. import FakeTelegram + +pytestmark = pytest.mark.asyncio chat = types.Chat(**CHAT) @@ -59,3 +64,10 @@ def test_chat_actions(): assert types.ChatActions.FIND_LOCATION == 'find_location' assert types.ChatActions.RECORD_VIDEO_NOTE == 'record_video_note' assert types.ChatActions.UPLOAD_VIDEO_NOTE == 'upload_video_note' + + +async def test_update_chat(bot: Bot): + Bot.set_current(bot) + async with FakeTelegram(message_data=FULL_CHAT): + await chat.update_chat() + assert chat.to_python() == types.Chat(**FULL_CHAT).to_python()