mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
add Bot.download_file aliases ability to save files to a directory and automatically create directories (#694)
* add destination_dir and make_dirs parameters to bot download aliases * add the ability to save files to a directory with path completion based on file_path, * add an option to automatically create directories in the file path * Downloadable mixin uses directory creation parameter in bot methods
This commit is contained in:
parent
204a2a1ec0
commit
b98ec3efad
4 changed files with 127 additions and 17 deletions
78
tests/test_bot/test_bot_download_file.py
Normal file
78
tests/test_bot/test_bot_download_file.py
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
import os
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from aiogram import Bot
|
||||
from aiogram.types import File
|
||||
from tests import TOKEN
|
||||
from tests.types.dataset import FILE
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
@pytest.fixture(name='bot')
|
||||
async def bot_fixture():
|
||||
async def get_file():
|
||||
return File(**FILE)
|
||||
|
||||
""" Bot fixture """
|
||||
_bot = Bot(TOKEN)
|
||||
_bot.get_file = get_file
|
||||
yield _bot
|
||||
await _bot.session.close()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def file():
|
||||
return File(**FILE)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def tmppath(tmpdir, request):
|
||||
os.chdir(tmpdir)
|
||||
yield Path(tmpdir)
|
||||
os.chdir(request.config.invocation_dir)
|
||||
|
||||
|
||||
class TestBotDownload:
|
||||
async def test_download_file(self, tmppath, bot, file):
|
||||
f = await bot.download_file(file_path=file.file_path)
|
||||
assert len(f.read()) != 0
|
||||
|
||||
async def test_download_file_destination(self, tmppath, bot, file):
|
||||
await bot.download_file(file_path=file.file_path, destination="test.file")
|
||||
assert os.path.isfile(tmppath.joinpath('test.file'))
|
||||
|
||||
async def test_download_file_destination_with_dir(self, tmppath, bot, file):
|
||||
await bot.download_file(file_path=file.file_path,
|
||||
destination=os.path.join('dir_name', 'file_name'))
|
||||
assert os.path.isfile(tmppath.joinpath('dir_name', 'file_name'))
|
||||
|
||||
async def test_download_file_destination_raise_file_not_found(self, tmppath, bot, file):
|
||||
with pytest.raises(FileNotFoundError):
|
||||
await bot.download_file(file_path=file.file_path,
|
||||
destination=os.path.join('dir_name', 'file_name'),
|
||||
make_dirs=False)
|
||||
|
||||
async def test_download_file_destination_io_bytes(self, tmppath, bot, file):
|
||||
f = BytesIO()
|
||||
await bot.download_file(file_path=file.file_path,
|
||||
destination=f)
|
||||
assert len(f.read()) != 0
|
||||
|
||||
async def test_download_file_raise_value_error(self, tmppath, bot, file):
|
||||
with pytest.raises(ValueError):
|
||||
await bot.download_file(file_path=file.file_path, destination="a", destination_dir="b")
|
||||
|
||||
async def test_download_file_destination_dir(self, tmppath, bot, file):
|
||||
await bot.download_file(file_path=file.file_path, destination_dir='test_dir')
|
||||
assert os.path.isfile(tmppath.joinpath('test_dir', file.file_path))
|
||||
|
||||
async def test_download_file_destination_dir_raise_file_not_found(self, tmppath, bot, file):
|
||||
with pytest.raises(FileNotFoundError):
|
||||
await bot.download_file(file_path=file.file_path,
|
||||
destination_dir='test_dir',
|
||||
make_dirs=False)
|
||||
assert os.path.isfile(tmppath.joinpath('test_dir', file.file_path))
|
||||
Loading…
Add table
Add a link
Reference in a new issue