diff --git a/tests/_files/test_animation.py b/tests/_files/test_animation.py index e6516ab62..8aeed67cd 100644 --- a/tests/_files/test_animation.py +++ b/tests/_files/test_animation.py @@ -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): diff --git a/tests/_files/test_audio.py b/tests/_files/test_audio.py index 786744138..fe7cc2774 100644 --- a/tests/_files/test_audio.py +++ b/tests/_files/test_audio.py @@ -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( diff --git a/tests/_files/test_chatphoto.py b/tests/_files/test_chatphoto.py index 9162b7ff1..d09876e28 100644 --- a/tests/_files/test_chatphoto.py +++ b/tests/_files/test_chatphoto.py @@ -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 diff --git a/tests/_files/test_document.py b/tests/_files/test_document.py index e7842901c..4ab040e14 100644 --- a/tests/_files/test_document.py +++ b/tests/_files/test_document.py @@ -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) diff --git a/tests/_files/test_file.py b/tests/_files/test_file.py index f4324f9a5..7b3a47c3c 100644 --- a/tests/_files/test_file.py +++ b/tests/_files/test_file.py @@ -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"] diff --git a/tests/_files/test_photo.py b/tests/_files/test_photo.py index dee157a07..6419ff7b3 100644 --- a/tests/_files/test_photo.py +++ b/tests/_files/test_photo.py @@ -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) diff --git a/tests/_files/test_sticker.py b/tests/_files/test_sticker.py index db0313bc0..c29d33d9b 100644 --- a/tests/_files/test_sticker.py +++ b/tests/_files/test_sticker.py @@ -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) diff --git a/tests/_files/test_video.py b/tests/_files/test_video.py index 103225e3c..5722cf753 100644 --- a/tests/_files/test_video.py +++ b/tests/_files/test_video.py @@ -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) diff --git a/tests/_files/test_videonote.py b/tests/_files/test_videonote.py index b0fbdc45c..5973e797f 100644 --- a/tests/_files/test_videonote.py +++ b/tests/_files/test_videonote.py @@ -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) diff --git a/tests/_files/test_voice.py b/tests/_files/test_voice.py index 90f197bbd..24899d4af 100644 --- a/tests/_files/test_voice.py +++ b/tests/_files/test_voice.py @@ -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) diff --git a/tests/conftest.py b/tests/conftest.py index 817cd5039..7c6a9a661 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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