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_math_commands.py | 55 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/cogs/test_math_commands.py (limited to 'tests/cogs/test_math_commands.py') diff --git a/tests/cogs/test_math_commands.py b/tests/cogs/test_math_commands.py new file mode 100644 index 0000000..0acac4a --- /dev/null +++ b/tests/cogs/test_math_commands.py @@ -0,0 +1,55 @@ +import pytest +from unittest.mock import AsyncMock, patch, MagicMock +from tests.conftest import MockApplicationContext + + +@pytest.fixture +def math_cog(bot): + from cogs.math_commands import Math + cog = Math(bot) + return cog + + +@pytest.mark.asyncio +async def test_math_command_valid_expression(math_cog, mock_channel, mock_guild, bot): + ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) + + mock_process = MagicMock() + mock_process.returncode = 0 + mock_process.communicate = AsyncMock(return_value=(b"4", b"")) + + with patch('cogs.math_commands.asyncio.create_subprocess_exec', return_value=mock_process) as mock_subprocess: + with patch('cogs.math_commands.re.search', return_value=None): + with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: + await math_cog.math.callback(math_cog, ctx, equation="2+2") + mock_subprocess.assert_called_once() + mock_respond.assert_called_once() + + +@pytest.mark.asyncio +async def test_math_command_invalid_expression(math_cog, mock_channel, mock_guild, bot): + ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) + + with patch('cogs.math_commands.re.search', return_value=MagicMock()): + with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: + await math_cog.math.callback(math_cog, ctx, equation="eval(1)") + mock_respond.assert_called_once() + args = mock_respond.call_args + assert "Invalid" in str(args) + + +@pytest.mark.asyncio +async def test_math_command_process_error(math_cog, mock_channel, mock_guild, bot): + ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) + + mock_process = MagicMock() + mock_process.returncode = 1 + mock_process.communicate = AsyncMock(return_value=(b"", b"Error: invalid expression")) + + with patch('cogs.math_commands.asyncio.create_subprocess_exec', return_value=mock_process): + with patch('cogs.math_commands.re.search', return_value=None): + with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: + await math_cog.math.callback(math_cog, ctx, equation="2++") + mock_respond.assert_called_once() + args = mock_respond.call_args + assert "error" in str(args).lower() or "Error" in str(args) \ No newline at end of file -- cgit v1.2.3