import pytest from unittest.mock import AsyncMock, patch, MagicMock import os from tests.conftest import MockApplicationContext def test_parse_expression(): from cogs.plot import parse_expression assert parse_expression("x^2") == "x**2" assert parse_expression("x!") == "factorial(x)" assert parse_expression("2x") == "2*x" assert parse_expression("x2") == "x*2" assert parse_expression("sin(x)") == "sin(x)" def test_is_implicit(): from cogs.plot import is_implicit assert is_implicit("x^2 + y^2 = 1") == True assert is_implicit("x^2 = y") == True assert is_implicit("x^2 + 1") == False def test_parse_implicit(): from cogs.plot import parse_implicit lhs, rhs = parse_implicit("x^2 + y = 2") assert "x**2" in lhs assert "+ y" in lhs or "+y" in lhs assert rhs == "2" @pytest.fixture def plot_cog(bot): from cogs.plot import Plot cog = Plot(bot) return cog @pytest.mark.asyncio async def test_plot_command_not_available(plot_cog, mock_channel, mock_guild, bot): ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) with patch('cogs.plot._ensure_plot', return_value=False): with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: await plot_cog.plot.callback(plot_cog, ctx, funkcja="x^2") mock_respond.assert_called_once() args = mock_respond.call_args assert "matplotlib" in str(args).lower() or "nie jest dostępne" in str(args) @pytest.mark.asyncio async def test_plot_command_success(plot_cog, mock_channel, mock_guild, bot): ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) with patch('cogs.plot._ensure_plot', return_value=True): with patch('cogs.plot._plt_module') as mock_plt: with patch('cogs.utils.temp.get_temp_dir', return_value="/tmp"): with patch('os.path.exists', return_value=True): with patch('os.unlink'): with patch.object(ctx, 'followup', send=AsyncMock()) as mock_followup: mock_fig = MagicMock() mock_plt.subplots.return_value = (mock_fig, MagicMock()) await plot_cog.plot.callback(plot_cog, ctx, funkcja="x^2") mock_followup.send.assert_called_once() @pytest.mark.asyncio async def test_plot_command_error_handling(plot_cog, mock_channel, mock_guild, bot): ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) with patch('cogs.plot._ensure_plot', return_value=True): with patch('cogs.plot._plt_module') as mock_plt: with patch('cogs.utils.temp.get_temp_dir', return_value="/tmp"): with patch('os.path.exists', return_value=True): with patch('os.unlink'): with patch.object(ctx, 'followup', send=AsyncMock()) as mock_followup: mock_plt.subplots.side_effect = ValueError("Test error") await plot_cog.plot.callback(plot_cog, ctx, funkcja="invalid!!") mock_followup.send.assert_called_once() args = mock_followup.send.call_args assert "Nie można" in str(args) or "Błąd" in str(args)