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
160 lines (129 loc) · 5 KB
/
test_official.py
File metadata and controls
160 lines (129 loc) · 5 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
import sys
import inspect
import warnings
from collections import namedtuple
import platform
import certifi
import logging
from bs4 import BeautifulSoup
sys.path.append('.')
from telegram.vendor.ptb_urllib3 import urllib3
import telegram
IGNORED_OBJECTS = ('ResponseParameters', 'CallbackGame')
IGNORED_PARAMETERS = {'self', 'args', 'kwargs', 'read_latency', 'network_delay', 'timeout', 'bot'}
logger = logging.getLogger(__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):
table = find_next_sibling_until(h4, 'table', h4.find_next_sibling('h4'))
if not table:
return []
head = [td.text for td in table.tr.find_all('td')]
row = namedtuple('{}TableRow'.format(h4.text), ','.join(head))
t = []
for tr in table.find_all('tr')[1:]:
t.append(row(*[td.text for td in tr.find_all('td')]))
return t
def check_method(h4):
name = h4.text
method = getattr(telegram.Bot, name)
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.Parameters)
logger.debug(parameter)
assert param is not None
# TODO: Check type via docstring
# TODO: Check if optional or required
checked.append(parameter.Parameters)
ignored = IGNORED_PARAMETERS.copy()
if name == 'getUpdates':
ignored -= {'timeout'} # Has it's own timeout parameter that we do wanna check for
elif name == 'sendDocument':
ignored |= {'filename'} # Undocumented
elif name == 'setGameScore':
ignored |= {'edit_message'} # TODO: Now deprecated, so no longer in telegrams docs
elif name == 'sendContact':
ignored |= {'contact'} # Added for ease of use
elif name == 'sendLocation':
ignored |= {'location'} # Added for ease of use
elif name == 'sendVenue':
ignored |= {'venue'} # Added for ease of use
logger.debug((sig.parameters.keys(), checked, ignored,
sig.parameters.keys() - checked - ignored))
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
sig = inspect.signature(obj, follow_wrapped=True)
checked = []
for parameter in table:
field = parameter.Field
if field == 'from':
field = 'from_user'
elif name.startswith('InlineQueryResult') and field == 'type':
continue
elif field == 'remove_keyboard':
continue
param = sig.parameters.get(field)
logger.debug(parameter)
assert param is not None
# TODO: Check type via docstring
# TODO: Check if optional or required
checked.append(field)
ignored = IGNORED_PARAMETERS.copy()
if name == 'InputFile':
ignored |= {'data'}
elif name == 'InlineQueryResult':
ignored |= {'id'}
elif name == 'User':
ignored |= {'type'} # TODO: Deprecation
if name.startswith('InlineQueryResult'):
ignored |= {'type'}
logger.debug((sig.parameters.keys(), checked, ignored,
sig.parameters.keys() - checked - ignored))
assert (sig.parameters.keys() ^ checked) - ignored == set()
def test_official():
if not sys.version_info >= (3, 5) or platform.python_implementation() != 'CPython':
warnings.warn('Not running "official" tests, since follow_wrapped is not supported'
'on this platform (cpython version >= 3.5 required)')
return
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
name = h4.text
test = None
# Is it a method
if h4.text[0].lower() == h4.text[0]:
test = check_method
else: # Or a type/object
if name not in IGNORED_OBJECTS:
test = check_object
if test:
def fn():
return test(h4)
fn.description = '{}({}) ({})'.format(test.__name__, h4.text, __name__)
yield fn
if __name__ == '__main__':
# Since we don't have the nice unittest asserts which show the comparison
# We turn on debugging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.DEBUG)
for f in test_official():
f()