-
Notifications
You must be signed in to change notification settings - Fork 28
/
test_Inlinequerygenerator.py
133 lines (105 loc) · 4.91 KB
/
test_Inlinequerygenerator.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
#!/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.errors import (BadBotException, BadUserException)
from ptbtest import InlineQueryGenerator
from ptbtest import Mockbot
from ptbtest import UserGenerator
from telegram import ChosenInlineResult
from telegram import InlineQuery
from telegram import Location
from telegram import Update
from telegram import User
class TestInlineQueryGenerator(unittest.TestCase):
def setUp(self):
self.iqg = InlineQueryGenerator()
def test_standard(self):
u = self.iqg.get_inline_query()
self.assertIsInstance(u, Update)
self.assertIsInstance(u.inline_query, InlineQuery)
self.assertIsInstance(u.inline_query.from_user, User)
bot = Mockbot(username="testbot")
iqg2 = InlineQueryGenerator(bot=bot)
self.assertEqual(bot.username, iqg2.bot.username)
with self.assertRaises(BadBotException):
iqg3 = InlineQueryGenerator(bot="bot")
def test_with_user(self):
ug = UserGenerator()
user = ug.get_user()
u = self.iqg.get_inline_query(user=user)
self.assertEqual(u.inline_query.from_user.id, user.id)
with self.assertRaises(BadUserException):
self.iqg.get_inline_query(user="user")
def test_query(self):
u = self.iqg.get_inline_query(query="test")
self.assertEqual(u.inline_query.query, "test")
with self.assertRaisesRegexp(AttributeError, "query"):
self.iqg.get_inline_query(query=True)
def test_offset(self):
u = self.iqg.get_inline_query(offset="44")
self.assertEqual(u.inline_query.offset, "44")
with self.assertRaisesRegexp(AttributeError, "offset"):
self.iqg.get_inline_query(offset=True)
def test_location(self):
u = self.iqg.get_inline_query(location=True)
self.assertIsInstance(u.inline_query.location, Location)
loc = Location(23.0, 90.0)
u = self.iqg.get_inline_query(location=loc)
self.assertEqual(u.inline_query.location.longitude, 23.0)
with self.assertRaisesRegexp(AttributeError, "telegram\.Location"):
self.iqg.get_inline_query(location="location")
class TestChosenInlineResult(unittest.TestCase):
def setUp(self):
self.iqc = InlineQueryGenerator()
def test_chosen_inline_result(self):
u = self.iqc.get_chosen_inline_result("testid")
self.assertIsInstance(u, Update)
self.assertIsInstance(u.chosen_inline_result, ChosenInlineResult)
self.assertIsInstance(u.chosen_inline_result.from_user, User)
self.assertEqual(u.chosen_inline_result.result_id, "testid")
with self.assertRaisesRegexp(AttributeError, "chosen_inline_result"):
self.iqc.get_chosen_inline_result()
def test_with_location(self):
u = self.iqc.get_chosen_inline_result("testid", location=True)
self.assertIsInstance(u.chosen_inline_result.location, Location)
loc = Location(23.0, 90.0)
u = self.iqc.get_chosen_inline_result("testid", location=loc)
self.assertEqual(u.chosen_inline_result.location.longitude, 23.0)
with self.assertRaisesRegexp(AttributeError, "telegram\.Location"):
self.iqc.get_chosen_inline_result("test_id", location="loc")
def test_inline_message_id(self):
u = self.iqc.get_chosen_inline_result("test")
self.assertIsInstance(u.chosen_inline_result.inline_message_id, str)
u = self.iqc.get_chosen_inline_result(
"test", inline_message_id="myidilike")
self.assertEqual(u.chosen_inline_result.inline_message_id, "myidilike")
def test_user(self):
ug = UserGenerator()
user = ug.get_user()
u = self.iqc.get_chosen_inline_result("test", user=user)
self.assertEqual(u.chosen_inline_result.from_user.id, user.id)
with self.assertRaises(BadUserException):
self.iqc.get_chosen_inline_result("test", user="user")
if __name__ == '__main__':
unittest.main()