-
Notifications
You must be signed in to change notification settings - Fork 28
/
test_Callbackquerygenerator.py
109 lines (93 loc) · 4.63 KB
/
test_Callbackquerygenerator.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
#!/usr/bin/env python
# pylint: disable=E0611,E0213,E1102,C0103,E1101,W0613,R0913,R0904
#
# A library that provides a testing suite fot python-telegram-bot
# wich can be found on https://github.com/python-telegram-bot/python-telegram-bot
# Copyright (C) 2017
# Pieter Schutz - https://github.com/eldinnie
#
# 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/].
from __future__ import absolute_import
from os import sys
sys.path.append("..")
import unittest
from ptbtest import (BadBotException, BadCallbackQueryException,
BadUserException, BadMessageException)
from ptbtest import (CallbackQueryGenerator, MessageGenerator, Mockbot,
UserGenerator)
from telegram import (CallbackQuery, Message, Update, User)
class TestMessageGeneratorChannelPost(unittest.TestCase):
def setUp(self):
self.cqg = CallbackQueryGenerator()
def test_invalid_calls(self):
with self.assertRaisesRegexp(BadCallbackQueryException,
"message and inline_message_id"):
self.cqg.get_callback_query()
with self.assertRaisesRegexp(BadCallbackQueryException,
"message and inline_message_id"):
self.cqg.get_callback_query(message=True, inline_message_id=True)
with self.assertRaisesRegexp(BadCallbackQueryException,
"data and game_short_name"):
self.cqg.get_callback_query(message=True)
with self.assertRaisesRegexp(BadCallbackQueryException,
"data and game_short_name"):
self.cqg.get_callback_query(
message=True, data="test-data", game_short_name="mygame")
def test_required_auto_set(self):
u = self.cqg.get_callback_query(
inline_message_id=True, data="test-data")
self.assertIsInstance(u.callback_query.from_user, User)
self.assertIsInstance(u.callback_query.chat_instance, str)
bot = Mockbot(username="testbot")
cqg2 = CallbackQueryGenerator(bot=bot)
self.assertEqual(bot.username, cqg2.bot.username)
with self.assertRaises(BadBotException):
cqg3 = CallbackQueryGenerator(bot="bot")
def test_message(self):
mg = MessageGenerator()
message = mg.get_message().message
u = self.cqg.get_callback_query(message=message, data="test-data")
self.assertIsInstance(u, Update)
self.assertIsInstance(u.callback_query, CallbackQuery)
self.assertEqual(u.callback_query.message.message_id,
message.message_id)
u = self.cqg.get_callback_query(message=True, data="test-data")
self.assertIsInstance(u.callback_query.message, Message)
self.assertEqual(u.callback_query.message.from_user.username,
self.cqg.bot.username)
with self.assertRaises(BadMessageException):
self.cqg.get_callback_query(message="message", data="test-data")
def test_inline_message_id(self):
u = self.cqg.get_callback_query(
inline_message_id="myidilike", data="test-data")
self.assertEqual(u.callback_query.inline_message_id, "myidilike")
u = self.cqg.get_callback_query(
inline_message_id=True, data="test-data")
self.assertIsInstance(u.callback_query.inline_message_id, str)
with self.assertRaisesRegexp(BadCallbackQueryException,
"string or True"):
self.cqg.get_callback_query(
inline_message_id=3.98, data="test-data")
def test_user(self):
ug = UserGenerator()
user = ug.get_user()
u = self.cqg.get_callback_query(
user=user, message=True, data="test-data")
self.assertEqual(user.id, u.callback_query.from_user.id)
self.assertNotEqual(user.id, u.callback_query.message.from_user.id)
with self.assertRaises(BadUserException):
u = self.cqg.get_callback_query(
user="user", inline_message_id=True, data="test-data")
if __name__ == '__main__':
unittest.main()