diff options
| author | salmonik <salmonik@noreply.codeberg.org> | 2026-04-19 18:10:09 +0200 |
|---|---|---|
| committer | salmonik <salmonik@noreply.codeberg.org> | 2026-04-19 18:10:09 +0200 |
| commit | f8e1cf36e1efa0602bf2e450835f2a67654762a6 (patch) | |
| tree | fe6b12226d7eebe2a0dc6525de3414d0c3718cc8 /tests/cogs/test_downloader.py | |
| parent | add support for x^2+y^2=0,4 and alike (diff) | |
| parent | fix: proper aiohttp async context manager mocking in test (diff) | |
| download | senator-sperma-f8e1cf36e1efa0602bf2e450835f2a67654762a6.tar.gz senator-sperma-f8e1cf36e1efa0602bf2e450835f2a67654762a6.zip | |
Merge pull request 'massive changes' (#3) from dev into main
Reviewed-on: https://codeberg.org/salmonik/senator-sperma/pulls/3
stable enough
Diffstat (limited to 'tests/cogs/test_downloader.py')
| -rw-r--r-- | tests/cogs/test_downloader.py | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/tests/cogs/test_downloader.py b/tests/cogs/test_downloader.py new file mode 100644 index 0000000..6ac8ae7 --- /dev/null +++ b/tests/cogs/test_downloader.py | |||
| @@ -0,0 +1,93 @@ | |||
| 1 | import pytest | ||
| 2 | from unittest.mock import AsyncMock, patch, MagicMock | ||
| 3 | import discord | ||
| 4 | from tests.conftest import MockApplicationContext, MockMessage | ||
| 5 | |||
| 6 | |||
| 7 | @pytest.fixture | ||
| 8 | def downloader_cog(bot): | ||
| 9 | from cogs.downloader import Downloader | ||
| 10 | cog = Downloader(bot) | ||
| 11 | return cog | ||
| 12 | |||
| 13 | |||
| 14 | @pytest.mark.asyncio | ||
| 15 | async def test_download_command_success_file(downloader_cog, mock_channel, mock_guild, bot): | ||
| 16 | ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) | ||
| 17 | |||
| 18 | mock_file_path = "/tmp/test_video.mp4" | ||
| 19 | |||
| 20 | with patch('cogs.downloader.Downloader.download_standalone', return_value={ | ||
| 21 | "status": "success", | ||
| 22 | "type": "file", | ||
| 23 | "content": mock_file_path, | ||
| 24 | "filename": "test_video.mp4" | ||
| 25 | }): | ||
| 26 | with patch('discord.File', return_value=MagicMock()) as mock_file: | ||
| 27 | with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: | ||
| 28 | await downloader_cog.download.callback(downloader_cog, ctx, url="https://example.com/video", catbox=False, fformat="mp4", filename_user=None) | ||
| 29 | mock_respond.assert_called_once() | ||
| 30 | call_args = mock_respond.call_args | ||
| 31 | assert 'file' in str(call_args) or mock_file.called | ||
| 32 | |||
| 33 | |||
| 34 | @pytest.mark.asyncio | ||
| 35 | async def test_download_command_success_url(downloader_cog, mock_channel, mock_guild, bot): | ||
| 36 | ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) | ||
| 37 | |||
| 38 | with patch('cogs.downloader.Downloader.download_standalone', return_value={ | ||
| 39 | "status": "success", | ||
| 40 | "type": "url", | ||
| 41 | "content": "https://catbox.2137697.xyz/sperma/test.mp4" | ||
| 42 | }): | ||
| 43 | with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: | ||
| 44 | await downloader_cog.download.callback(downloader_cog, ctx, url="https://example.com/video", catbox=True, fformat="mp4", filename_user=None) | ||
| 45 | mock_respond.assert_called_once() | ||
| 46 | call_args = mock_respond.call_args | ||
| 47 | assert "pobioron" in str(call_args) | ||
| 48 | |||
| 49 | |||
| 50 | @pytest.mark.asyncio | ||
| 51 | async def test_download_command_error(downloader_cog, mock_channel, mock_guild, bot): | ||
| 52 | ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) | ||
| 53 | |||
| 54 | with patch('cogs.downloader.Downloader.download_standalone', return_value={ | ||
| 55 | "status": "error", | ||
| 56 | "type": "error", | ||
| 57 | "content": "Download failed", | ||
| 58 | "details": "Connection timeout" | ||
| 59 | }): | ||
| 60 | with patch('cogs.downloader.logger') as mock_logger: | ||
| 61 | await downloader_cog.download.callback(downloader_cog, ctx, url="https://badurl.com", catbox=False, fformat="mp4", filename_user=None) | ||
| 62 | assert mock_logger.error.called | ||
| 63 | |||
| 64 | |||
| 65 | @pytest.mark.asyncio | ||
| 66 | async def test_message_download_with_url(downloader_cog, mock_channel, mock_guild, bot, mock_user): | ||
| 67 | ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) | ||
| 68 | |||
| 69 | mock_message = MagicMock() | ||
| 70 | mock_message.content = "Check this out https://example.com/video" | ||
| 71 | |||
| 72 | with patch('cogs.downloader.Downloader.download_standalone', return_value={ | ||
| 73 | "status": "success", | ||
| 74 | "type": "file", | ||
| 75 | "content": "/tmp/test.mp4" | ||
| 76 | }): | ||
| 77 | with patch.object(downloader_cog, 'download', new_callable=AsyncMock) as mock_download: | ||
| 78 | await downloader_cog.message_download.callback(downloader_cog, ctx, message=mock_message) | ||
| 79 | mock_download.assert_called_once() | ||
| 80 | |||
| 81 | |||
| 82 | @pytest.mark.asyncio | ||
| 83 | async def test_message_download_no_url(downloader_cog, mock_channel, mock_guild, bot): | ||
| 84 | ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild) | ||
| 85 | |||
| 86 | mock_message = MagicMock() | ||
| 87 | mock_message.content = "No URL here" | ||
| 88 | |||
| 89 | with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond: | ||
| 90 | await downloader_cog.message_download.callback(downloader_cog, ctx, message=mock_message) | ||
| 91 | mock_respond.assert_called_once() | ||
| 92 | call_args = mock_respond.call_args | ||
| 93 | assert "Brak" in str(call_args) | ||
