musicube/test/test_bot.py

55 lines
1.7 KiB
Python

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)
# 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
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
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_stream(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.stream(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