musicube/test/test_bot.py

50 lines
1.6 KiB
Python
Raw Normal View History

2021-09-25 01:00:20 +00:00
import asyncio
2021-09-24 21:37:46 +00:00
from unittest.mock import Mock, AsyncMock, patch
2021-09-24 21:07:32 +00:00
import pytest
from discord.ext import commands
@pytest.mark.asyncio
2021-09-25 01:49:46 +00:00
async def test_bot_ensure_voice(mbot, ctx):
2021-09-25 01:00:20 +00:00
# Connects to voice channel of author if possible
2021-09-24 21:07:32 +00:00
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)
2021-09-25 01:00:20 +00:00
# Error if author not inside a channel
2021-09-24 21:07:32 +00:00
ctx.voice_client = None
ctx.author.voice = None
with pytest.raises(commands.CommandError):
await mbot.ensure_voice(ctx)
2021-09-24 21:37:46 +00:00
@pytest.mark.asyncio
2021-09-25 01:49:46 +00:00
async def test_bot_play(mbot, ctx):
2021-09-25 01:00:20 +00:00
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