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_qr.py | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 tests/cogs/test_qr.py (limited to 'tests/cogs/test_qr.py') diff --git a/tests/cogs/test_qr.py b/tests/cogs/test_qr.py new file mode 100644 index 0000000..f3688e3 --- /dev/null +++ b/tests/cogs/test_qr.py @@ -0,0 +1,75 @@ +import pytest +from unittest.mock import AsyncMock, patch, MagicMock, mock_open +import os +from cogs.qr import QR +from tests.conftest import MockApplicationContext + + +@pytest.fixture +def qr_cog(bot): + cog = QR(bot) + return cog + + +@pytest.fixture +def mock_temp_file(): + with patch('tempfile.NamedTemporaryFile') as mock: + mock_file = MagicMock() + mock_file.name = "/tmp/test_qr.png" + mock.return_value = mock_file + yield mock_file + + +@pytest.mark.asyncio +async def test_qr_generate_success(qr_cog, mock_channel, mock_guild, bot, mock_temp_file): + ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) + + with patch('cogs.qr.QR._generate_qr') as mock_generate: + with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: + with patch('os.path.exists', return_value=True): + with patch('os.unlink'): + await qr_cog.qr.callback(qr_cog, ctx, text="https://example.com") + mock_generate.assert_called_once() + mock_respond.assert_called_once() + + +@pytest.mark.asyncio +async def test_qr_generate_empty_text(qr_cog, mock_channel, mock_guild, bot): + ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) + + with patch('cogs.qr.QR._generate_qr'): + with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: + with patch('os.path.exists', return_value=False): + await qr_cog.qr.callback(qr_cog, ctx, text="") + mock_respond.assert_called_once() + + +@pytest.mark.asyncio +async def test_qr_decode_not_available(qr_cog, mock_channel, mock_guild, bot): + ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) + + mock_attachment = MagicMock() + mock_attachment.url = "https://example.com/qr.png" + + with patch('cogs.qr._ensure_qr', return_value=False): + with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: + await qr_cog.decode.callback(qr_cog, ctx, image=mock_attachment) + mock_respond.assert_called_once() + args = mock_respond.call_args + assert "nie jest dostępne" in str(args) or "not available" in str(args).lower() + + +@pytest.mark.asyncio +async def test_qr_decode_download_failure(qr_cog, mock_channel, mock_guild, bot): + ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) + + mock_attachment = MagicMock() + mock_attachment.url = "https://example.com/qr.png" + + with patch('cogs.qr._ensure_qr', return_value=True): + with patch.object(qr_cog, '_download_image', return_value=None): + with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: + await qr_cog.decode.callback(qr_cog, ctx, image=mock_attachment) + mock_respond.assert_called_once() + args = mock_respond.call_args + assert "Nie udało się" in str(args) or "failed" in str(args).lower() \ No newline at end of file -- cgit v1.2.3