Merge pull request #187 from python-telegram-bot/flaky-tests

Flaky tests
This commit is contained in:
Jannes Höke 2016-02-28 01:02:11 +01:00
commit 4aca4d1d1f
27 changed files with 283 additions and 442 deletions

View file

@ -11,7 +11,7 @@ install:
- pip install -r requirements.txt
- pip install -r requirements-dev.txt
script:
- nosetests --with-coverage --cover-package telegram/
- nosetests -v --with-flaky --no-flaky-report --with-coverage --cover-package=telegram/
- flake8 telegram
- 'if [[ $TRAVIS_PYTHON_VERSION != 2.6 ]]; then pylint -E telegram --disable=no-name-in-module,import-error; fi'
after_success:

View file

@ -3,3 +3,4 @@ nose
pep257
pylint
unittest2
flaky

View file

@ -143,29 +143,39 @@ class Bot(TelegramObject):
decorator
"""
url, data = func(self, *args, **kwargs)
if not data.get('chat_id'):
raise TelegramError('Invalid chat_id')
if kwargs.get('reply_to_message_id'):
reply_to_message_id = kwargs.get('reply_to_message_id')
data['reply_to_message_id'] = reply_to_message_id
if kwargs.get('reply_markup'):
reply_markup = kwargs.get('reply_markup')
if isinstance(reply_markup, ReplyMarkup):
data['reply_markup'] = reply_markup.to_json()
else:
data['reply_markup'] = reply_markup
result = request.post(url, data)
if result is True:
return result
return Message.de_json(result)
return Bot._post_message(url, data, kwargs)
return decorator
@staticmethod
def _post_message(url, data, kwargs, timeout=None, network_delay=2.):
"""Posts a message to the telegram servers.
Returns:
telegram.Message
"""
if not data.get('chat_id'):
raise TelegramError('Invalid chat_id')
if kwargs.get('reply_to_message_id'):
reply_to_message_id = kwargs.get('reply_to_message_id')
data['reply_to_message_id'] = reply_to_message_id
if kwargs.get('reply_markup'):
reply_markup = kwargs.get('reply_markup')
if isinstance(reply_markup, ReplyMarkup):
data['reply_markup'] = reply_markup.to_json()
else:
data['reply_markup'] = reply_markup
result = request.post(url, data, timeout=timeout,
network_delay=network_delay)
if result is True:
return result
return Message.de_json(result)
@log
def getMe(self):
"""A simple method for testing your bot's auth token.
@ -431,12 +441,12 @@ class Bot(TelegramObject):
return url, data
@log
@message
def sendVideo(self,
chat_id,
video,
duration=None,
caption=None,
timeout=None,
**kwargs):
"""Use this method to send video files, Telegram clients support mp4
videos (other formats may be sent as telegram.Document).
@ -453,6 +463,9 @@ class Bot(TelegramObject):
caption:
Video caption (may also be used when resending videos by file_id).
[Optional]
timeout:
float. If this value is specified, use it as the definitive timeout
(in seconds) for urlopen() operations. [Optional]
reply_to_message_id:
If the message is a reply, ID of the original message. [Optional]
reply_markup:
@ -474,7 +487,7 @@ class Bot(TelegramObject):
if caption:
data['caption'] = caption
return url, data
return self._post_message(url, data, kwargs, timeout=timeout)
@log
@message

View file

@ -53,6 +53,14 @@ class PhotoSize(TelegramObject):
# Optionals
self.file_size = int(kwargs.get('file_size', 0))
def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
return (self.file_id == other.file_id and
self.width == other.width and
self.height == other.height and
self.file_size == other.file_size)
@staticmethod
def de_json(data):
"""

View file

