Skip to content

Make network based tests as concurrent as possible #4271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions tests/_files/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2024
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# 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 tests.auxil.files import data_file
from tests.auxil.networking import expect_bad_request


@pytest.fixture()
def animation_file():
with data_file("game.gif").open("rb") as f:
yield f


@pytest.fixture(scope="session")
async def animation(bot, chat_id, aiolib):
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 audio_file():
with data_file("telegram.mp3").open("rb") as f:
yield f


@pytest.fixture(scope="session")
async def audio(bot, chat_id, aiolib):
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 document_file():
with data_file("telegram.png").open("rb") as f:
yield f


@pytest.fixture(scope="session")
async def document(bot, chat_id, aiolib):
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 photo_file():
with data_file("telegram.jpg").open("rb") as f:
yield f


@pytest.fixture(scope="session")
async def photolist(bot, chat_id, aiolib):
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="session")
def thumb(photolist):
return photolist[0]


@pytest.fixture(scope="session")
def photo(photolist):
return photolist[-1]


@pytest.fixture()
def video_file():
with data_file("telegram.mp4").open("rb") as f:
yield f


@pytest.fixture(scope="session")
async def video(bot, chat_id, aiolib):
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_sticker_file():
with data_file("telegram_video_sticker.webm").open("rb") as f:
yield f
14 changes: 0 additions & 14 deletions tests/_files/test_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,6 @@
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 TestAnimationBase:
animation_file_id = "CgADAQADngIAAuyVeEez0xRovKi9VAI"
animation_file_unique_id = "adc3145fd2e84d95b64d68eaa22aa33e"
Expand Down
12 changes: 0 additions & 12 deletions tests/_files/test_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@
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 TestAudioBase:
caption = "Test *audio*"
performer = "Leandro Toledo"
Expand Down
12 changes: 0 additions & 12 deletions tests/_files/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@
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 TestDocumentBase:
caption = "DocumentTest - *Caption*"
document_file_url = "https://python-telegram-bot.org/static/testfiles/telegram.gif"
Expand Down
78 changes: 28 additions & 50 deletions tests/_files/test_inputmedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,14 @@
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):
Expand Down Expand Up @@ -192,7 +174,7 @@ def test_to_dict(self, input_media_video):
== 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_
Expand All @@ -202,7 +184,7 @@ def test_with_video(self, video): # noqa: F811
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_
Expand Down Expand Up @@ -282,14 +264,14 @@ def test_to_dict(self, input_media_photo):
== 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_
Expand Down Expand Up @@ -353,14 +335,14 @@ def test_to_dict(self, input_media_animation):
== 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_
Expand Down Expand Up @@ -421,7 +403,7 @@ def test_to_dict(self, input_media_audio):
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_
Expand All @@ -431,7 +413,7 @@ def test_with_audio(self, audio): # noqa: F811
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_
Expand Down Expand Up @@ -492,14 +474,14 @@ def test_to_dict(self, input_media_document):
== 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_
Expand All @@ -515,7 +497,7 @@ def test_with_local_files(self):


@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="<b>photo</b> 2", parse_mode="HTML"),
Expand All @@ -526,20 +508,20 @@ 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)]),
]


@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"),
Expand Down Expand Up @@ -570,10 +552,10 @@ async def test_send_media_group_custom_filename(
self,
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):
Expand All @@ -597,7 +579,7 @@ async def make_assertion(url, request_data: RequestData, *args, **kwargs):
await bot.send_media_group(chat_id, media)

async def test_send_media_group_with_thumbs(
self, bot, chat_id, video_file, photo_file, monkeypatch # noqa: F811
self, bot, chat_id, video_file, photo_file, monkeypatch
):
async def make_assertion(method, url, request_data: RequestData, *args, **kwargs):
nonlocal input_video
Expand All @@ -615,7 +597,7 @@ async def make_assertion(method, url, request_data: RequestData, *args, **kwargs
await bot.send_media_group(chat_id, [input_video, input_video])

async def test_edit_message_media_with_thumb(
self, bot, chat_id, video_file, photo_file, monkeypatch # noqa: F811
self, bot, chat_id, video_file, photo_file, monkeypatch
):
async def make_assertion(
method: str, url: str, request_data: Optional[RequestData] = None, *args, **kwargs
Expand Down Expand Up @@ -684,9 +666,7 @@ async def test_send_media_group_photo(self, bot, chat_id, media_group):
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,
Expand Down Expand Up @@ -722,7 +702,7 @@ async def test_send_media_group_different_sequences(
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,
Expand Down Expand Up @@ -821,9 +801,7 @@ async def test_send_media_group_all_args(self, bot, raw_bot, chat_id, media_grou
)
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),
Expand Down Expand Up @@ -966,11 +944,11 @@ async def test_edit_message_media_default_parse_mode(
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 = "<b>bold</b> <i>italic</i> <code>code</code>"
markdown_caption = "*bold* _italic_ `code`"
Expand Down
Loading
Loading