mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-12-28 07:20:17 +01:00
Use Temporary Files for Testing File Downloads (#3777)
This commit is contained in:
parent
1d27a0fadb
commit
5534ddfaa0
11 changed files with 35 additions and 53 deletions
|
@ -260,15 +260,12 @@ class TestAnimationWithRequest(TestAnimationBase):
|
|||
except AssertionError:
|
||||
pytest.xfail("This is a bug on Telegram's end")
|
||||
|
||||
async def test_get_and_download(self, bot, animation):
|
||||
path = Path("game.gif")
|
||||
path.unlink(missing_ok=True)
|
||||
|
||||
async def test_get_and_download(self, bot, animation, tmp_file):
|
||||
new_file = await bot.get_file(animation.file_id)
|
||||
|
||||
assert new_file.file_path.startswith("https://")
|
||||
|
||||
new_filepath = await new_file.download_to_drive("game.gif")
|
||||
new_filepath = await new_file.download_to_drive(tmp_file)
|
||||
assert new_filepath.is_file()
|
||||
|
||||
async def test_send_animation_url_file(self, bot, chat_id, animation):
|
||||
|
|
|
@ -259,18 +259,15 @@ class TestAudioWithRequest(TestAudioBase):
|
|||
assert message.audio.thumbnail.height == self.thumb_height
|
||||
assert message.has_protected_content
|
||||
|
||||
async def test_get_and_download(self, bot, chat_id, audio):
|
||||
path = Path("telegram.mp3")
|
||||
path.unlink(missing_ok=True)
|
||||
|
||||
async def test_get_and_download(self, bot, chat_id, audio, tmp_file):
|
||||
new_file = await bot.get_file(audio.file_id)
|
||||
|
||||
assert new_file.file_size == self.file_size
|
||||
assert new_file.file_unique_id == audio.file_unique_id
|
||||
assert str(new_file.file_path).startswith("https://")
|
||||
|
||||
await new_file.download_to_drive("telegram.mp3")
|
||||
assert path.is_file()
|
||||
await new_file.download_to_drive(tmp_file)
|
||||
assert tmp_file.is_file()
|
||||
|
||||
async def test_send_mp3_url_file(self, bot, chat_id, audio):
|
||||
message = await bot.send_audio(
|
||||
|
|
|
@ -155,10 +155,7 @@ class TestChatPhotoWithoutRequest(TestChatPhotoBase):
|
|||
|
||||
|
||||
class TestChatPhotoWithRequest:
|
||||
async def test_get_and_download(self, bot, chat_photo):
|
||||
jpg_file = Path("telegram.jpg")
|
||||
jpg_file.unlink(missing_ok=True)
|
||||
|
||||
async def test_get_and_download(self, bot, chat_photo, tmp_file):
|
||||
tasks = {bot.get_file(chat_photo.small_file_id), bot.get_file(chat_photo.big_file_id)}
|
||||
asserts = []
|
||||
|
||||
|
@ -170,8 +167,8 @@ class TestChatPhotoWithRequest:
|
|||
asserts.append("big")
|
||||
assert file.file_path.startswith("https://")
|
||||
|
||||
await file.download_to_drive(jpg_file)
|
||||
assert jpg_file.is_file()
|
||||
await file.download_to_drive(tmp_file)
|
||||
assert tmp_file.is_file()
|
||||
|
||||
assert "small" in asserts
|
||||
assert "big" in asserts
|
||||
|
|
|
@ -229,19 +229,16 @@ class TestDocumentWithRequest(TestDocumentBase):
|
|||
with pytest.raises(TelegramError):
|
||||
await bot.send_document(chat_id=chat_id, document="")
|
||||
|
||||
async def test_get_and_download(self, bot, document, chat_id):
|
||||
path = Path("telegram.png")
|
||||
path.unlink(missing_ok=True)
|
||||
|
||||
async def test_get_and_download(self, bot, document, chat_id, tmp_file):
|
||||
new_file = await bot.get_file(document.file_id)
|
||||
|
||||
assert new_file.file_size == document.file_size
|
||||
assert new_file.file_unique_id == document.file_unique_id
|
||||
assert new_file.file_path.startswith("https://")
|
||||
|
||||
await new_file.download_to_drive("telegram.png")
|
||||
await new_file.download_to_drive(tmp_file)
|
||||
|
||||
assert path.is_file()
|
||||
assert tmp_file.is_file()
|
||||
|
||||
async def test_send_resend(self, bot, chat_id, document):
|
||||
message = await bot.send_document(chat_id=chat_id, document=document.file_id)
|
||||
|
|
|
@ -280,6 +280,8 @@ class TestFileWithRequest(TestFileBase):
|
|||
|
||||
async def test_download_local_file(self, local_file):
|
||||
assert await local_file.download_to_drive() == Path(local_file.file_path)
|
||||
# Ensure that the file contents didn't change
|
||||
assert Path(local_file.file_path).read_bytes() == self.file_content
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"custom_path_type", [str, Path], ids=["str custom_path", "pathlib.Path custom_path"]
|
||||
|
|
|
@ -360,19 +360,16 @@ class TestPhotoWithRequest(TestPhotoBase):
|
|||
chat_id, photo_file, reply_to_message_id=reply_to_message.message_id
|
||||
)
|
||||
|
||||
async def test_get_and_download(self, bot, photo):
|
||||
path = Path("telegram.jpg")
|
||||
path.unlink(missing_ok=True)
|
||||
|
||||
async def test_get_and_download(self, bot, photo, tmp_file):
|
||||
new_file = await bot.getFile(photo.file_id)
|
||||
|
||||
assert new_file.file_size == photo.file_size
|
||||
assert new_file.file_unique_id == photo.file_unique_id
|
||||
assert new_file.file_path.startswith("https://") is True
|
||||
|
||||
await new_file.download_to_drive("telegram.jpg")
|
||||
await new_file.download_to_drive(tmp_file)
|
||||
|
||||
assert path.is_file()
|
||||
assert tmp_file.is_file()
|
||||
|
||||
async def test_send_url_jpg_file(self, bot, chat_id):
|
||||
message = await bot.send_photo(chat_id, photo=self.photo_file_url)
|
||||
|
|
|
@ -334,19 +334,16 @@ class TestStickerWithRequest(TestStickerBase):
|
|||
assert message.sticker.thumbnail.height == sticker.thumbnail.height
|
||||
assert message.sticker.thumbnail.file_size == sticker.thumbnail.file_size
|
||||
|
||||
async def test_get_and_download(self, bot, sticker):
|
||||
path = Path("telegram.webp")
|
||||
path.unlink(missing_ok=True)
|
||||
|
||||
async def test_get_and_download(self, bot, sticker, tmp_file):
|
||||
new_file = await bot.get_file(sticker.file_id)
|
||||
|
||||
assert new_file.file_size == sticker.file_size
|
||||
assert new_file.file_unique_id == sticker.file_unique_id
|
||||
assert new_file.file_path.startswith("https://")
|
||||
|
||||
await new_file.download_to_drive("telegram.webp")
|
||||
await new_file.download_to_drive(tmp_file)
|
||||
|
||||
assert path.is_file()
|
||||
assert tmp_file.is_file()
|
||||
|
||||
async def test_resend(self, bot, chat_id, sticker):
|
||||
message = await bot.send_sticker(chat_id=chat_id, sticker=sticker.file_id)
|
||||
|
|
|
@ -277,19 +277,16 @@ class TestVideoWithRequest(TestVideoBase):
|
|||
assert message.has_protected_content
|
||||
assert message.has_media_spoiler
|
||||
|
||||
async def test_get_and_download(self, bot, video, chat_id):
|
||||
path = Path("telegram.mp4")
|
||||
path.unlink(missing_ok=True)
|
||||
|
||||
async def test_get_and_download(self, bot, video, chat_id, tmp_file):
|
||||
new_file = await bot.get_file(video.file_id)
|
||||
|
||||
assert new_file.file_size == self.file_size
|
||||
assert new_file.file_unique_id == video.file_unique_id
|
||||
assert new_file.file_path.startswith("https://")
|
||||
|
||||
await new_file.download_to_drive("telegram.mp4")
|
||||
await new_file.download_to_drive(tmp_file)
|
||||
|
||||
assert path.is_file()
|
||||
assert tmp_file.is_file()
|
||||
|
||||
async def test_send_mp4_file_url(self, bot, chat_id, video):
|
||||
message = await bot.send_video(chat_id, self.video_file_url, caption=self.caption)
|
||||
|
|
|
@ -248,19 +248,16 @@ class TestVideoNoteWithRequest(TestVideoNoteBase):
|
|||
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):
|
||||
path = Path("telegram2.mp4")
|
||||
path.unlink(missing_ok=True)
|
||||
|
||||
async def test_get_and_download(self, bot, video_note, chat_id, tmp_file):
|
||||
new_file = await bot.get_file(video_note.file_id)
|
||||
|
||||
assert new_file.file_size == self.file_size
|
||||
assert new_file.file_unique_id == video_note.file_unique_id
|
||||
assert new_file.file_path.startswith("https://")
|
||||
|
||||
await new_file.download_to_drive("telegram2.mp4")
|
||||
await new_file.download_to_drive(tmp_file)
|
||||
|
||||
assert path.is_file()
|
||||
assert tmp_file.is_file()
|
||||
|
||||
async def test_resend(self, bot, chat_id, video_note):
|
||||
message = await bot.send_video_note(chat_id, video_note.file_id)
|
||||
|
|
|
@ -199,19 +199,16 @@ class TestVoiceWithRequest(TestVoiceBase):
|
|||
assert message.caption == self.caption.replace("*", "")
|
||||
assert message.has_protected_content
|
||||
|
||||
async def test_get_and_download(self, bot, voice, chat_id):
|
||||
path = Path("telegram.ogg")
|
||||
path.unlink(missing_ok=True)
|
||||
|
||||
async def test_get_and_download(self, bot, voice, chat_id, tmp_file):
|
||||
new_file = await bot.get_file(voice.file_id)
|
||||
|
||||
assert new_file.file_size == voice.file_size
|
||||
assert new_file.file_unique_id == voice.file_unique_id
|
||||
assert new_file.file_path.startswith("https://")
|
||||
|
||||
await new_file.download_to_drive("telegram.ogg")
|
||||
await new_file.download_to_drive(tmp_file)
|
||||
|
||||
assert path.is_file()
|
||||
assert tmp_file.is_file()
|
||||
|
||||
async def test_send_ogg_url_file(self, bot, chat_id, voice):
|
||||
message = await bot.sendVoice(chat_id, self.voice_file_url, duration=self.duration)
|
||||
|
|
|
@ -20,6 +20,7 @@ import asyncio
|
|||
import datetime
|
||||
import sys
|
||||
from typing import Dict, List
|
||||
from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
|
||||
|
@ -277,3 +278,9 @@ def tzinfo(request):
|
|||
@pytest.fixture(scope="session")
|
||||
def timezone(tzinfo):
|
||||
return tzinfo
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def tmp_file(tmp_path):
|
||||
with tmp_path / uuid4().hex as file:
|
||||
yield file
|
||||
|
|
Loading…
Reference in a new issue