-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
test_applicationbuilder.py
426 lines (366 loc) · 15.4 KB
/
test_applicationbuilder.py
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2022
# 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/].
import asyncio
import os
from dataclasses import dataclass
import httpx
import pytest
from telegram.ext import (
AIORateLimiter,
Application,
ApplicationBuilder,
CallbackDataCache,
ContextTypes,
Defaults,
ExtBot,
JobQueue,
PicklePersistence,
Updater,
)
from telegram.ext._applicationbuilder import _BOT_CHECKS
from telegram.request import HTTPXRequest
from .conftest import PRIVATE_KEY, data_file, env_var_2_bool
TEST_WITH_OPT_DEPS = env_var_2_bool(os.getenv("TEST_WITH_OPT_DEPS", True))
@pytest.fixture(scope="function")
def builder():
return ApplicationBuilder()
@pytest.mark.skipif(TEST_WITH_OPT_DEPS, reason="Optional dependencies are installed")
class TestApplicationBuilderNoOptDeps:
@pytest.mark.filterwarnings("ignore::telegram.warnings.PTBUserWarning")
def test_init(self, builder):
builder.token("token")
app = builder.build()
assert app.job_queue is None
@pytest.mark.skipif(not TEST_WITH_OPT_DEPS, reason="Optional dependencies not installed")
class TestApplicationBuilder:
def test_slot_behaviour(self, builder, mro_slots):
for attr in builder.__slots__:
assert getattr(builder, attr, "err") != "err", f"got extra slot '{attr}'"
assert len(mro_slots(builder)) == len(set(mro_slots(builder))), "duplicate slot"
def test_job_queue_init_exception(self, monkeypatch):
def init_raises_runtime_error(*args, **kwargs):
raise RuntimeError("RuntimeError")
monkeypatch.setattr(JobQueue, "__init__", init_raises_runtime_error)
with pytest.raises(RuntimeError, match="RuntimeError"):
ApplicationBuilder()
def test_build_without_token(self, builder):
with pytest.raises(RuntimeError, match="No bot token was set."):
builder.build()
def test_build_custom_bot(self, builder, bot):
builder.bot(bot)
app = builder.build()
assert app.bot is bot
assert app.updater.bot is bot
def test_default_values(self, bot, monkeypatch, builder):
@dataclass
class Client:
timeout: object
proxies: object
limits: object
monkeypatch.setattr(httpx, "AsyncClient", Client)
app = builder.token(bot.token).build()
assert isinstance(app, Application)
assert app.concurrent_updates == 0
assert isinstance(app.bot, ExtBot)
assert isinstance(app.bot.request, HTTPXRequest)
assert "api.telegram.org" in app.bot.base_url
assert bot.token in app.bot.base_url
assert "api.telegram.org" in app.bot.base_file_url
assert bot.token in app.bot.base_file_url
assert app.bot.private_key is None
assert app.bot.callback_data_cache is None
assert app.bot.defaults is None
assert app.bot.rate_limiter is None
assert app.bot.local_mode is False
get_updates_client = app.bot._request[0]._client
assert get_updates_client.limits == httpx.Limits(
max_connections=1, max_keepalive_connections=1
)
assert get_updates_client.proxies is None
assert get_updates_client.timeout == httpx.Timeout(
connect=5.0, read=5.0, write=5.0, pool=1.0
)
client = app.bot.request._client
assert client.limits == httpx.Limits(max_connections=256, max_keepalive_connections=256)
assert client.proxies is None
assert client.timeout == httpx.Timeout(connect=5.0, read=5.0, write=5.0, pool=1.0)
assert isinstance(app.update_queue, asyncio.Queue)
assert isinstance(app.updater, Updater)
assert app.updater.bot is app.bot
assert app.updater.update_queue is app.update_queue
assert isinstance(app.job_queue, JobQueue)
assert app.job_queue.application is app
assert app.persistence is None
assert app.post_init is None
assert app.post_shutdown is None
@pytest.mark.parametrize(
"method, description", _BOT_CHECKS, ids=[entry[0] for entry in _BOT_CHECKS]
)
def test_mutually_exclusive_for_bot(self, builder, method, description):
# First test that e.g. `bot` can't be set if `request` was already set
# We pass the private key since `private_key` is the only method that doesn't just save
# the passed value
getattr(builder, method)(data_file("private.key"))
with pytest.raises(RuntimeError, match=f"`bot` may only be set, if no {description}"):
builder.bot(None)
# Now test that `request` can't be set if `bot` was already set
builder = builder.__class__()
builder.bot(None)
with pytest.raises(RuntimeError, match=f"`{method}` may only be set, if no bot instance"):
getattr(builder, method)(data_file("private.key"))
@pytest.mark.parametrize(
"method",
(
"connection_pool_size",
"connect_timeout",
"pool_timeout",
"read_timeout",
"write_timeout",
"proxy_url",
"bot",
"updater",
),
)
def test_mutually_exclusive_for_request(self, builder, method):
builder.request(1)
with pytest.raises(
RuntimeError, match=f"`{method}` may only be set, if no request instance"
):
getattr(builder, method)(data_file("private.key"))
builder = ApplicationBuilder()
getattr(builder, method)(1)
with pytest.raises(RuntimeError, match="`request` may only be set, if no"):
builder.request(1)
@pytest.mark.parametrize(
"method",
(
"get_updates_connection_pool_size",
"get_updates_connect_timeout",
"get_updates_pool_timeout",
"get_updates_read_timeout",
"get_updates_write_timeout",
"get_updates_proxy_url",
"bot",
"updater",
),
)
def test_mutually_exclusive_for_get_updates_request(self, builder, method):
builder.get_updates_request(1)
with pytest.raises(
RuntimeError,
match=f"`{method}` may only be set, if no get_updates_request instance",
):
getattr(builder, method)(data_file("private.key"))
builder = ApplicationBuilder()
getattr(builder, method)(1)
with pytest.raises(RuntimeError, match="`get_updates_request` may only be set, if no"):
builder.get_updates_request(1)
@pytest.mark.parametrize(
"method",
[
"get_updates_connection_pool_size",
"get_updates_connect_timeout",
"get_updates_pool_timeout",
"get_updates_read_timeout",
"get_updates_write_timeout",
"get_updates_proxy_url",
"connection_pool_size",
"connect_timeout",
"pool_timeout",
"read_timeout",
"write_timeout",
"proxy_url",
"bot",
"update_queue",
"rate_limiter",
]
+ [entry[0] for entry in _BOT_CHECKS],
)
def test_mutually_exclusive_for_updater(self, builder, method):
builder.updater(1)
with pytest.raises(
RuntimeError,
match=f"`{method}` may only be set, if no updater",
):
getattr(builder, method)(data_file("private.key"))
builder = ApplicationBuilder()
getattr(builder, method)(data_file("private.key"))
with pytest.raises(RuntimeError, match=f"`updater` may only be set, if no {method}"):
builder.updater(1)
@pytest.mark.parametrize(
"method",
[
"get_updates_connection_pool_size",
"get_updates_connect_timeout",
"get_updates_pool_timeout",
"get_updates_read_timeout",
"get_updates_write_timeout",
"get_updates_proxy_url",
"connection_pool_size",
"connect_timeout",
"pool_timeout",
"read_timeout",
"write_timeout",
"proxy_url",
"bot",
]
+ [entry[0] for entry in _BOT_CHECKS],
)
def test_mutually_non_exclusive_for_updater(self, builder, method):
# If no updater is to be used, all these parameters should be settable
# Since the parameters themself are tested in the other tests, we here just make sure
# that no exception is raised
builder.updater(None)
getattr(builder, method)(data_file("private.key"))
builder = ApplicationBuilder()
getattr(builder, method)(data_file("private.key"))
builder.updater(None)
def test_all_bot_args_custom(self, builder, bot, monkeypatch):
defaults = Defaults()
request = HTTPXRequest()
get_updates_request = HTTPXRequest()
rate_limiter = AIORateLimiter()
builder.token(bot.token).base_url("base_url").base_file_url("base_file_url").private_key(
PRIVATE_KEY
).defaults(defaults).arbitrary_callback_data(42).request(request).get_updates_request(
get_updates_request
).rate_limiter(
rate_limiter
).local_mode(
True
)
built_bot = builder.build().bot
# In the following we access some private attributes of bot and request. this is not
# really nice as we want to test the public interface, but here it's hard to ensure by
# other means that the parameters are passed correctly
assert built_bot.token == bot.token
assert built_bot.base_url == "base_url" + bot.token
assert built_bot.base_file_url == "base_file_url" + bot.token
assert built_bot.defaults is defaults
assert built_bot.request is request
assert built_bot._request[0] is get_updates_request
assert built_bot.callback_data_cache.maxsize == 42
assert built_bot.private_key
assert built_bot.rate_limiter is rate_limiter
assert built_bot.local_mode is True
@dataclass
class Client:
timeout: object
proxies: object
limits: object
monkeypatch.setattr(httpx, "AsyncClient", Client)
builder = ApplicationBuilder().token(bot.token)
builder.connection_pool_size(1).connect_timeout(2).pool_timeout(3).read_timeout(
4
).write_timeout(5).proxy_url("proxy_url")
app = builder.build()
client = app.bot.request._client
assert client.timeout == httpx.Timeout(pool=3, connect=2, read=4, write=5)
assert client.limits == httpx.Limits(max_connections=1, max_keepalive_connections=1)
assert client.proxies == "proxy_url"
builder = ApplicationBuilder().token(bot.token)
builder.get_updates_connection_pool_size(1).get_updates_connect_timeout(
2
).get_updates_pool_timeout(3).get_updates_read_timeout(4).get_updates_write_timeout(
5
).get_updates_proxy_url(
"proxy_url"
)
app = builder.build()
client = app.bot._request[0]._client
assert client.timeout == httpx.Timeout(pool=3, connect=2, read=4, write=5)
assert client.limits == httpx.Limits(max_connections=1, max_keepalive_connections=1)
assert client.proxies == "proxy_url"
def test_custom_application_class(self, bot, builder):
class CustomApplication(Application):
def __init__(self, arg, **kwargs):
super().__init__(**kwargs)
self.arg = arg
builder.application_class(CustomApplication, kwargs={"arg": 2}).token(bot.token)
app = builder.build()
assert isinstance(app, CustomApplication)
assert app.arg == 2
def test_all_application_args_custom(self, builder, bot, monkeypatch):
job_queue = JobQueue()
persistence = PicklePersistence("file_path")
update_queue = asyncio.Queue()
context_types = ContextTypes()
concurrent_updates = 123
async def post_init(app: Application) -> None:
pass
async def post_shutdown(app: Application) -> None:
pass
app = (
builder.token(bot.token)
.job_queue(job_queue)
.persistence(persistence)
.update_queue(update_queue)
.context_types(context_types)
.concurrent_updates(concurrent_updates)
.post_init(post_init)
.post_shutdown(post_shutdown)
.arbitrary_callback_data(True)
).build()
assert app.job_queue is job_queue
assert app.job_queue.application is app
assert app.persistence is persistence
assert app.persistence.bot is app.bot
assert app.update_queue is update_queue
assert app.updater.update_queue is update_queue
assert app.updater.bot is app.bot
assert app.context_types is context_types
assert app.concurrent_updates == concurrent_updates
assert app.post_init is post_init
assert app.post_shutdown is post_shutdown
assert isinstance(app.bot.callback_data_cache, CallbackDataCache)
updater = Updater(bot=bot, update_queue=update_queue)
app = ApplicationBuilder().updater(updater).build()
assert app.updater is updater
assert app.bot is updater.bot
assert app.update_queue is updater.update_queue
@pytest.mark.parametrize("input_type", ("bytes", "str", "Path"))
def test_all_private_key_input_types(self, builder, bot, input_type):
private_key = data_file("private.key")
password = data_file("private_key.password")
if input_type == "bytes":
private_key = private_key.read_bytes()
password = password.read_bytes()
if input_type == "str":
private_key = str(private_key)
password = str(password)
builder.token(bot.token).private_key(
private_key=private_key,
password=password,
)
bot = builder.build().bot
assert bot.private_key
def test_no_updater(self, bot, builder):
app = builder.token(bot.token).updater(None).build()
assert app.bot.token == bot.token
assert app.updater is None
assert isinstance(app.update_queue, asyncio.Queue)
assert isinstance(app.job_queue, JobQueue)
assert app.job_queue.application is app
@pytest.mark.filterwarnings("ignore::telegram.warnings.PTBUserWarning")
def test_no_job_queue(self, bot, builder):
app = builder.token(bot.token).job_queue(None).build()
assert app.bot.token == bot.token
assert app.job_queue is None
assert isinstance(app.update_queue, asyncio.Queue)
assert isinstance(app.updater, Updater)