forked from python-telegram-bot/python-telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_official.py
More file actions
219 lines (188 loc) · 7.84 KB
/
test_official.py
File metadata and controls
219 lines (188 loc) · 7.84 KB
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
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2021
# Leandro Toledo de Souza <[email protected]>
#
# 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/].
import os
import inspect
from typing import List
import certifi
import pytest
from bs4 import BeautifulSoup
from telegram.vendor.ptb_urllib3 import urllib3
import telegram
from tests.conftest import env_var_2_bool
IGNORED_OBJECTS = ("ResponseParameters", "CallbackGame")
IGNORED_PARAMETERS = {
"self",
"args",
"_kwargs",
"read_latency",
"network_delay",
"timeout",
"bot",
"api_kwargs",
"kwargs",
}
ignored_param_requirements = { # Ignore these since there's convenience params in them (eg. Venue)
"send_location": {"latitude", "longitude"},
"edit_message_live_location": {"latitude", "longitude"},
"send_venue": {"latitude", "longitude", "title", "address"},
"send_contact": {"phone_number", "first_name"},
}
def find_next_sibling_until(tag, name, until):
for sibling in tag.next_siblings:
if sibling is until:
return
if sibling.name == name:
return sibling
def parse_table(h4) -> List[List[str]]:
"""Parses the Telegram doc table and has an output of a 2D list."""
table = find_next_sibling_until(h4, "table", h4.find_next_sibling("h4"))
if not table:
return []
t = []
for tr in table.find_all("tr")[1:]:
t.append([td.text for td in tr.find_all("td")])
return t
def check_method(h4):
name = h4.text # name of the method in telegram's docs.
method = getattr(telegram.Bot, name) # Retrieve our lib method
table = parse_table(h4)
# Check arguments based on source
sig = inspect.signature(method, follow_wrapped=True)
checked = []
for parameter in table:
param = sig.parameters.get(parameter[0])
assert param is not None, f"Parameter {parameter[0]} not found in {method.__name__}"
# TODO: Check type via docstring
assert check_required_param(
parameter, param.name, sig, method.__name__
), f"Param {param.name!r} of method {method.__name__!r} requirement mismatch!"
checked.append(parameter[0])
ignored = IGNORED_PARAMETERS.copy()
if name == "getUpdates":
ignored -= {"timeout"} # Has it's own timeout parameter that we do wanna check for
elif name in (
f"send{media_type}"
for media_type in [
"Animation",
"Audio",
"Document",
"Photo",
"Video",
"VideoNote",
"Voice",
]
):
ignored |= {"filename"} # Convenience parameter
elif name == "sendContact":
ignored |= {"contact"} # Added for ease of use
elif name in ["sendLocation", "editMessageLiveLocation"]:
ignored |= {"location"} # Added for ease of use
elif name == "sendVenue":
ignored |= {"venue"} # Added for ease of use
elif name == "answerInlineQuery":
ignored |= {"current_offset"} # Added for ease of use
assert (sig.parameters.keys() ^ checked) - ignored == set()
def check_object(h4):
name = h4.text
obj = getattr(telegram, name)
table = parse_table(h4)
# Check arguments based on source. Makes sure to only check __init__'s signature & nothing else
sig = inspect.signature(obj.__init__, follow_wrapped=True)
checked = set()
for parameter in table:
field = parameter[0]
if field == "from":
field = "from_user"
elif (
name.startswith("InlineQueryResult")
or name.startswith("InputMedia")
or name.startswith("BotCommandScope")
) and field == "type":
continue
elif (name.startswith("ChatMember")) and field == "status": # We autofill the status
continue
elif (
name.startswith("PassportElementError") and field == "source"
) or field == "remove_keyboard":
continue
elif name.startswith("ForceReply") and field == "force_reply": # this param is always True
continue
param = sig.parameters.get(field)
assert param is not None, f"Attribute {field} not found in {obj.__name__}"
# TODO: Check type via docstring
assert check_required_param(
parameter, field, sig, obj.__name__
), f"{obj.__name__!r} parameter {param.name!r} requirement mismatch"
checked.add(field)
ignored = IGNORED_PARAMETERS.copy()
if name == "InputFile":
return
if name == "InlineQueryResult":
ignored |= {"id", "type"} # attributes common to all subclasses
if name == "ChatMember":
ignored |= {"user", "status"} # attributes common to all subclasses
if name == "BotCommandScope":
ignored |= {"type"} # attributes common to all subclasses
elif name in ("PassportFile", "EncryptedPassportElement"):
ignored |= {"credentials"}
elif name == "PassportElementError":
ignored |= {"message", "type", "source"}
elif name.startswith("InputMedia"):
ignored |= {"filename"} # Convenience parameter
assert (sig.parameters.keys() ^ checked) - ignored == set()
def check_required_param(
param_desc: List[str], param_name: str, sig: inspect.Signature, method_or_obj_name: str
) -> bool:
"""Checks if the method/class parameter is a required/optional param as per Telegram docs."""
if len(param_desc) == 4: # this means that there is a dedicated 'Required' column present.
# Handle cases where we provide convenience intentionally-
if param_name in ignored_param_requirements.get(method_or_obj_name, {}):
return True
is_required = True if param_desc[2] in {"Required", "Yes"} else False
is_ours_required = sig.parameters[param_name].default is inspect.Signature.empty
return is_required is is_ours_required
if len(param_desc) == 3: # The docs mention the requirement in the description for classes...
if param_name in ignored_param_requirements.get(method_or_obj_name, {}):
return True
is_required = False if param_desc[2].split(".", 1)[0] == "Optional" else True
is_ours_required = sig.parameters[param_name].default is inspect.Signature.empty
return is_required is is_ours_required
argvalues = []
names = []
http = urllib3.PoolManager(cert_reqs="CERT_REQUIRED", ca_certs=certifi.where())
request = http.request("GET", "https://core.telegram.org/bots/api")
soup = BeautifulSoup(request.data.decode("utf-8"), "html.parser")
for thing in soup.select("h4 > a.anchor"):
# Methods and types don't have spaces in them, luckily all other sections of the docs do
# TODO: don't depend on that
if "-" not in thing["name"]:
h4 = thing.parent
# Is it a method
if h4.text[0].lower() == h4.text[0]:
argvalues.append((check_method, h4))
names.append(h4.text)
elif h4.text not in IGNORED_OBJECTS: # Or a type/object
argvalues.append((check_object, h4))
names.append(h4.text)
@pytest.mark.parametrize(("method", "data"), argvalues=argvalues, ids=names)
@pytest.mark.skipif(
not env_var_2_bool(os.getenv("TEST_OFFICIAL")), reason="test_official is not enabled"
)
def test_official(method, data):
method(data)