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