From 28d19c3b9a65e7dd5f68c1172e35acdcac20fd78 Mon Sep 17 00:00:00 2001 From: Bibo-Joshi <22366557+Bibo-Joshi@users.noreply.github.com> Date: Sat, 21 Sep 2024 11:33:52 +0200 Subject: [PATCH] Introduce `conftest.py` for File Related Tests (#4488) Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com> --- tests/_files/conftest.py | 189 ++++++++++++++++++++++++++++++ tests/_files/test_animation.py | 14 --- tests/_files/test_audio.py | 12 -- tests/_files/test_document.py | 12 -- tests/_files/test_inputmedia.py | 88 ++++++-------- tests/_files/test_inputsticker.py | 3 +- tests/_files/test_photo.py | 28 ----- tests/_files/test_sticker.py | 102 ++-------------- tests/_files/test_video.py | 12 -- tests/_files/test_videonote.py | 2 +- tests/auxil/constants.py | 4 + tests/conftest.py | 26 +++- tests/test_bot.py | 5 +- tests/test_forum.py | 29 +---- 14 files changed, 263 insertions(+), 263 deletions(-) create mode 100644 tests/_files/conftest.py diff --git a/tests/_files/conftest.py b/tests/_files/conftest.py new file mode 100644 index 000000000..f2ae1d3ab --- /dev/null +++ b/tests/_files/conftest.py @@ -0,0 +1,189 @@ +#!/usr/bin/env python +# +# A library that provides a Python interface to the Telegram Bot API +# Copyright (C) 2015-2024 +# Leandro Toledo de Souza +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser Public License for more details. +# +# You should have received a copy of the GNU Lesser Public License +# along with this program. If not, see [http://www.gnu.org/licenses/]. +"""Module to provide fixtures most of which are used in test_inputmedia.py.""" +import pytest + +from telegram.error import BadRequest +from tests.auxil.files import data_file +from tests.auxil.networking import expect_bad_request + + +@pytest.fixture(scope="session") +async def animation(bot, chat_id): + with data_file("game.gif").open("rb") as f, data_file("thumb.jpg").open("rb") as thumb: + return ( + await bot.send_animation(chat_id, animation=f, read_timeout=50, thumbnail=thumb) + ).animation + + +@pytest.fixture +def animation_file(): + with data_file("game.gif").open("rb") as f: + yield f + + +@pytest.fixture(scope="module") +async def animated_sticker(bot, chat_id): + with data_file("telegram_animated_sticker.tgs").open("rb") as f: + return (await bot.send_sticker(chat_id, sticker=f, read_timeout=50)).sticker + + +@pytest.fixture +def animated_sticker_file(): + with data_file("telegram_animated_sticker.tgs").open("rb") as f: + yield f + + +@pytest.fixture +async def animated_sticker_set(bot): + ss = await bot.get_sticker_set(f"animated_test_by_{bot.username}") + if len(ss.stickers) > 100: + try: + for i in range(1, 50): + await bot.delete_sticker_from_set(ss.stickers[-i].file_id) + except BadRequest as e: + if e.message == "Stickerset_not_modified": + return ss + raise Exception("stickerset is growing too large.") from None + return ss + + +@pytest.fixture(scope="session") +async def audio(bot, chat_id): + with data_file("telegram.mp3").open("rb") as f, data_file("thumb.jpg").open("rb") as thumb: + return (await bot.send_audio(chat_id, audio=f, read_timeout=50, thumbnail=thumb)).audio + + +@pytest.fixture +def audio_file(): + with data_file("telegram.mp3").open("rb") as f: + yield f + + +@pytest.fixture(scope="session") +async def document(bot, chat_id): + with data_file("telegram.png").open("rb") as f: + return (await bot.send_document(chat_id, document=f, read_timeout=50)).document + + +@pytest.fixture +def document_file(): + with data_file("telegram.png").open("rb") as f: + yield f + + +@pytest.fixture(scope="session") +def photo(photolist): + return photolist[-1] + + +@pytest.fixture +def photo_file(): + with data_file("telegram.jpg").open("rb") as f: + yield f + + +@pytest.fixture(scope="session") +async def photolist(bot, chat_id): + async def func(): + with data_file("telegram.jpg").open("rb") as f: + return (await bot.send_photo(chat_id, photo=f, read_timeout=50)).photo + + return await expect_bad_request( + func, "Type of file mismatch", "Telegram did not accept the file." + ) + + +@pytest.fixture(scope="module") +async def sticker(bot, chat_id): + with data_file("telegram.webp").open("rb") as f: + sticker = (await bot.send_sticker(chat_id, sticker=f, read_timeout=50)).sticker + # necessary to properly test needs_repainting + with sticker._unfrozen(): + sticker.needs_repainting = True + return sticker + + +@pytest.fixture +def sticker_file(): + with data_file("telegram.webp").open("rb") as file: + yield file + + +@pytest.fixture +async def sticker_set(bot): + ss = await bot.get_sticker_set(f"test_by_{bot.username}") + if len(ss.stickers) > 100: + try: + for i in range(1, 50): + await bot.delete_sticker_from_set(ss.stickers[-i].file_id) + except BadRequest as e: + if e.message == "Stickerset_not_modified": + return ss + raise Exception("stickerset is growing too large.") from None + return ss + + +@pytest.fixture +def sticker_set_thumb_file(): + with data_file("sticker_set_thumb.png").open("rb") as file: + yield file + + +@pytest.fixture(scope="session") +def thumb(photolist): + return photolist[0] + + +@pytest.fixture(scope="session") +async def video(bot, chat_id): + with data_file("telegram.mp4").open("rb") as f: + return (await bot.send_video(chat_id, video=f, read_timeout=50)).video + + +@pytest.fixture +def video_file(): + with data_file("telegram.mp4").open("rb") as f: + yield f + + +@pytest.fixture +def video_sticker_file(): + with data_file("telegram_video_sticker.webm").open("rb") as f: + yield f + + +@pytest.fixture(scope="module") +def video_sticker(bot, chat_id): + with data_file("telegram_video_sticker.webm").open("rb") as f: + return bot.send_sticker(chat_id, sticker=f, timeout=50).sticker + + +@pytest.fixture +async def video_sticker_set(bot): + ss = await bot.get_sticker_set(f"video_test_by_{bot.username}") + if len(ss.stickers) > 100: + try: + for i in range(1, 50): + await bot.delete_sticker_from_set(ss.stickers[-i].file_id) + except BadRequest as e: + if e.message == "Stickerset_not_modified": + return ss + raise Exception("stickerset is growing too large.") from None + return ss diff --git a/tests/_files/test_animation.py b/tests/_files/test_animation.py index 55c2076e2..a312d3575 100644 --- a/tests/_files/test_animation.py +++ b/tests/_files/test_animation.py @@ -37,20 +37,6 @@ from tests.auxil.files import data_file from tests.auxil.slots import mro_slots -@pytest.fixture -def animation_file(): - with data_file("game.gif").open("rb") as f: - yield f - - -@pytest.fixture(scope="module") -async def animation(bot, chat_id): - with data_file("game.gif").open("rb") as f, data_file("thumb.jpg").open("rb") as thumb: - return ( - await bot.send_animation(chat_id, animation=f, read_timeout=50, thumbnail=thumb) - ).animation - - class AnimationTestBase: animation_file_id = "CgADAQADngIAAuyVeEez0xRovKi9VAI" animation_file_unique_id = "adc3145fd2e84d95b64d68eaa22aa33e" diff --git a/tests/_files/test_audio.py b/tests/_files/test_audio.py index ed0d74d4d..08e598cb2 100644 --- a/tests/_files/test_audio.py +++ b/tests/_files/test_audio.py @@ -37,18 +37,6 @@ from tests.auxil.files import data_file from tests.auxil.slots import mro_slots -@pytest.fixture -def audio_file(): - with data_file("telegram.mp3").open("rb") as f: - yield f - - -@pytest.fixture(scope="module") -async def audio(bot, chat_id): - with data_file("telegram.mp3").open("rb") as f, data_file("thumb.jpg").open("rb") as thumb: - return (await bot.send_audio(chat_id, audio=f, read_timeout=50, thumbnail=thumb)).audio - - class AudioTestBase: caption = "Test *audio*" performer = "Leandro Toledo" diff --git a/tests/_files/test_document.py b/tests/_files/test_document.py index 90b6bdf11..e60374034 100644 --- a/tests/_files/test_document.py +++ b/tests/_files/test_document.py @@ -37,18 +37,6 @@ from tests.auxil.files import data_file from tests.auxil.slots import mro_slots -@pytest.fixture -def document_file(): - with data_file("telegram.png").open("rb") as f: - yield f - - -@pytest.fixture(scope="module") -async def document(bot, chat_id): - with data_file("telegram.png").open("rb") as f: - return (await bot.send_document(chat_id, document=f, read_timeout=50)).document - - class DocumentTestBase: caption = "DocumentTest - *Caption*" document_file_url = "https://python-telegram-bot.org/static/testfiles/telegram.gif" diff --git a/tests/_files/test_inputmedia.py b/tests/_files/test_inputmedia.py index 6b54e4d19..e75a5adb6 100644 --- a/tests/_files/test_inputmedia.py +++ b/tests/_files/test_inputmedia.py @@ -38,32 +38,14 @@ from telegram import ( ReplyParameters, ) from telegram.constants import InputMediaType, ParseMode - -# noinspection PyUnresolvedReferences from telegram.error import BadRequest from telegram.request import RequestData -from tests._files.test_animation import animation, animation_file # noqa: F401 from tests.auxil.files import data_file from tests.auxil.networking import expect_bad_request from tests.auxil.slots import mro_slots -# noinspection PyUnresolvedReferences -from tests.test_forum import emoji_id, real_topic # noqa: F401 - from ..auxil.build_messages import make_message -# noinspection PyUnresolvedReferences -from .test_audio import audio, audio_file # noqa: F401 - -# noinspection PyUnresolvedReferences -from .test_document import document, document_file # noqa: F401 - -# noinspection PyUnresolvedReferences -from .test_photo import photo, photo_file, photolist, thumb # noqa: F401 - -# noinspection PyUnresolvedReferences -from .test_video import video, video_file # noqa: F401 - @pytest.fixture(scope="module") def input_media_video(class_thumb_file): @@ -213,7 +195,7 @@ class TestInputMediaVideoWithoutRequest(InputMediaVideoTestBase): == input_media_video.show_caption_above_media ) - def test_with_video(self, video): # noqa: F811 + def test_with_video(self, video): # fixture found in test_video input_media_video = InputMediaVideo(video, caption="test 3") assert input_media_video.type == self.type_ @@ -223,7 +205,7 @@ class TestInputMediaVideoWithoutRequest(InputMediaVideoTestBase): assert input_media_video.duration == video.duration assert input_media_video.caption == "test 3" - def test_with_video_file(self, video_file): # noqa: F811 + def test_with_video_file(self, video_file): # fixture found in test_video input_media_video = InputMediaVideo(video_file, caption="test 3") assert input_media_video.type == self.type_ @@ -303,14 +285,14 @@ class TestInputMediaPhotoWithoutRequest(InputMediaPhotoTestBase): == input_media_photo.show_caption_above_media ) - def test_with_photo(self, photo): # noqa: F811 + def test_with_photo(self, photo): # fixture found in test_photo input_media_photo = InputMediaPhoto(photo, caption="test 2") assert input_media_photo.type == self.type_ assert input_media_photo.media == photo.file_id assert input_media_photo.caption == "test 2" - def test_with_photo_file(self, photo_file): # noqa: F811 + def test_with_photo_file(self, photo_file): # fixture found in test_photo input_media_photo = InputMediaPhoto(photo_file, caption="test 2") assert input_media_photo.type == self.type_ @@ -374,14 +356,14 @@ class TestInputMediaAnimationWithoutRequest(InputMediaAnimationTestBase): == input_media_animation.show_caption_above_media ) - def test_with_animation(self, animation): # noqa: F811 + def test_with_animation(self, animation): # fixture found in test_animation input_media_animation = InputMediaAnimation(animation, caption="test 2") assert input_media_animation.type == self.type_ assert input_media_animation.media == animation.file_id assert input_media_animation.caption == "test 2" - def test_with_animation_file(self, animation_file): # noqa: F811 + def test_with_animation_file(self, animation_file): # fixture found in test_animation input_media_animation = InputMediaAnimation(animation_file, caption="test 2") assert input_media_animation.type == self.type_ @@ -442,7 +424,7 @@ class TestInputMediaAudioWithoutRequest(InputMediaAudioTestBase): ce.to_dict() for ce in input_media_audio.caption_entities ] - def test_with_audio(self, audio): # noqa: F811 + def test_with_audio(self, audio): # fixture found in test_audio input_media_audio = InputMediaAudio(audio, caption="test 3") assert input_media_audio.type == self.type_ @@ -452,7 +434,7 @@ class TestInputMediaAudioWithoutRequest(InputMediaAudioTestBase): assert input_media_audio.title == audio.title assert input_media_audio.caption == "test 3" - def test_with_audio_file(self, audio_file): # noqa: F811 + def test_with_audio_file(self, audio_file): # fixture found in test_audio input_media_audio = InputMediaAudio(audio_file, caption="test 3") assert input_media_audio.type == self.type_ @@ -513,14 +495,14 @@ class TestInputMediaDocumentWithoutRequest(InputMediaDocumentTestBase): == input_media_document.disable_content_type_detection ) - def test_with_document(self, document): # noqa: F811 + def test_with_document(self, document): # fixture found in test_document input_media_document = InputMediaDocument(document, caption="test 3") assert input_media_document.type == self.type_ assert input_media_document.media == document.file_id assert input_media_document.caption == "test 3" - def test_with_document_file(self, document_file): # noqa: F811 + def test_with_document_file(self, document_file): # fixture found in test_document input_media_document = InputMediaDocument(document_file, caption="test 3") assert input_media_document.type == self.type_ @@ -551,13 +533,13 @@ class TestInputPaidMediaPhotoWithoutRequest(InputMediaPhotoTestBase): assert input_paid_media_photo_dict["type"] == input_paid_media_photo.type assert input_paid_media_photo_dict["media"] == input_paid_media_photo.media - def test_with_photo(self, photo): # noqa: F811 + def test_with_photo(self, photo): # fixture found in test_photo input_paid_media_photo = InputPaidMediaPhoto(photo) assert input_paid_media_photo.type == self.type_ assert input_paid_media_photo.media == photo.file_id - def test_with_photo_file(self, photo_file): # noqa: F811 + def test_with_photo_file(self, photo_file): # fixture found in test_photo input_paid_media_photo = InputPaidMediaPhoto(photo_file) assert input_paid_media_photo.type == self.type_ @@ -597,7 +579,7 @@ class TestInputPaidMediaVideoWithoutRequest(InputMediaVideoTestBase): ) assert input_paid_media_video_dict["thumbnail"] == input_paid_media_video.thumbnail - def test_with_video(self, video): # noqa: F811 + def test_with_video(self, video): # fixture found in test_video input_paid_media_video = InputPaidMediaVideo(video) assert input_paid_media_video.type == self.type_ @@ -606,7 +588,7 @@ class TestInputPaidMediaVideoWithoutRequest(InputMediaVideoTestBase): assert input_paid_media_video.height == video.height assert input_paid_media_video.duration == video.duration - def test_with_video_file(self, video_file): # noqa: F811 + def test_with_video_file(self, video_file): # fixture found in test_video input_paid_media_video = InputPaidMediaVideo(video_file) assert input_paid_media_video.type == self.type_ @@ -621,7 +603,7 @@ class TestInputPaidMediaVideoWithoutRequest(InputMediaVideoTestBase): @pytest.fixture(scope="module") -def media_group(photo, thumb): # noqa: F811 +def media_group(photo, thumb): return [ InputMediaPhoto(photo, caption="*photo* 1", parse_mode="Markdown"), InputMediaPhoto(thumb, caption="photo 2", parse_mode="HTML"), @@ -632,12 +614,12 @@ def media_group(photo, thumb): # noqa: F811 @pytest.fixture(scope="module") -def media_group_no_caption_args(photo, thumb): # noqa: F811 +def media_group_no_caption_args(photo, thumb): return [InputMediaPhoto(photo), InputMediaPhoto(thumb), InputMediaPhoto(photo)] @pytest.fixture(scope="module") -def media_group_no_caption_only_caption_entities(photo, thumb): # noqa: F811 +def media_group_no_caption_only_caption_entities(photo, thumb): return [ InputMediaPhoto(photo, caption_entities=[MessageEntity(MessageEntity.BOLD, 0, 5)]), InputMediaPhoto(photo, caption_entities=[MessageEntity(MessageEntity.BOLD, 0, 5)]), @@ -645,7 +627,7 @@ def media_group_no_caption_only_caption_entities(photo, thumb): # noqa: F811 @pytest.fixture(scope="module") -def media_group_no_caption_only_parse_mode(photo, thumb): # noqa: F811 +def media_group_no_caption_only_parse_mode(photo, thumb): return [ InputMediaPhoto(photo, parse_mode="Markdown"), InputMediaPhoto(thumb, parse_mode="HTML"), @@ -676,10 +658,10 @@ class TestSendMediaGroupWithoutRequest: self, offline_bot, chat_id, - photo_file, # noqa: F811 - animation_file, # noqa: F811 - audio_file, # noqa: F811 - video_file, # noqa: F811 + photo_file, + animation_file, + audio_file, + video_file, monkeypatch, ): async def make_assertion(url, request_data: RequestData, *args, **kwargs): @@ -703,7 +685,7 @@ class TestSendMediaGroupWithoutRequest: await offline_bot.send_media_group(chat_id, media) async def test_send_media_group_with_thumbs( - self, offline_bot, chat_id, video_file, photo_file, monkeypatch # noqa: F811 + self, offline_bot, chat_id, video_file, photo_file, monkeypatch ): async def make_assertion(method, url, request_data: RequestData, *args, **kwargs): nonlocal input_video @@ -721,7 +703,7 @@ class TestSendMediaGroupWithoutRequest: await offline_bot.send_media_group(chat_id, [input_video, input_video]) async def test_edit_message_media_with_thumb( - self, offline_bot, chat_id, video_file, photo_file, monkeypatch # noqa: F811 + self, offline_bot, chat_id, video_file, photo_file, monkeypatch ): async def make_assertion( method: str, url: str, request_data: Optional[RequestData] = None, *args, **kwargs @@ -792,9 +774,7 @@ class TestSendMediaGroupWithRequest: mes.caption_entities == (MessageEntity(MessageEntity.BOLD, 0, 5),) for mes in messages ) - async def test_send_media_group_new_files( - self, bot, chat_id, video_file, photo_file # noqa: F811 - ): + async def test_send_media_group_new_files(self, bot, chat_id, video_file, photo_file): async def func(): return await bot.send_media_group( chat_id, @@ -830,7 +810,7 @@ class TestSendMediaGroupWithRequest: assert all(mes.media_group_id == messages[0].media_group_id for mes in messages) async def test_send_media_group_with_message_thread_id( - self, bot, real_topic, forum_group_id, media_group # noqa: F811 + self, bot, real_topic, forum_group_id, media_group ): messages = await bot.send_media_group( forum_group_id, @@ -929,9 +909,7 @@ class TestSendMediaGroupWithRequest: ) assert all(mes.has_protected_content for mes in messages) - async def test_send_media_group_with_spoiler( - self, bot, chat_id, photo_file, video_file # noqa: F811 - ): + async def test_send_media_group_with_spoiler(self, bot, chat_id, photo_file, video_file): # Media groups can't contain Animations, so that is tested in test_animation.py media = [ InputMediaPhoto(photo_file, has_spoiler=True), @@ -1074,11 +1052,11 @@ class TestSendMediaGroupWithRequest: chat_id, default_bot, media_type, - animation, # noqa: F811 - document, # noqa: F811 - audio, # noqa: F811 - photo, # noqa: F811 - video, # noqa: F811 + animation, + document, + audio, + photo, + video, ): html_caption = "bold italic code" markdown_caption = "*bold* _italic_ `code`" @@ -1153,7 +1131,7 @@ class TestSendMediaGroupWithRequest: # make sure that the media was not modified assert media.parse_mode == copied_media.parse_mode - async def test_send_paid_media(self, bot, channel_id, photo_file, video_file): # noqa: F811 + async def test_send_paid_media(self, bot, channel_id, photo_file, video_file): msg = await bot.send_paid_media( chat_id=channel_id, star_count=20, diff --git a/tests/_files/test_inputsticker.py b/tests/_files/test_inputsticker.py index bd45bbdea..cfbe20560 100644 --- a/tests/_files/test_inputsticker.py +++ b/tests/_files/test_inputsticker.py @@ -21,7 +21,6 @@ import pytest from telegram import InputSticker, MaskPosition from telegram._files.inputfile import InputFile -from tests._files.test_sticker import video_sticker_file # noqa: F401 from tests.auxil.files import data_file from tests.auxil.slots import mro_slots @@ -77,7 +76,7 @@ class TestInputStickerWithoutRequest(InputStickerTestBase): assert input_sticker_dict["keywords"] == list(input_sticker.keywords) assert input_sticker_dict["format"] == input_sticker.format - def test_with_sticker_input_types(self, video_sticker_file): # noqa: F811 + def test_with_sticker_input_types(self, video_sticker_file): sticker = InputSticker(sticker=video_sticker_file, emoji_list=["👍"], format="video") assert isinstance(sticker.sticker, InputFile) sticker = InputSticker(data_file("telegram_video_sticker.webm"), ["👍"], "video") diff --git a/tests/_files/test_photo.py b/tests/_files/test_photo.py index 919c3155b..bdf34f72b 100644 --- a/tests/_files/test_photo.py +++ b/tests/_files/test_photo.py @@ -34,37 +34,9 @@ from tests.auxil.bot_method_checks import ( ) from tests.auxil.build_messages import make_message from tests.auxil.files import data_file -from tests.auxil.networking import expect_bad_request from tests.auxil.slots import mro_slots -@pytest.fixture -def photo_file(): - with data_file("telegram.jpg").open("rb") as f: - yield f - - -@pytest.fixture(scope="module") -async def photolist(bot, chat_id): - async def func(): - with data_file("telegram.jpg").open("rb") as f: - return (await bot.send_photo(chat_id, photo=f, read_timeout=50)).photo - - return await expect_bad_request( - func, "Type of file mismatch", "Telegram did not accept the file." - ) - - -@pytest.fixture(scope="module") -def thumb(photolist): - return photolist[0] - - -@pytest.fixture(scope="module") -def photo(photolist): - return photolist[-1] - - class PhotoTestBase: width = 800 height = 800 diff --git a/tests/_files/test_sticker.py b/tests/_files/test_sticker.py index 3fa608e3a..d77f93ac7 100644 --- a/tests/_files/test_sticker.py +++ b/tests/_files/test_sticker.py @@ -49,46 +49,6 @@ from tests.auxil.files import data_file from tests.auxil.slots import mro_slots -@pytest.fixture -def sticker_file(): - with data_file("telegram.webp").open("rb") as file: - yield file - - -@pytest.fixture(scope="module") -async def sticker(bot, chat_id): - with data_file("telegram.webp").open("rb") as f: - sticker = (await bot.send_sticker(chat_id, sticker=f, read_timeout=50)).sticker - # necessary to properly test needs_repainting - with sticker._unfrozen(): - sticker.needs_repainting = StickerTestBase.needs_repainting - return sticker - - -@pytest.fixture -def animated_sticker_file(): - with data_file("telegram_animated_sticker.tgs").open("rb") as f: - yield f - - -@pytest.fixture(scope="module") -async def animated_sticker(bot, chat_id): - with data_file("telegram_animated_sticker.tgs").open("rb") as f: - return (await bot.send_sticker(chat_id, sticker=f, read_timeout=50)).sticker - - -@pytest.fixture -def video_sticker_file(): - with data_file("telegram_video_sticker.webm").open("rb") as f: - yield f - - -@pytest.fixture(scope="module") -def video_sticker(bot, chat_id): - with data_file("telegram_video_sticker.webm").open("rb") as f: - return bot.send_sticker(chat_id, sticker=f, timeout=50).sticker - - class StickerTestBase: # sticker_file_url = 'https://python-telegram-bot.org/static/testfiles/telegram.webp' # Serving sticker from gh since our server sends wrong content_type @@ -524,54 +484,6 @@ class TestStickerWithRequest(StickerTestBase): await bot.send_sticker(chat_id, "") -@pytest.fixture -async def sticker_set(bot): - ss = await bot.get_sticker_set(f"test_by_{bot.username}") - if len(ss.stickers) > 100: - try: - for i in range(1, 50): - await bot.delete_sticker_from_set(ss.stickers[-i].file_id) - except BadRequest as e: - if e.message == "Stickerset_not_modified": - return ss - raise Exception("stickerset is growing too large.") from None - return ss - - -@pytest.fixture -async def animated_sticker_set(bot): - ss = await bot.get_sticker_set(f"animated_test_by_{bot.username}") - if len(ss.stickers) > 100: - try: - for i in range(1, 50): - await bot.delete_sticker_from_set(ss.stickers[-i].file_id) - except BadRequest as e: - if e.message == "Stickerset_not_modified": - return ss - raise Exception("stickerset is growing too large.") from None - return ss - - -@pytest.fixture -async def video_sticker_set(bot): - ss = await bot.get_sticker_set(f"video_test_by_{bot.username}") - if len(ss.stickers) > 100: - try: - for i in range(1, 50): - await bot.delete_sticker_from_set(ss.stickers[-i].file_id) - except BadRequest as e: - if e.message == "Stickerset_not_modified": - return ss - raise Exception("stickerset is growing too large.") from None - return ss - - -@pytest.fixture -def sticker_set_thumb_file(): - with data_file("sticker_set_thumb.png").open("rb") as file: - yield file - - class StickerSetTestBase: title = "Test stickers" stickers = [Sticker("file_id", "file_un_id", 512, 512, True, True, Sticker.REGULAR)] @@ -1067,6 +979,13 @@ class TestStickerSetWithRequest: ) +class MaskPositionTestBase: + point = MaskPosition.EYES + x_shift = -1 + y_shift = 1 + scale = 2 + + @pytest.fixture(scope="module") def mask_position(): return MaskPosition( @@ -1077,13 +996,6 @@ def mask_position(): ) -class MaskPositionTestBase: - point = MaskPosition.EYES - x_shift = -1 - y_shift = 1 - scale = 2 - - class TestMaskPositionWithoutRequest(MaskPositionTestBase): def test_slot_behaviour(self, mask_position): inst = mask_position diff --git a/tests/_files/test_video.py b/tests/_files/test_video.py index d4ace54e3..66230389f 100644 --- a/tests/_files/test_video.py +++ b/tests/_files/test_video.py @@ -37,18 +37,6 @@ from tests.auxil.files import data_file from tests.auxil.slots import mro_slots -@pytest.fixture -def video_file(): - with data_file("telegram.mp4").open("rb") as f: - yield f - - -@pytest.fixture(scope="module") -async def video(bot, chat_id): - with data_file("telegram.mp4").open("rb") as f: - return (await bot.send_video(chat_id, video=f, read_timeout=50)).video - - class VideoTestBase: width = 360 height = 640 diff --git a/tests/_files/test_videonote.py b/tests/_files/test_videonote.py index d85de9fb4..ce69fa8f8 100644 --- a/tests/_files/test_videonote.py +++ b/tests/_files/test_videonote.py @@ -248,7 +248,7 @@ class TestVideoNoteWithRequest(VideoNoteTestBase): assert message.video_note.thumbnail.height == self.thumb_height assert message.has_protected_content - async def test_get_and_download(self, bot, video_note, chat_id, tmp_file): + async def test_get_and_download(self, bot, video_note, tmp_file): new_file = await bot.get_file(video_note.file_id) assert new_file.file_size == self.file_size diff --git a/tests/auxil/constants.py b/tests/auxil/constants.py index 8ec314bfb..5c90824ba 100644 --- a/tests/auxil/constants.py +++ b/tests/auxil/constants.py @@ -20,3 +20,7 @@ # THIS KEY IS OBVIOUSLY COMPROMISED # DO NOT USE IN PRODUCTION! PRIVATE_KEY = b"-----BEGIN RSA PRIVATE KEY-----\r\nMIIEowIBAAKCAQEA0AvEbNaOnfIL3GjB8VI4M5IaWe+GcK8eSPHkLkXREIsaddum\r\nwPBm/+w8lFYdnY+O06OEJrsaDtwGdU//8cbGJ/H/9cJH3dh0tNbfszP7nTrQD+88\r\nydlcYHzClaG8G+oTe9uEZSVdDXj5IUqR0y6rDXXb9tC9l+oSz+ShYg6+C4grAb3E\r\nSTv5khZ9Zsi/JEPWStqNdpoNuRh7qEYc3t4B/a5BH7bsQENyJSc8AWrfv+drPAEe\r\njQ8xm1ygzWvJp8yZPwOIYuL+obtANcoVT2G2150Wy6qLC0bD88Bm40GqLbSazueC\r\nRHZRug0B9rMUKvKc4FhG4AlNzBCaKgIcCWEqKwIDAQABAoIBACcIjin9d3Sa3S7V\r\nWM32JyVF3DvTfN3XfU8iUzV7U+ZOswA53eeFM04A/Ly4C4ZsUNfUbg72O8Vd8rg/\r\n8j1ilfsYpHVvphwxaHQlfIMa1bKCPlc/A6C7b2GLBtccKTbzjARJA2YWxIaqk9Nz\r\nMjj1IJK98i80qt29xRnMQ5sqOO3gn2SxTErvNchtBiwOH8NirqERXig8VCY6fr3n\r\nz7ZImPU3G/4qpD0+9ULrt9x/VkjqVvNdK1l7CyAuve3D7ha3jPMfVHFtVH5gqbyp\r\nKotyIHAyD+Ex3FQ1JV+H7DkP0cPctQiss7OiO9Zd9C1G2OrfQz9el7ewAPqOmZtC\r\nKjB3hUECgYEA/4MfKa1cvaCqzd3yUprp1JhvssVkhM1HyucIxB5xmBcVLX2/Kdhn\r\nhiDApZXARK0O9IRpFF6QVeMEX7TzFwB6dfkyIePsGxputA5SPbtBlHOvjZa8omMl\r\nEYfNa8x/mJkvSEpzvkWPascuHJWv1cEypqphu/70DxubWB5UKo/8o6cCgYEA0HFy\r\ncgwPMB//nltHGrmaQZPFT7/Qgl9ErZT3G9S8teWY4o4CXnkdU75tBoKAaJnpSfX3\r\nq8VuRerF45AFhqCKhlG4l51oW7TUH50qE3GM+4ivaH5YZB3biwQ9Wqw+QyNLAh/Q\r\nnS4/Wwb8qC9QuyEgcCju5lsCaPEXZiZqtPVxZd0CgYEAshBG31yZjO0zG1TZUwfy\r\nfN3euc8mRgZpSdXIHiS5NSyg7Zr8ZcUSID8jAkJiQ3n3OiAsuq1MGQ6kNa582kLT\r\nFPQdI9Ea8ahyDbkNR0gAY9xbM2kg/Gnro1PorH9PTKE0ekSodKk1UUyNrg4DBAwn\r\nqE6E3ebHXt/2WmqIbUD653ECgYBQCC8EAQNX3AFegPd1GGxU33Lz4tchJ4kMCNU0\r\nN2NZh9VCr3nTYjdTbxsXU8YP44CCKFG2/zAO4kymyiaFAWEOn5P7irGF/JExrjt4\r\nibGy5lFLEq/HiPtBjhgsl1O0nXlwUFzd7OLghXc+8CPUJaz5w42unqT3PBJa40c3\r\nQcIPdQKBgBnSb7BcDAAQ/Qx9juo/RKpvhyeqlnp0GzPSQjvtWi9dQRIu9Pe7luHc\r\nm1Img1EO1OyE3dis/rLaDsAa2AKu1Yx6h85EmNjavBqP9wqmFa0NIQQH8fvzKY3/\r\nP8IHY6009aoamLqYaexvrkHVq7fFKiI6k8myMJ6qblVNFv14+KXU\r\n-----END RSA PRIVATE KEY-----" # noqa: E501 + +TEST_MSG_TEXT = "Topics are forever" +TEST_TOPIC_ICON_COLOR = 0x6FB9F0 +TEST_TOPIC_NAME = "Sad bot true: real stories" diff --git a/tests/conftest.py b/tests/conftest.py index b637f8957..02f83b475 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -40,7 +40,7 @@ from telegram import ( from telegram.ext import Defaults from tests.auxil.build_messages import DATE from tests.auxil.ci_bots import BOT_INFO_PROVIDER, JOB_INDEX -from tests.auxil.constants import PRIVATE_KEY +from tests.auxil.constants import PRIVATE_KEY, TEST_TOPIC_ICON_COLOR, TEST_TOPIC_NAME from tests.auxil.envvars import GITHUB_ACTION, RUN_TEST_OFFICIAL, TEST_WITH_OPT_DEPS from tests.auxil.files import data_file from tests.auxil.networking import NonchalantHttpxRequest @@ -264,6 +264,30 @@ def class_thumb_file(): yield f +@pytest.fixture(scope="session") +async def emoji_id(bot): + emoji_sticker_list = await bot.get_forum_topic_icon_stickers() + first_sticker = emoji_sticker_list[0] + return first_sticker.custom_emoji_id + + +@pytest.fixture +async def real_topic(bot, emoji_id, forum_group_id): + result = await bot.create_forum_topic( + chat_id=forum_group_id, + name=TEST_TOPIC_NAME, + icon_color=TEST_TOPIC_ICON_COLOR, + icon_custom_emoji_id=emoji_id, + ) + + yield result + + result = await bot.delete_forum_topic( + chat_id=forum_group_id, message_thread_id=result.message_thread_id + ) + assert result is True, "Topic was not deleted" + + def _get_false_update_fixture_decorator_params(): message = Message(1, DATE, Chat(1, ""), from_user=User(1, "", False), text="test") params = [ diff --git a/tests/test_bot.py b/tests/test_bot.py index fa723093f..00f653858 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -103,11 +103,10 @@ from tests.auxil.networking import OfflineRequest, expect_bad_request from tests.auxil.pytest_classes import PytestBot, PytestExtBot, make_bot from tests.auxil.slots import mro_slots -from ._files.test_photo import photo_file from .auxil.build_messages import make_message -@pytest.fixture +@pytest.fixture(scope="module") async def message(bot, chat_id): # mostly used in tests for edit_message out = await bot.send_message( chat_id, "Text", disable_web_page_preview=True, disable_notification=True @@ -1128,7 +1127,7 @@ class TestBotWithoutRequest: ) # Test with send media group - media = InputMediaPhoto(photo_file) + media = InputMediaPhoto("") with pytest.raises(ValueError, match="`reply_to_message_id` and"): await offline_bot.send_media_group( chat_id, media, reply_to_message_id=1, reply_parameters=True diff --git a/tests/test_forum.py b/tests/test_forum.py index 4fd65c5d8..70bca8a02 100644 --- a/tests/test_forum.py +++ b/tests/test_forum.py @@ -32,19 +32,9 @@ from telegram import ( Sticker, ) from telegram.error import BadRequest +from tests.auxil.constants import TEST_MSG_TEXT, TEST_TOPIC_ICON_COLOR, TEST_TOPIC_NAME from tests.auxil.slots import mro_slots -TEST_MSG_TEXT = "Topics are forever" -TEST_TOPIC_ICON_COLOR = 0x6FB9F0 -TEST_TOPIC_NAME = "Sad bot true: real stories" - - -@pytest.fixture(scope="module") -async def emoji_id(bot): - emoji_sticker_list = await bot.get_forum_topic_icon_stickers() - first_sticker = emoji_sticker_list[0] - return first_sticker.custom_emoji_id - @pytest.fixture(scope="module") async def forum_topic_object(forum_group_id, emoji_id): @@ -56,23 +46,6 @@ async def forum_topic_object(forum_group_id, emoji_id): ) -@pytest.fixture -async def real_topic(bot, emoji_id, forum_group_id): - result = await bot.create_forum_topic( - chat_id=forum_group_id, - name=TEST_TOPIC_NAME, - icon_color=TEST_TOPIC_ICON_COLOR, - icon_custom_emoji_id=emoji_id, - ) - - yield result - - result = await bot.delete_forum_topic( - chat_id=forum_group_id, message_thread_id=result.message_thread_id - ) - assert result is True, "Topic was not deleted" - - class TestForumTopicWithoutRequest: def test_slot_behaviour(self, forum_topic_object): inst = forum_topic_object