Add improvements to tests

This commit is contained in:
Leandro Toledo 2015-08-28 13:28:58 -03:00
parent 942706e20f
commit 1c4595123c
2 changed files with 77 additions and 50 deletions

View file

View file

@ -38,32 +38,40 @@ class BotTest(unittest.TestCase):
def setUp(self): def setUp(self):
bot = telegram.Bot(token=os.environ.get('TOKEN')) bot = telegram.Bot(token=os.environ.get('TOKEN'))
chat_id = os.environ.get('CHAT_ID')
self._bot = bot self._bot = bot
self._chat_id = chat_id
print('Testing the Bot API class') print('Testing the Bot API class')
def testGetMe(self): def testGetMe(self):
'''Test the telegram.Bot getMe method''' '''Test the telegram.Bot getMe method'''
print('Testing getMe') print('Testing getMe')
bot = self._bot.getMe() bot = self._bot.getMe()
self.assertTrue(self.is_json(bot.to_json())) self.assertTrue(self.is_json(bot.to_json()))
self.assertEqual(120405045, bot.id) self.assertEqual(bot.id, 120405045)
self.assertEqual('Toledo\'s Palace Bot', bot.first_name) self.assertEqual(bot.first_name, 'Toledo\'s Palace Bot')
self.assertEqual(None, bot.last_name) self.assertEqual(bot.last_name, None)
self.assertEqual('ToledosPalaceBot', bot.username) self.assertEqual(bot.username, 'ToledosPalaceBot')
self.assertEqual(bot.name, '@ToledosPalaceBot')
def testSendMessage(self): def testSendMessage(self):
'''Test the telegram.Bot sendMessage method''' '''Test the telegram.Bot sendMessage method'''
print('Testing sendMessage') print('Testing sendMessage')
message = self._bot.sendMessage(chat_id=12173560, message = self._bot.sendMessage(chat_id=self._chat_id,
text='Моё судно на воздушной подушке полно угрей') text='Моё судно на воздушной подушке полно угрей')
self.assertTrue(self.is_json(message.to_json())) self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(u'Моё судно на воздушной подушке полно угрей', message.text) self.assertEqual(message.text, u'Моё судно на воздушной подушке полно угрей')
self.assertTrue(isinstance(message.date, datetime)) self.assertTrue(isinstance(message.date, datetime))
def testGetUpdates(self): def testGetUpdates(self):
'''Test the telegram.Bot getUpdates method''' '''Test the telegram.Bot getUpdates method'''
print('Testing getUpdates') print('Testing getUpdates')
updates = self._bot.getUpdates() updates = self._bot.getUpdates()
if updates: if updates:
self.assertTrue(self.is_json(updates[0].to_json())) self.assertTrue(self.is_json(updates[0].to_json()))
self.assertTrue(isinstance(updates[0], telegram.Update)) self.assertTrue(isinstance(updates[0], telegram.Update))
@ -71,12 +79,13 @@ class BotTest(unittest.TestCase):
def testForwardMessage(self): def testForwardMessage(self):
'''Test the telegram.Bot forwardMessage method''' '''Test the telegram.Bot forwardMessage method'''
print('Testing forwardMessage') print('Testing forwardMessage')
message = self._bot.forwardMessage(chat_id=12173560, message = self._bot.forwardMessage(chat_id=self._chat_id,
from_chat_id=12173560, from_chat_id=self._chat_id,
message_id=138) message_id=138)
self.assertTrue(self.is_json(message.to_json())) self.assertTrue(self.is_json(message.to_json()))
self.assertEqual('Oi', message.text) self.assertEqual(message.text, 'Oi')
self.assertEqual('leandrotoledo', message.forward_from.username) self.assertEqual(message.forward_from.username, 'leandrotoledo')
self.assertTrue(isinstance(message.forward_date, datetime)) self.assertTrue(isinstance(message.forward_date, datetime))
def testSendPhoto(self): def testSendPhoto(self):
@ -84,152 +93,170 @@ class BotTest(unittest.TestCase):
print('Testing sendPhoto - File') print('Testing sendPhoto - File')
message = self._bot.sendPhoto(photo=open('tests/data/telegram.png', 'rb'), message = self._bot.sendPhoto(photo=open('tests/data/telegram.png', 'rb'),
caption='testSendPhoto', caption='testSendPhoto',
chat_id=12173560) chat_id=self._chat_id)
self.assertTrue(self.is_json(message.to_json())) self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(1451, message.photo[0].file_size) self.assertEqual(message.photo[0].file_size, 1451)
self.assertEqual('testSendPhoto', message.caption) self.assertEqual(message.caption, 'testSendPhoto')
def testResendPhoto(self): def testResendPhoto(self):
'''Test the telegram.Bot sendPhoto method''' '''Test the telegram.Bot sendPhoto method'''
print('Testing sendPhoto - Resend') print('Testing sendPhoto - Resend')
message = self._bot.sendPhoto(photo='AgADAQADr6cxGzU8LQe6q0dMJD2rHYkP2ykABFymiQqJgjxRGGMAAgI', message = self._bot.sendPhoto(photo='AgADAQADr6cxGzU8LQe6q0dMJD2rHYkP2ykABFymiQqJgjxRGGMAAgI',
chat_id=12173560) chat_id=self._chat_id)
self.assertTrue(self.is_json(message.to_json())) self.assertTrue(self.is_json(message.to_json()))
self.assertEqual('AgADAQADr6cxGzU8LQe6q0dMJD2rHYkP2ykABFymiQqJgjxRGGMAAgI', message.photo[0].file_id) self.assertEqual(message.photo[0].file_id, 'AgADAQADr6cxGzU8LQe6q0dMJD2rHYkP2ykABFymiQqJgjxRGGMAAgI')
def testSendJPGURLPhoto(self): def testSendJPGURLPhoto(self):
'''Test the telegram.Bot sendPhoto method''' '''Test the telegram.Bot sendPhoto method'''
print('Testing testSendJPGURLPhoto - URL') print('Testing testSendJPGURLPhoto - URL')
message = self._bot.sendPhoto(photo='http://dummyimage.com/600x400/000/fff.jpg&text=telegram', message = self._bot.sendPhoto(photo='http://dummyimage.com/600x400/000/fff.jpg&text=telegram',
chat_id=12173560) chat_id=self._chat_id)
self.assertTrue(self.is_json(message.to_json())) self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(822, message.photo[0].file_size) self.assertEqual(message.photo[0].file_size, 822)
def testSendPNGURLPhoto(self): def testSendPNGURLPhoto(self):
'''Test the telegram.Bot sendPhoto method''' '''Test the telegram.Bot sendPhoto method'''
print('Testing testSendPNGURLPhoto - URL') print('Testing testSendPNGURLPhoto - URL')
message = self._bot.sendPhoto(photo='http://dummyimage.com/600x400/000/fff.png&text=telegram', message = self._bot.sendPhoto(photo='http://dummyimage.com/600x400/000/fff.png&text=telegram',
chat_id=12173560) chat_id=self._chat_id)
self.assertTrue(self.is_json(message.to_json())) self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(684, message.photo[0].file_size) self.assertEqual(message.photo[0].file_size, 684)
def testSendGIFURLPhoto(self): def testSendGIFURLPhoto(self):
'''Test the telegram.Bot sendPhoto method''' '''Test the telegram.Bot sendPhoto method'''
print('Testing testSendGIFURLPhoto - URL') print('Testing testSendGIFURLPhoto - URL')
message = self._bot.sendPhoto(photo='http://dummyimage.com/600x400/000/fff.gif&text=telegram', message = self._bot.sendPhoto(photo='http://dummyimage.com/600x400/000/fff.gif&text=telegram',
chat_id=12173560) chat_id=self._chat_id)
self.assertTrue(self.is_json(message.to_json())) self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(684, message.photo[0].file_size) self.assertEqual(message.photo[0].file_size, 684)
def testSendAudio(self): def testSendAudio(self):
'''Test the telegram.Bot sendAudio method''' '''Test the telegram.Bot sendAudio method'''
print('Testing sendAudio - File') print('Testing sendAudio - File')
message = self._bot.sendAudio(audio=open('tests/data/telegram.mp3', 'rb'), message = self._bot.sendAudio(audio=open('tests/data/telegram.mp3', 'rb'),
chat_id=12173560, chat_id=self._chat_id,
performer='Leandro Toledo', performer='Leandro Toledo',
title='Teste') title='Teste')
self.assertTrue(self.is_json(message.to_json())) self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(28232, message.audio.file_size) self.assertEqual(message.audio.file_size, 28232)
self.assertEqual('Leandro Toledo', message.audio.performer) self.assertEqual(message.audio.performer, 'Leandro Toledo')
self.assertEqual('Teste', message.audio.title) self.assertEqual(message.audio.title, 'Teste')
def testResendAudio(self): def testResendAudio(self):
'''Test the telegram.Bot sendAudio method''' '''Test the telegram.Bot sendAudio method'''
print('Testing sendAudio - Resend') print('Testing sendAudio - Resend')
message = self._bot.sendAudio(audio='BQADAQADwwcAAjU8LQdBRsl3_qD2TAI', message = self._bot.sendAudio(audio='BQADAQADwwcAAjU8LQdBRsl3_qD2TAI',
chat_id=12173560, chat_id=self._chat_id,
performer='Leandro Toledo', # Bug #39 performer='Leandro Toledo', # Bug #39
title='Teste') # Bug #39 title='Teste') # Bug #39
self.assertTrue(self.is_json(message.to_json())) self.assertTrue(self.is_json(message.to_json()))
self.assertEqual('BQADAQADwwcAAjU8LQdBRsl3_qD2TAI', message.audio.file_id) self.assertEqual(message.audio.file_id, 'BQADAQADwwcAAjU8LQdBRsl3_qD2TAI')
def testSendVoice(self): def testSendVoice(self):
'''Test the telegram.Bot sendVoice method''' '''Test the telegram.Bot sendVoice method'''
print('Testing sendVoice - File') print('Testing sendVoice - File')
message = self._bot.sendVoice(voice=open('tests/data/telegram.ogg', 'rb'), message = self._bot.sendVoice(voice=open('tests/data/telegram.ogg', 'rb'),
chat_id=12173560) chat_id=self._chat_id)
self.assertTrue(self.is_json(message.to_json())) self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(9199, message.voice.file_size) self.assertEqual(message.voice.file_size, 9199)
def testResendVoice(self): def testResendVoice(self):
'''Test the telegram.Bot sendVoice method''' '''Test the telegram.Bot sendVoice method'''
print('Testing sendVoice - Resend') print('Testing sendVoice - Resend')
message = self._bot.sendVoice(voice='AwADAQADIQEAAvjAuQABSAXg_GhkhZcC', message = self._bot.sendVoice(voice='AwADAQADIQEAAvjAuQABSAXg_GhkhZcC',
chat_id=12173560) chat_id=self._chat_id)
self.assertTrue(self.is_json(message.to_json())) self.assertTrue(self.is_json(message.to_json()))
self.assertEqual('AwADAQADIQEAAvjAuQABSAXg_GhkhZcC', message.voice.file_id) self.assertEqual(message.voice.file_id, 'AwADAQADIQEAAvjAuQABSAXg_GhkhZcC')
def testSendDocument(self): def testSendDocument(self):
'''Test the telegram.Bot sendDocument method''' '''Test the telegram.Bot sendDocument method'''
print('Testing sendDocument - File') print('Testing sendDocument - File')
message = self._bot.sendDocument(document=open('tests/data/telegram.png', 'rb'), message = self._bot.sendDocument(document=open('tests/data/telegram.png', 'rb'),
chat_id=12173560) chat_id=self._chat_id)
self.assertTrue(self.is_json(message.to_json())) self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(12948, message.document.file_size) self.assertEqual(message.document.file_size, 12948)
def testSendGIFURLDocument(self): def testSendGIFURLDocument(self):
'''Test the telegram.Bot sendDocument method''' '''Test the telegram.Bot sendDocument method'''
print('Testing sendDocument - File') print('Testing sendDocument - File')
message = self._bot.sendPhoto(photo='http://dummyimage.com/600x400/000/fff.gif&text=telegram', message = self._bot.sendPhoto(photo='http://dummyimage.com/600x400/000/fff.gif&text=telegram',
chat_id=12173560) chat_id=self._chat_id)
self.assertTrue(self.is_json(message.to_json())) self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(684, message.photo[0].file_size) self.assertEqual(message.photo[0].file_size, 684)
def testResendDocument(self): def testResendDocument(self):
'''Test the telegram.Bot sendDocument method''' '''Test the telegram.Bot sendDocument method'''
print('Testing sendDocument - Resend') print('Testing sendDocument - Resend')
message = self._bot.sendDocument(document='BQADAQADHAADNTwtBxZxUGKyxYbYAg', message = self._bot.sendDocument(document='BQADAQADHAADNTwtBxZxUGKyxYbYAg',
chat_id=12173560) chat_id=self._chat_id)
self.assertTrue(self.is_json(message.to_json())) self.assertTrue(self.is_json(message.to_json()))
self.assertEqual('BQADAQADHAADNTwtBxZxUGKyxYbYAg', message.document.file_id) self.assertEqual(message.document.file_id, 'BQADAQADHAADNTwtBxZxUGKyxYbYAg')
def testSendVideo(self): def testSendVideo(self):
'''Test the telegram.Bot sendVideo method''' '''Test the telegram.Bot sendVideo method'''
print('Testing sendVideo - File') print('Testing sendVideo - File')
message = self._bot.sendVideo(video=open('tests/data/telegram.mp4', 'rb'), message = self._bot.sendVideo(video=open('tests/data/telegram.mp4', 'rb'),
caption='testSendVideo', caption='testSendVideo',
chat_id=12173560) chat_id=self._chat_id)
self.assertTrue(self.is_json(message.to_json())) self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(326534, message.video.file_size) self.assertEqual(message.video.file_size, 326534)
self.assertEqual('testSendVideo', message.caption) self.assertEqual(message.caption, 'testSendVideo')
def testResendVideo(self): def testResendVideo(self):
'''Test the telegram.Bot sendVideo method''' '''Test the telegram.Bot sendVideo method'''
print('Testing sendVideo - Resend') print('Testing sendVideo - Resend')
message = self._bot.sendVideo(video='BAADAQADIgEAAvjAuQABOuTB937fPTgC', message = self._bot.sendVideo(video='BAADAQADIgEAAvjAuQABOuTB937fPTgC',
chat_id=12173560) chat_id=self._chat_id)
self.assertTrue(self.is_json(message.to_json())) self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(4, message.video.duration) self.assertEqual(message.video.duration, 4)
def testResendSticker(self): def testResendSticker(self):
'''Test the telegram.Bot sendSticker method''' '''Test the telegram.Bot sendSticker method'''
print('Testing sendSticker - Resend') print('Testing sendSticker - Resend')
message = self._bot.sendSticker(sticker='BQADAQADHAADyIsGAAFZfq1bphjqlgI', message = self._bot.sendSticker(sticker='BQADAQADHAADyIsGAAFZfq1bphjqlgI',
chat_id=12173560) chat_id=self._chat_id)
self.assertTrue(self.is_json(message.to_json())) self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(39518, message.sticker.file_size) self.assertEqual(message.sticker.file_size, 39518)
def testSendLocation(self): def testSendLocation(self):
'''Test the telegram.Bot sendLocation method''' '''Test the telegram.Bot sendLocation method'''
print('Testing sendLocation') print('Testing sendLocation')
message = self._bot.sendLocation(latitude=-23.558873, message = self._bot.sendLocation(latitude=-23.558873,
longitude=-46.659732, longitude=-46.659732,
chat_id=12173560) chat_id=self._chat_id)
self.assertTrue(self.is_json(message.to_json())) self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(-23.558873, message.location.latitude) self.assertEqual(message.location.latitude, -23.558873)
self.assertEqual(-46.659732, message.location.longitude) self.assertEqual(message.location.longitude, -46.659732)
def testSendChatAction(self): def testSendChatAction(self):
'''Test the telegram.Bot sendChatAction method''' '''Test the telegram.Bot sendChatAction method'''
print('Testing sendChatAction - ChatAction.TYPING') print('Testing sendChatAction - ChatAction.TYPING')
self._bot.sendChatAction(action=telegram.ChatAction.TYPING, self._bot.sendChatAction(action=telegram.ChatAction.TYPING,
chat_id=12173560) chat_id=self._chat_id)
def testGetUserProfilePhotos(self): def testGetUserProfilePhotos(self):
'''Test the telegram.Bot getUserProfilePhotos method''' '''Test the telegram.Bot getUserProfilePhotos method'''
print('Testing getUserProfilePhotos') print('Testing getUserProfilePhotos')
upf = self._bot.getUserProfilePhotos(user_id=12173560) upf = self._bot.getUserProfilePhotos(user_id=self._chat_id)
self.assertTrue(self.is_json(upf.to_json())) self.assertTrue(self.is_json(upf.to_json()))
self.assertEqual(6547, upf.photos[0][0].file_size) self.assertEqual(upf.photos[0][0].file_size, 6547)
unittest.main() unittest.main()