Skip to content
7 changes: 2 additions & 5 deletions tests/_files/test_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,12 @@ async def test_send_all_args(self, bot, chat_id, animation_file, animation, thum
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):
Expand Down
9 changes: 3 additions & 6 deletions tests/_files/test_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,18 +259,15 @@ async def test_send_all_args(self, bot, chat_id, audio_file, thumb_file):
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(
Expand Down
9 changes: 3 additions & 6 deletions tests/_files/test_chatphoto.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,7 @@ async def make_assertion(*_, **kwargs):


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 = []

Expand All @@ -170,8 +167,8 @@ async def test_get_and_download(self, bot, chat_photo):
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
Expand Down
9 changes: 3 additions & 6 deletions tests/_files/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,16 @@ async def test_error_send_empty_file_id(self, bot, chat_id):
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)
Expand Down
2 changes: 2 additions & 0 deletions tests/_files/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ async def test_error_get_empty_file_id(self, bot):

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"]
Expand Down
9 changes: 3 additions & 6 deletions tests/_files/test_photo.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,19 +360,16 @@ async def test_send_photo_default_allow_sending_without_reply(
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)
Expand Down
9 changes: 3 additions & 6 deletions tests/_files/test_sticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,19 +334,16 @@ async def test_send_all_args(self, bot, chat_id, sticker_file, sticker):
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)
Expand Down
9 changes: 3 additions & 6 deletions tests/_files/test_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,19 +277,16 @@ async def test_send_all_args(self, bot, chat_id, video_file, video, thumb_file):
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)
Expand Down
9 changes: 3 additions & 6 deletions tests/_files/test_videonote.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,19 +248,16 @@ async def test_send_all_args(self, bot, chat_id, video_note_file, video_note, th
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)
Expand Down
9 changes: 3 additions & 6 deletions tests/_files/test_voice.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,19 +199,16 @@ async def test_send_all_args(self, bot, chat_id, voice_file, voice):
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)
Expand Down
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import datetime
import sys
from typing import Dict, List
from uuid import uuid4

import pytest

Expand Down Expand Up @@ -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