-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathtest_components.py
More file actions
372 lines (273 loc) · 10.2 KB
/
test_components.py
File metadata and controls
372 lines (273 loc) · 10.2 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
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
"""
Tests for botogram/components.py
Copyright (c) 2015 Pietro Albini <pietro@pietroalbini.io>
Released under the MIT license
"""
import copy
import botogram.components
def test_add_before_processing_hook(bot, sample_update):
# This will be true if the before_processing hook was processed
before_hook_processed = False
process_hook_processed = False
# Configure the test bot
def before_processing(chat, message):
nonlocal before_hook_processed
before_hook_processed = True
# Test provided arguments are OK
assert chat.id == -1
assert message.text == "test"
assert message.chat == chat
# This will test if the hook processing order is right
def process_message():
nonlocal process_hook_processed
process_hook_processed = True
# Should be called after before_processing
assert before_hook_processed
component = botogram.components.Component()
component.add_before_processing_hook(before_processing)
component.add_process_message_hook(process_message)
bot.use(component)
bot.process(sample_update)
assert before_hook_processed
assert process_hook_processed
def test_add_process_message_hook(bot, sample_update):
# This will be true if the process_message hook was processed
process_hook_processed = False
# Configure the test bot
def process_message(chat, message):
nonlocal process_hook_processed
process_hook_processed = True
# Test provided arguments are OK
assert chat.id == -1
assert message.text == "test"
assert message.chat == chat
component = botogram.components.Component()
component.add_process_message_hook(process_message)
bot.use(component)
bot.process(sample_update)
assert process_hook_processed
def test_add_message_equals_hook(bot, sample_update):
# meanings: ins[ensitive], sen[sitive], rig[ht], wro[ng]
ins_wro_processed = False
ins_rig_processed = False
sen_wro_processed = False
sen_rig_processed = False
def ins_wro_hook(chat, message):
nonlocal ins_wro_processed
ins_wro_processed = True
def ins_rig_hook(chat, message):
nonlocal ins_rig_processed
ins_rig_processed = True
assert chat.id == -1
assert message.text == "test"
def sen_wro_hook(chat, message):
nonlocal sen_wro_processed
sen_wro_processed = True
def sen_rig_hook(chat, message):
nonlocal sen_rig_processed
sen_rig_processed = True
ins_comp = botogram.components.Component("ins")
ins_comp.add_message_equals_hook("hi", ins_wro_hook)
ins_comp.add_message_equals_hook("Test", ins_rig_hook)
sen_comp = botogram.components.Component("sen")
sen_comp.add_message_equals_hook("Test", sen_wro_hook, ignore_case=False)
sen_comp.add_message_equals_hook("test", sen_rig_hook, ignore_case=False)
for comp in ins_comp, sen_comp:
localbot = copy.copy(bot)
localbot.use(comp)
localbot.process(sample_update)
assert ins_wro_processed == False
assert sen_wro_processed == False
assert ins_rig_processed == True
assert sen_rig_processed == True
def test_add_message_contains_hook(bot, sample_update):
# meanings: ins[ensitive], sen[sitive], rig[ht], wro[ng]
ins_wro_processed = False
ins_rig_processed = False
sen_wro_processed = False
sen_rig_processed = False
single_processed = 0
multiple_processed = 0
def ins_wro(chat, message):
nonlocal ins_wro_processed
ins_wro_processed = True
def ins_rig(chat, message):
nonlocal ins_rig_processed, single_processed
ins_rig_processed = True
single_processed += 1
assert chat.id == -1
assert message.text == "test test test"
assert message.chat == chat
def sen_wro(chat, message):
nonlocal sen_wro_processed
sen_wro_processed = True
def sen_rig(chat, message):
nonlocal sen_rig_processed
sen_rig_processed = True
def multiple(chat, message):
nonlocal multiple_processed
multiple_processed += 1
comp1 = botogram.Component("ins")
comp1.add_message_contains_hook("hi", ins_wro)
comp1.add_message_contains_hook("test", ins_rig)
comp2 = botogram.Component("sen")
comp2.add_message_contains_hook("Test", sen_wro, ignore_case=False)
comp2.add_message_contains_hook("test", sen_rig, ignore_case=False)
comp3 = botogram.Component("multiple")
comp3.add_message_contains_hook("test", multiple, multiple=True)
sample_update.message.text = "test test test"
for comp in comp1, comp2, comp3:
localbot = copy.copy(bot)
localbot.use(comp)
localbot.process(sample_update)
assert ins_wro_processed == False
assert ins_rig_processed == True
assert sen_wro_processed == False
assert sen_rig_processed == True
assert single_processed == 1
assert multiple_processed == 3
def test_add_message_matches_hook(bot, sample_update):
# meanings: ins[ensitive], sen[sitive], rig[ht], wro[ng]
wrong_processed = False
right_processed = False
single_processed = 0
multiple_processed = 0
def wrong(chat, message, matches):
nonlocal wrong_processed
wrong_processed = True
def right(chat, message, matches):
nonlocal right_processed, single_processed
right_processed = True
single_processed += 1
assert chat.id == -1
assert message.text == "a_b_c c_a_b b_c_a"
assert message.chat == chat
assert matches == ('a', 'b', 'c')
def multiple(chat, message, matches):
nonlocal multiple_processed
multiple_processed += 1
comp1 = botogram.Component("single")
comp1.add_message_matches_hook(r"([a-c])\-([a-c])\-([a-c])", wrong)
comp1.add_message_matches_hook(r"([a-c])_([a-c])_([a-c])", right)
comp2 = botogram.Component("multiple")
comp2.add_message_matches_hook(r"([a-c])_([a-c])_([a-c])", multiple,
multiple=True)
sample_update.message.text = "a_b_c c_a_b b_c_a"
for comp in comp1, comp2:
localbot = copy.copy(bot)
localbot.use(comp)
localbot.process(sample_update)
assert wrong_processed == False
assert right_processed == True
assert single_processed == 1
assert multiple_processed == 3
def test_add_command(bot, sample_update):
sample1_processed = False
sample2_processed = False
sample3_processed = False
sample4_processed = False
def sample1(chat, message, args):
nonlocal sample1_processed
sample1_processed = True
def sample2(chat, message, args):
nonlocal sample2_processed
sample2_processed = True
assert chat.id == -1
assert message.text == "/sample2 a b c\n\nd\t\t\te"
assert message.chat == chat
assert args == ["a", "b", "c", "d", "e"]
def sample3(chat, message, args):
nonlocal sample3_processed
sample3_processed = True
assert message.text == "/sample3@test_bot a b c\n\nd\t\t\te"
def sample4(chat, message, args):
nonlocal sample4_processed
sample4_processed = True
assert args == []
comp = botogram.Component("test")
comp.add_command("sample1", sample1)
comp.add_command("sample2", sample2)
comp.add_command("sample3", sample3)
comp.add_command("sample4", sample4)
bot.use(comp)
for cmd in "sample1@another_bot", "sample2", "sample3@test_bot":
sample_update.message.text = "/%s a b c\n\nd\t\t\te" % cmd
bot.process(sample_update)
sample_update.message.text = "/sample4"
bot.process(sample_update)
assert sample1_processed == False
assert sample2_processed == True
assert sample3_processed == True
assert sample4_processed == True
def test_add_shared_memory_initializer(bot, sample_update):
expected = "test"
actual = None
def initializer(shared):
shared["test"] = expected
def command(bot, shared):
nonlocal actual
actual = shared["test"]
comp = botogram.Component("test")
comp.add_shared_memory_initializer(initializer)
comp.add_process_message_hook(command)
bot.use(comp)
bot.process(sample_update)
assert actual == expected
def test_add_chat_unavailable_hook(mock_req, bot, sample_update):
mock_req({
"sendMessage": {"ok": True, "result":{}},
"forwardMessage": {
"ok": False, "error_code": 403, "description": "blocked test",
},
})
chat_unavailable_called = False
def command1(chat):
chat.send("Hey")
def command2(chat, message):
message.forward_to(chat.id)
def chat_unavailable(chat_id, reason):
nonlocal chat_unavailable_called
chat_unavailable_called = True
assert chat_id == -1
assert reason == "blocked"
comp = botogram.Component("test")
comp.add_command("command1", command1)
comp.add_command("command2", command2)
comp.add_chat_unavailable_hook(chat_unavailable)
bot.use(comp)
sample_update.message.text = "/command1"
bot.process(sample_update)
assert not chat_unavailable_called
sample_update.message.text = "/command2"
bot.process(sample_update)
assert chat_unavailable_called
def test_add_timer(bot):
global_bot = bot
timer1_calls = 0
timer2_calls = 0
timer3_calls = 0
def timer1():
nonlocal timer1_calls
timer1_calls += 1
def timer2():
nonlocal timer2_calls
timer2_calls += 1
def timer3(bot):
nonlocal timer3_calls
timer3_calls += 1
# Here bot is the one local of the function, not the parent one
assert bot == global_bot
comp = botogram.Component("test")
comp.add_timer(1, timer1)
comp.add_timer(3, timer2)
comp.add_timer(30, timer3)
bot.use(comp)
# This will simulate running the bot for 10 seconds
now = 1420070400 # 01/01/2015
for i in range(10):
scheduled = bot.scheduled_tasks(now+i)
for job in scheduled:
job()
assert timer1_calls == 10 # 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
assert timer2_calls == 4 # 0, 3, 6, 9
assert timer3_calls == 1 # 0