-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_bot.py
91 lines (67 loc) · 2.21 KB
/
test_bot.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
import pytest
import bot as b
import config
import content as c
# NOTE: Test time may vary depending on connection speed
# and server availability.
testbot_username = '@rieltor_spbstu_bot'
test_token = '1034733347:AAFFVYmKbVAVnz3GvTLXF5BOCOr8iCuTkzE'
test_id = 905484789
test_answers = {
'name': 'testman', 'place': 'program', 'pay': 'Cash',
'budgets': '100', 'type_build': 'New',
'remont': 'remonted)', 'num': '6811500121'
}
# test_user_data
tud = {
'username': 'bot_tester', 'first_name': 'tester',
'last_name': 'None', 'id': test_id
}
class Dct(dict):
# TODO: Change to Mock object.
pass
class Message(Dct):
def __init__(self, text):
self.text = text
self.chat = Dct()
self.from_user = Dct()
self.chat.id = test_id
self.from_user.id = test_id
class Call(Dct):
def __init__(self, data):
self.data = data
self.from_user = Dct()
self.from_user.username = tud['username']
self.from_user.first_name = tud['first_name']
self.from_user.last_name = tud['last_name']
self.from_user.id = tud['id']
self.message = Message('Да')
@pytest.fixture
def provide_data(monkeypatch):
'''Patch input data and return tuple(id, answers)'''
monkeypatch.setattr(config, 'admin', test_id)
monkeypatch.setattr(b, 'answers', test_answers)
return test_id, test_answers
def test_start():
message = Message('/start')
r = b.start_message(message)
assert r.text == c.welcome_message.strip()
def test_cancel():
message = Message('отменить')
r = b.get_name(message)
assert r.text == message.text
def test_get_num():
message = Message(test_answers['num'])
b.answers.update(test_answers) # XXX: Consider using monkeypatch if psbl
r = b.get_num(message)
assert r.text == c.question.format(**test_answers)
def test_integration(monkeypatch, provide_data):
monkeypatch.setattr(b, 'ctime', lambda: 'testtime')
expected = '{}{}{}'.format(
c.query_date.format(current_time=b.ctime()),
c.user_answers.format(**test_answers),
c.user_data.format(**tud),
)
call = Call('yes')
r = b.callback_worker(call)
assert r.text == expected.strip()