Update tests

This commit is contained in:
ekzyis 2021-09-25 03:00:20 +02:00
parent 8ee2fc2682
commit d96108119e
1 changed files with 27 additions and 26 deletions

View File

@ -1,3 +1,4 @@
import asyncio
from unittest.mock import Mock, AsyncMock, patch from unittest.mock import Mock, AsyncMock, patch
import pytest import pytest
@ -10,28 +11,14 @@ from bot import Music
async def test_bot_ensure_voice(bot, ctx): async def test_bot_ensure_voice(bot, ctx):
mbot = Music(bot) mbot = Music(bot)
# 1. Inside a voice channel # Connects to voice channel of author if possible
# 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.voice_client = None
ctx.author.voice = AsyncMock() ctx.author.voice = AsyncMock()
await mbot.ensure_voice(ctx) await mbot.ensure_voice(ctx)
assert ctx.author.voice.channel.connect.call_count == 1 assert ctx.author.voice.channel.connect.call_count == 1
ctx.reset_mock(return_value=True) 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.voice_client = None
ctx.author.voice = None ctx.author.voice = None
with pytest.raises(commands.CommandError): with pytest.raises(commands.CommandError):
@ -42,13 +29,27 @@ async def test_bot_ensure_voice(bot, ctx):
async def test_bot_play(bot, ctx): async def test_bot_play(bot, ctx):
mbot = Music(bot) mbot = Music(bot)
with patch('bot.YTDLSource', new_callable=AsyncMock) as ytdl_source: with patch.object(mbot, '_ytdl') as ytdl:
player = Mock() with patch('discord.FFmpegPCMAudio') as ffmpeg_pcm_audio:
ytdl_source.from_url.return_value = player ctx.voice_client.is_playing.return_value = False
url = 'A Day To Remember - All I Want' url = "https://www.youtube.com/watch?v=Wr9LZ1hAFpQ"
# pylint: disable=too-many-function-args title = "In Flames - Deliver Us (Official Video)"
await mbot.play(mbot, ctx, url=url) ytdl.extract_info.return_value = {
assert ytdl_source.from_url.await_args.args == (url,) "entries": [
assert ytdl_source.from_url.await_args.kwargs == {"loop": bot.loop, "stream": True} {
assert ctx.voice_client.play.call_args.args == (player,) "url": url,
assert ctx.send.call_count == 1 "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