aboutsummaryrefslogtreecommitdiffstats
path: root/tests/cogs/test_plot.py
blob: e38a2c766be95209f606ef6aab1741559e5b6f3b (plain) (blame)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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)