From 6ddeaa441746d47c1e2db282ffa48b08e01c3c29 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Mon, 2 Dec 2024 02:25:29 +0300 Subject: [PATCH] Remove path to string conversion --- aiogram/client/bot.py | 6 +++--- aiogram/client/telegram.py | 2 +- tests/test_api/test_client/test_api_server.py | 12 ++++++++---- tests/test_api/test_client/test_bot.py | 6 ++++-- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/aiogram/client/bot.py b/aiogram/client/bot.py index b538485f..17e1fa58 100644 --- a/aiogram/client/bot.py +++ b/aiogram/client/bot.py @@ -387,7 +387,7 @@ class Bot: @classmethod async def __aiofiles_reader( - cls, file: str, chunk_size: int = 65536 + cls, file: Union[str, pathlib.Path], chunk_size: int = 65536 ) -> AsyncGenerator[bytes, None]: async with aiofiles.open(file, "rb") as f: while chunk := await f.read(chunk_size): @@ -395,7 +395,7 @@ class Bot: async def download_file( self, - file_path: str, + file_path: Union[str, pathlib.Path], destination: Optional[Union[BinaryIO, pathlib.Path, str]] = None, timeout: int = 30, chunk_size: int = 65536, @@ -419,7 +419,7 @@ class Bot: close_stream = False if self.session.api.is_local: stream = self.__aiofiles_reader( - str(self.session.api.wrap_local_file.to_local(file_path)), chunk_size=chunk_size + self.session.api.wrap_local_file.to_local(file_path), chunk_size=chunk_size ) close_stream = True else: diff --git a/aiogram/client/telegram.py b/aiogram/client/telegram.py index 5e29722f..cfb3c49d 100644 --- a/aiogram/client/telegram.py +++ b/aiogram/client/telegram.py @@ -67,7 +67,7 @@ class TelegramAPIServer: """ return self.base.format(token=token, method=method) - def file_url(self, token: str, path: str) -> str: + def file_url(self, token: str, path: Union[str, Path]) -> str: """ Generate URL for downloading files diff --git a/tests/test_api/test_client/test_api_server.py b/tests/test_api/test_client/test_api_server.py index 118ea630..36fd5140 100644 --- a/tests/test_api/test_client/test_api_server.py +++ b/tests/test_api/test_client/test_api_server.py @@ -1,5 +1,7 @@ from pathlib import Path +import pytest + from aiogram.client.telegram import ( PRODUCTION, BareFilesPathWrapper, @@ -13,15 +15,17 @@ class TestAPIServer: method_url = PRODUCTION.api_url(token="42:TEST", method="apiMethod") assert method_url == "https://api.telegram.org/bot42:TEST/apiMethod" - def test_file_url(self): - file_url = PRODUCTION.file_url(token="42:TEST", path="path") + @pytest.mark.parametrize("path", ["path", Path("path")]) + def test_file_url(self, path): + file_url = PRODUCTION.file_url(token="42:TEST", path=path) assert file_url == "https://api.telegram.org/file/bot42:TEST/path" - def test_from_base(self): + @pytest.mark.parametrize("path", ["path", Path("path")]) + def test_from_base(self, path): local_server = TelegramAPIServer.from_base("http://localhost:8081", is_local=True) method_url = local_server.api_url("42:TEST", method="apiMethod") - file_url = local_server.file_url(token="42:TEST", path="path") + file_url = local_server.file_url(token="42:TEST", path=path) assert method_url == "http://localhost:8081/bot42:TEST/apiMethod" assert file_url == "http://localhost:8081/file/bot42:TEST/path" diff --git a/tests/test_api/test_client/test_bot.py b/tests/test_api/test_client/test_bot.py index 34593864..11a11c34 100644 --- a/tests/test_api/test_client/test_bot.py +++ b/tests/test_api/test_client/test_bot.py @@ -1,5 +1,6 @@ import io import os +from pathlib import Path from tempfile import mkstemp from unittest.mock import AsyncMock, MagicMock, patch @@ -112,7 +113,8 @@ class TestBot: mocked_close.assert_not_awaited() await session.close() - async def test_download_file(self, aresponses: ResponsesMockServer): + @pytest.mark.parametrize("file_path", ["file.png", Path("file.png")]) + async def test_download_file(self, aresponses: ResponsesMockServer, file_path): aresponses.add( method_pattern="get", response=aresponses.Response(status=200, body=b"\f" * 10), @@ -127,7 +129,7 @@ class TestBot: async with Bot("42:TEST").context() as bot: with patch("aiofiles.threadpool.sync_open", return_value=mock_file): - await bot.download_file("TEST", "file.png") + await bot.download_file("TEST", file_path) mock_file.write.assert_called_once_with(b"\f" * 10) async def test_download_file_default_destination(