discordBot/tests/test_commands_morning.py
deadzilla 78106e7b4c fix: добавить обработку ошибок в commands/morning.py
Если run_morning выбрасывает исключение, ошибка теперь
перехватывается: логируется с exc_info, пользователю
отправлено сообщение об ошибке. Тест обновлён — проверяется
что исключение не пробрасывается наружу.
2026-07-22 22:36:55 +05:00

88 lines
2.7 KiB
Python

"""Тесты для commands/morning.py — команда !morning."""
import sys
from pathlib import Path
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
ROOT_DIR = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT_DIR))
def make_context() -> MagicMock:
"""Создать мокированный ctx."""
ctx = MagicMock()
ctx.author.name = "TestUser"
ctx.send = AsyncMock(return_value=None)
ctx.bot = MagicMock()
ctx.channel = MagicMock()
ctx.channel.name = "test-channel"
return ctx
class TestMorningInit:
"""Тесты инициализации Morning cog."""
def test_morning_cog_instantiates(self) -> None:
"""Morning должен создаваться без параметров."""
from commands.morning import Morning
cog = Morning()
assert cog is not None
def test_morning_has_command(self) -> None:
"""Morning должен содержать команду morning."""
from commands.morning import Morning
cog = Morning()
assert cog.morning.name == "morning"
class TestMorningCommand:
"""Тесты команды !morning."""
@pytest.mark.asyncio
async def test_morning_calls_run_morning(self) -> None:
"""!morning должен вызвать run_morning."""
from commands.morning import Morning
cog = Morning()
ctx = make_context()
with patch("commands.morning.run_morning", new_callable=AsyncMock) as mock_run:
await cog.morning(cog, ctx)
mock_run.assert_awaited_once_with(ctx.bot, ctx.channel)
@pytest.mark.asyncio
async def test_morning_passes_bot_and_channel(self) -> None:
"""!morning передаёт ctx.bot и ctx.channel в run_morning."""
from commands.morning import Morning
cog = Morning()
ctx = make_context()
with patch("commands.morning.run_morning", new_callable=AsyncMock) as mock_run:
await cog.morning(cog, ctx)
call_args = mock_run.call_args
assert call_args[0][0] is ctx.bot
assert call_args[0][1] is ctx.channel
@pytest.mark.asyncio
async def test_morning_run_morning_raises(self) -> None:
"""Ошибка в run_morning ловится, пользователю отправлено сообщение."""
from commands.morning import Morning
cog = Morning()
ctx = make_context()
with patch("commands.morning.run_morning", new_callable=AsyncMock) as mock_run:
mock_run.side_effect = Exception("api error")
await cog.morning(cog, ctx)
ctx.send.assert_awaited_once()
message = ctx.send.call_args[0][0]
assert "Ошибка" in message