musicube/test/test_bot.py

56 lines
1.6 KiB
Python

import asyncio
from unittest.mock import Mock, AsyncMock, patch
import pytest
from discord.ext import commands
from bot import Music
@pytest.mark.asyncio
async def test_bot_ensure_voice(bot, ctx):
mbot = Music(bot)
# 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)
# Error if author not inside a channel
ctx.voice_client = None
ctx.author.voice = None
with pytest.raises(commands.CommandError):
await mbot.ensure_voice(ctx)
@pytest.mark.asyncio
async def test_bot_play(bot, ctx):
mbot = Music(bot)
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