mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-04-20 09:01:59 +03:00
Added caption extraction when send a msg with a file (#4499)
Closes #4499
This commit is contained in:
parent
3f589b287d
commit
3a1d25c80a
|
@ -826,6 +826,8 @@ class MessageMethods:
|
|||
await client.send_message(chat, 'Hi, future!', schedule=timedelta(minutes=5))
|
||||
"""
|
||||
if file is not None:
|
||||
if isinstance(message, types.Message):
|
||||
message = message.message
|
||||
return await self.send_file(
|
||||
entity, file, caption=message, reply_to=reply_to,
|
||||
attributes=attributes, parse_mode=parse_mode,
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
import inspect
|
||||
from unittest import mock
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from telethon import TelegramClient
|
||||
from telethon.client import MessageMethods
|
||||
from telethon.tl.types import PeerChat, MessageMediaDocument, Message
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
@ -38,3 +42,36 @@ async def test_send_message_with_file_forwards_args():
|
|||
|
||||
client = MockedClient()
|
||||
assert (await client.send_message('a', file='b', **arguments)) == sentinel
|
||||
|
||||
|
||||
class TestMessageMethods:
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_msg_and_file(self):
|
||||
async def async_func(result): # AsyncMock was added only in 3.8
|
||||
return result
|
||||
msg_methods = MessageMethods()
|
||||
expected_result = Message(
|
||||
id=0, peer_id=PeerChat(chat_id=0), message='', date=None,
|
||||
)
|
||||
entity = 'test_entity'
|
||||
message = Message(
|
||||
id=1, peer_id=PeerChat(chat_id=0), message='expected_caption', date=None,
|
||||
)
|
||||
media_file = MessageMediaDocument()
|
||||
|
||||
with mock.patch.object(
|
||||
target=MessageMethods, attribute='send_file',
|
||||
new=MagicMock(return_value=async_func(expected_result)), create=True,
|
||||
) as mock_obj:
|
||||
result = await msg_methods.send_message(
|
||||
entity=entity, message=message, file=media_file,
|
||||
)
|
||||
mock_obj.assert_called_once_with(
|
||||
entity, media_file, caption=message.message,
|
||||
reply_to=None, attributes=None, parse_mode=(),
|
||||
force_document=False, thumb=None, buttons=None,
|
||||
clear_draft=False, silent=None, schedule=None,
|
||||
supports_streaming=False, formatting_entities=None,
|
||||
comment_to=None, background=None, nosound_video=None,
|
||||
)
|
||||
assert result == expected_result
|
||||
|
|
Loading…
Reference in New Issue
Block a user