mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 10:11:52 +00:00
Reworked InputFile sending (#1238)
* Reworked InputFile sending * Added changelog
This commit is contained in:
parent
c7b7714959
commit
a7916c1103
7 changed files with 32 additions and 34 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import asyncio
|
||||
from typing import Any, AsyncContextManager, AsyncGenerator, Dict, List
|
||||
from typing import Any, AsyncContextManager, AsyncGenerator, AsyncIterable, Dict, List
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import aiohttp_socks
|
||||
|
|
@ -156,7 +156,7 @@ class TestAiohttpSession:
|
|||
assert fields[1][2].startswith("attach://")
|
||||
assert fields[2][0]["name"] == fields[1][2][9:]
|
||||
assert fields[2][0]["filename"] == "file.txt"
|
||||
assert isinstance(fields[2][2], BareInputFile)
|
||||
assert isinstance(fields[2][2], AsyncIterable)
|
||||
|
||||
async def test_make_request(self, bot: MockedBot, aresponses: ResponsesMockServer):
|
||||
aresponses.add(
|
||||
|
|
|
|||
|
|
@ -24,11 +24,7 @@ from aiogram.methods import (
|
|||
SendVideoNote,
|
||||
SendVoice,
|
||||
)
|
||||
from aiogram.types import (
|
||||
Chat,
|
||||
ChatJoinRequest,
|
||||
User,
|
||||
)
|
||||
from aiogram.types import Chat, ChatJoinRequest, User
|
||||
|
||||
|
||||
class TestChatJoinRequest:
|
||||
|
|
|
|||
|
|
@ -12,19 +12,18 @@ class TestInputFile:
|
|||
file = FSInputFile(__file__)
|
||||
|
||||
assert isinstance(file, InputFile)
|
||||
assert isinstance(file, AsyncIterable)
|
||||
assert file.filename is not None
|
||||
assert file.filename.startswith("test_")
|
||||
assert file.filename.endswith(".py")
|
||||
assert file.chunk_size > 0
|
||||
|
||||
async def test_fs_input_file_readable(self):
|
||||
async def test_fs_input_file_readable(self, bot: MockedBot):
|
||||
file = FSInputFile(__file__, chunk_size=1)
|
||||
|
||||
assert file.chunk_size == 1
|
||||
|
||||
size = 0
|
||||
async for chunk in file:
|
||||
async for chunk in file.read(bot):
|
||||
chunk_size = len(chunk)
|
||||
assert chunk_size == 1
|
||||
size += chunk_size
|
||||
|
|
@ -34,15 +33,14 @@ class TestInputFile:
|
|||
file = BufferedInputFile(b"\f" * 10, filename="file.bin")
|
||||
|
||||
assert isinstance(file, InputFile)
|
||||
assert isinstance(file, AsyncIterable)
|
||||
assert file.filename == "file.bin"
|
||||
assert isinstance(file.data, bytes)
|
||||
|
||||
async def test_buffered_input_file_readable(self):
|
||||
async def test_buffered_input_file_readable(self, bot: MockedBot):
|
||||
file = BufferedInputFile(b"\f" * 10, filename="file.bin", chunk_size=1)
|
||||
|
||||
size = 0
|
||||
async for chunk in file:
|
||||
async for chunk in file.read(bot):
|
||||
chunk_size = len(chunk)
|
||||
assert chunk_size == 1
|
||||
size += chunk_size
|
||||
|
|
@ -52,32 +50,31 @@ class TestInputFile:
|
|||
file = BufferedInputFile.from_file(__file__, chunk_size=10)
|
||||
|
||||
assert isinstance(file, InputFile)
|
||||
assert isinstance(file, AsyncIterable)
|
||||
assert file.filename is not None
|
||||
assert file.filename.startswith("test_")
|
||||
assert file.filename.endswith(".py")
|
||||
assert isinstance(file.data, bytes)
|
||||
assert file.chunk_size == 10
|
||||
|
||||
async def test_buffered_input_file_from_file_readable(self):
|
||||
async def test_buffered_input_file_from_file_readable(self, bot: MockedBot):
|
||||
file = BufferedInputFile.from_file(__file__, chunk_size=1)
|
||||
|
||||
size = 0
|
||||
async for chunk in file:
|
||||
async for chunk in file.read(bot):
|
||||
chunk_size = len(chunk)
|
||||
assert chunk_size == 1
|
||||
size += chunk_size
|
||||
assert size > 0
|
||||
|
||||
async def test_uri_input_file(self, aresponses: ResponsesMockServer):
|
||||
async def test_url_input_file(self, aresponses: ResponsesMockServer):
|
||||
aresponses.add(
|
||||
aresponses.ANY, aresponses.ANY, "get", aresponses.Response(status=200, body=b"\f" * 10)
|
||||
)
|
||||
bot = Bot(token="42:TEST")
|
||||
file = URLInputFile("https://test.org/", bot, chunk_size=1)
|
||||
file = URLInputFile("https://test.org/", chunk_size=1)
|
||||
|
||||
size = 0
|
||||
async for chunk in file:
|
||||
async for chunk in file.read(bot):
|
||||
assert chunk == b"\f"
|
||||
chunk_size = len(chunk)
|
||||
assert chunk_size == 1
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue