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="******") iqg2 = InlineQueryGenerator(bot=bot) self.assertEqual(bot.username, iqg2.bot.username) with self.assertRaises(BadBotException): iqg3 = InlineQueryGenerator(bot="bot")
def setUp(self): self.iqc = InlineQueryGenerator()
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="******")
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="******") 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="******") 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")
def setUp(self): # For use within the tests we nee some stuff. Starting with a Mockbot self.bot = Mockbot() # And an InlineQueryGenerator and updater (for use with the bot.) self.iqg = InlineQueryGenerator(self.bot) self.updater = Updater(bot=self.bot)
class TestInlineBot(unittest.TestCase): def setUp(self): # For use within the tests we nee some stuff. Starting with a Mockbot self.bot = Mockbot() # And an InlineQueryGenerator and updater (for use with the bot.) self.iqg = InlineQueryGenerator(self.bot) self.updater = Updater(bot=self.bot) def test_inline_bot(self): # create some handlers and add them def escape_markdown(text): """Helper function to escape telegram markup symbols""" escape_chars = '\*_`\[' return re.sub(r'([%s])' % escape_chars, r'\\\1', text) def inlinequery(bot, update): query = update.inline_query.query results = list() results.append( InlineQueryResultArticle( id=uuid4(), title="Caps", input_message_content=InputTextMessageContent( query.upper()))) results.append( InlineQueryResultArticle( id=uuid4(), title="Bold", input_message_content=InputTextMessageContent( "*%s*" % escape_markdown(query), parse_mode=ParseMode.MARKDOWN))) results.append( InlineQueryResultArticle( id=uuid4(), title="Italic", input_message_content=InputTextMessageContent( "_%s_" % escape_markdown(query), parse_mode=ParseMode.MARKDOWN))) update.inline_query.answer(results) dp = self.updater.dispatcher dp.add_handler(InlineQueryHandler(inlinequery)) self.updater.start_polling() # Now test the handler u1 = self.iqg.get_inline_query(query="test data") self.bot.insertUpdate(u1) data = self.bot.sent_messages[-1] self.assertEqual(len(data['results']), 3) results = data['results'] self.assertEqual(results[0]['title'], "Caps") self.assertEqual(results[0]['input_message_content']['message_text'], "TEST DATA") self.assertEqual(results[1]['title'], "Bold") self.assertEqual(results[1]['input_message_content']['message_text'], "*test data*") self.assertEqual(results[2]['title'], "Italic") self.assertEqual(results[2]['input_message_content']['message_text'], "_test data_") self.updater.stop()