diff options
Diffstat (limited to '')
| -rw-r--r-- | tests/cogs/test_ai.py | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/tests/cogs/test_ai.py b/tests/cogs/test_ai.py index bd8f813..a1c3fd4 100644 --- a/tests/cogs/test_ai.py +++ b/tests/cogs/test_ai.py | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | import pytest | 1 | import pytest |
| 2 | from unittest.mock import AsyncMock, patch, MagicMock | 2 | from unittest.mock import AsyncMock, patch, MagicMock |
| 3 | import discord | 3 | import discord |
| 4 | import httpx | ||
| 4 | from tests.conftest import MockApplicationContext | 5 | from tests.conftest import MockApplicationContext |
| 5 | 6 | ||
| 6 | 7 | ||
| @@ -27,12 +28,18 @@ def mock_ai_response(): | |||
| 27 | @pytest.mark.asyncio | 28 | @pytest.mark.asyncio |
| 28 | async def test_senator_command_success(ai_cog, mock_channel, mock_guild, bot, mock_ai_response): | 29 | async def test_senator_command_success(ai_cog, mock_channel, mock_guild, bot, mock_ai_response): |
| 29 | ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) | 30 | ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) |
| 30 | 31 | ||
| 31 | mock_response = MagicMock() | 32 | mock_response = MagicMock() |
| 32 | mock_response.json = MagicMock(return_value=mock_ai_response) | 33 | mock_response.json = MagicMock(return_value=mock_ai_response) |
| 33 | mock_response.status_code = 200 | 34 | |
| 34 | 35 | mock_client = MagicMock() | |
| 35 | with patch('cogs.ai.requests.post', return_value=mock_response): | 36 | mock_client.post = AsyncMock(return_value=mock_response) |
| 37 | mock_client.__aenter__ = AsyncMock(return_value=mock_response) | ||
| 38 | mock_client.__aexit__ = AsyncMock(return_value=None) | ||
| 39 | |||
| 40 | with patch('cogs.ai.httpx.AsyncClient') as mock_httpx: | ||
| 41 | instance = mock_httpx.return_value.__aenter__.return_value | ||
| 42 | instance.post = AsyncMock(return_value=mock_response) | ||
| 36 | with patch.object(ctx, 'send_followup', new_callable=AsyncMock) as mock_followup: | 43 | with patch.object(ctx, 'send_followup', new_callable=AsyncMock) as mock_followup: |
| 37 | await ai_cog.senator.callback(ai_cog, ctx, text="Hello", tts=False) | 44 | await ai_cog.senator.callback(ai_cog, ctx, text="Hello", tts=False) |
| 38 | mock_followup.assert_called_once_with("Test response from AI") | 45 | mock_followup.assert_called_once_with("Test response from AI") |
| @@ -41,12 +48,18 @@ async def test_senator_command_success(ai_cog, mock_channel, mock_guild, bot, mo | |||
| 41 | @pytest.mark.asyncio | 48 | @pytest.mark.asyncio |
| 42 | async def test_senator_command_empty_choices(ai_cog, mock_channel, mock_guild, bot): | 49 | async def test_senator_command_empty_choices(ai_cog, mock_channel, mock_guild, bot): |
| 43 | ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) | 50 | ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) |
| 44 | 51 | ||
| 45 | mock_response = MagicMock() | 52 | mock_response = MagicMock() |
| 46 | mock_response.json = MagicMock(return_value={"choices": []}) | 53 | mock_response.json = MagicMock(return_value={"choices": []}) |
| 47 | mock_response.status_code = 200 | 54 | |
| 48 | 55 | mock_client = MagicMock() | |
| 49 | with patch('cogs.ai.requests.post', return_value=mock_response): | 56 | mock_client.post = AsyncMock(return_value=mock_response) |
| 57 | mock_client.__aenter__ = AsyncMock(return_value=mock_response) | ||
| 58 | mock_client.__aexit__ = AsyncMock(return_value=None) | ||
| 59 | |||
| 60 | with patch('cogs.ai.httpx.AsyncClient') as mock_httpx: | ||
| 61 | instance = mock_httpx.return_value.__aenter__.return_value | ||
| 62 | instance.post = AsyncMock(return_value=mock_response) | ||
| 50 | with patch.object(ctx, 'send_followup', new_callable=AsyncMock) as mock_followup: | 63 | with patch.object(ctx, 'send_followup', new_callable=AsyncMock) as mock_followup: |
| 51 | await ai_cog.senator.callback(ai_cog, ctx, text="Hello", tts=False) | 64 | await ai_cog.senator.callback(ai_cog, ctx, text="Hello", tts=False) |
| 52 | mock_followup.assert_called_once_with("bot siem zesroł") | 65 | mock_followup.assert_called_once_with("bot siem zesroł") |
| @@ -55,15 +68,16 @@ async def test_senator_command_empty_choices(ai_cog, mock_channel, mock_guild, b | |||
| 55 | @pytest.mark.asyncio | 68 | @pytest.mark.asyncio |
| 56 | async def test_senator_command_no_choices_key(ai_cog, mock_channel, mock_guild, bot): | 69 | async def test_senator_command_no_choices_key(ai_cog, mock_channel, mock_guild, bot): |
| 57 | ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) | 70 | ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) |
| 58 | 71 | ||
| 59 | mock_response = MagicMock() | 72 | mock_response = MagicMock() |
| 60 | mock_response.json = MagicMock(return_value={}) | 73 | mock_response.json = MagicMock(return_value={}) |
| 61 | mock_response.status_code = 200 | 74 | |
| 62 | 75 | with patch('cogs.ai.httpx.AsyncClient') as mock_httpx: | |
| 63 | with patch('cogs.ai.requests.post', return_value=mock_response): | 76 | instance = mock_httpx.return_value.__aenter__.return_value |
| 77 | instance.post = AsyncMock(return_value=mock_response) | ||
| 64 | with patch('cogs.ai.logger') as mock_logger: | 78 | with patch('cogs.ai.logger') as mock_logger: |
| 65 | try: | 79 | try: |
| 66 | await ai_cog.senator.callback(ai_cog, ctx, text="Hello", tts=False) | 80 | await ai_cog.senator.callback(ai_cog, ctx, text="Hello", tts=False) |
| 67 | except KeyError: | 81 | except KeyError: |
| 68 | pass | 82 | pass |
| 69 | assert mock_logger.info.called \ No newline at end of file | 83 | assert mock_logger.info.called |
