forked from szastupov/aiotg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_callbacks.py
90 lines (66 loc) · 1.88 KB
/
test_callbacks.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
import pytest
import random
from aiotg import TgBot
from aiotg import MESSAGE_TYPES
from testfixtures import LogCapture
API_TOKEN = "test_token"
bot = TgBot(API_TOKEN)
def custom_msg(msg):
template = {
"message_id": 0,
"from": { "first_name": "John" },
"chat": { "id": 0, "type": "private" }
}
template.update(msg)
return template
def text_msg(text):
return custom_msg({ "text": text })
def test_command():
called_with = None
@bot.command(r"/echo (.+)")
def echo(chat, match):
nonlocal called_with
called_with = match.group(1)
# Let's check sender repr as well
assert repr(chat.sender) == "John"
bot._process_message(text_msg("/echo foo"))
assert called_with == "foo"
def test_default():
called_with = None
@bot.default
def default(chat, message):
nonlocal called_with
called_with = message["text"]
bot._process_message(text_msg("foo bar"))
assert called_with == "foo bar"
def test_updates():
update = {
"update_id" : 0,
"message": text_msg("foo bar")
}
updates = { "result": [update], "ok": True }
called_with = None
@bot.default
def default(chat, message):
nonlocal called_with
called_with = message["text"]
bot._process_updates(updates)
assert called_with == "foo bar"
def test_updates_failed():
updates = {
"ok": False,
"description": "Opps"
}
with LogCapture() as l:
bot._process_updates(updates)
l.check(('aiotg', 'ERROR', 'getUpdates error: Opps'))
@pytest.mark.parametrize("mt", MESSAGE_TYPES)
def test_handle(mt):
called_with = None
@bot.handle(mt)
def handle(chat, media):
nonlocal called_with
called_with = media
value = random.random()
bot._process_message(custom_msg({ mt: value }))
assert called_with == value