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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2025
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""
This module is intentionally named without "test_" prefix.
These tests are supposed to be run on GitHub when building docs.
The tests require Python 3.10+ (just like AdmonitionInserter being tested),
so they cannot be included in the main suite while older versions of Python are supported.
"""
import collections.abc
import pytest
import telegram.ext
from docs.auxil.admonition_inserter import AdmonitionInserter
@pytest.fixture(scope="session")
def admonition_inserter():
return AdmonitionInserter()
class TestAdmonitionInserter:
"""This is a minimal-effort test to ensure that the `AdmonitionInserter`
used for automatically inserting references in the docs works as expected.
It does not aim to cover all links in the documentation, but rather checks that several special
cases (which where discovered during the implementation of `AdmonitionInserter`) are handled
correctly.
"""
def test_admonitions_dict(self, admonition_inserter):
# there are keys for every type of admonition
assert len(admonition_inserter.admonitions) == len(
admonition_inserter.ALL_ADMONITION_TYPES
)
# for each type of admonitions, there is at least one entry
# ({class/method: admonition text})
for admonition_type in admonition_inserter.ALL_ADMONITION_TYPES:
assert admonition_type in admonition_inserter.admonitions
assert len(admonition_inserter.admonitions[admonition_type].keys()) > 0
# checking class admonitions
for admonition_type in admonition_inserter.CLASS_ADMONITION_TYPES:
# keys are telegram classes
for cls in admonition_inserter.admonitions[admonition_type]:
# Test classes crop up in AppBuilder, they can't come from code being tested.
if "tests." in str(cls):
continue
assert isinstance(cls, type)
assert str(cls).startswith("<class 'telegram."), (
rf"Class {cls} does not belong to Telegram classes. Admonition:\n"
rf"{admonition_inserter.admonitions[admonition_type][cls]}"
)
# checking Bot method admonitions
for admonition_type in admonition_inserter.METHOD_ADMONITION_TYPES:
for method in admonition_inserter.admonitions[admonition_type]:
assert isinstance(method, collections.abc.Callable)
assert str(method).startswith("<function Bot."), (
f"Method {method} does not belong to methods that should get admonitions."
"Admonition:\n"
f"{admonition_inserter.admonitions[admonition_type][method]}"
)
@pytest.mark.parametrize(
("admonition_type", "cls", "link"),
[
(
"available_in",
telegram.ChatMember,
":attr:`telegram.ChatMemberUpdated.new_chat_member`",
),
(
"available_in",
telegram.ChatMemberAdministrator,
":attr:`telegram.ChatMemberUpdated.new_chat_member`",
),
(
"available_in",
telegram.Sticker,
":attr:`telegram.StickerSet.stickers`", # tuple[telegram.Sticker]
),
(
"available_in",
telegram.ResidentialAddress, # mentioned on the second line of docstring of .data
":attr:`telegram.EncryptedPassportElement.data`",
),
(
"available_in",
telegram.ext.JobQueue,
":attr:`telegram.ext.CallbackContext.job_queue`",
),
(
"available_in",
telegram.ext.Application,
":attr:`telegram.ext.CallbackContext.application`",
),
(
"available_in",
telegram.Bot,
":attr:`telegram.ext.CallbackContext.bot`",
),
(
"available_in",
telegram.Bot,
":attr:`telegram.ext.Application.bot`",
),
(
"returned_in",
telegram.StickerSet,
":meth:`telegram.Bot.get_sticker_set`",
),
(
"returned_in",
telegram.ChatMember,
":meth:`telegram.Bot.get_chat_member`",
),
(
"returned_in",
telegram.GameHighScore,
":meth:`telegram.Bot.get_game_high_scores`",
),
(
"returned_in",
telegram.ChatMemberOwner,
":meth:`telegram.Bot.get_chat_member`", # subclass
),
(
"returned_in",
telegram.Message,
":meth:`telegram.Bot.edit_message_live_location`", # Union[Message, bool]
),
(
"returned_in",
telegram.ext.Application,
":meth:`telegram.ext.ApplicationBuilder.build`", # <class 'types.GenericAlias'>
),
(
"shortcuts",
telegram.Bot.edit_message_caption,
# this method in CallbackQuery contains two return statements,
# one of which is with Bot
":meth:`telegram.CallbackQuery.edit_message_caption`",
),
(
"shortcuts",
telegram.Bot.ban_chat_member,
# ban_member is defined on the private parent class _ChatBase
":meth:`telegram.Chat.ban_member`",
),
(
"shortcuts",
telegram.Bot.ban_chat_member,
# ban_member is defined on the private parent class _ChatBase
":meth:`telegram.ChatFullInfo.ban_member`",
),
(
"use_in",
telegram.InlineQueryResult,
":meth:`telegram.Bot.answer_web_app_query`", # ForwardRef
),
(
"use_in",
telegram.InputMediaPhoto,
":meth:`telegram.Bot.send_media_group`", # Sequence[Union[...]]
),
(
"use_in",
telegram.InlineKeyboardMarkup,
":meth:`telegram.Bot.send_message`", # optional
),
(
"use_in",
telegram.Sticker,
":meth:`telegram.Bot.get_file`", # .file_id with lots of piped types
),
(
"use_in",
telegram.ext.BasePersistence,
":meth:`telegram.ext.ApplicationBuilder.persistence`",
),
("use_in", telegram.ext.Defaults, ":meth:`telegram.ext.ApplicationBuilder.defaults`"),
(
"use_in",
telegram.ext.JobQueue,
":meth:`telegram.ext.ApplicationBuilder.job_queue`", # TypeVar
),
(
"use_in",
telegram.ext.PicklePersistence, # subclass
":meth:`telegram.ext.ApplicationBuilder.persistence`",
),
],
)
def test_check_presence(self, admonition_inserter, admonition_type, cls, link):
"""Checks if a given link is present in the admonition of a given type for a given
class.
"""
admonitions = admonition_inserter.admonitions
assert cls in admonitions[admonition_type]
# exactly one of the lines in the admonition for this class must consist of the link
# (this is a stricter check than just checking if the entire admonition contains the link)
lines_with_link = [
line
for line in admonitions[admonition_type][cls].splitlines()
# remove whitespaces and occasional bullet list marker
if line.strip().removeprefix("* ") == link
]
assert lines_with_link, (
f"Class {cls}, does not have link {link} in a {admonition_type} admonition:\n"
f"{admonitions[admonition_type][cls]}"
)
assert len(lines_with_link) == 1, (
f"Class {cls}, must contain only one link {link} in a {admonition_type} admonition:\n"
f"{admonitions[admonition_type][cls]}"
)
@pytest.mark.parametrize(
("admonition_type", "cls", "link"),
[
(
"returned_in",
telegram.ext.CallbackContext,
# -> Application[BT, CCT, UD, CD, BD, JQ].
# The type vars are not really part of the return value, so we don't expect them
":meth:`telegram.ext.ApplicationBuilder.build`",
),
(
"returned_in",
telegram.Bot,
# -> Application[BT, CCT, UD, CD, BD, JQ].
# The type vars are not really part of the return value, so we don't expect them
":meth:`telegram.ext.ApplicationBuilder.bot`",
),
],
)
def test_check_absence(self, admonition_inserter, admonition_type, cls, link):
"""Checks if a given link is **absent** in the admonition of a given type for a given
class.
If a given class has no admonition of this type at all, the test will also pass.
"""
admonitions = admonition_inserter.admonitions
assert not (
cls in admonitions[admonition_type]
and [
line
for line in admonitions[admonition_type][cls].splitlines()
# remove whitespaces and occasional bullet list marker
if line.strip().removeprefix("* ") == link
]
), (
f"Class {cls} is not supposed to have link {link} in a {admonition_type} admonition:\n"
f"{admonitions[admonition_type][cls]}"
)
|