1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
import pytest
from unittest.mock import AsyncMock, patch, MagicMock
import discord
from conftest import MockApplicationContext
@pytest.fixture
def tts_cog(bot):
from cogs.tts import TTS
cog = TTS(bot)
return cog
@pytest.mark.asyncio
async def test_tts_file_output(tts_cog, mock_channel, mock_guild, bot, mock_user):
ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild)
ctx.user = mock_user
ctx.author = mock_user
"/tmp/test_tts.mp3"
with patch('cogs.tts.gTTS') as _:
with patch('os.path.exists', return_value=True):
with patch('os.unlink'):
with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond:
with patch('cogs.tts.get_volume', return_value=1.0):
await tts_cog.tts.callback(tts_cog, ctx, text="Hello", output="file")
mock_respond.assert_called_once()
@pytest.mark.asyncio
async def test_tts_voice_output_not_in_channel(tts_cog, mock_channel, mock_guild, bot, mock_user):
ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild)
ctx.user = mock_user
ctx.author = mock_user
ctx.author.voice = None
with patch('cogs.tts.gTTS'):
with patch('os.path.exists', return_value=True):
with patch('os.unlink'):
with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond:
with patch('cogs.tts.get_volume', return_value=1.0):
await tts_cog.tts.callback(tts_cog, ctx, text="Hello", output="vc")
@pytest.mark.asyncio
async def test_tts_error_handling(tts_cog, mock_channel, mock_guild, bot, mock_user):
ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild)
ctx.user = mock_user
ctx.author = mock_user
with patch('cogs.tts.gTTS', side_effect=Exception("TTS Error")):
with patch('os.path.exists', return_value=False):
with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond:
await tts_cog.tts.callback(tts_cog, ctx, text="Hello", output="file")
mock_respond.assert_called_once()
args = mock_respond.call_args
assert "Error" in str(args) or "eror" in str(args)
|