/
rnj
/
SuaiTestBot
Обзор
Документация
Войти
/
rnj
/
SuaiTestBot
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
tests/test_bot.py
70 строк
3 KB
Dmitriy Pozdeev
init commit
03 июн 2026, 23:58
03 июн 2026, 23:58
110bad2
Код
Авторство
О чём код?
import pytest from contextlib import asynccontextmanager from unittest.mock import AsyncMock, patch from telebot.types import Chat, Message, User as TgUser from database import crud def _message(text: str, user_id: int = 456, chat_id: int = 123) -> Message: msg = Message( message_id=1, date=123, chat=Chat(id=chat_id, type="private"), from_user=TgUser(id=user_id, first_name="Test", is_bot=False), content_type="text", options={}, json_string="{}", ) msg.text = text return msg @asynccontextmanager async def _fake_bot_data(*args, **kwargs): yield {} @pytest.mark.asyncio async def test_start_registers_user(): message = _message("/start", user_id=99901) with patch("bot.handlers.start.crud.get_user_by_telegram_id", return_value=None): with patch("bot.handlers.start.crud.add_user") as mock_add: with patch("bot.handlers.start.bot.send_message", new_callable=AsyncMock) as mock_send: with patch("bot.handlers.start.groups_inline_keyboard"): from bot.handlers.start import cmd_start mock_add.return_value = type("U", (), {"group_id": None, "is_admin": False})() await cmd_start(message) mock_add.assert_called_once() mock_send.assert_called() @pytest.mark.asyncio async def test_change_group_shows_keyboard(): message = _message("/change_group", user_id=99903) with patch("bot.handlers.start.crud.user_exists", return_value=True): with patch("bot.handlers.start.crud.get_user_by_telegram_id") as mock_user: with patch("bot.handlers.start.groups_inline_keyboard") as mock_kb: with patch("bot.handlers.start.bot.send_message", new_callable=AsyncMock) as mock_send: mock_user.return_value = type("U", (), {"group_id": 1})() mock_kb.return_value = None from bot.handlers.start import cmd_change_group await cmd_change_group(message) mock_send.assert_called_once() assert "группу" in mock_send.call_args[0][1].lower() @pytest.mark.asyncio async def test_subscribe_requires_group(): message = _message("/subscribe", user_id=99902) user = type("U", (), {"group_id": None})() with patch("bot.handlers.subscribe.crud.user_exists", return_value=True): with patch("bot.handlers.subscribe.crud.get_user_by_telegram_id", return_value=user): with patch("bot.handlers.subscribe.bot.send_message", new_callable=AsyncMock) as mock_send: from bot.handlers.subscribe import cmd_subscribe await cmd_subscribe(message) assert "группу" in mock_send.call_args[0][1].lower()