diff --git a/test/test_bot.py b/test/test_bot.py index 1f0b6a6..230c8a7 100644 --- a/test/test_bot.py +++ b/test/test_bot.py @@ -1,3 +1,4 @@ +import asyncio from unittest.mock import Mock, AsyncMock, patch import pytest @@ -10,28 +11,14 @@ from bot import Music async def test_bot_ensure_voice(bot, ctx): mbot = Music(bot) - # 1. Inside a voice channel - # 1.1 Does not call stop if no sound is playing - ctx.voice_client.is_playing.return_value = False - await mbot.ensure_voice(ctx) - assert ctx.voice_client.stop.call_count == 0 - ctx.reset_mock(return_value=True) - - # 1.2 Does call stop if sound is playing - ctx.voice_client.is_playing.return_value = True - await mbot.ensure_voice(ctx) - assert ctx.voice_client.stop.call_count == 1 - ctx.reset_mock(return_value=True) - - # 2. Not inside a voice channel - # 2.1 Connects to voice channel of author if possible + # Connects to voice channel of author if possible ctx.voice_client = None ctx.author.voice = AsyncMock() await mbot.ensure_voice(ctx) assert ctx.author.voice.channel.connect.call_count == 1 ctx.reset_mock(return_value=True) - # 2.2 Error if author not inside a channel + # Error if author not inside a channel ctx.voice_client = None ctx.author.voice = None with pytest.raises(commands.CommandError): @@ -42,13 +29,27 @@ async def test_bot_ensure_voice(bot, ctx): async def test_bot_play(bot, ctx): mbot = Music(bot) - with patch('bot.YTDLSource', new_callable=AsyncMock) as ytdl_source: - player = Mock() - ytdl_source.from_url.return_value = player - url = 'A Day To Remember - All I Want' - # pylint: disable=too-many-function-args - await mbot.play(mbot, ctx, url=url) - assert ytdl_source.from_url.await_args.args == (url,) - assert ytdl_source.from_url.await_args.kwargs == {"loop": bot.loop, "stream": True} - assert ctx.voice_client.play.call_args.args == (player,) - assert ctx.send.call_count == 1 + with patch.object(mbot, '_ytdl') as ytdl: + with patch('discord.FFmpegPCMAudio') as ffmpeg_pcm_audio: + ctx.voice_client.is_playing.return_value = False + url = "https://www.youtube.com/watch?v=Wr9LZ1hAFpQ" + title = "In Flames - Deliver Us (Official Video)" + ytdl.extract_info.return_value = { + "entries": [ + { + "url": url, + "title": title + } + ] + } + audio = Mock() + ffmpeg_pcm_audio.return_value = audio + query = 'in flames deliver us' + # pylint: disable=too-many-function-args + await mbot.play(mbot, ctx, query=query) + await asyncio.sleep(0) + assert ytdl.extract_info.call_args.args == (query,) + assert ytdl.extract_info.call_args.kwargs == {"download": False} + assert ffmpeg_pcm_audio.call_args.args == (url,) + assert ctx.voice_client.play.call_args.args == (audio,) + assert ctx.send.call_count == 1