From ecdbc2fa4582bc26bcfb9da4db1f1d2a7c4fc3f2 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Fri, 24 Sep 2021 23:37:46 +0200 Subject: [PATCH] Add test for stream --- test/conftest.py | 5 ++++- test/test_bot.py | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/test/conftest.py b/test/conftest.py index 0a6029a..1fa415c 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -1,5 +1,6 @@ import sys from pathlib import Path +from unittest.mock import Mock import pytest from pytest_mock import MockerFixture @@ -11,7 +12,9 @@ sys.path.insert(0, SRC_PATH) @pytest.fixture def bot(mocker: MockerFixture): - yield mocker.patch('discord.ext.commands.Bot', autospec=True) + bot_mock = mocker.patch('discord.ext.commands.Bot', autospec=True) + bot_mock.loop = Mock() + yield bot_mock @pytest.fixture diff --git a/test/test_bot.py b/test/test_bot.py index 8c0ca09..1bbe70d 100644 --- a/test/test_bot.py +++ b/test/test_bot.py @@ -1,4 +1,4 @@ -from unittest.mock import AsyncMock +from unittest.mock import Mock, AsyncMock, patch import pytest from discord.ext import commands @@ -36,3 +36,19 @@ async def test_bot_ensure_voice(bot, ctx): 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