1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
import pytest
from unittest.mock import AsyncMock, patch, MagicMock
from cogs.web import Web
from 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 = '''
<html>
<a class="result__a" href="https://example.com">Test Result</a>
<a class="result__a" href="https://example.com/2">Test Result 2</a>
</html>
'''
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 = '<html><body></body></html>'
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)
|