aboutsummaryrefslogtreecommitdiffstats
path: root/tests/cogs/test_ai.py
diff options
context:
space:
mode:
authorsalmonik <raisersoft@proton.me>2026-04-19 15:44:33 +0200
committersalmonik <raisersoft@proton.me>2026-04-19 15:44:33 +0200
commitb343381fea3eab26e334a1b3bba202d715f9ea2e (patch)
tree792210018d1e11ac3b160edfe29cf878eb4e3f56 /tests/cogs/test_ai.py
parentchore: add pycord dependency, update main (diff)
downloadsenator-sperma-b343381fea3eab26e334a1b3bba202d715f9ea2e.tar.gz
senator-sperma-b343381fea3eab26e334a1b3bba202d715f9ea2e.zip
test: improve test assertions and edge cases
Diffstat (limited to 'tests/cogs/test_ai.py')
-rw-r--r--tests/cogs/test_ai.py40
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 @@
1import pytest 1import pytest
2from unittest.mock import AsyncMock, patch, MagicMock 2from unittest.mock import AsyncMock, patch, MagicMock
3import discord 3import discord
4import httpx
4from tests.conftest import MockApplicationContext 5from tests.conftest import MockApplicationContext
5 6
6 7
@@ -27,12 +28,18 @@ def mock_ai_response():
27@pytest.mark.asyncio 28@pytest.mark.asyncio
28async def test_senator_command_success(ai_cog, mock_channel, mock_guild, bot, mock_ai_response): 29async 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
42async def test_senator_command_empty_choices(ai_cog, mock_channel, mock_guild, bot): 49async 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
56async def test_senator_command_no_choices_key(ai_cog, mock_channel, mock_guild, bot): 69async 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