From 645e6556a436a31c6194d4766ecc4429bdbc3d7d Mon Sep 17 00:00:00 2001 From: ekzyis Date: Sat, 25 Sep 2021 20:58:09 +0200 Subject: [PATCH] Add skip test --- test/conftest.py | 4 +++- test/test_bot.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/test/conftest.py b/test/conftest.py index 65744e7..6902468 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -29,7 +29,9 @@ def mbot(bot): @pytest.fixture def ctx(mocker: MockerFixture): - yield mocker.patch('discord.ext.commands.Context', autospec=True) + ctx_mock = mocker.patch('discord.ext.commands.Context', autospec=True) + ctx_mock.voice_client.stop = lambda: ctx_mock.voice_client.play.call_args.kwargs["after"](None) + return ctx_mock @pytest.fixture diff --git a/test/test_bot.py b/test/test_bot.py index b7d4968..7152ed2 100644 --- a/test/test_bot.py +++ b/test/test_bot.py @@ -96,3 +96,51 @@ async def test_bot_playback(mbot, ctx): assert \ ctx.voice_client.play.call_args.args == (time_of_dying_audio,), \ "Did not queue next song" + + +@pytest.mark.asyncio +async def test_bot_skip(mbot, ctx): + 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)" + mock_ytdl_extract_info(ytdl, url, title) + deliver_us_audio = mock_ffmpeg_pcm_audio(ffmpeg_pcm_audio) + query = 'in flames deliver us' + # pylint: disable=too-many-function-args + await mbot.play(mbot, ctx, query=query) + await asyncio.sleep(0) + assert \ + ctx.voice_client.play.call_args.args == (deliver_us_audio,), \ + "Did not playback correct audio" + + ctx.voice_client.is_playing.return_value = True + url = "https://www.youtube.com/watch?v=pMDcYX2wRSg" + title = "Three Days Grace - Time of Dying (lyrics)" + mock_ytdl_extract_info(ytdl, url, title) + time_of_dying_audio = mock_ffmpeg_pcm_audio(ffmpeg_pcm_audio) + # pylint: disable=too-many-function-args + query = "three days grace time of dying" + await mbot.play(mbot, ctx, query=query) + await asyncio.sleep(0) + assert \ + not ctx.voice_client.play.call_args.args == (time_of_dying_audio,), \ + "Did immediately playback audio instead of being queued" + + await mbot.skip(mbot, ctx) + await asyncio.sleep(0) + assert \ + ctx.voice_client.play.call_args.args == (time_of_dying_audio,), \ + "Did not skip song" + + # TEST: Error if no song playing + ctx.voice_client.is_playing.return_value = False + with pytest.raises(commands.CommandError): + await mbot.skip(mbot, ctx) + + # TEST: Error if no voice client + ctx.voice_client = None + with pytest.raises(commands.CommandError): + await mbot.skip(mbot, ctx)