diff options
Diffstat (limited to '')
| -rw-r--r-- | tests/cogs/test_web.py | 57 |
1 files changed, 57 insertions, 0 deletions
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 @@ | |||
| 1 | import pytest | ||
| 2 | from unittest.mock import AsyncMock, patch, MagicMock | ||
| 3 | from cogs.web import Web | ||
| 4 | from tests.conftest import MockApplicationContext | ||
| 5 | |||
| 6 | |||
| 7 | @pytest.fixture | ||
| 8 | def web_cog(bot): | ||
| 9 | cog = Web(bot) | ||
| 10 | return cog | ||
| 11 | |||
| 12 | |||
| 13 | @pytest.mark.asyncio | ||
| 14 | async def test_web_command_success(web_cog, mock_channel, mock_guild, bot): | ||
| 15 | ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) | ||
| 16 | |||
| 17 | mock_response = MagicMock() | ||
| 18 | mock_response.status_code = 200 | ||
| 19 | mock_response.text = ''' | ||
| 20 | <html> | ||
| 21 | <a class="result__a" href="https://example.com">Test Result</a> | ||
| 22 | <a class="result__a" href="https://example.com/2">Test Result 2</a> | ||
| 23 | </html> | ||
| 24 | ''' | ||
| 25 | |||
| 26 | with patch('cogs.web.requests.get', return_value=mock_response) as mock_get: | ||
| 27 | with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: | ||
| 28 | await web_cog.web.callback(web_cog, ctx, query="test query") | ||
| 29 | mock_get.assert_called_once() | ||
| 30 | mock_respond.assert_called_once() | ||
| 31 | |||
| 32 | |||
| 33 | @pytest.mark.asyncio | ||
| 34 | async def test_web_command_no_results(web_cog, mock_channel, mock_guild, bot): | ||
| 35 | ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) | ||
| 36 | |||
| 37 | mock_response = MagicMock() | ||
| 38 | mock_response.status_code = 200 | ||
| 39 | mock_response.text = '<html><body></body></html>' | ||
| 40 | |||
| 41 | with patch('cogs.web.requests.get', return_value=mock_response): | ||
| 42 | with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: | ||
| 43 | await web_cog.web.callback(web_cog, ctx, query="test") | ||
| 44 | mock_respond.assert_called_once_with(content="No results found.", ephemeral=True) | ||
| 45 | |||
| 46 | |||
| 47 | @pytest.mark.asyncio | ||
| 48 | async def test_web_command_http_error(web_cog, mock_channel, mock_guild, bot): | ||
| 49 | ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) | ||
| 50 | |||
| 51 | mock_response = MagicMock() | ||
| 52 | mock_response.status_code = 500 | ||
| 53 | |||
| 54 | with patch('cogs.web.requests.get', return_value=mock_response): | ||
| 55 | with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: | ||
| 56 | await web_cog.web.callback(web_cog, ctx, query="test") | ||
| 57 | mock_respond.assert_called_once_with(content="Error: 500", ephemeral=True) \ No newline at end of file | ||