@ -127,6 +127,7 @@ def get(url):
@_try_except_req
def post(url,
data,
timeout=None,
network_delay=2.):
"""Request an URL.
Args:
@ -134,20 +135,29 @@ def post(url,
The web location we want to retrieve.
data:
A dict of (str, unicode) key/value pairs.
timeout:
float. If this value is specified, use it as the definitive timeout (in
seconds) for urlopen() operations. [Optional]
network_delay:
Additional timeout in seconds to allow the response from Telegram to
take some time.
float. If using the timeout specified in `data` (which is a timeout for
the Telegram servers operation), then `network_delay` as an extra delay
(in seconds) to compensate for network latency.
default: 2 [Optional]
Notes:
If neither `timeout` nor `data['timeout']` is specified. The underlying
defaults are used.
Returns:
A JSON object.
"""
# Add time to the timeout of urlopen to allow data to be transferred over
# the network.
if 'timeout' in data:
timeout = data['timeout'] + network_delay
else:
timeout = None
"""
urlopen_kwargs = {}
if timeout is not None:
urlopen_kwargs['timeout'] = timeout
elif 'timeout' in data:
urlopen_kwargs['timeout'] = data['timeout'] + network_delay
if InputFile.is_inputfile(data):
data = InputFile(data)
@ -160,7 +170,7 @@ def post(url,
data=data.encode(),
headers={'Content-Type': 'application/json'})
result = urlopen(request, timeout=timeout).read()
result = urlopen(request, **urlopen_kwargs).read()
return _parse(result)

View file

@ -21,6 +21,11 @@
import os
import sys
import signal
import traceback
from nose.tools import make_decorator
sys.path.append('.')
import json
@ -54,3 +59,32 @@ class BaseTest(object):
return True
return False
class TestTimedOut(AssertionError):
def __init__(self, time_limit, frame):
super(TestTimedOut, self).__init__('time_limit={0}'.format(time_limit))
self.time_limit = time_limit
self.frame = frame
def timeout(time_limit):
def decorator(func):
def timed_out(_signum, frame):
raise TestTimedOut(time_limit, frame)
def newfunc(*args, **kwargs):
orig_handler = signal.signal(signal.SIGALRM, timed_out)
signal.alarm(time_limit)
try:
rc = func(*args, **kwargs)
finally:
signal.alarm(0)
signal.signal(signal.SIGALRM, orig_handler)
return rc
newfunc = make_decorator(func)(newfunc)
return newfunc
return decorator

View file

@ -22,10 +22,12 @@
import os
import unittest
import sys
from flaky import flaky
sys.path.append('.')
import telegram
from tests.base import BaseTest
from tests.base import BaseTest, timeout
class AudioTest(BaseTest, unittest.TestCase):
@ -50,10 +52,9 @@ class AudioTest(BaseTest, unittest.TestCase):
'file_size': self.file_size
}
@flaky(3, 1)
@timeout(10)
def test_send_audio_required_args_only(self):
"""Test telegram.Bot sendAudio method"""
print('Testing bot.sendAudio - With required arguments only')
message = self._bot.sendAudio(self._chat_id,
self.audio_file)
@ -67,10 +68,9 @@ class AudioTest(BaseTest, unittest.TestCase):
self.assertEqual(audio.mime_type, self.mime_type)
self.assertEqual(audio.file_size, self.file_size)
@flaky(3, 1)
@timeout(10)
def test_send_audio_all_args(self):
"""Test telegram.Bot sendAudio method"""
print('Testing bot.sendAudio - With all arguments')
message = self._bot.sendAudio(self._chat_id,
self.audio_file,
duration=self.duration,
@ -89,10 +89,9 @@ class AudioTest(BaseTest, unittest.TestCase):
self.assertEqual(audio.mime_type, self.mime_type)
self.assertEqual(audio.file_size, self.file_size)
@flaky(3, 1)
@timeout(10)
def test_send_audio_mp3_file(self):
"""Test telegram.Bot sendAudio method"""
print('Testing bot.sendAudio - MP3 File')
message = self._bot.sendAudio(chat_id=self._chat_id,
audio=self.audio_file,
duration=self.duration,
@ -109,10 +108,9 @@ class AudioTest(BaseTest, unittest.TestCase):
self.assertEqual(audio.mime_type, self.mime_type)
self.assertEqual(audio.file_size, self.file_size)
@flaky(3, 1)
@timeout(10)
def test_send_audio_mp3_file_custom_filename(self):
"""Test telegram.Bot sendAudio method"""
print('Testing bot.sendAudio - MP3 File with custom filename')
message = self._bot.sendAudio(chat_id=self._chat_id,
audio=self.audio_file,
duration=self.duration,
@ -130,10 +128,9 @@ class AudioTest(BaseTest, unittest.TestCase):
self.assertEqual(audio.mime_type, self.mime_type)
self.assertEqual(audio.file_size, self.file_size)
@flaky(3, 1)
@timeout(10)
def test_send_audio_mp3_url_file(self):
"""Test telegram.Bot sendAudio method"""
print('Testing bot.sendAudio - MP3 File by URL')
message = self._bot.sendAudio(chat_id=self._chat_id,
audio=self.audio_file_url,
duration=self.duration,
@ -150,10 +147,9 @@ class AudioTest(BaseTest, unittest.TestCase):
self.assertEqual(audio.mime_type, self.mime_type)
self.assertEqual(audio.file_size, self.file_size)
@flaky(3, 1)
@timeout(10)
def test_send_audio_resend(self):
"""Test telegram.Bot sendAudio method"""
print('Testing bot.sendAudio - Resend by file_id')
message = self._bot.sendAudio(chat_id=self._chat_id,
audio=self.audio_file_id,
duration=self.duration,
@ -169,9 +165,6 @@ class AudioTest(BaseTest, unittest.TestCase):
self.assertEqual(audio.mime_type, self.mime_type)
def test_audio_de_json(self):
"""Test Audio.de_json() method"""
print('Testing Audio.de_json()')
audio = telegram.Audio.de_json(self.json_dict)
self.assertEqual(audio.file_id, self.audio_file_id)
@ -182,17 +175,11 @@ class AudioTest(BaseTest, unittest.TestCase):
self.assertEqual(audio.file_size, self.file_size)
def test_audio_to_json(self):
"""Test Audio.to_json() method"""
print('Testing Audio.to_json()')
audio = telegram.Audio.de_json(self.json_dict)
self.assertTrue(self.is_json(audio.to_json()))
def test_audio_to_dict(self):
"""Test Audio.to_dict() method"""
print('Testing Audio.to_dict()')
audio = telegram.Audio.de_json(self.json_dict)
self.assertTrue(self.is_dict(audio.to_dict()))
@ -203,9 +190,9 @@ class AudioTest(BaseTest, unittest.TestCase):
self.assertEqual(audio['mime_type'], self.mime_type)
self.assertEqual(audio['file_size'], self.file_size)
@flaky(3, 1)
@timeout(10)
def test_error_send_audio_empty_file(self):
print('Testing bot.sendAudio - Null file')
json_dict = self.json_dict
del(json_dict['file_id'])
@ -215,9 +202,9 @@ class AudioTest(BaseTest, unittest.TestCase):
lambda: self._bot.sendAudio(chat_id=self._chat_id,
**json_dict))
@flaky(3, 1)
@timeout(10)
def test_error_send_audio_empty_file_id(self):
print('Testing bot.sendAudio - Empty file_id')
json_dict = self.json_dict
del(json_dict['file_id'])
@ -227,9 +214,9 @@ class AudioTest(BaseTest, unittest.TestCase):
lambda: self._bot.sendAudio(chat_id=self._chat_id,
**json_dict))
@flaky(3, 1)
@timeout(10)
def test_error_audio_without_required_args(self):
print('Testing bot.sendAudio - Without required arguments')
json_dict = self.json_dict
del(json_dict['file_id'])

View file

@ -23,6 +23,7 @@
import os
from datetime import datetime
import sys
from flaky import flaky
if sys.version_info[0:2] == (2, 6):
import unittest2 as unittest
@ -32,15 +33,15 @@ else:
sys.path.append('.')
import telegram
from tests.base import BaseTest
from tests.base import BaseTest, timeout
class BotTest(BaseTest, unittest.TestCase):
"""This object represents Tests for Telegram Bot."""
@flaky(3, 1)
@timeout(10)
def testGetMe(self):
'''Test the telegram.Bot getMe method'''
print('Testing getMe')
bot = self._bot.getMe()
self.assertTrue(self.is_json(bot.to_json()))
@ -50,9 +51,9 @@ class BotTest(BaseTest, unittest.TestCase):
self.assertEqual(bot.username, 'PythonTelegramBot')
self.assertEqual(bot.name, '@PythonTelegramBot')
@flaky(3, 1)
@timeout(10)
def testSendMessage(self):
'''Test the telegram.Bot sendMessage method'''
print('Testing sendMessage')
message = self._bot.sendMessage(chat_id=self._chat_id,
text='Моё судно на воздушной подушке полно угрей')
@ -60,18 +61,18 @@ class BotTest(BaseTest, unittest.TestCase):
self.assertEqual(message.text, u'Моё судно на воздушной подушке полно угрей')
self.assertTrue(isinstance(message.date, datetime))
@flaky(3, 1)
@timeout(10)
def testGetUpdates(self):
'''Test the telegram.Bot getUpdates method'''
print('Testing getUpdates')
updates = self._bot.getUpdates()
if updates:
self.assertTrue(self.is_json(updates[0].to_json()))
self.assertTrue(isinstance(updates[0], telegram.Update))
@flaky(3, 1)
@timeout(10)
def testForwardMessage(self):
'''Test the telegram.Bot forwardMessage method'''
print('Testing forwardMessage')
message = self._bot.forwardMessage(chat_id=self._chat_id,
from_chat_id=self._chat_id,
message_id=2398)
@ -81,9 +82,9 @@ class BotTest(BaseTest, unittest.TestCase):
self.assertEqual(message.forward_from.username, 'leandrotoledo')
self.assertTrue(isinstance(message.forward_date, datetime))
@flaky(3, 1)
@timeout(10)
def testSendPhoto(self):
'''Test the telegram.Bot sendPhoto method'''
print('Testing sendPhoto - File')
message = self._bot.sendPhoto(photo=open('tests/data/telegram.png', 'rb'),
caption='testSendPhoto',
chat_id=self._chat_id)
@ -92,59 +93,57 @@ class BotTest(BaseTest, unittest.TestCase):
self.assertEqual(message.photo[0].file_size, 1451)
self.assertEqual(message.caption, 'testSendPhoto')
@flaky(3, 1)
@timeout(10)
def testResendPhoto(self):
'''Test the telegram.Bot sendPhoto method'''
print('Testing sendPhoto - Resend')
message = self._bot.sendPhoto(photo='AgADAQADyKcxGx8j9Qdp6d-gpUsw4Gja1i8ABEVJsVqQk8LfJ3wAAgI',
chat_id=self._chat_id)
self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(message.photo[0].file_id, 'AgADAQADyKcxGx8j9Qdp6d-gpUsw4Gja1i8ABEVJsVqQk8LfJ3wAAgI')
@flaky(3, 1)
@timeout(10)
def testSendJPGURLPhoto(self):
'''Test the telegram.Bot sendPhoto method'''
print('Testing testSendJPGURLPhoto - URL')
message = self._bot.sendPhoto(photo='http://dummyimage.com/600x400/000/fff.jpg&text=telegram',
chat_id=self._chat_id)
self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(message.photo[0].file_size, 822)
@flaky(3, 1)
@timeout(10)
def testSendPNGURLPhoto(self):
'''Test the telegram.Bot sendPhoto method'''
print('Testing testSendPNGURLPhoto - URL')
message = self._bot.sendPhoto(photo='http://dummyimage.com/600x400/000/fff.png&text=telegram',
chat_id=self._chat_id)
self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(message.photo[0].file_size, 684)
@flaky(3, 1)
@timeout(10)
def testSendGIFURLPhoto(self):
'''Test the telegram.Bot sendPhoto method'''
print('Testing testSendGIFURLPhoto - URL')
message = self._bot.sendPhoto(photo='http://dummyimage.com/600x400/000/fff.gif&text=telegram',
chat_id=self._chat_id)
self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(message.photo[0].file_size, 684)
@flaky(3, 1)
@timeout(10)
def testSendChatAction(self):
'''Test the telegram.Bot sendChatAction method'''
print('Testing sendChatAction - ChatAction.TYPING')
self._bot.sendChatAction(action=telegram.ChatAction.TYPING,
chat_id=self._chat_id)
@flaky(3, 1)
@timeout(10)
def testGetUserProfilePhotos(self):
'''Test the telegram.Bot getUserProfilePhotos method'''
print('Testing getUserProfilePhotos')
upf = self._bot.getUserProfilePhotos(user_id=self._chat_id)
self.assertTrue(self.is_json(upf.to_json()))
self.assertEqual(upf.photos[0][0].file_size, 12421)
def _test_invalid_token(self, token):
print('Testing invalid token: {0}'.format(token))
self.assertRaisesRegexp(telegram.error.InvalidToken, 'Invalid token', telegram.Bot, token)
def testInvalidToken1(self):
@ -157,13 +156,11 @@ class BotTest(BaseTest, unittest.TestCase):
self._test_invalid_token('12:')
def testUnauthToken(self):
print('Testing unauthorized token')
with self.assertRaisesRegexp(telegram.error.Unauthorized, 'Unauthorized'):
bot = telegram.Bot('1234:abcd1234')
bot.getMe()
def testInvalidSrvResp(self):
print('Testing invalid server response')
with self.assertRaisesRegexp(telegram.TelegramError, 'Invalid server response'):
# bypass the valid token check
bot_cls = type('bot_cls', (telegram.Bot, ), {'_valid_token': lambda self, token: token})

View file

@ -5,6 +5,8 @@
import os
import unittest
import sys
from flaky import flaky
sys.path.append('.')
from telegram.utils.botan import Botan
@ -21,22 +23,19 @@ class MessageMock(object):
return "{}"
@flaky(3, 1)
class BotanTest(BaseTest, unittest.TestCase):
"""This object represents Tests for Botan analytics integration."""
token = os.environ.get('BOTAN_TOKEN')
def test_track(self):
"""Test sending event to botan"""
print('Test sending event to botan')
botan = Botan(self.token)
message = MessageMock(self._chat_id)
result = botan.track(message, 'named event')
self.assertTrue(result)
def test_track_fail(self):
"""Test fail when sending event to botan"""
print('Test fail when sending event to botan')
botan = Botan(self.token)
botan.url_template = 'https://api.botan.io/traccc?token={token}&uid={uid}&name={name}'
message = MessageMock(self._chat_id)
@ -44,8 +43,6 @@ class BotanTest(BaseTest, unittest.TestCase):
self.assertFalse(result)
def test_wrong_message(self):
"""Test sending wrong message"""
print('Test sending wrong message')
botan = Botan(self.token)
message = MessageMock(self._chat_id)
message = delattr(message, 'chat_id')
@ -53,8 +50,6 @@ class BotanTest(BaseTest, unittest.TestCase):
self.assertFalse(result)
def test_wrong_endpoint(self):
"""Test wrong endpoint"""
print('Test wrong endpoint')
botan = Botan(self.token)
botan.url_template = 'https://api.botaaaaan.io/traccc?token={token}&uid={uid}&name={name}'
message = MessageMock(self._chat_id)

View file

@ -43,17 +43,11 @@ class ChatTest(BaseTest, unittest.TestCase):
}
def test_group_chat_de_json_empty_json(self):
"""Test Chat.de_json() method"""
print('Testing Chat.de_json() - Empty JSON')
group_chat = telegram.Chat.de_json({})
self.assertEqual(group_chat, None)
def test_group_chat_de_json(self):
"""Test Chat.de_json() method"""
print('Testing Chat.de_json()')
group_chat = telegram.Chat.de_json(self.json_dict)
self.assertEqual(group_chat.id, self.id)
@ -61,17 +55,11 @@ class ChatTest(BaseTest, unittest.TestCase):
self.assertEqual(group_chat.type, self.type)
def test_group_chat_to_json(self):
"""Test Chat.to_json() method"""
print('Testing Chat.to_json()')
group_chat = telegram.Chat.de_json(self.json_dict)
self.assertTrue(self.is_json(group_chat.to_json()))
def test_group_chat_to_dict(self):
"""Test Chat.to_dict() method"""
print('Testing Chat.to_dict()')
group_chat = telegram.Chat.de_json(self.json_dict)
self.assertTrue(self.is_dict(group_chat.to_dict()))

View file

@ -45,9 +45,6 @@ class ContactTest(BaseTest, unittest.TestCase):
}
def test_contact_de_json(self):
"""Test Contact.de_json() method"""
print('Testing Contact.de_json()')
contact = telegram.Contact.de_json(self.json_dict)
self.assertEqual(contact.phone_number, self.phone_number)
@ -56,17 +53,11 @@ class ContactTest(BaseTest, unittest.TestCase):
self.assertEqual(contact.user_id, self.user_id)
def test_contact_to_json(self):
"""Test Contact.to_json() method"""
print('Testing Contact.to_json()')
contact = telegram.Contact.de_json(self.json_dict)
self.assertTrue(self.is_json(contact.to_json()))
def test_contact_to_dict(self):
"""Test Contact.to_dict() method"""
print('Testing Contact.to_dict()')
contact = telegram.Contact.de_json(self.json_dict)
self.assertTrue(self.is_dict(contact.to_dict()))

View file

@ -22,10 +22,12 @@
import os
import unittest
import sys
from flaky import flaky
sys.path.append('.')
import telegram
from tests.base import BaseTest
from tests.base import BaseTest, timeout
class DocumentTest(BaseTest, unittest.TestCase):
@ -51,10 +53,9 @@ class DocumentTest(BaseTest, unittest.TestCase):
'file_size': self.file_size
}
@flaky(3, 1)
@timeout(10)
def test_send_document_png_file(self):
"""Test telegram.Bot sendDocument method"""
print('Testing bot.sendDocument - PNG File')
message = self._bot.sendDocument(self._chat_id,
self.document_file)
@ -67,10 +68,9 @@ class DocumentTest(BaseTest, unittest.TestCase):
self.assertEqual(document.mime_type, self.mime_type)
self.assertEqual(document.file_size, self.file_size)
@flaky(3, 1)
@timeout(10)
def test_send_document_png_file_with_custom_file_name(self):
"""Test telegram.Bot sendDocument method"""
print('Testing bot.sendDocument - PNG File with custom filename')
message = self._bot.sendDocument(self._chat_id,
self.document_file,
filename='telegram_custom.png')
@ -84,10 +84,9 @@ class DocumentTest(BaseTest, unittest.TestCase):
self.assertEqual(document.mime_type, self.mime_type)
self.assertEqual(document.file_size, self.file_size)
@flaky(3, 1)
@timeout(10)
def test_send_document_url_gif_file(self):
"""Test telegram.Bot sendDocument method"""
print('Testing bot.sendDocument - GIF File by URL')
message = self._bot.sendDocument(self._chat_id,
self.document_file_url)
@ -100,10 +99,9 @@ class DocumentTest(BaseTest, unittest.TestCase):
self.assertEqual(document.mime_type, 'image/gif')
self.assertEqual(document.file_size, 3878)
@flaky(3, 1)
@timeout(10)
def test_send_document_resend(self):
"""Test telegram.Bot sendDocument method"""
print('Testing bot.sendDocument - Resend by file_id')
message = self._bot.sendDocument(chat_id=self._chat_id,
document=self.document_file_id)
@ -115,9 +113,6 @@ class DocumentTest(BaseTest, unittest.TestCase):
self.assertEqual(document.mime_type, self.mime_type)
def test_document_de_json(self):
"""Test Document.de_json() method"""
print('Testing Document.de_json()')
document = telegram.Document.de_json(self.json_dict)
self.assertEqual(document.file_id, self.document_file_id)
@ -127,17 +122,11 @@ class DocumentTest(BaseTest, unittest.TestCase):
self.assertEqual(document.file_size, self.file_size)
def test_document_to_json(self):
"""Test Document.to_json() method"""
print('Testing Document.to_json()')
document = telegram.Document.de_json(self.json_dict)
self.assertTrue(self.is_json(document.to_json()))
def test_document_to_dict(self):
"""Test Document.to_dict() method"""
print('Testing Document.to_dict()')
document = telegram.Document.de_json(self.json_dict)
self.assertTrue(self.is_dict(document.to_dict()))
@ -147,9 +136,9 @@ class DocumentTest(BaseTest, unittest.TestCase):
self.assertEqual(document['mime_type'], self.mime_type)
self.assertEqual(document['file_size'], self.file_size)
@flaky(3, 1)
@timeout(10)
def test_error_send_document_empty_file(self):
print('Testing bot.sendDocument - Null file')
json_dict = self.json_dict
del(json_dict['file_id'])
@ -159,9 +148,9 @@ class DocumentTest(BaseTest, unittest.TestCase):
lambda: self._bot.sendDocument(chat_id=self._chat_id,
**json_dict))
@flaky(3, 1)
@timeout(10)
def test_error_send_document_empty_file_id(self):
print('Testing bot.sendDocument - Empty file_id')
json_dict = self.json_dict
del(json_dict['file_id'])
@ -171,9 +160,9 @@ class DocumentTest(BaseTest, unittest.TestCase):
lambda: self._bot.sendDocument(chat_id=self._chat_id,
**json_dict))
@flaky(3, 1)
@timeout(10)
def test_error_document_without_required_args(self):
print('Testing bot.sendDocument - Without required arguments')
json_dict = self.json_dict
del(json_dict['file_id'])

View file

@ -33,9 +33,6 @@ class EmojiTest(BaseTest, unittest.TestCase):
"""This object represents Tests for Telegram Emoji."""
def test_emoji(self):
"""Test Emoji class"""
print('Testing Emoji class')
for attr in dir(Emoji):
if attr[0] != '_': # TODO: dirty way to filter out functions
self.assertTrue(type(getattr(Emoji, attr)) is str)

View file

@ -46,9 +46,6 @@ class FileTest(BaseTest, unittest.TestCase):
}
def test_get_and_download_file_audio(self):
"""Test telegram.Bot getFile method - Audio"""
print('Testing bot.getFile - With Audio.file_id')
newFile = self._bot.getFile(self.audio_file_id)
self.assertEqual(newFile.file_size, 28232)
@ -60,9 +57,6 @@ class FileTest(BaseTest, unittest.TestCase):
self.assertTrue(os.path.isfile('telegram.mp3'))
def test_get_and_download_file_document(self):
"""Test telegram.Bot getFile method - Document"""
print('Testing bot.getFile - With Document.file_id')
newFile = self._bot.getFile(self.document_file_id)
self.assertEqual(newFile.file_size, 12948)
@ -74,9 +68,6 @@ class FileTest(BaseTest, unittest.TestCase):
self.assertTrue(os.path.isfile('telegram.png'))
def test_get_and_download_file_sticker(self):
"""Test telegram.Bot getFile method - Sticker"""
print('Testing bot.getFile - With Sticker.file_id')
newFile = self._bot.getFile(self.sticker_file_id)
self.assertEqual(newFile.file_size, 39518)
@ -88,9 +79,6 @@ class FileTest(BaseTest, unittest.TestCase):
self.assertTrue(os.path.isfile('telegram.webp'))
def test_get_and_download_file_video(self):
"""Test telegram.Bot getFile method - Video"""
print('Testing bot.getFile - With Video.file_id')
newFile = self._bot.getFile(self.video_file_id)
self.assertEqual(newFile.file_size, 326534)
@ -102,9 +90,6 @@ class FileTest(BaseTest, unittest.TestCase):
self.assertTrue(os.path.isfile('telegram.mp4'))
def test_get_and_download_file_voice(self):
"""Test telegram.Bot getFile method - Voice"""
print('Testing bot.getFile - With Voice.file_id')
newFile = self._bot.getFile(self.voice_file_id)
self.assertEqual(newFile.file_size, 9199)
@ -116,9 +101,6 @@ class FileTest(BaseTest, unittest.TestCase):
self.assertTrue(os.path.isfile('telegram.ogg'))
def test_file_de_json(self):
"""Test File.de_json() method"""
print('Testing File.de_json()')
newFile = telegram.File.de_json(self.json_dict)
self.assertEqual(newFile.file_id, self.json_dict['file_id'])
@ -126,17 +108,11 @@ class FileTest(BaseTest, unittest.TestCase):
self.assertEqual(newFile.file_size, self.json_dict['file_size'])
def test_file_to_json(self):
"""Test File.to_json() method"""
print('Testing File.to_json()')
newFile = telegram.File.de_json(self.json_dict)
self.assertTrue(self.is_json(newFile.to_json()))
def test_file_to_dict(self):
"""Test File.to_dict() method"""
print('Testing File.to_dict()')
newFile = telegram.File.de_json(self.json_dict)
self.assertTrue(self.is_dict(newFile.to_dict()))
@ -145,10 +121,7 @@ class FileTest(BaseTest, unittest.TestCase):
self.assertEqual(newFile['file_size'], self.json_dict['file_size'])
def test_error_get_empty_file_id(self):
print('Testing bot.getFile - Null file_id')
json_dict = self.json_dict
json_dict['file_id'] = ''
del(json_dict['file_path'])
del(json_dict['file_size'])
@ -157,8 +130,6 @@ class FileTest(BaseTest, unittest.TestCase):
lambda: self._bot.getFile(**json_dict))
def test_error_file_without_required_args(self):
print('Testing bot.getFile - Without required arguments')
json_dict = self.json_dict
del(json_dict['file_id'])

View file

@ -41,9 +41,6 @@ class ForceReplyTest(BaseTest, unittest.TestCase):
}
def test_send_message_with_force_reply(self):
"""Test telegram.Bot sendMessage method with ForceReply"""
print('Testing bot.sendMessage - with ForceReply')
message = self._bot.sendMessage(self._chat_id,
'Моё судно на воздушной подушке полно угрей',
reply_markup=telegram.ForceReply.de_json(self.json_dict))
@ -52,26 +49,17 @@ class ForceReplyTest(BaseTest, unittest.TestCase):
self.assertEqual(message.text, u'Моё судно на воздушной подушке полно угрей')
def test_force_reply_de_json(self):
"""Test ForceReply.de_json() method"""
print('Testing ForceReply.de_json()')
force_reply = telegram.ForceReply.de_json(self.json_dict)
self.assertEqual(force_reply.force_reply, self.force_reply)
self.assertEqual(force_reply.selective, self.selective)
def test_force_reply_to_json(self):
"""Test ForceReply.to_json() method"""
print('Testing ForceReply.to_json()')
force_reply = telegram.ForceReply.de_json(self.json_dict)
self.assertTrue(self.is_json(force_reply.to_json()))
def test_force_reply_to_dict(self):
"""Test ForceReply.to_dict() method"""
print('Testing ForceReply.to_dict()')
force_reply = telegram.ForceReply.de_json(self.json_dict)
self.assertEqual(force_reply['force_reply'], self.force_reply)

View file

@ -72,19 +72,16 @@ class JobQueueTest(BaseTest, unittest.TestCase):
raise Exception("Test Error")
def test_basic(self):
print('Testing basic job queue function')
self.jq.put(self.job1, 0.1)
sleep(1.5)
self.assertGreaterEqual(self.result, 10)
def test_noRepeat(self):
print('Testing job queue without repeat')
self.jq.put(self.job1, 0.1, repeat=False)
sleep(0.5)
self.assertEqual(1, self.result)
def test_nextT(self):
print('Testing job queue with a set next_t value')
self.jq.put(self.job1, 0.1, next_t=0.5)
sleep(0.45)
self.assertEqual(0, self.result)
@ -92,7 +89,6 @@ class JobQueueTest(BaseTest, unittest.TestCase):
self.assertEqual(1, self.result)
def test_multiple(self):
print('Testing job queue with multiple jobs')
self.jq.put(self.job1, 0.1, repeat=False)
self.jq.put(self.job1, 0.2, repeat=False)
self.jq.put(self.job1, 0.4)
@ -100,7 +96,6 @@ class JobQueueTest(BaseTest, unittest.TestCase):
self.assertEqual(4, self.result)
def test_error(self):
print('Testing job queue starting twice with an erroneous job')
self.jq.put(self.job2, 0.1)
self.jq.put(self.job1, 0.2)
self.jq.start()
@ -108,7 +103,6 @@ class JobQueueTest(BaseTest, unittest.TestCase):
self.assertEqual(1, self.result)
def test_inUpdater(self):
print('Testing job queue created by updater')
u = Updater(bot="MockBot", job_queue_tick_interval=0.005)
u.job_queue.put(self.job1, 0.1)
sleep(0.15)

View file

@ -41,9 +41,6 @@ class LocationTest(BaseTest, unittest.TestCase):
}
def test_send_location_implicit_args(self):
"""Test telegram.Bot sendLocation method"""
print('Testing bot.sendLocation - Implicit arguments')
message = self._bot.sendLocation(self._chat_id,
self.latitude,
self.longitude)
@ -54,9 +51,6 @@ class LocationTest(BaseTest, unittest.TestCase):
self.assertEqual(location.longitude, self.longitude)
def test_send_location_explicit_args(self):
"""Test telegram.Bot sendLocation method"""
print('Testing bot.sendLocation - Explicit arguments')
message = self._bot.sendLocation(chat_id=self._chat_id,
latitude=self.latitude,
longitude=self.longitude)
@ -67,34 +61,23 @@ class LocationTest(BaseTest, unittest.TestCase):
self.assertEqual(location.longitude, self.longitude)
def test_location_de_json(self):
"""Test Location.de_json() method"""
print('Testing Location.de_json()')
location = telegram.Location.de_json(self.json_dict)
self.assertEqual(location.latitude, self.latitude)
self.assertEqual(location.longitude, self.longitude)
def test_location_to_json(self):
"""Test Location.to_json() method"""
print('Testing Location.to_json()')
location = telegram.Location.de_json(self.json_dict)
self.assertTrue(self.is_json(location.to_json()))
def test_location_to_dict(self):
"""Test Location.to_dict() method"""
print('Testing Location.to_dict()')
location = telegram.Location.de_json(self.json_dict)
self.assertEqual(location['latitude'], self.latitude)
self.assertEqual(location['longitude'], self.longitude)
def test_error_send_location_empty_args(self):
print('Testing bot.sendLocation - Empty arguments')
json_dict = self.json_dict
json_dict['latitude'] = ''
@ -105,8 +88,6 @@ class LocationTest(BaseTest, unittest.TestCase):
**json_dict))
def test_error_location_without_required_args(self):
print('Testing bot.sendLocation - Without required arguments')
json_dict = self.json_dict
del(json_dict['latitude'])

View file

@ -36,8 +36,6 @@ class ParseMode(BaseTest, unittest.TestCase):
self.formatted_text_formatted = u'bold italic link.'
def test_send_message_with_parse_mode_markdown(self):
'''Test the telegram.Bot sendMessage method with markdown parse mode'''
print('Testing sendMessage - with markdown parsemode')
message = self._bot.sendMessage(chat_id=self._chat_id,
text=self.markdown_text,
parse_mode=telegram.ParseMode.MARKDOWN)
@ -46,8 +44,6 @@ class ParseMode(BaseTest, unittest.TestCase):
self.assertEqual(message.text, self.formatted_text_formatted)
def test_send_message_with_parse_mode_html(self):
'''Test the telegram.Bot sendMessage method with html parse mode'''
print('Testing sendMessage - with html parse mode')
message = self._bot.sendMessage(chat_id=self._chat_id,
text=self.html_text,
parse_mode=telegram.ParseMode.HTML)
@ -56,4 +52,4 @@ class ParseMode(BaseTest, unittest.TestCase):
self.assertEqual(message.text, self.formatted_text_formatted)
if __name__ == '__main__':
unittest.main()
unittest.main()

View file

@ -22,10 +22,12 @@
import os
import unittest
import sys
from flaky import flaky
sys.path.append('.')
import telegram
from tests.base import BaseTest
from tests.base import BaseTest, timeout
class PhotoTest(BaseTest, unittest.TestCase):
@ -53,10 +55,9 @@ class PhotoTest(BaseTest, unittest.TestCase):
'file_size': self.file_size
}
@flaky(3, 1)
@timeout(10)
def test_sendphotoo_all_args(self):
"""Test telegram.Bot sendAudio method"""
print('Testing bot.sendPhoto - With all arguments')
message = self._bot.sendPhoto(self._chat_id,
self.photo_file,
caption=self.caption)
@ -79,10 +80,9 @@ class PhotoTest(BaseTest, unittest.TestCase):
self.assertEqual(message.caption, self.caption)
@flaky(3, 1)
@timeout(10)
def test_send_photo_jpg_file(self):
"""Test telegram.Bot sendPhoto method"""
print('Testing bot.sendPhoto - JPG File')
message = self._bot.sendPhoto(self._chat_id,
self.photo_file)
@ -102,10 +102,9 @@ class PhotoTest(BaseTest, unittest.TestCase):
self.assertEqual(photo.height, self.height)
self.assertEqual(photo.file_size, self.file_size)
@flaky(3, 1)
@timeout(10)
def test_send_photo_url_jpg_file(self):
"""Test telegram.Bot sendPhoto method"""
print('Testing bot.sendPhoto - JPG File by URL')
message = self._bot.sendPhoto(self._chat_id,
self.photo_file_url)
@ -125,10 +124,9 @@ class PhotoTest(BaseTest, unittest.TestCase):
self.assertEqual(photo.height, self.height)
self.assertEqual(photo.file_size, self.file_size)
@flaky(3, 1)
@timeout(10)
def test_send_photo_resend(self):
"""Test telegram.Bot sendPhoto method"""
print('Testing bot.sendPhoto - Resend by file_id')
message = self._bot.sendPhoto(chat_id=self._chat_id,
photo=self.photo_file_id)
@ -145,9 +143,6 @@ class PhotoTest(BaseTest, unittest.TestCase):
self.assertEqual(photo.height, self.height)
def test_photo_de_json(self):
"""Test Photo.de_json() method"""
print('Testing Photo.de_json()')
photo = telegram.PhotoSize.de_json(self.json_dict)
self.assertEqual(photo.file_id, self.photo_file_id)
@ -157,17 +152,11 @@ class PhotoTest(BaseTest, unittest.TestCase):
self.assertEqual(photo.file_size, self.file_size)
def test_photo_to_json(self):
"""Test Photo.to_json() method"""
print('Testing Photo.to_json()')
photo = telegram.PhotoSize.de_json(self.json_dict)
self.assertTrue(self.is_json(photo.to_json()))
def test_photo_to_dict(self):
"""Test Photo.to_dict() method"""
print('Testing Photo.to_dict()')
photo = telegram.PhotoSize.de_json(self.json_dict)
self.assertTrue(self.is_dict(photo.to_dict()))
@ -177,9 +166,9 @@ class PhotoTest(BaseTest, unittest.TestCase):
self.assertEqual(photo['height'], self.height)
self.assertEqual(photo['file_size'], self.file_size)
@flaky(3, 1)
@timeout(10)
def test_error_send_photo_empty_file(self):
print('Testing bot.sendPhoto - Null file')
json_dict = self.json_dict
del(json_dict['file_id'])
@ -189,9 +178,9 @@ class PhotoTest(BaseTest, unittest.TestCase):
lambda: self._bot.sendPhoto(chat_id=self._chat_id,
**json_dict))
@flaky(3, 1)
@timeout(10)
def test_error_send_photo_empty_file_id(self):
print('Testing bot.sendPhoto - Empty file_id')
json_dict = self.json_dict
del(json_dict['file_id'])
@ -201,9 +190,9 @@ class PhotoTest(BaseTest, unittest.TestCase):
lambda: self._bot.sendPhoto(chat_id=self._chat_id,
**json_dict))
@flaky(3, 1)
@timeout(10)
def test_error_photo_without_required_args(self):
print('Testing bot.sendPhoto - Without required arguments')
json_dict = self.json_dict
del(json_dict['file_id'])

View file

@ -41,9 +41,6 @@ class ReplyKeyboardHideTest(BaseTest, unittest.TestCase):
}
def test_send_message_with_reply_keyboard_hide(self):
"""Test telegram.Bot sendMessage method with ReplyKeyboardHide"""
print('Testing bot.sendMessage - with ReplyKeyboardHide')
message = self._bot.sendMessage(self._chat_id,
'Моё судно на воздушной подушке полно угрей',
reply_markup=telegram.ReplyKeyboardHide.de_json(self.json_dict))
@ -52,26 +49,17 @@ class ReplyKeyboardHideTest(BaseTest, unittest.TestCase):
self.assertEqual(message.text, u'Моё судно на воздушной подушке полно угрей')
def test_reply_keyboard_hide_de_json(self):
"""Test ReplyKeboardHide.de_json() method"""
print('Testing ReplyKeyboardHide.de_json()')
reply_keyboard_hide = telegram.ReplyKeyboardHide.de_json(self.json_dict)
self.assertEqual(reply_keyboard_hide.hide_keyboard, self.hide_keyboard)
self.assertEqual(reply_keyboard_hide.selective, self.selective)
def test_reply_keyboard_hide_to_json(self):
"""Test ReplyKeyboardHide.to_json() method"""
print('Testing ReplyKeyboardHide.to_json()')
reply_keyboard_hide = telegram.ReplyKeyboardHide.de_json(self.json_dict)
self.assertTrue(self.is_json(reply_keyboard_hide.to_json()))
def test_reply_keyboard_hide_to_dict(self):
"""Test ReplyKeyboardHide.to_dict() method"""
print('Testing ReplyKeyboardHide.to_dict()')
reply_keyboard_hide = telegram.ReplyKeyboardHide.de_json(self.json_dict)
self.assertEqual(reply_keyboard_hide['hide_keyboard'], self.hide_keyboard)

View file

@ -45,9 +45,6 @@ class ReplyKeyboardMarkupTest(BaseTest, unittest.TestCase):
}
def test_send_message_with_reply_keyboard_markup(self):
"""Test telegram.Bot sendMessage method with ReplyKeyboardMarkup"""
print('Testing bot.sendMessage - with ReplyKeyboardMarkup')
message = self._bot.sendMessage(self._chat_id,
'Моё судно на воздушной подушке полно угрей',
reply_markup=telegram.ReplyKeyboardMarkup.de_json(self.json_dict))
@ -56,9 +53,6 @@ class ReplyKeyboardMarkupTest(BaseTest, unittest.TestCase):
self.assertEqual(message.text, u'Моё судно на воздушной подушке полно угрей')
def test_reply_keyboard_markup_de_json(self):
"""Test ReplyKeboardMarkup.de_json() method"""
print('Testing ReplyKeyboardMarkup.de_json()')
reply_keyboard_markup = telegram.ReplyKeyboardMarkup.de_json(self.json_dict)
self.assertEqual(reply_keyboard_markup.keyboard, self.keyboard)
@ -67,17 +61,11 @@ class ReplyKeyboardMarkupTest(BaseTest, unittest.TestCase):
self.assertEqual(reply_keyboard_markup.selective, self.selective)
def test_reply_keyboard_markup_to_json(self):
"""Test ReplyKeyboardMarkup.to_json() method"""
print('Testing ReplyKeyboardMarkup.to_json()')
reply_keyboard_markup = telegram.ReplyKeyboardMarkup.de_json(self.json_dict)
self.assertTrue(self.is_json(reply_keyboard_markup.to_json()))
def test_reply_keyboard_markup_to_dict(self):
"""Test ReplyKeyboardMarkup.to_dict() method"""
print('Testing ReplyKeyboardMarkup.to_dict()')
reply_keyboard_markup = telegram.ReplyKeyboardMarkup.de_json(self.json_dict)
self.assertEqual(reply_keyboard_markup['keyboard'], self.keyboard)

View file

@ -22,10 +22,12 @@
import os
import unittest
import sys
from flaky import flaky
sys.path.append('.')
import telegram
from tests.base import BaseTest
from tests.base import BaseTest, timeout
class StickerTest(BaseTest, unittest.TestCase):
@ -49,13 +51,14 @@ class StickerTest(BaseTest, unittest.TestCase):
'file_size': self.file_size
}
@flaky(3, 1)
@timeout(10)
def test_send_sticker_file(self):
pass
@flaky(3, 1)
@timeout(10)
def test_send_sticker_resend(self):
"""Test telegram.Bot sendSticker method"""
print('Testing bot.sendSticker - Resend by file_id')
message = self._bot.sendSticker(chat_id=self._chat_id,
sticker=self.sticker_file_id)
@ -68,9 +71,6 @@ class StickerTest(BaseTest, unittest.TestCase):
self.assertEqual(sticker.file_size, self.file_size)
def test_sticker_de_json(self):
"""Test Sticker.de_json() method"""
print('Testing Sticker.de_json()')
sticker = telegram.Sticker.de_json(self.json_dict)
self.assertEqual(sticker.file_id, self.sticker_file_id)
@ -80,17 +80,11 @@ class StickerTest(BaseTest, unittest.TestCase):
self.assertEqual(sticker.file_size, self.file_size)
def test_sticker_to_json(self):
"""Test Sticker.to_json() method"""
print('Testing Sticker.to_json()')
sticker = telegram.Sticker.de_json(self.json_dict)
self.assertTrue(self.is_json(sticker.to_json()))
def test_sticker_to_dict(self):
"""Test Sticker.to_dict() method"""
print('Testing Sticker.to_dict()')
sticker = telegram.Sticker.de_json(self.json_dict)
self.assertEqual(sticker['file_id'], self.sticker_file_id)
@ -99,9 +93,9 @@ class StickerTest(BaseTest, unittest.TestCase):
self.assertTrue(isinstance(sticker['thumb'], telegram.PhotoSize))
self.assertEqual(sticker['file_size'], self.file_size)
@flaky(3, 1)
@timeout(10)
def test_error_send_sticker_empty_file(self):
print('Testing bot.sendSticker - Null file')
json_dict = self.json_dict
del(json_dict['file_id'])
@ -111,9 +105,9 @@ class StickerTest(BaseTest, unittest.TestCase):
lambda: self._bot.sendSticker(chat_id=self._chat_id,
**json_dict))
@flaky(3, 1)
@timeout(10)
def test_error_send_sticker_empty_file_id(self):
print('Testing bot.sendSticker - Empty file_id')
json_dict = self.json_dict
del(json_dict['file_id'])
@ -123,9 +117,9 @@ class StickerTest(BaseTest, unittest.TestCase):
lambda: self._bot.sendSticker(chat_id=self._chat_id,
**json_dict))
@flaky(3, 1)
@timeout(10)
def test_error_sticker_without_required_args(self):
print('Testing bot.sendSticker - Without required arguments')
json_dict = self.json_dict
del(json_dict['file_id'])

View file

@ -52,26 +52,17 @@ class UpdateTest(BaseTest, unittest.TestCase):
}
def test_update_de_json(self):
"""Test Update.de_json() method"""
print('Testing Update.de_json()')
update = telegram.Update.de_json(self.json_dict)
self.assertEqual(update.update_id, self.update_id)
self.assertTrue(isinstance(update.message, telegram.Message))
def test_update_to_json(self):
"""Test Update.to_json() method"""
print('Testing Update.to_json()')
update = telegram.Update.de_json(self.json_dict)
self.assertTrue(self.is_json(update.to_json()))
def test_update_to_dict(self):
"""Test Update.to_dict() method"""
print('Testing Update.to_dict()')
update = telegram.Update.de_json(self.json_dict)
self.assertTrue(self.is_dict(update.to_dict()))

View file

@ -141,7 +141,6 @@ class UpdaterTest(BaseTest, unittest.TestCase):
self.message_count += 1
def test_addRemoveTelegramMessageHandler(self):
print('Testing add/removeTelegramMessageHandler')
self._setup_updater('Test')
d = self.updater.dispatcher
d.addTelegramMessageHandler(
@ -159,7 +158,6 @@ class UpdaterTest(BaseTest, unittest.TestCase):
self.assertTrue(None is self.received_message)
def test_addTelegramMessageHandlerMultipleMessages(self):
print('Testing addTelegramMessageHandler and send 100 messages...')
self._setup_updater('Multiple', 100)
self.updater.dispatcher.addTelegramMessageHandler(
self.telegramHandlerTest)
@ -169,7 +167,6 @@ class UpdaterTest(BaseTest, unittest.TestCase):
self.assertEqual(self.message_count, 100)
def test_addRemoveTelegramRegexHandler(self):
print('Testing add/removeStringRegexHandler')
self._setup_updater('Test2')
d = self.updater.dispatcher
regobj = re.compile('Te.*')
@ -188,7 +185,6 @@ class UpdaterTest(BaseTest, unittest.TestCase):
self.assertTrue(None is self.received_message)
def test_addRemoveTelegramCommandHandler(self):
print('Testing add/removeTelegramCommandHandler')
self._setup_updater('/test')
d = self.updater.dispatcher
self.updater.dispatcher.addTelegramCommandHandler(
@ -206,7 +202,6 @@ class UpdaterTest(BaseTest, unittest.TestCase):
self.assertTrue(None is self.received_message)
def test_addRemoveUnknownTelegramCommandHandler(self):
print('Testing add/removeUnknownTelegramCommandHandler')
self._setup_updater('/test2')
d = self.updater.dispatcher
self.updater.dispatcher.addUnknownTelegramCommandHandler(
@ -224,7 +219,6 @@ class UpdaterTest(BaseTest, unittest.TestCase):
self.assertTrue(None is self.received_message)
def test_addRemoveStringRegexHandler(self):
print('Testing add/removeStringRegexHandler')
self._setup_updater('', messages=0)
d = self.updater.dispatcher
d.addStringRegexHandler('Te.*', self.stringHandlerTest)
@ -242,7 +236,6 @@ class UpdaterTest(BaseTest, unittest.TestCase):
self.assertTrue(None is self.received_message)
def test_addRemoveStringCommandHandler(self):
print('Testing add/removeStringCommandHandler')
self._setup_updater('', messages=0)
d = self.updater.dispatcher
d.addStringCommandHandler(
@ -262,7 +255,6 @@ class UpdaterTest(BaseTest, unittest.TestCase):
self.assertTrue(None is self.received_message)
def test_addRemoveUnknownStringCommandHandler(self):
print('Testing add/removeUnknownStringCommandHandler')
self._setup_updater('/test')
d = self.updater.dispatcher
d.addUnknownStringCommandHandler(
@ -281,7 +273,6 @@ class UpdaterTest(BaseTest, unittest.TestCase):
self.assertTrue(None is self.received_message)
def test_addRemoveErrorHandler(self):
print('Testing add/removeErrorHandler')
self._setup_updater('', messages=0)
d = self.updater.dispatcher
d.addErrorHandler(self.errorHandlerTest)
@ -300,7 +291,6 @@ class UpdaterTest(BaseTest, unittest.TestCase):
self.assertTrue(None is self.received_message)
def test_errorInHandler(self):
print('Testing error in Handler')
self._setup_updater('', messages=0)
d = self.updater.dispatcher
d.addStringRegexHandler('.*', self.errorRaisingHandlerTest)
@ -312,7 +302,6 @@ class UpdaterTest(BaseTest, unittest.TestCase):
self.assertEqual(self.received_message, 'Test Error 1')
def test_errorOnGetUpdates(self):
print('Testing error on getUpdates')
self._setup_updater('', raise_error=True)
d = self.updater.dispatcher
d.addErrorHandler(self.errorHandlerTest)
@ -321,7 +310,6 @@ class UpdaterTest(BaseTest, unittest.TestCase):
self.assertEqual(self.received_message, "Test Error 2")
def test_addRemoveTypeHandler(self):
print('Testing add/removeTypeHandler')
self._setup_updater('', messages=0)
d = self.updater.dispatcher
d.addTypeHandler(dict, self.stringHandlerTest)
@ -364,7 +352,6 @@ class UpdaterTest(BaseTest, unittest.TestCase):
self.assertTrue(None is self.received_message)
def test_runAsync(self):
print('Testing @run_async')
self._setup_updater('Test5', messages=2)
d = self.updater.dispatcher
d.addTelegramMessageHandler(
@ -375,7 +362,6 @@ class UpdaterTest(BaseTest, unittest.TestCase):
self.assertEqual(self.message_count, 2)
def test_additionalArgs(self):
print('Testing additional arguments for handlers')
self._setup_updater('', messages=0)
self.updater.dispatcher.addStringCommandHandler(
'test5', self.additionalArgsTest)
@ -387,7 +373,6 @@ class UpdaterTest(BaseTest, unittest.TestCase):
self.assertEqual(self.message_count, 2)
def test_context(self):
print('Testing context for handlers')
context = "context_data"
self._setup_updater('', messages=0)
self.updater.dispatcher.addStringCommandHandler(
@ -401,7 +386,6 @@ class UpdaterTest(BaseTest, unittest.TestCase):
self.assertEqual(self.context, context)
def test_regexGroupHandler(self):
print('Testing optional groups and groupdict parameters')
self._setup_updater('', messages=0)
d = self.updater.dispatcher
d.addStringRegexHandler('^(This).*?(?P<testgroup>regex group).*',
@ -414,7 +398,6 @@ class UpdaterTest(BaseTest, unittest.TestCase):
def test_runAsyncWithAdditionalArgs(self):
print('Testing @run_async with additional parameters')
self._setup_updater('Test6', messages=2)
d = self.updater.dispatcher
d.addTelegramMessageHandler(
@ -425,7 +408,6 @@ class UpdaterTest(BaseTest, unittest.TestCase):
self.assertEqual(self.message_count, 2)
def test_webhook(self):
print('Testing Webhook')
self._setup_updater('', messages=0)
d = self.updater.dispatcher
d.addTelegramMessageHandler(
@ -471,7 +453,6 @@ class UpdaterTest(BaseTest, unittest.TestCase):
self.assertTrue(True)
def test_webhook_no_ssl(self):
print('Testing Webhook without SSL')
self._setup_updater('', messages=0)
d = self.updater.dispatcher
d.addTelegramMessageHandler(
@ -567,7 +548,6 @@ class UpdaterTest(BaseTest, unittest.TestCase):
os.kill(os.getpid(), signal.SIGTERM)
def test_idle(self):
print('Testing idle')
self._setup_updater('Test6', messages=0)
self.updater.start_polling(poll_interval=0.01)
Thread(target=self.signalsender).start()

View file

@ -47,9 +47,6 @@ class UserTest(BaseTest, unittest.TestCase):
}
def test_user_de_json(self):
"""Test User.de_json() method"""
print('Testing User.de_json()')
user = telegram.User.de_json(self.json_dict)
self.assertEqual(user.id, self.id)
@ -61,9 +58,6 @@ class UserTest(BaseTest, unittest.TestCase):
self.assertEqual(user.name, '@leandrotoledo')
def test_user_de_json_without_username(self):
"""Test User.de_json() method"""
print('Testing User.de_json() - Without username')
json_dict = self.json_dict
del(json_dict['username'])
@ -79,9 +73,6 @@ class UserTest(BaseTest, unittest.TestCase):
def test_user_de_json_without_username_and_lastname(self):
"""Test User.de_json() method"""
print('Testing User.de_json() - Without username and last_name')
json_dict = self.json_dict
del(json_dict['username'])
@ -95,17 +86,11 @@ class UserTest(BaseTest, unittest.TestCase):
self.assertEqual(user.name, self.first_name)
def test_user_to_json(self):
"""Test User.to_json() method"""
print('Testing User.to_json()')
user = telegram.User.de_json(self.json_dict)
self.assertTrue(self.is_json(user.to_json()))
def test_user_to_dict(self):
"""Test User.to_dict() method"""
print('Testing User.to_dict()')
user = telegram.User.de_json(self.json_dict)
self.assertTrue(self.is_dict(user.to_dict()))

View file

@ -22,10 +22,12 @@
import os
import unittest
import sys
from flaky import flaky
sys.path.append('.')
import telegram
from tests.base import BaseTest
from tests.base import BaseTest, timeout
class VideoTest(BaseTest, unittest.TestCase):
@ -37,8 +39,12 @@ class VideoTest(BaseTest, unittest.TestCase):
self.video_file_url = 'https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/tests/data/telegram.mp4'
self.width = 360
self.height = 640
self.duration = 4
self.thumb = telegram.PhotoSize.de_json({})
self.duration = 5
self.thumb = telegram.PhotoSize.de_json(
{'file_id': 'AAQBABOMsecvAAQqqoY1Pee_MqcyAAIC',
'file_size': 645,
'height': 90,
'width': 51})
self.mime_type = 'video/mp4'
self.file_size = 326534
@ -50,35 +56,35 @@ class VideoTest(BaseTest, unittest.TestCase):
'width': self.width,
'height': self.height,
'duration': self.duration,
'thumb': self.thumb,
'thumb': self.thumb.to_dict(),
'mime_type': self.mime_type,
'file_size': self.file_size
}
@flaky(3, 1)
@timeout(10)
def test_send_video_required_args_only(self):
"""Test telegram.Bot sendVideo method"""
print('Testing bot.sendVideo - With required arguments only')
message = self._bot.sendVideo(self._chat_id,
self.video_file)
video = message.video
self.assertTrue(isinstance(video.file_id, str))
self.assertNotEqual(video.file_id, '')
self.assertEqual(video.width, 0)
self.assertEqual(video.height, 0)
self.assertEqual(video.duration, 0)
self.assertEqual(video.thumb, None)
self.assertEqual(video.mime_type, '')
self.assertEqual(video.file_size, self.file_size)
def test_send_video_all_args(self):
"""Test telegram.Bot sendAudio method"""
print('Testing bot.sendVideo - With all arguments')
message = self._bot.sendVideo(self._chat_id,
self.video_file,
timeout=10)
video = message.video
self.assertTrue(isinstance(video.file_id, str))
self.assertNotEqual(video.file_id, '')
self.assertEqual(video.width, self.width)
self.assertEqual(video.height, self.height)
self.assertEqual(video.duration, self.duration)
self.assertEqual(video.thumb, self.thumb)
self.assertEqual(video.mime_type, '')
self.assertEqual(video.file_size, self.file_size)
@flaky(3, 1)
@timeout(10)
def test_send_video_all_args(self):
message = self._bot.sendVideo(self._chat_id,
self.video_file,
timeout=10,
duration=self.duration,
caption=self.caption)
@ -86,21 +92,21 @@ class VideoTest(BaseTest, unittest.TestCase):
self.assertTrue(isinstance(video.file_id, str))
self.assertNotEqual(video.file_id, '')
self.assertEqual(video.width, 0)
self.assertEqual(video.height, 0)
self.assertEqual(video.width, self.width)
self.assertEqual(video.height, self.height)
self.assertEqual(video.duration, self.duration)
self.assertEqual(video.thumb, None)
self.assertEqual(video.thumb, self.thumb)
self.assertEqual(video.mime_type, '')
self.assertEqual(video.file_size, self.file_size)
self.assertEqual(message.caption, self.caption)
@flaky(3, 1)
@timeout(10)
def test_send_video_mp4_file(self):
"""Test telegram.Bot sendVideo method"""
print('Testing bot.sendVideo - MP4 File')
message = self._bot.sendVideo(chat_id=self._chat_id,
video=self.video_file,
timeout=10,
duration=self.duration,
caption=self.caption)
@ -108,21 +114,21 @@ class VideoTest(BaseTest, unittest.TestCase):
self.assertTrue(isinstance(video.file_id, str))
self.assertNotEqual(video.file_id, '')
self.assertEqual(video.width, 0)
self.assertEqual(video.height, 0)
self.assertEqual(video.width, self.width)
self.assertEqual(video.height, self.height)
self.assertEqual(video.duration, self.duration)
self.assertEqual(video.thumb, None)
self.assertEqual(video.thumb, self.thumb)
self.assertEqual(video.mime_type, '')
self.assertEqual(video.file_size, self.file_size)
self.assertEqual(message.caption, self.caption)
@flaky(3, 1)
@timeout(10)
def test_send_video_mp4_file_with_custom_filename(self):
"""Test telegram.Bot sendVideo method"""
print('Testing bot.sendVideo - MP4 File with custom filename')
message = self._bot.sendVideo(chat_id=self._chat_id,
video=self.video_file,
timeout=10,
duration=self.duration,
caption=self.caption,
filename='telegram_custom.mp4')
@ -131,21 +137,21 @@ class VideoTest(BaseTest, unittest.TestCase):
self.assertTrue(isinstance(video.file_id, str))
self.assertNotEqual(video.file_id, '')
self.assertEqual(video.width, 0)
self.assertEqual(video.height, 0)
self.assertEqual(video.width, self.width)
self.assertEqual(video.height, self.height)
self.assertEqual(video.duration, self.duration)
self.assertEqual(video.thumb, None)
self.assertEqual(video.thumb, self.thumb)
self.assertEqual(video.mime_type, '')
self.assertEqual(video.file_size, self.file_size)
self.assertEqual(message.caption, self.caption)
@flaky(3, 1)
@timeout(10)
def test_send_video_mp4_file_url(self):
"""Test telegram.Bot sendVideo method"""
print('Testing bot.sendVideo - MP4 File by URL')
message = self._bot.sendVideo(chat_id=self._chat_id,
video=self.video_file_url,
timeout=10,
duration=self.duration,
caption=self.caption)
@ -153,21 +159,20 @@ class VideoTest(BaseTest, unittest.TestCase):
self.assertTrue(isinstance(video.file_id, str))
self.assertNotEqual(video.file_id, '')
self.assertEqual(video.width, 0)
self.assertEqual(video.height, 0)
self.assertEqual(video.height, self.height)
self.assertEqual(video.duration, self.duration)
self.assertEqual(video.thumb, None)
self.assertEqual(video.thumb, self.thumb)
self.assertEqual(video.mime_type, '')
self.assertEqual(video.file_size, self.file_size)
self.assertEqual(message.caption, self.caption)
@flaky(3, 1)
@timeout(10)
def test_send_video_resend(self):
"""Test telegram.Bot sendVideo method"""
print('Testing bot.sendVideo - Resend by file_id')
message = self._bot.sendVideo(chat_id=self._chat_id,
video=self.video_file_id,
timeout=10,
duration=self.duration,
caption=self.caption)
@ -181,31 +186,22 @@ class VideoTest(BaseTest, unittest.TestCase):
self.assertEqual(message.caption, self.caption)
def test_video_de_json(self):
"""Test Video.de_json() method"""
print('Testing Video.de_json()')
video = telegram.Video.de_json(self.json_dict)
self.assertEqual(video.file_id, self.video_file_id)
self.assertEqual(video.width, self.width)
self.assertEqual(video.height, self.height)
self.assertEqual(video.duration, self.duration)
self.assertEqual(video.thumb, None)
self.assertEqual(video.thumb, self.thumb)
self.assertEqual(video.mime_type, self.mime_type)
self.assertEqual(video.file_size, self.file_size)
def test_video_to_json(self):
"""Test Video.to_json() method"""
print('Testing Video.to_json()')
video = telegram.Video.de_json(self.json_dict)
self.assertTrue(self.is_json(video.to_json()))
def test_video_to_dict(self):
"""Test Video.to_dict() method"""
print('Testing Video.to_dict()')
video = telegram.Video.de_json(self.json_dict)
self.assertTrue(self.is_dict(video.to_dict()))
@ -216,9 +212,9 @@ class VideoTest(BaseTest, unittest.TestCase):
self.assertEqual(video['mime_type'], self.mime_type)
self.assertEqual(video['file_size'], self.file_size)
@flaky(3, 1)
@timeout(10)
def test_error_send_video_empty_file(self):
print('Testing bot.sendVideo - Null file')
json_dict = self.json_dict
del(json_dict['file_id'])
@ -226,11 +222,12 @@ class VideoTest(BaseTest, unittest.TestCase):
self.assertRaises(telegram.TelegramError,
lambda: self._bot.sendVideo(chat_id=self._chat_id,
timeout=10,
**json_dict))
@flaky(3, 1)
@timeout(10)
def test_error_send_video_empty_file_id(self):
print('Testing bot.sendVideo - Empty file_id')
json_dict = self.json_dict
del(json_dict['file_id'])
@ -238,11 +235,12 @@ class VideoTest(BaseTest, unittest.TestCase):
self.assertRaises(telegram.TelegramError,
lambda: self._bot.sendVideo(chat_id=self._chat_id,
timeout=10,
**json_dict))
@flaky(3, 1)
@timeout(10)
def test_error_video_without_required_args(self):
print('Testing bot.sendVideo - Without required arguments')
json_dict = self.json_dict
del(json_dict['file_id'])
@ -250,6 +248,7 @@ class VideoTest(BaseTest, unittest.TestCase):
self.assertRaises(TypeError,
lambda: self._bot.sendVideo(chat_id=self._chat_id,
timeout=10,
**json_dict))
if __name__ == '__main__':

View file

@ -22,10 +22,12 @@
import os
import unittest
import sys
from flaky import flaky
sys.path.append('.')
import telegram
from tests.base import BaseTest
from tests.base import BaseTest, timeout
class VoiceTest(BaseTest, unittest.TestCase):
@ -35,7 +37,7 @@ class VoiceTest(BaseTest, unittest.TestCase):
self.voice_file = open('tests/data/telegram.ogg', 'rb')
self.voice_file_id = 'AwADAQADTgADHyP1B_mbw34svXPHAg'
self.voice_file_url = 'https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/tests/data/telegram.ogg'
self.duration = 0
self.duration = 3
self.mime_type = 'audio/ogg'
self.file_size = 9199
@ -46,10 +48,9 @@ class VoiceTest(BaseTest, unittest.TestCase):
'file_size': self.file_size
}
@flaky(3, 1)
@timeout(10)
def test_send_voice_required_args_only(self):
"""Test telegram.Bot sendVoice method"""
print('Testing bot.sendVoice - With required arguments only')
message = self._bot.sendVoice(self._chat_id,
self.voice_file)
@ -61,10 +62,9 @@ class VoiceTest(BaseTest, unittest.TestCase):
self.assertEqual(voice.mime_type, self.mime_type)
self.assertEqual(voice.file_size, self.file_size)
@flaky(3, 1)
@timeout(10)
def test_send_voice_all_args(self):
"""Test telegram.Bot sendAudio method"""
print('Testing bot.sendVoice - With all arguments')
message = self._bot.sendVoice(self._chat_id,
self.voice_file,
self.duration,
@ -79,10 +79,9 @@ class VoiceTest(BaseTest, unittest.TestCase):
self.assertEqual(voice.mime_type, self.mime_type)
self.assertEqual(voice.file_size, self.file_size)
@flaky(3, 1)
@timeout(10)
def test_send_voice_ogg_file(self):
"""Test telegram.Bot sendVoice method"""
print('Testing bot.sendVoice - Ogg File')
message = self._bot.sendVoice(chat_id=self._chat_id,
voice=self.voice_file,
duration=self.duration)
@ -95,10 +94,9 @@ class VoiceTest(BaseTest, unittest.TestCase):
self.assertEqual(voice.mime_type, self.mime_type)
self.assertEqual(voice.file_size, self.file_size)
@flaky(3, 1)
@timeout(10)
def test_send_voice_ogg_file_with_custom_filename(self):
"""Test telegram.Bot sendVoice method"""
print('Testing bot.sendVoice - Ogg File with custom filename')
message = self._bot.sendVoice(chat_id=self._chat_id,
voice=self.voice_file,
duration=self.duration,
@ -112,10 +110,9 @@ class VoiceTest(BaseTest, unittest.TestCase):
self.assertEqual(voice.mime_type, self.mime_type)
self.assertEqual(voice.file_size, self.file_size)
@flaky(3, 1)
@timeout(10)
def test_send_voice_ogg_url_file(self):
"""Test telegram.Bot sendVoice method"""
print('Testing bot.sendVoice - Ogg File by URL')
message = self._bot.sendVoice(chat_id=self._chat_id,
voice=self.voice_file_url,
duration=self.duration)
@ -128,10 +125,9 @@ class VoiceTest(BaseTest, unittest.TestCase):
self.assertEqual(voice.mime_type, self.mime_type)
self.assertEqual(voice.file_size, self.file_size)
@flaky(3, 1)
@timeout(10)
def test_send_voice_resend(self):
"""Test telegram.Bot sendVoice method"""
print('Testing bot.sendVoice - Resend by file_id')
message = self._bot.sendVoice(chat_id=self._chat_id,
voice=self.voice_file_id,
duration=self.duration)
@ -139,13 +135,10 @@ class VoiceTest(BaseTest, unittest.TestCase):
voice = message.voice
self.assertEqual(voice.file_id, self.voice_file_id)
self.assertEqual(voice.duration, self.duration)
self.assertEqual(voice.duration, 0)
self.assertEqual(voice.mime_type, self.mime_type)
def test_voice_de_json(self):
"""Test Voice.de_json() method"""
print('Testing Voice.de_json()')
voice = telegram.Voice.de_json(self.json_dict)
self.assertEqual(voice.file_id, self.voice_file_id)
@ -154,17 +147,11 @@ class VoiceTest(BaseTest, unittest.TestCase):
self.assertEqual(voice.file_size, self.file_size)
def test_voice_to_json(self):
"""Test Voice.to_json() method"""
print('Testing Voice.to_json()')
voice = telegram.Voice.de_json(self.json_dict)
self.assertTrue(self.is_json(voice.to_json()))
def test_voice_to_dict(self):
"""Test Voice.to_dict() method"""
print('Testing Voice.to_dict()')
voice = telegram.Voice.de_json(self.json_dict)
self.assertTrue(self.is_dict(voice.to_dict()))
@ -173,9 +160,9 @@ class VoiceTest(BaseTest, unittest.TestCase):
self.assertEqual(voice['mime_type'], self.mime_type)
self.assertEqual(voice['file_size'], self.file_size)
@flaky(3, 1)
@timeout(10)
def test_error_send_voice_empty_file(self):
print('Testing bot.sendVoice - Null file')
json_dict = self.json_dict
del(json_dict['file_id'])
@ -185,9 +172,9 @@ class VoiceTest(BaseTest, unittest.TestCase):
lambda: self._bot.sendVoice(chat_id=self._chat_id,
**json_dict))
@flaky(3, 1)
@timeout(10)
def test_error_send_voice_empty_file_id(self):
print('Testing bot.sendVoice - Empty file_id')
json_dict = self.json_dict
del(json_dict['file_id'])
@ -197,9 +184,9 @@ class VoiceTest(BaseTest, unittest.TestCase):
lambda: self._bot.sendVoice(chat_id=self._chat_id,
**json_dict))
@flaky(3, 1)
@timeout(10)
def test_error_voice_without_required_args(self):
print('Testing bot.sendVoice - Without required arguments')
json_dict = self.json_dict
del(json_dict['file_id'])