39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
|
|
"""Stub telegram before bot.py is imported so pytest doesn't need the real package."""
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import sys
|
||
|
|
import types
|
||
|
|
from unittest.mock import MagicMock
|
||
|
|
|
||
|
|
|
||
|
|
def _make_telegram_stub() -> types.ModuleType:
|
||
|
|
mod = types.ModuleType("telegram")
|
||
|
|
mod.Update = MagicMock
|
||
|
|
mod.InlineKeyboardButton = MagicMock
|
||
|
|
mod.InlineKeyboardMarkup = MagicMock
|
||
|
|
return mod
|
||
|
|
|
||
|
|
|
||
|
|
def _make_telegram_ext_stub() -> types.ModuleType:
|
||
|
|
mod = types.ModuleType("telegram.ext")
|
||
|
|
mod.ApplicationBuilder = MagicMock
|
||
|
|
|
||
|
|
# ContextTypes.DEFAULT_TYPE is referenced as a type annotation at class-body
|
||
|
|
# evaluation time, so it must be a real attribute, not a dynamic MagicMock attr.
|
||
|
|
ContextTypesMock = MagicMock()
|
||
|
|
ContextTypesMock.DEFAULT_TYPE = type(None)
|
||
|
|
mod.ContextTypes = ContextTypesMock
|
||
|
|
|
||
|
|
mod.CommandHandler = MagicMock
|
||
|
|
mod.CallbackQueryHandler = MagicMock
|
||
|
|
mod.MessageHandler = MagicMock
|
||
|
|
mod.filters = MagicMock()
|
||
|
|
return mod
|
||
|
|
|
||
|
|
|
||
|
|
# Insert before any import of bot.py
|
||
|
|
if "telegram" not in sys.modules:
|
||
|
|
sys.modules["telegram"] = _make_telegram_stub()
|
||
|
|
if "telegram.ext" not in sys.modules:
|
||
|
|
sys.modules["telegram.ext"] = _make_telegram_ext_stub()
|