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()