From 857a7364ddb9b10529593c5e9c3578eb95c1da9b Mon Sep 17 00:00:00 2001 From: salmonik Date: Sat, 18 Apr 2026 16:24:42 +0200 Subject: add a test suite Signed-off-by: salmonik --- tests/cogs/test_web.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 tests/cogs/test_web.py (limited to 'tests/cogs/test_web.py') diff --git a/tests/cogs/test_web.py b/tests/cogs/test_web.py new file mode 100644 index 0000000..53a44e5 --- /dev/null +++ b/tests/cogs/test_web.py @@ -0,0 +1,57 @@ +import pytest +from unittest.mock import AsyncMock, patch, MagicMock +from cogs.web import Web +from tests.conftest import MockApplicationContext + + +@pytest.fixture +def web_cog(bot): + cog = Web(bot) + return cog + + +@pytest.mark.asyncio +async def test_web_command_success(web_cog, mock_channel, mock_guild, bot): + ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) + + mock_response = MagicMock() + mock_response.status_code = 200 + mock_response.text = ''' + + Test Result + Test Result 2 + + ''' + + with patch('cogs.web.requests.get', return_value=mock_response) as mock_get: + with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: + await web_cog.web.callback(web_cog, ctx, query="test query") + mock_get.assert_called_once() + mock_respond.assert_called_once() + + +@pytest.mark.asyncio +async def test_web_command_no_results(web_cog, mock_channel, mock_guild, bot): + ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) + + mock_response = MagicMock() + mock_response.status_code = 200 + mock_response.text = '' + + with patch('cogs.web.requests.get', return_value=mock_response): + with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: + await web_cog.web.callback(web_cog, ctx, query="test") + mock_respond.assert_called_once_with(content="No results found.", ephemeral=True) + + +@pytest.mark.asyncio +async def test_web_command_http_error(web_cog, mock_channel, mock_guild, bot): + ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) + + mock_response = MagicMock() + mock_response.status_code = 500 + + with patch('cogs.web.requests.get', return_value=mock_response): + with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: + await web_cog.web.callback(web_cog, ctx, query="test") + mock_respond.assert_called_once_with(content="Error: 500", ephemeral=True) \ No newline at end of file -- cgit v1.2.3