Merge pull request #683 from python-telegram-bot/no_hardcoded_fileids

No hardcoded fileids
This commit is contained in:
Noam Meltzer 2017-06-23 00:47:13 +03:00 committed by GitHub
commit 8703ae0b8d
13 changed files with 764 additions and 876 deletions

1
.gitignore vendored
View file

@ -69,6 +69,7 @@ telegram2.mp4
telegram.ogg
telegram.png
telegram.webp
telegram.jpg
# original files from merges
*.orig

66
tests/bots.py Normal file
View file

@ -0,0 +1,66 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2017
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""Provide a bot to tests"""
import os
import sys
bot_settings = {
'APPVEYOR':
{
'token': '133505823:AAHZFMHno3mzVLErU5b5jJvaeG--qUyLyG0',
'payment_provider_token': '284685063:TEST:ZGJlMmQxZDI3ZTc3'
},
'TRAVIS':
{
'token': '133505823:AAHZFMHno3mzVLErU5b5jJvaeG--qUyLyG0',
'payment_provider_token': '284685063:TEST:ZGJlMmQxZDI3ZTc3'
},
'FALLBACK':
{
'token': '133505823:AAHZFMHno3mzVLErU5b5jJvaeG--qUyLyG0',
'payment_provider_token': '284685063:TEST:ZGJlMmQxZDI3ZTc3'
}
}
def get_bot():
# TODO: Add version info with different bots
# ver = sys.version_info
# pyver = "{}{}".format(ver[0], ver[1])
#
bot = None
if os.environ.get('TRAVIS', False):
bot = bot_settings.get('TRAVIS', None)
# TODO:
# bot = bot_setting.get('TRAVIS'+pyver, None)
elif os.environ.get('APPVEYOR', False):
bot = bot_settings.get('APPVEYOR', None)
# TODO:
# bot = bot_setting.get('TRAVIS'+pyver, None)
if not bot:
bot = bot_settings['FALLBACK']
bot.update({
'chat_id': '12173560',
'group_id': '-49740850',
'channel_id': '@pythontelegrambottests'
})
return bot

Binary file not shown.

BIN
tests/data/telegram.webp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

View file

@ -18,58 +18,58 @@
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents Tests for Telegram Audio"""
import sys
import unittest
import os
import unittest
from flaky import flaky
sys.path.append('.')
import telegram
from tests.base import BaseTest, timeout
from tests.bots import get_bot
class AudioTest(BaseTest, unittest.TestCase):
"""This object represents Tests for Telegram Audio."""
@classmethod
def setUpClass(cls):
cls.caption = "Test audio"
cls.performer = 'Leandro Toledo'
cls.title = 'Teste'
cls.audio_file_url = 'https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/tests/data/telegram.mp3'
bot_info = get_bot()
cls._chat_id = bot_info['chat_id']
cls._bot = telegram.Bot(bot_info['token'])
audio_file = open('tests/data/telegram.mp3', 'rb')
audio = cls._bot.send_audio(cls._chat_id, audio=audio_file, timeout=10).audio
cls.audio = audio
# Make sure file has been uploaded.
# Simple assertions PY2 Only
assert isinstance(cls.audio, telegram.Audio)
assert isinstance(cls.audio.file_id, str)
assert cls.audio.file_id is not ''
def setUp(self):
self.audio_file = open('tests/data/telegram.mp3', 'rb')
self.audio_file_id = 'CQADAQADDwADHyP1B6PSPq2HjX8kAg'
self.audio_file_url = 'https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/tests/data/telegram.mp3'
self.duration = 4
self.performer = 'Leandro Toledo'
self.title = 'Teste'
self.caption = "Test audio"
self.mime_type = 'audio/mpeg'
self.file_size = 28232
self.json_dict = {
'file_id': self.audio_file_id,
'duration': self.duration,
'file_id': self.audio.file_id,
'duration': self.audio.duration,
'performer': self.performer,
'title': self.title,
'caption': self.caption,
'mime_type': self.mime_type,
'file_size': self.file_size
'mime_type': self.audio.mime_type,
'file_size': self.audio.file_size
}
@flaky(3, 1)
@timeout(10)
def test_send_audio_required_args_only(self):
message = self._bot.sendAudio(self._chat_id, self.audio_file)
self.assertEqual(message.caption, None)
audio = message.audio
self.assertTrue(isinstance(audio.file_id, str))
self.assertNotEqual(audio.file_id, None)
# self.assertEqual(audio.duration, 4)
self.assertEqual(audio.performer, None)
self.assertEqual(audio.title, None)
self.assertEqual(audio.mime_type, self.mime_type)
self.assertEqual(audio.file_size, self.file_size)
def test_expected_values(self):
self.assertEqual(self.audio.duration, 3)
self.assertEqual(self.audio.performer, None)
self.assertEqual(self.audio.title, None)
self.assertEqual(self.audio.mime_type, 'audio/mpeg')
self.assertEqual(self.audio.file_size, 122920)
@flaky(3, 1)
@timeout(10)
@ -77,85 +77,51 @@ class AudioTest(BaseTest, unittest.TestCase):
message = self._bot.sendAudio(
self._chat_id,
self.audio_file,
duration=self.duration,
caption=self.caption,
duration=self.audio.duration,
performer=self.performer,
title=self.title,
caption=self.caption,
mime_type=self.mime_type,
file_size=self.file_size)
disable_notification=False)
self.assertEqual(message.caption, self.caption)
audio = message.audio
self.assertTrue(isinstance(audio.file_id, str))
self.assertIsInstance(audio, telegram.Audio)
self.assertIsInstance(audio.file_id, str)
self.assertNotEqual(audio.file_id, None)
self.assertEqual(audio.duration, self.duration)
self.assertEqual(audio.duration, self.audio.duration)
self.assertEqual(audio.performer, self.performer)
self.assertEqual(audio.title, self.title)
self.assertEqual(audio.mime_type, self.mime_type)
self.assertEqual(audio.file_size, self.file_size)
self.assertEqual(audio.mime_type, self.audio.mime_type)
self.assertEqual(audio.file_size, self.audio.file_size)
@flaky(3, 1)
@timeout(10)
def test_send_audio_mp3_file(self):
message = self._bot.sendAudio(
chat_id=self._chat_id,
audio=self.audio_file,
duration=self.duration,
performer=self.performer,
title=self.title,
caption=self.caption)
def test_get_and_download_audio(self):
new_file = self._bot.getFile(self.audio.file_id)
self.assertEqual(message.caption, self.caption)
self.assertEqual(new_file.file_size, self.audio.file_size)
self.assertEqual(new_file.file_id, self.audio.file_id)
self.assertTrue(new_file.file_path.startswith('https://'))
audio = message.audio
new_file.download('telegram.mp3')
self.assertTrue(isinstance(audio.file_id, str))
self.assertNotEqual(audio.file_id, None)
self.assertEqual(audio.duration, self.duration)
self.assertEqual(audio.performer, self.performer)
self.assertEqual(audio.title, self.title)
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):
message = self._bot.sendAudio(
chat_id=self._chat_id,
audio=self.audio_file,
duration=self.duration,
performer=self.performer,
title=self.title,
caption=self.caption,
filename='telegram_custom.mp3')
self.assertEqual(message.caption, self.caption)
audio = message.audio
self.assertTrue(isinstance(audio.file_id, str))
self.assertNotEqual(audio.file_id, None)
self.assertEqual(audio.duration, self.duration)
self.assertEqual(audio.performer, self.performer)
self.assertEqual(audio.title, self.title)
self.assertEqual(audio.mime_type, self.mime_type)
self.assertEqual(audio.file_size, self.file_size)
self.assertTrue(os.path.isfile('telegram.mp3'))
@flaky(3, 1)
@timeout(10)
def test_send_audio_mp3_url_file(self):
message = self._bot.sendAudio(
chat_id=self._chat_id, audio=self.audio_file_url, duration=self.duration)
message = self._bot.sendAudio(chat_id=self._chat_id, audio=self.audio_file_url)
audio = message.audio
self.assertTrue(isinstance(audio.file_id, str))
self.assertIsInstance(audio, telegram.Audio)
self.assertIsInstance(audio.file_id, str)
self.assertNotEqual(audio.file_id, None)
self.assertEqual(audio.duration, self.duration)
self.assertEqual(audio.mime_type, self.mime_type)
self.assertEqual(audio.file_size, self.file_size)
self.assertEqual(audio.duration, self.audio.duration)
self.assertEqual(audio.mime_type, self.audio.mime_type)
self.assertEqual(audio.file_size, self.audio.file_size)
@flaky(3, 1)
@timeout(10)
@ -163,65 +129,52 @@ class AudioTest(BaseTest, unittest.TestCase):
message = self._bot.sendAudio(
chat_id=self._chat_id,
audio=self.audio_file_url,
duration=self.duration,
caption=self.caption)
self.assertEqual(message.caption, self.caption)
audio = message.audio
self.assertTrue(isinstance(audio.file_id, str))
self.assertIsInstance(audio, telegram.Audio)
self.assertIsInstance(audio.file_id, str)
self.assertNotEqual(audio.file_id, None)
self.assertEqual(audio.duration, self.duration)
self.assertEqual(audio.mime_type, self.mime_type)
self.assertEqual(audio.file_size, self.file_size)
self.assertEqual(audio.duration, self.audio.duration)
self.assertEqual(audio.mime_type, self.audio.mime_type)
self.assertEqual(audio.file_size, self.audio.file_size)
@flaky(3, 1)
@timeout(10)
def test_send_audio_resend(self):
message = self._bot.sendAudio(
chat_id=self._chat_id,
audio=self.audio_file_id,
duration=self.duration,
performer=self.performer,
title=self.title,
caption=self.caption)
self.assertEqual(message.caption, self.caption)
audio=self.audio.file_id)
audio = message.audio
self.assertEqual(audio.file_id, self.audio_file_id)
self.assertEqual(audio.duration, self.duration)
self.assertEqual(audio.performer, self.performer)
self.assertEqual(audio.title, self.title)
self.assertEqual(audio.mime_type, self.mime_type)
self.assertEqual(audio, self.audio)
def test_audio_de_json(self):
audio = telegram.Audio.de_json(self.json_dict, self._bot)
self.assertEqual(audio.file_id, self.audio_file_id)
self.assertEqual(audio.duration, self.duration)
self.assertIsInstance(audio, telegram.Audio)
self.assertEqual(audio.file_id, self.audio.file_id)
self.assertEqual(audio.duration, self.audio.duration)
self.assertEqual(audio.performer, self.performer)
self.assertEqual(audio.title, self.title)
self.assertEqual(audio.mime_type, self.mime_type)
self.assertEqual(audio.file_size, self.file_size)
self.assertEqual(audio.mime_type, self.audio.mime_type)
self.assertEqual(audio.file_size, self.audio.file_size)
def test_audio_to_json(self):
audio = telegram.Audio.de_json(self.json_dict, self._bot)
self.assertTrue(self.is_json(audio.to_json()))
self.assertTrue(self.is_json(self.audio.to_json()))
def test_audio_to_dict(self):
audio = telegram.Audio.de_json(self.json_dict, self._bot)
audio = self.audio.to_dict()
self.assertTrue(self.is_dict(audio.to_dict()))
self.assertEqual(audio['file_id'], self.audio_file_id)
self.assertEqual(audio['duration'], self.duration)
self.assertEqual(audio['performer'], self.performer)
self.assertEqual(audio['title'], self.title)
self.assertEqual(audio['mime_type'], self.mime_type)
self.assertEqual(audio['file_size'], self.file_size)
self.assertTrue(self.is_dict(audio))
self.assertEqual(audio['file_id'], self.audio.file_id)
self.assertEqual(audio['duration'], self.audio.duration)
self.assertEqual(audio['mime_type'], self.audio.mime_type)
self.assertEqual(audio['file_size'], self.audio.file_size)
@flaky(3, 1)
@timeout(10)
@ -231,9 +184,8 @@ class AudioTest(BaseTest, unittest.TestCase):
del (json_dict['file_id'])
json_dict['audio'] = open(os.devnull, 'rb')
self.assertRaises(
telegram.TelegramError,
lambda: self._bot.sendAudio(chat_id=self._chat_id, **json_dict))
with self.assertRaises(telegram.TelegramError):
self._bot.sendAudio(chat_id=self._chat_id, **json_dict)
@flaky(3, 1)
@timeout(10)
@ -243,9 +195,8 @@ class AudioTest(BaseTest, unittest.TestCase):
del (json_dict['file_id'])
json_dict['audio'] = ''
self.assertRaises(
telegram.TelegramError,
lambda: self._bot.sendAudio(chat_id=self._chat_id, **json_dict))
with self.assertRaises(telegram.TelegramError):
self._bot.sendAudio(chat_id=self._chat_id, **json_dict)
@flaky(3, 1)
@timeout(10)
@ -255,25 +206,26 @@ class AudioTest(BaseTest, unittest.TestCase):
del (json_dict['file_id'])
del (json_dict['duration'])
self.assertRaises(
TypeError,
lambda: self._bot.sendAudio(chat_id=self._chat_id, **json_dict))
with self.assertRaises(TypeError):
self._bot.sendAudio(chat_id=self._chat_id, **json_dict)
@flaky(3, 1)
@timeout(10)
def test_reply_audio(self):
"""Test for Message.reply_audio"""
message = self._bot.sendMessage(self._chat_id, '.')
message = message.reply_audio(self.audio_file)
audio = message.reply_audio(self.audio_file).audio
self.assertNotEqual(message.audio.file_id, None)
self.assertIsInstance(audio, telegram.Audio)
self.assertIsInstance(audio.file_id, str)
self.assertNotEqual(audio.file_id, None)
def test_equality(self):
a = telegram.Audio(self.audio_file_id, self.duration)
b = telegram.Audio(self.audio_file_id, self.duration)
c = telegram.Audio(self.audio_file_id, 0)
d = telegram.Audio("", self.duration)
e = telegram.Voice(self.audio_file_id, self.duration)
a = telegram.Audio(self.audio.file_id, self.audio.duration)
b = telegram.Audio(self.audio.file_id, self.audio.duration)
c = telegram.Audio(self.audio.file_id, 0)
d = telegram.Audio("", self.audio.duration)
e = telegram.Voice(self.audio.file_id, self.audio.duration)
self.assertEqual(a, b)
self.assertEqual(hash(a), hash(b))

View file

@ -121,78 +121,6 @@ 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):
message = self._bot.sendPhoto(
photo=open('tests/data/telegram.png', 'rb'),
caption='testSendPhoto',
chat_id=self._chat_id)
self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(message.photo[0].file_size, 1451)
self.assertEqual(message.caption, 'testSendPhoto')
@flaky(3, 1)
@timeout(10)
def testSilentSendPhoto(self):
message = self._bot.sendPhoto(
photo=open('tests/data/telegram.png', 'rb'),
caption='testSendPhoto',
chat_id=self._chat_id,
disable_notification=True)
self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(message.photo[0].file_size, 1451)
self.assertEqual(message.caption, 'testSendPhoto')
@flaky(3, 1)
@timeout(10)
def testResendPhoto(self):
message = self._bot.sendPhoto(
photo='AgADAQAD1y0yGx8j9Qf8f_m3CKeS6Iy95y8ABI1ggfVJ4-UvwJcAAgI', chat_id=self._chat_id)
self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(message.photo[0].file_id,
'AgADAQAD1y0yGx8j9Qf8f_m3CKeS6Iy95y8ABI1ggfVJ4-UvwJcAAgI')
@flaky(3, 1)
@timeout(10)
def testSendJPGURLPhoto(self):
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, 813)
@flaky(3, 1)
@timeout(10)
def testSendPNGURLPhoto(self):
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, 670)
@flaky(3, 1)
@timeout(10)
def testSendGIFURLPhoto(self):
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, 670)
@flaky(3, 1)
@timeout(10)
def testSendBufferedReaderPhoto(self):
photo = open('tests/data/telegram.png', 'rb')
br_photo = io.BufferedReader(io.BytesIO(photo.read()))
message = self._bot.sendPhoto(photo=br_photo, chat_id=self._chat_id)
self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(message.photo[0].file_size, 1451)
@flaky(3, 1)
@timeout(10)
def testSendGame(self):

View file

@ -18,56 +18,85 @@
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents Tests for Telegram Document"""
import sys
import unittest
import os
import unittest
from flaky import flaky
sys.path.append('.')
import telegram
from tests.base import BaseTest, timeout
from tests.bots import get_bot
class DocumentTest(BaseTest, unittest.TestCase):
"""This object represents Tests for Telegram Document."""
@classmethod
def setUpClass(cls):
cls.caption = u'DocumentTest - Caption'
cls.document_file_url = 'https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/tests/data/telegram.gif'
bot_info = get_bot()
cls._chat_id = bot_info['chat_id']
cls._bot = telegram.Bot(bot_info['token'])
document_file = open('tests/data/telegram.png', 'rb')
document = cls._bot.send_document(cls._chat_id, document=document_file, timeout=10).document
cls.document = document
# Make sure file has been uploaded.
# Simple assertions PY2 Only
assert isinstance(cls.document, telegram.Document)
assert isinstance(cls.document.file_id, str)
assert cls.document.file_id is not ''
def setUp(self):
self.document_file = open('tests/data/telegram.png', 'rb')
self.document_file_id = 'BQADAQADpAADHyP1B04ipZxJTe2BAg'
self.document_file_url = 'https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/tests/data/telegram.gif'
self.thumb = {
'width': 90,
'height': 90,
'file_id': 'BQADAQADoQADHyP1B0mzJMVyzcB0Ag',
'file_size': 2364
}
self.file_name = 'telegram.png'
self.mime_type = 'image/png'
self.file_size = 12948
self.json_dict = {
'file_id': self.document_file_id,
'thumb': self.thumb,
'file_name': self.file_name,
'mime_type': self.mime_type,
'file_size': self.file_size
'file_id': self.document.file_id,
'thumb': self.document.thumb.to_dict(),
'file_name': self.document.file_name,
'mime_type': self.document.mime_type,
'file_size': self.document.file_size
}
def test_expected_values(self):
self.assertEqual(self.document.file_size, 12948)
self.assertEqual(self.document.mime_type, 'image/png')
self.assertEqual(self.document.file_name, 'telegram.png')
self.assertEqual(self.document.thumb.file_size, 2364)
self.assertEqual(self.document.thumb.width, 90)
self.assertEqual(self.document.thumb.height, 90)
@flaky(3, 1)
@timeout(10)
def test_send_document_png_file(self):
message = self._bot.sendDocument(self._chat_id, self.document_file, caption='caption text')
def test_send_document_all_args(self):
message = self._bot.sendDocument(self._chat_id, document=self.document_file, caption=self.caption,
disable_notification=False)
document = message.document
self.assertTrue(isinstance(document.file_id, str))
self.assertIsInstance(document, telegram.Document)
self.assertIsInstance(document.file_id, str)
self.assertNotEqual(document.file_id, '')
self.assertTrue(isinstance(document.thumb, telegram.PhotoSize))
self.assertEqual(document.file_name, self.file_name)
self.assertEqual(document.mime_type, self.mime_type)
self.assertEqual(document.file_size, self.file_size)
self.assertEqual(document.file_name, self.document.file_name)
self.assertEqual(document.mime_type, self.document.mime_type)
self.assertEqual(document.file_size, self.document.file_size)
self.assertEqual(document.thumb, self.document.thumb)
self.assertEqual(message.caption, self.caption)
@flaky(3, 1)
@timeout(10)
def test_get_and_download_document(self):
new_file = self._bot.getFile(self.document.file_id)
self.assertEqual(new_file.file_size, self.document.file_size)
self.assertEqual(new_file.file_id, self.document.file_id)
self.assertTrue(new_file.file_path.startswith('https://'))
new_file.download('telegram.png')
self.assertTrue(os.path.isfile('telegram.png'))
@flaky(3, 1)
@timeout(10)
@ -77,12 +106,7 @@ class DocumentTest(BaseTest, unittest.TestCase):
document = message.document
self.assertTrue(isinstance(document.file_id, str))
self.assertNotEqual(document.file_id, '')
self.assertTrue(isinstance(document.thumb, telegram.PhotoSize))
self.assertEqual(document.file_name, 'telegram_custom.png')
self.assertEqual(document.mime_type, self.mime_type)
self.assertEqual(document.file_size, self.file_size)
@flaky(3, 1)
@timeout(10)
@ -91,7 +115,8 @@ class DocumentTest(BaseTest, unittest.TestCase):
document = message.document
self.assertTrue(isinstance(document.file_id, str))
self.assertIsInstance(document, telegram.Document)
self.assertIsInstance(document.file_id, str)
self.assertNotEqual(document.file_id, '')
self.assertTrue(isinstance(document.thumb, telegram.PhotoSize))
self.assertEqual(document.file_name, 'telegram.gif')
@ -101,38 +126,28 @@ class DocumentTest(BaseTest, unittest.TestCase):
@flaky(3, 1)
@timeout(10)
def test_send_document_resend(self):
message = self._bot.sendDocument(chat_id=self._chat_id, document=self.document_file_id)
message = self._bot.sendDocument(chat_id=self._chat_id, document=self.document.file_id)
document = message.document
self.assertEqual(document.file_id, self.document_file_id)
self.assertTrue(isinstance(document.thumb, telegram.PhotoSize))
self.assertEqual(document.file_name, self.file_name)
self.assertEqual(document.mime_type, self.mime_type)
self.assertEqual(document, self.document)
def test_document_de_json(self):
document = telegram.Document.de_json(self.json_dict, self._bot)
self.assertEqual(document.file_id, self.document_file_id)
self.assertTrue(isinstance(document.thumb, telegram.PhotoSize))
self.assertEqual(document.file_name, self.file_name)
self.assertEqual(document.mime_type, self.mime_type)
self.assertEqual(document.file_size, self.file_size)
self.assertEqual(document, self.document)
def test_document_to_json(self):
document = telegram.Document.de_json(self.json_dict, self._bot)
self.assertTrue(self.is_json(document.to_json()))
self.assertTrue(self.is_json(self.document.to_json()))
def test_document_to_dict(self):
document = telegram.Document.de_json(self.json_dict, self._bot)
document = self.document.to_dict()
self.assertTrue(self.is_dict(document.to_dict()))
self.assertEqual(document['file_id'], self.document_file_id)
self.assertTrue(isinstance(document['thumb'], telegram.PhotoSize))
self.assertEqual(document['file_name'], self.file_name)
self.assertEqual(document['mime_type'], self.mime_type)
self.assertEqual(document['file_size'], self.file_size)
self.assertTrue(self.is_dict(document))
self.assertEqual(document['file_id'], self.document.file_id)
self.assertEqual(document['file_name'], self.document.file_name)
self.assertEqual(document['mime_type'], self.document.mime_type)
self.assertEqual(document['file_size'], self.document.file_size)
@flaky(3, 1)
@timeout(10)
@ -142,9 +157,8 @@ class DocumentTest(BaseTest, unittest.TestCase):
del (json_dict['file_id'])
json_dict['document'] = open(os.devnull, 'rb')
self.assertRaises(telegram.TelegramError,
lambda: self._bot.sendDocument(chat_id=self._chat_id,
**json_dict))
with self.assertRaises(telegram.TelegramError):
self._bot.sendDocument(chat_id=self._chat_id, **json_dict)
@flaky(3, 1)
@timeout(10)
@ -154,9 +168,8 @@ class DocumentTest(BaseTest, unittest.TestCase):
del (json_dict['file_id'])
json_dict['document'] = ''
self.assertRaises(telegram.TelegramError,
lambda: self._bot.sendDocument(chat_id=self._chat_id,
**json_dict))
with self.assertRaises(telegram.TelegramError):
self._bot.sendDocument(chat_id=self._chat_id, **json_dict)
@flaky(3, 1)
@timeout(10)
@ -165,9 +178,7 @@ class DocumentTest(BaseTest, unittest.TestCase):
del (json_dict['file_id'])
self.assertRaises(TypeError,
lambda: self._bot.sendDocument(chat_id=self._chat_id,
**json_dict))
with self.assertRaises(TypeError): self._bot.sendDocument(chat_id=self._chat_id, **json_dict)
@flaky(3, 1)
@timeout(10)
@ -176,13 +187,18 @@ class DocumentTest(BaseTest, unittest.TestCase):
message = self._bot.sendMessage(self._chat_id, '.')
message = message.reply_document(self.document_file)
self.assertNotEqual(message.document.file_id, '')
document = message.document
self.assertIsInstance(document, telegram.Document)
self.assertIsInstance(document.file_id, str)
self.assertNotEqual(document.file_id, '')
self.assertTrue(isinstance(document.thumb, telegram.PhotoSize))
def test_equality(self):
a = telegram.Document(self.document_file_id)
b = telegram.Document(self.document_file_id)
a = telegram.Document(self.document.file_id)
b = telegram.Document(self.document.file_id)
d = telegram.Document("")
e = telegram.Voice(self.document_file_id, 0)
e = telegram.Voice(self.document.file_id, 0)
self.assertEqual(a, b)
self.assertEqual(hash(a), hash(b))

View file

@ -32,93 +32,32 @@ class FileTest(BaseTest, unittest.TestCase):
"""This object represents Tests for Telegram File."""
def setUp(self):
self.audio_file_id = 'BQADAQADDwADHyP1B6PSPq2HjX8kAg'
self.document_file_id = 'BQADAQADpAADHyP1B04ipZxJTe2BAg'
self.sticker_file_id = 'BQADAQADHAADyIsGAAFZfq1bphjqlgI'
self.video_file_id = 'BAADAQADXwADHyP1BwJFTcmY2RYCAg'
self.voice_file_id = 'AwADAQADTgADHyP1B_mbw34svXPHAg'
self.json_dict = {
'file_id': self.audio_file_id,
'file_id': "NOTVALIDDONTMATTER",
'file_path':
'https://api.telegram.org/file/bot133505823:AAHZFMHno3mzVLErU5b5jJvaeG--qUyLyG0/document/file_3',
'file_size': 28232
}
def test_get_and_download_file_audio(self):
newFile = self._bot.getFile(self.audio_file_id)
self.assertEqual(newFile.file_size, 28232)
self.assertEqual(newFile.file_id, self.audio_file_id)
self.assertTrue(newFile.file_path.startswith('https://'))
newFile.download('telegram.mp3')
self.assertTrue(os.path.isfile('telegram.mp3'))
def test_get_and_download_file_document(self):
newFile = self._bot.getFile(self.document_file_id)
self.assertEqual(newFile.file_size, 12948)
self.assertEqual(newFile.file_id, self.document_file_id)
self.assertTrue(newFile.file_path.startswith('https://'))
newFile.download('telegram.png')
self.assertTrue(os.path.isfile('telegram.png'))
def test_get_and_download_file_sticker(self):
newFile = self._bot.getFile(self.sticker_file_id)
self.assertEqual(newFile.file_size, 39518)
self.assertEqual(newFile.file_id, self.sticker_file_id)
self.assertTrue(newFile.file_path.startswith('https://'))
newFile.download('telegram.webp')
self.assertTrue(os.path.isfile('telegram.webp'))
def test_get_and_download_file_video(self):
newFile = self._bot.getFile(self.video_file_id)
self.assertEqual(newFile.file_size, 326534)
self.assertEqual(newFile.file_id, self.video_file_id)
self.assertTrue(newFile.file_path.startswith('https://'))
newFile.download('telegram.mp4')
self.assertTrue(os.path.isfile('telegram.mp4'))
def test_get_and_download_file_voice(self):
newFile = self._bot.getFile(self.voice_file_id)
self.assertEqual(newFile.file_size, 9199)
self.assertEqual(newFile.file_id, self.voice_file_id)
self.assertTrue(newFile.file_path.startswith('https://'))
newFile.download('telegram.ogg')
self.assertTrue(os.path.isfile('telegram.ogg'))
def test_file_de_json(self):
newFile = telegram.File.de_json(self.json_dict, self._bot)
new_file = telegram.File.de_json(self.json_dict, self._bot)
self.assertEqual(newFile.file_id, self.json_dict['file_id'])
self.assertEqual(newFile.file_path, self.json_dict['file_path'])
self.assertEqual(newFile.file_size, self.json_dict['file_size'])
self.assertEqual(new_file.file_id, self.json_dict['file_id'])
self.assertEqual(new_file.file_path, self.json_dict['file_path'])
self.assertEqual(new_file.file_size, self.json_dict['file_size'])
def test_file_to_json(self):
newFile = telegram.File.de_json(self.json_dict, self._bot)
new_file = telegram.File.de_json(self.json_dict, self._bot)
self.assertTrue(self.is_json(newFile.to_json()))
self.assertTrue(self.is_json(new_file.to_json()))
def test_file_to_dict(self):
newFile = telegram.File.de_json(self.json_dict, self._bot)
new_file = telegram.File.de_json(self.json_dict, self._bot).to_dict()
self.assertTrue(self.is_dict(newFile.to_dict()))
self.assertEqual(newFile['file_id'], self.json_dict['file_id'])
self.assertEqual(newFile['file_path'], self.json_dict['file_path'])
self.assertEqual(newFile['file_size'], self.json_dict['file_size'])
self.assertTrue(self.is_dict(new_file))
self.assertEqual(new_file['file_id'], self.json_dict['file_id'])
self.assertEqual(new_file['file_path'], self.json_dict['file_path'])
self.assertEqual(new_file['file_size'], self.json_dict['file_size'])
def test_error_get_empty_file_id(self):
json_dict = self.json_dict
@ -126,7 +65,8 @@ class FileTest(BaseTest, unittest.TestCase):
del (json_dict['file_path'])
del (json_dict['file_size'])
self.assertRaises(telegram.TelegramError, lambda: self._bot.getFile(**json_dict))
with self.assertRaises(telegram.TelegramError):
self._bot.getFile(**json_dict)
def test_error_file_without_required_args(self):
json_dict = self.json_dict
@ -135,14 +75,16 @@ class FileTest(BaseTest, unittest.TestCase):
del (json_dict['file_path'])
del (json_dict['file_size'])
self.assertRaises(TypeError, lambda: self._bot.getFile(**json_dict))
with self.assertRaises(TypeError):
self._bot.getFile(**json_dict)
def test_equality(self):
a = telegram.File(self.audio_file_id, self._bot)
b = telegram.File(self.audio_file_id, self._bot)
c = telegram.File(self.audio_file_id, None)
d = telegram.File(self.document_file_id, self._bot)
e = telegram.Voice(self.audio_file_id, 0)
a = telegram.File("DOESNTMATTER", self._bot)
b = telegram.File("DOESNTMATTER", self._bot)
c = telegram.File("DOESNTMATTER", None)
d = telegram.File("DOESNTMATTER2", self._bot)
e = telegram.Voice("DOESNTMATTER", 0)
self.assertEqual(a, b)
self.assertEqual(hash(a), hash(b))

View file

@ -18,111 +18,140 @@
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents Tests for Telegram Photo"""
from io import BytesIO
import sys
import unittest
import os
import unittest
from io import BytesIO
from flaky import flaky
sys.path.append('.')
import telegram
from tests.base import BaseTest, timeout
from tests.bots import get_bot
class PhotoTest(BaseTest, unittest.TestCase):
"""This object represents Tests for Telegram Photo."""
@classmethod
def setUpClass(cls):
cls.caption = u'PhotoTest - Caption'
cls.photo_file_url = 'https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/tests/data/telegram.jpg'
bot_info = get_bot()
cls._chat_id = bot_info['chat_id']
cls._bot = telegram.Bot(bot_info['token'])
photo_file = open('tests/data/telegram.jpg', 'rb')
photo = cls._bot.send_photo(cls._chat_id, photo=photo_file, timeout=10).photo
cls.thumb, cls.photo = photo
# Make sure file has been uploaded.
# Simple assertions PY2 Only
assert isinstance(cls.photo, telegram.PhotoSize)
assert isinstance(cls.thumb, telegram.PhotoSize)
assert isinstance(cls.photo.file_id, str)
assert isinstance(cls.thumb.file_id, str)
assert cls.photo.file_id is not ''
assert cls.thumb.file_id is not ''
def setUp(self):
self.photo_file = open('tests/data/telegram.jpg', 'rb')
self.photo_file_id = 'AgADAQADgEsyGx8j9QfmDMmwkPBrFcKRzy8ABHW8ul9nW7FoNHYBAAEC'
self.photo_file_url = 'https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/tests/data/telegram.jpg'
self.photo_bytes_jpg_no_standard = 'tests/data/telegram_no_standard_header.jpg'
self.width = 300
self.height = 300
self.thumb = {
'width': 90,
'height': 90,
'file_id': 'AgADAQADgEsyGx8j9QeYW9oDz2mKRsKRzy8ABD64nkFkjujeNXYBAAEC',
'file_size': 1478
}
self.file_size = 10209
# caption is part of sendPhoto method but not Photo object
self.caption = u'PhotoTest - Caption'
self.json_dict = {
'file_id': self.photo_file_id,
'width': self.width,
'height': self.height,
'file_size': self.file_size
'file_id': self.photo.file_id,
'width': self.photo.width,
'height': self.photo.height,
'file_size': self.photo.file_size
}
def test_expected_values(self):
self.assertEqual(self.photo.width, 300)
self.assertEqual(self.photo.height, 300)
self.assertEqual(self.photo.file_size, 10209)
self.assertEqual(self.thumb.width, 90)
self.assertEqual(self.thumb.height, 90)
self.assertEqual(self.thumb.file_size, 1478)
@flaky(3, 1)
@timeout(10)
def test_sendphotoo_all_args(self):
message = self._bot.sendPhoto(self._chat_id, self.photo_file, caption=self.caption)
def test_sendphoto_all_args(self):
message = self._bot.sendPhoto(self._chat_id, self.photo_file, caption=self.caption, disable_notification=False)
thumb, photo = message.photo
self.assertTrue(isinstance(thumb.file_id, str))
self.assertIsInstance(thumb, telegram.PhotoSize)
self.assertIsInstance(thumb.file_id, str)
self.assertNotEqual(thumb.file_id, '')
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
self.assertEqual(thumb.width, self.thumb['width'])
self.assertEqual(thumb.height, self.thumb['height'])
self.assertEqual(thumb.file_size, self.thumb['file_size'])
self.assertEqual(thumb.width, self.thumb.width)
self.assertEqual(thumb.height, self.thumb.height)
self.assertEqual(thumb.file_size, self.thumb.file_size)
self.assertTrue(isinstance(photo.file_id, str))
self.assertIsInstance(photo, telegram.PhotoSize)
self.assertIsInstance(photo.file_id, str)
self.assertNotEqual(photo.file_id, '')
self.assertTrue(isinstance(photo, telegram.PhotoSize))
self.assertEqual(photo.width, self.width)
self.assertEqual(photo.height, self.height)
self.assertEqual(photo.file_size, self.file_size)
self.assertEqual(photo.width, self.photo.width)
self.assertEqual(photo.height, self.photo.height)
self.assertEqual(photo.file_size, self.photo.file_size)
self.assertEqual(message.caption, self.caption)
@flaky(3, 1)
@timeout(10)
def test_send_photo_jpg_file(self):
message = self._bot.sendPhoto(self._chat_id, self.photo_file)
def test_get_and_download_photo(self):
new_file = self._bot.getFile(self.photo.file_id)
thumb, photo = message.photo
self.assertEqual(new_file.file_size, self.photo.file_size)
self.assertEqual(new_file.file_id, self.photo.file_id)
self.assertTrue(new_file.file_path.startswith('https://'))
self.assertTrue(isinstance(thumb.file_id, str))
self.assertNotEqual(thumb.file_id, '')
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
self.assertEqual(thumb.width, self.thumb['width'])
self.assertEqual(thumb.height, self.thumb['height'])
self.assertEqual(thumb.file_size, self.thumb['file_size'])
new_file.download('telegram.jpg')
self.assertTrue(os.path.isfile('telegram.jpg'))
self.assertTrue(isinstance(photo.file_id, str))
self.assertNotEqual(photo.file_id, '')
self.assertTrue(isinstance(photo, telegram.PhotoSize))
self.assertEqual(photo.width, self.width)
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):
message = self._bot.sendPhoto(self._chat_id, self.photo_file_url)
message = self._bot.sendPhoto(self._chat_id, photo=self.photo_file_url)
thumb, photo = message.photo
self.assertTrue(isinstance(thumb.file_id, str))
self.assertIsInstance(thumb, telegram.PhotoSize)
self.assertIsInstance(thumb.file_id, str)
self.assertNotEqual(thumb.file_id, '')
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
self.assertEqual(thumb.width, self.thumb['width'])
self.assertEqual(thumb.height, self.thumb['height'])
self.assertEqual(thumb.file_size, self.thumb['file_size'])
self.assertEqual(thumb.width, self.thumb.width)
self.assertEqual(thumb.height, self.thumb.height)
self.assertEqual(thumb.file_size, self.thumb.file_size)
self.assertTrue(isinstance(photo.file_id, str))
self.assertIsInstance(photo, telegram.PhotoSize)
self.assertIsInstance(photo.file_id, str)
self.assertNotEqual(photo.file_id, '')
self.assertEqual(photo.width, self.photo.width)
self.assertEqual(photo.height, self.photo.height)
self.assertEqual(photo.file_size, self.photo.file_size)
@flaky(3, 1)
@timeout(10)
def test_send_photo_url_png_file(self):
message = self._bot.sendPhoto(
photo='http://dummyimage.com/600x400/000/fff.png&text=telegram', chat_id=self._chat_id)
photo = message.photo[-1]
self.assertIsInstance(photo, telegram.PhotoSize)
self.assertIsInstance(photo.file_id, str)
self.assertNotEqual(photo.file_id, '')
@flaky(3, 1)
@timeout(10)
def test_send_photo_url_gif_file(self):
message = self._bot.sendPhoto(
photo='http://dummyimage.com/600x400/000/fff.png&text=telegram', chat_id=self._chat_id)
photo = message.photo[-1]
self.assertIsInstance(photo, telegram.PhotoSize)
self.assertIsInstance(photo.file_id, str)
self.assertNotEqual(photo.file_id, '')
self.assertTrue(isinstance(photo, telegram.PhotoSize))
self.assertEqual(photo.width, self.width)
self.assertEqual(photo.height, self.height)
self.assertEqual(photo.file_size, self.file_size)
@flaky(3, 1)
@timeout(10)
@ -142,53 +171,62 @@ class PhotoTest(BaseTest, unittest.TestCase):
raw_bytes = BytesIO(open(self.photo_bytes_jpg_no_standard, 'rb').read())
message = self._bot.sendPhoto(self._chat_id, photo=raw_bytes)
photo = message.photo[-1]
self.assertTrue(isinstance(photo.file_id, str))
self.assertIsInstance(photo.file_id, str)
self.assertNotEqual(photo.file_id, '')
self.assertTrue(isinstance(photo, telegram.PhotoSize))
self.assertIsInstance(photo, telegram.PhotoSize)
self.assertEqual(photo.width, 1920)
self.assertEqual(photo.height, 1080)
self.assertEqual(photo.file_size, 30907)
@flaky(3, 1)
@timeout(10)
def test_silent_send_photo(self):
message = self._bot.sendPhoto(photo=self.photo_file, chat_id=self._chat_id, disable_notification=True)
thumb, photo = message.photo
self.assertIsInstance(thumb, telegram.PhotoSize)
self.assertIsInstance(thumb.file_id, str)
self.assertNotEqual(thumb.file_id, '')
self.assertIsInstance(photo, telegram.PhotoSize)
self.assertIsInstance(photo.file_id, str)
self.assertNotEqual(photo.file_id, '')
@flaky(3, 1)
@timeout(10)
def test_send_photo_resend(self):
message = self._bot.sendPhoto(chat_id=self._chat_id, photo=self.photo_file_id)
message = self._bot.sendPhoto(chat_id=self._chat_id, photo=self.photo.file_id)
thumb, photo = message.photo
self.assertEqual(thumb.file_id, self.thumb['file_id'])
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
self.assertEqual(thumb.width, self.thumb['width'])
self.assertEqual(thumb.height, self.thumb['height'])
self.assertIsInstance(thumb, telegram.PhotoSize)
self.assertEqual(thumb.file_id, self.thumb.file_id)
self.assertEqual(thumb.width, self.thumb.width)
self.assertEqual(thumb.height, self.thumb.height)
self.assertEqual(thumb.file_size, self.thumb.file_size)
self.assertEqual(photo.file_id, self.photo_file_id)
self.assertTrue(isinstance(photo, telegram.PhotoSize))
self.assertEqual(photo.width, self.width)
self.assertEqual(photo.height, self.height)
self.assertIsInstance(photo, telegram.PhotoSize)
self.assertEqual(photo.file_id, self.photo.file_id)
self.assertEqual(photo.width, self.photo.width)
self.assertEqual(photo.height, self.photo.height)
self.assertEqual(photo.file_size, self.photo.file_size)
def test_photo_de_json(self):
photo = telegram.PhotoSize.de_json(self.json_dict, self._bot)
self.assertEqual(photo.file_id, self.photo_file_id)
self.assertTrue(isinstance(photo, telegram.PhotoSize))
self.assertEqual(photo.width, self.width)
self.assertEqual(photo.height, self.height)
self.assertEqual(photo.file_size, self.file_size)
self.assertEqual(photo, self.photo)
def test_photo_to_json(self):
photo = telegram.PhotoSize.de_json(self.json_dict, self._bot)
self.assertTrue(self.is_json(photo.to_json()))
self.assertTrue(self.is_json(self.photo.to_json()))
def test_photo_to_dict(self):
photo = telegram.PhotoSize.de_json(self.json_dict, self._bot)
photo = self.photo.to_dict()
self.assertTrue(self.is_dict(photo.to_dict()))
self.assertEqual(photo['file_id'], self.photo_file_id)
self.assertTrue(isinstance(photo, telegram.PhotoSize))
self.assertEqual(photo['width'], self.width)
self.assertEqual(photo['height'], self.height)
self.assertEqual(photo['file_size'], self.file_size)
self.assertTrue(self.is_dict(photo))
self.assertEqual(photo['file_id'], self.photo.file_id)
self.assertEqual(photo['width'], self.photo.width)
self.assertEqual(photo['height'], self.photo.height)
self.assertEqual(photo['file_size'], self.photo.file_size)
@flaky(3, 1)
@timeout(10)
@ -198,9 +236,8 @@ class PhotoTest(BaseTest, unittest.TestCase):
del (json_dict['file_id'])
json_dict['photo'] = open(os.devnull, 'rb')
self.assertRaises(
telegram.TelegramError,
lambda: self._bot.sendPhoto(chat_id=self._chat_id, **json_dict))
with self.assertRaises(telegram.TelegramError):
self._bot.sendPhoto(chat_id=self._chat_id, **json_dict)
@flaky(3, 1)
@timeout(10)
@ -210,9 +247,8 @@ class PhotoTest(BaseTest, unittest.TestCase):
del (json_dict['file_id'])
json_dict['photo'] = ''
self.assertRaises(
telegram.TelegramError,
lambda: self._bot.sendPhoto(chat_id=self._chat_id, **json_dict))
with self.assertRaises(telegram.TelegramError):
self._bot.sendPhoto(chat_id=self._chat_id, **json_dict)
@flaky(3, 1)
@timeout(10)
@ -223,25 +259,30 @@ class PhotoTest(BaseTest, unittest.TestCase):
del (json_dict['width'])
del (json_dict['height'])
self.assertRaises(
TypeError,
lambda: self._bot.sendPhoto(chat_id=self._chat_id, **json_dict))
with self.assertRaises(TypeError):
self._bot.sendPhoto(chat_id=self._chat_id, **json_dict)
@flaky(3, 1)
@timeout(10)
def test_reply_photo(self):
"""Test for Message.reply_photo"""
message = self._bot.sendMessage(self._chat_id, '.')
message = message.reply_photo(self.photo_file)
thumb, photo = message.reply_photo(self.photo_file).photo
self.assertNotEqual(message.photo[0].file_id, '')
self.assertIsInstance(thumb, telegram.PhotoSize)
self.assertIsInstance(thumb.file_id, str)
self.assertNotEqual(thumb.file_id, '')
self.assertIsInstance(photo, telegram.PhotoSize)
self.assertIsInstance(photo.file_id, str)
self.assertNotEqual(photo.file_id, '')
def test_equality(self):
a = telegram.PhotoSize(self.photo_file_id, self.width, self.height)
b = telegram.PhotoSize(self.photo_file_id, self.width, self.height)
c = telegram.PhotoSize(self.photo_file_id, 0, 0)
d = telegram.PhotoSize("", self.width, self.height)
e = telegram.Sticker(self.photo_file_id, self.width, self.height)
a = telegram.PhotoSize(self.photo.file_id, self.photo.width, self.photo.height)
b = telegram.PhotoSize(self.photo.file_id, self.photo.width, self.photo.height)
c = telegram.PhotoSize(self.photo.file_id, 0, 0)
d = telegram.PhotoSize("", self.photo.width, self.photo.height)
e = telegram.Sticker(self.photo.file_id, self.photo.width, self.photo.height)
self.assertEqual(a, b)
self.assertEqual(hash(a), hash(b))

View file

@ -18,90 +18,149 @@
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents Tests for Telegram Sticker"""
import sys
import unittest
import os
import unittest
from flaky import flaky
from future.utils import PY2
sys.path.append('.')
import telegram
from tests.base import BaseTest, timeout
from tests.bots import get_bot
class StickerTest(BaseTest, unittest.TestCase):
"""This object represents Tests for Telegram Sticker."""
def setUp(self):
self.sticker_file_id = 'CAADAQADHAADyIsGAAFZfq1bphjqlgI'
self.width = 510
self.height = 512
self.thumb = {
'width': 90,
'height': 90,
'file_id': 'BQADAQADoQADHyP1B0mzJMVyzcB0Ag',
'file_size': 2364
}
self.emoji = telegram.Emoji.FLEXED_BICEPS
self.file_size = 39518
@classmethod
def setUpClass(cls):
cls.emoji = telegram.Emoji.FLEXED_BICEPS
cls.sticker_file_url = "https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/tests/data/telegram.webp" # noqa
bot_info = get_bot()
cls._chat_id = bot_info['chat_id']
cls._bot = telegram.Bot(bot_info['token'])
sticker_file = open('tests/data/telegram.webp', 'rb')
sticker = cls._bot.send_sticker(cls._chat_id, sticker=sticker_file, timeout=10).sticker
cls.sticker = sticker
cls.thumb = sticker.thumb
# Make sure file has been uploaded.
# Simple assertions PY2 Only
assert isinstance(cls.sticker, telegram.Sticker)
assert isinstance(cls.sticker.file_id, str)
assert cls.sticker.file_id is not ''
assert isinstance(cls.thumb, telegram.PhotoSize)
assert isinstance(cls.thumb.file_id, str)
assert cls.thumb.file_id is not ''
def setUp(self):
self.sticker_file = open('tests/data/telegram.webp', 'rb')
self.json_dict = {
'file_id': self.sticker_file_id,
'width': self.width,
'height': self.height,
'thumb': self.thumb,
'file_id': self.sticker.file_id,
'width': self.sticker.width,
'height': self.sticker.height,
'thumb': self.thumb.to_dict(),
'emoji': self.emoji,
'file_size': self.file_size
'file_size': self.sticker.file_size
}
def test_expected_values(self):
self.assertEqual(self.sticker.width, 510)
self.assertEqual(self.sticker.height, 512)
self.assertEqual(self.sticker.file_size, 39518)
self.assertEqual(self.thumb.width, 90)
self.assertEqual(self.thumb.height, 90)
self.assertEqual(self.thumb.file_size, 3672)
@flaky(3, 1)
@timeout(10)
def test_send_sticker_file(self):
pass
def test_send_sticker_all_args(self):
message = self._bot.sendSticker(chat_id=self._chat_id, sticker=self.sticker.file_id, disable_notification=False)
sticker = message.sticker
self.assertEqual(sticker, self.sticker)
@flaky(3, 1)
@timeout(10)
def test_get_and_download_sticker(self):
new_file = self._bot.getFile(self.sticker.file_id)
self.assertEqual(new_file.file_size, self.sticker.file_size)
self.assertEqual(new_file.file_id, self.sticker.file_id)
self.assertTrue(new_file.file_path.startswith('https://'))
new_file.download('telegram.webp')
self.assertTrue(os.path.isfile('telegram.webp'))
@flaky(3, 1)
@timeout(10)
def test_send_sticker_resend(self):
message = self._bot.sendSticker(chat_id=self._chat_id, sticker=self.sticker_file_id)
message = self._bot.sendSticker(chat_id=self._chat_id, sticker=self.sticker.file_id)
sticker = message.sticker
self.assertEqual(sticker.file_id, self.sticker_file_id)
self.assertEqual(sticker.width, self.width)
self.assertEqual(sticker.height, self.height)
self.assertTrue(isinstance(sticker.thumb, telegram.PhotoSize))
self.assertEqual(sticker.file_id, self.sticker.file_id)
self.assertEqual(sticker.width, self.sticker.width)
self.assertEqual(sticker.height, self.sticker.height)
self.assertIsInstance(sticker.thumb, telegram.PhotoSize)
self.assertEqual(sticker.file_size, self.sticker.file_size)
@flaky(3, 1)
@timeout(10)
def test_sticker_on_server_emoji(self):
server_file_id = "CAADAQADHAADyIsGAAFZfq1bphjqlgI"
message = self._bot.sendSticker(chat_id=self._chat_id, sticker=server_file_id)
sticker = message.sticker
if PY2:
self.assertEqual(sticker.emoji, self.emoji.decode('utf-8'))
else:
self.assertEqual(sticker.emoji, self.emoji)
# self.assertEqual(sticker.file_size, self.file_size) # TODO
@flaky(3, 1)
@timeout(10)
def test_send_sticker_from_url(self):
message = self._bot.sendSticker(chat_id=self._chat_id, sticker=self.sticker_file_url)
sticker = message.sticker
self.assertIsInstance(sticker, telegram.Sticker)
self.assertIsInstance(sticker.file_id, str)
self.assertNotEqual(sticker.file_id, '')
self.assertEqual(sticker.file_size, self.sticker.file_size)
self.assertEqual(sticker.height, self.sticker.height)
self.assertEqual(sticker.width, self.sticker.width)
thumb = sticker.thumb
self.assertIsInstance(thumb, telegram.PhotoSize)
self.assertIsInstance(thumb.file_id, str)
self.assertNotEqual(thumb.file_id, '')
self.assertEqual(thumb.file_size, self.thumb.file_size)
self.assertEqual(thumb.width, self.thumb.width)
self.assertEqual(thumb.height, self.thumb.height)
def test_sticker_de_json(self):
sticker = telegram.Sticker.de_json(self.json_dict, self._bot)
self.assertEqual(sticker.file_id, self.sticker_file_id)
self.assertEqual(sticker.width, self.width)
self.assertEqual(sticker.height, self.height)
self.assertTrue(isinstance(sticker.thumb, telegram.PhotoSize))
self.assertEqual(sticker.file_id, self.sticker.file_id)
self.assertEqual(sticker.width, self.sticker.width)
self.assertEqual(sticker.height, self.sticker.height)
self.assertIsInstance(sticker.thumb, telegram.PhotoSize)
self.assertEqual(sticker.emoji, self.emoji)
self.assertEqual(sticker.file_size, self.file_size)
self.assertEqual(sticker.file_size, self.sticker.file_size)
def test_sticker_to_json(self):
sticker = telegram.Sticker.de_json(self.json_dict, self._bot)
self.assertTrue(self.is_json(sticker.to_json()))
self.assertTrue(self.is_json(self.sticker.to_json()))
def test_sticker_to_dict(self):
sticker = telegram.Sticker.de_json(self.json_dict, self._bot)
sticker = self.sticker.to_dict()
self.assertEqual(sticker['file_id'], self.sticker_file_id)
self.assertEqual(sticker['width'], self.width)
self.assertEqual(sticker['height'], self.height)
self.assertTrue(isinstance(sticker['thumb'], telegram.PhotoSize))
self.assertEqual(sticker['emoji'], self.emoji)
self.assertEqual(sticker['file_size'], self.file_size)
self.is_dict(sticker)
self.assertEqual(sticker['file_id'], self.sticker.file_id)
self.assertEqual(sticker['width'], self.sticker.width)
self.assertEqual(sticker['height'], self.sticker.height)
self.assertIsInstance(sticker['thumb'], dict)
self.assertEqual(sticker['file_size'], self.sticker.file_size)
@flaky(3, 1)
@timeout(10)
@ -111,9 +170,8 @@ class StickerTest(BaseTest, unittest.TestCase):
del (json_dict['file_id'])
json_dict['sticker'] = open(os.devnull, 'rb')
self.assertRaises(
telegram.TelegramError,
lambda: self._bot.sendSticker(chat_id=self._chat_id, **json_dict))
with self.assertRaises(telegram.TelegramError):
self._bot.sendSticker(chat_id=self._chat_id, **json_dict)
@flaky(3, 1)
@timeout(10)
@ -123,9 +181,8 @@ class StickerTest(BaseTest, unittest.TestCase):
del (json_dict['file_id'])
json_dict['sticker'] = ''
self.assertRaises(
telegram.TelegramError,
lambda: self._bot.sendSticker(chat_id=self._chat_id, **json_dict))
with self.assertRaises(telegram.TelegramError):
self._bot.sendSticker(chat_id=self._chat_id, **json_dict)
@flaky(3, 1)
@timeout(10)
@ -134,25 +191,24 @@ class StickerTest(BaseTest, unittest.TestCase):
del (json_dict['file_id'])
self.assertRaises(
TypeError,
lambda: self._bot.sendSticker(chat_id=self._chat_id, **json_dict))
with self.assertRaises(TypeError):
self._bot.sendSticker(chat_id=self._chat_id, **json_dict)
@flaky(3, 1)
@timeout(10)
def test_reply_sticker(self):
"""Test for Message.reply_sticker"""
message = self._bot.sendMessage(self._chat_id, '.')
message = message.reply_sticker(self.sticker_file_id)
message = message.reply_sticker(self.sticker.file_id)
self.assertNotEqual(message.sticker.file_id, '')
def test_equality(self):
a = telegram.Sticker(self.sticker_file_id, self.width, self.height)
b = telegram.Sticker(self.sticker_file_id, self.width, self.height)
c = telegram.Sticker(self.sticker_file_id, 0, 0)
d = telegram.Sticker("", self.width, self.height)
e = telegram.PhotoSize(self.sticker_file_id, self.width, self.height)
a = telegram.Sticker(self.sticker.file_id, self.sticker.width, self.sticker.height)
b = telegram.Sticker(self.sticker.file_id, self.sticker.width, self.sticker.height)
c = telegram.Sticker(self.sticker.file_id, 0, 0)
d = telegram.Sticker("", self.sticker.width, self.sticker.height)
e = telegram.PhotoSize(self.sticker.file_id, self.sticker.width, self.sticker.height)
self.assertEqual(a, b)
self.assertEqual(hash(a), hash(b))

View file

@ -18,71 +18,56 @@
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents Tests for Telegram Video"""
import sys
import unittest
import os
import unittest
from flaky import flaky
sys.path.append('.')
import telegram
from tests.base import BaseTest, timeout
from tests.bots import get_bot
class VideoTest(BaseTest, unittest.TestCase):
"""This object represents Tests for Telegram Video."""
@classmethod
def setUpClass(cls):
cls.caption = u'VideoTest - Caption'
cls.video_file_url = 'https://python-telegram-bot.org/static/website/telegram.mp4'
bot_info = get_bot()
cls._chat_id = bot_info['chat_id']
cls._bot = telegram.Bot(bot_info['token'])
video_file = open('tests/data/telegram.mp4', 'rb')
video = cls._bot.send_video(cls._chat_id, video=video_file, timeout=10).video
cls.video = video
# Make sure file has been uploaded.
# Simple assertions PY2 Only
assert isinstance(cls.video, telegram.Video)
assert isinstance(cls.video.file_id, str)
assert cls.video.file_id is not ''
def setUp(self):
self.video_file = open('tests/data/telegram.mp4', 'rb')
self.video_file_id = 'BAADAQADXwADHyP1BwJFTcmY2RYCAg'
self.video_file_url = 'https://python-telegram-bot.org/static/website/telegram.mp4'
self.width = 360
self.height = 640
self.duration = 5
self.thumb = telegram.PhotoSize.de_json({
'file_id': 'AAQBABOMsecvAAQqqoY1Pee_MqcyAAIC',
'file_size': 645,
'height': 90,
'width': 51
}, self._bot)
self.thumb_from_url = telegram.PhotoSize.de_json({
'file_id': 'AAQEABPZU2EZAAQ_tPcvcRTF4i1GAQABAg',
'file_size': 645,
'height': 90,
'width': 51
}, self._bot)
self.mime_type = 'video/mp4'
self.file_size = 326534
# caption is part of sendVideo method but not Video object
self.caption = u'VideoTest - Caption'
self.json_dict = {
'file_id': self.video_file_id,
'width': self.width,
'height': self.height,
'duration': self.duration,
'thumb': self.thumb.to_dict(),
'mime_type': self.mime_type,
'file_size': self.file_size
'file_id': self.video.file_id,
'width': self.video.width,
'height': self.video.height,
'duration': self.video.duration,
'thumb': self.video.thumb.to_dict(),
'mime_type': self.video.mime_type,
'file_size': self.video.file_size
}
@flaky(3, 1)
@timeout(10)
def test_send_video_required_args_only(self):
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, None)
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, 'video/mp4')
self.assertEqual(video.file_size, self.file_size)
def test_expected_values(self):
self.assertEqual(self.video.width, 360)
self.assertEqual(self.video.height, 640)
self.assertEqual(self.video.duration, 5)
self.assertEqual(self.video.file_size, 326534)
self.assertEqual(self.video.mime_type, 'video/mp4')
@flaky(3, 1)
@timeout(10)
@ -91,69 +76,36 @@ class VideoTest(BaseTest, unittest.TestCase):
self._chat_id,
self.video_file,
timeout=10,
duration=self.duration,
caption=self.caption)
video = message.video
self.assertTrue(isinstance(video.file_id, str))
self.assertNotEqual(video.file_id, None)
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, 'video/mp4')
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):
message = self._bot.sendVideo(
chat_id=self._chat_id,
video=self.video_file,
timeout=10,
duration=self.duration,
caption=self.caption)
video = message.video
self.assertTrue(isinstance(video.file_id, str))
self.assertNotEqual(video.file_id, None)
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, 'video/mp4')
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):
message = self._bot.sendVideo(
chat_id=self._chat_id,
video=self.video_file,
timeout=10,
duration=self.duration,
duration=self.video.duration,
caption=self.caption,
filename='telegram_custom.mp4')
disable_notification=False)
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, 'video/mp4')
self.assertEqual(video.file_size, self.file_size)
self.assertNotEqual(video.file_id, None)
self.assertEqual(video.width, self.video.width)
self.assertEqual(video.height, self.video.height)
self.assertEqual(video.duration, self.video.duration)
self.assertEqual(video.thumb, self.video.thumb)
self.assertEqual(video.mime_type, self.video.mime_type)
self.assertEqual(video.file_size, self.video.file_size)
self.assertEqual(message.caption, self.caption)
@flaky(3, 1)
@timeout(10)
def test_get_and_download_video(self):
new_file = self._bot.getFile(self.video.file_id)
self.assertEqual(new_file.file_size, self.video.file_size)
self.assertEqual(new_file.file_id, self.video.file_id)
self.assertTrue(new_file.file_path.startswith('https://'))
new_file.download('telegram.mp4')
self.assertTrue(os.path.isfile('telegram.mp4'))
@flaky(3, 1)
@timeout(10)
def test_send_video_mp4_file_url(self):
@ -161,66 +113,55 @@ class VideoTest(BaseTest, unittest.TestCase):
chat_id=self._chat_id,
video=self.video_file_url,
timeout=10,
duration=self.duration,
caption=self.caption)
video = message.video
self.assertTrue(isinstance(video.file_id, str))
self.assertIsInstance(video.file_id, str)
self.assertNotEqual(video.file_id, None)
self.assertEqual(video.height, self.height)
self.assertEqual(video.duration, self.duration)
self.assertEqual(video.thumb, self.thumb_from_url)
self.assertEqual(video.mime_type, 'video/mp4')
self.assertEqual(video.file_size, self.file_size)
self.assertEqual(video.height, self.video.height)
self.assertEqual(video.duration, self.video.duration)
self.assertEqual(video.mime_type, self.video.mime_type)
self.assertEqual(video.file_size, self.video.file_size)
self.assertEqual(message.caption, self.caption)
thumb = video.thumb
self.assertEqual(thumb.height, self.video.thumb.height)
self.assertEqual(thumb.width, self.video.thumb.width)
self.assertEqual(thumb.file_size, self.video.thumb.file_size)
@flaky(3, 1)
@timeout(10)
def test_send_video_resend(self):
message = self._bot.sendVideo(
chat_id=self._chat_id,
video=self.video_file_id,
timeout=10,
duration=self.duration,
caption=self.caption)
video=self.video.file_id,
timeout=10)
video = message.video
self.assertEqual(video.file_id, self.video_file_id)
self.assertEqual(video.duration, 0)
self.assertEqual(video.thumb, None)
self.assertEqual(video.mime_type, 'video/mp4')
self.assertEqual(message.caption, self.caption)
self.assertEqual(video.file_id, self.video.file_id)
self.assertEqual(video.duration, self.video.duration)
self.assertEqual(video.thumb, self.video.thumb)
self.assertEqual(video.mime_type, self.video.mime_type)
def test_video_de_json(self):
video = telegram.Video.de_json(self.json_dict, self._bot)
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, self.thumb)
self.assertEqual(video.mime_type, self.mime_type)
self.assertEqual(video.file_size, self.file_size)
self.assertEqual(video, self.video)
def test_video_to_json(self):
video = telegram.Video.de_json(self.json_dict, self._bot)
self.assertTrue(self.is_json(video.to_json()))
self.assertTrue(self.is_json(self.video.to_json()))
def test_video_to_dict(self):
video = telegram.Video.de_json(self.json_dict, self._bot)
video = self.video.to_dict()
self.assertTrue(self.is_dict(video.to_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['mime_type'], self.mime_type)
self.assertEqual(video['file_size'], self.file_size)
self.assertTrue(self.is_dict(video))
self.assertEqual(video['file_id'], self.video.file_id)
self.assertEqual(video['width'], self.video.width)
self.assertEqual(video['height'], self.video.height)
self.assertEqual(video['duration'], self.video.duration)
self.assertEqual(video['mime_type'], self.video.mime_type)
self.assertEqual(video['file_size'], self.video.file_size)
@flaky(3, 1)
@timeout(10)
@ -230,10 +171,8 @@ class VideoTest(BaseTest, unittest.TestCase):
del (json_dict['file_id'])
json_dict['video'] = open(os.devnull, 'rb')
self.assertRaises(telegram.TelegramError,
lambda: self._bot.sendVideo(chat_id=self._chat_id,
timeout=10,
**json_dict))
with self.assertRaises(telegram.TelegramError):
self._bot.sendVideo(chat_id=self._chat_id, timeout=10, **json_dict)
@flaky(3, 1)
@timeout(10)
@ -243,23 +182,14 @@ class VideoTest(BaseTest, unittest.TestCase):
del (json_dict['file_id'])
json_dict['video'] = ''
self.assertRaises(telegram.TelegramError,
lambda: self._bot.sendVideo(chat_id=self._chat_id,
timeout=10,
**json_dict))
with self.assertRaises(telegram.TelegramError):
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):
json_dict = self.json_dict
del (json_dict['file_id'])
del (json_dict['duration'])
self.assertRaises(TypeError,
lambda: self._bot.sendVideo(chat_id=self._chat_id,
timeout=10,
**json_dict))
# Obsolete: only required args are chat_id and video. Both tested above
self.assertEqual(True, True)
@flaky(3, 1)
@timeout(10)
@ -271,11 +201,11 @@ class VideoTest(BaseTest, unittest.TestCase):
self.assertNotEqual(message.video.file_id, None)
def test_equality(self):
a = telegram.Video(self.video_file_id, self.width, self.height, self.duration)
b = telegram.Video(self.video_file_id, self.width, self.height, self.duration)
c = telegram.Video(self.video_file_id, 0, 0, 0)
d = telegram.Video("", self.width, self.height, self.duration)
e = telegram.Voice(self.video_file_id, self.duration)
a = telegram.Video(self.video.file_id, self.video.width, self.video.height, self.video.duration)
b = telegram.Video(self.video.file_id, self.video.width, self.video.height, self.video.duration)
c = telegram.Video(self.video.file_id, 0, 0, 0)
d = telegram.Video("", self.video.width, self.video.height, self.video.duration)
e = telegram.Voice(self.video.file_id, self.video.duration)
self.assertEqual(a, b)
self.assertEqual(hash(a), hash(b))

View file

@ -17,64 +17,52 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents Tests for Telegram VideoNote"""
import numbers
import sys
import unittest
import os
import unittest
from flaky import flaky
sys.path.append('.')
import telegram
from tests.base import BaseTest, timeout
from tests.bots import get_bot
class VideoNoteTest(BaseTest, unittest.TestCase):
"""This object represents Tests for Telegram VideoNote."""
@classmethod
def setUpClass(cls):
bot_info = get_bot()
cls._chat_id = bot_info['chat_id']
cls._bot = telegram.Bot(bot_info['token'])
videonote_file = open('tests/data/telegram2.mp4', 'rb')
video_note = cls._bot.send_video_note(cls._chat_id, video_note=videonote_file, timeout=10).video_note
cls.videonote = video_note
# Make sure file has been uploaded.
# Simple assertions PY2 Only
assert isinstance(cls.videonote, telegram.VideoNote)
assert isinstance(cls.videonote.file_id, str)
assert cls.videonote.file_id is not ''
def setUp(self):
self.videonote_file = open('tests/data/telegram2.mp4', 'rb')
self.videonote_file_id = 'DQADAQADBwAD5VIIRYemhHpbPmIQAg'
self.duration = 3
self.length = 1 # No bloody clue what this does
self.thumb = telegram.PhotoSize.de_json({
'file_id': 'AAQBABMsDPcvAAQX7NUVpGq-s34OAAIC',
'width': 90,
'file_size': 3043,
'height': 90
}, self._bot)
self.thumb_resend = telegram.PhotoSize.de_json({
'file_id': 'AAQBABOMsecvAAQqqoY1Pee_MqcyAAIC',
'width': 51,
'file_size': 645,
'height': 90
}, self._bot)
self.file_size = 132084
self.json_dict = {
'file_id': self.videonote_file_id,
'duration': self.duration,
'length': self.length,
'thumb': self.thumb.to_dict(),
'file_size': self.file_size
'file_id': self.videonote.file_id,
'duration': self.videonote.duration,
'length': self.videonote.length,
'thumb': self.videonote.thumb.to_dict(),
'file_size': self.videonote.file_size
}
@flaky(3, 1)
@timeout(10)
def test_send_videonote_required_args_only(self):
message = self._bot.sendVideoNote(self._chat_id, self.videonote_file, timeout=10)
videonote = message.video_note
self.assertTrue(isinstance(videonote.file_id, str))
self.assertNotEqual(videonote.file_id, None)
self.assertEqual(videonote.duration, self.duration)
# self.assertEqual(videonote.length, self.length)
self.assertIsInstance(videonote.length, numbers.Number)
self.assertEqual(videonote.thumb, self.thumb)
self.assertEqual(videonote.file_size, self.file_size)
def test_expected_values(self):
self.assertEqual(self.videonote.duration, 3)
self.assertEqual(self.videonote.length, 240)
self.assertEqual(self.videonote.file_size, 132084)
@flaky(3, 1)
@timeout(10)
@ -83,60 +71,65 @@ class VideoNoteTest(BaseTest, unittest.TestCase):
self._chat_id,
self.videonote_file,
timeout=10,
duration=self.duration,
length=self.length)
duration=self.videonote.duration,
length=self.videonote.length,
disable_notification=False)
videonote = message.video_note
self.assertTrue(isinstance(videonote.file_id, str))
self.assertIsInstance(videonote.file_id, str)
self.assertNotEqual(videonote.file_id, None)
# self.assertEqual(videonote.length, self.length)
self.assertIsInstance(videonote.length, numbers.Number)
self.assertEqual(videonote.duration, self.duration)
self.assertEqual(videonote.thumb, self.thumb)
self.assertEqual(videonote.file_size, self.file_size)
self.assertEqual(videonote.length, self.videonote.length)
self.assertEqual(videonote.duration, self.videonote.duration)
self.assertEqual(videonote.thumb, self.videonote.thumb)
self.assertEqual(videonote.file_size, self.videonote.file_size)
@flaky(3, 1)
@timeout(10)
def test_get_and_download_videonote(self):
new_file = self._bot.getFile(self.videonote.file_id)
self.assertEqual(new_file.file_size, self.videonote.file_size)
self.assertEqual(new_file.file_id, self.videonote.file_id)
self.assertTrue(new_file.file_path.startswith('https://'))
new_file.download('telegram2.mp4')
self.assertTrue(os.path.isfile('telegram2.mp4'))
@flaky(3, 1)
@timeout(10)
def test_send_videonote_resend(self):
message = self._bot.sendVideoNote(
chat_id=self._chat_id,
video_note=self.videonote_file_id,
video_note=self.videonote.file_id,
timeout=10
)
videonote = message.video_note
self.assertEqual(videonote.file_id, self.videonote_file_id)
# self.assertEqual(videonote.length, self.length)
self.assertIsInstance(videonote.length, numbers.Number)
self.assertEqual(videonote.duration, 5)
self.assertEqual(videonote.thumb, self.thumb_resend)
# Telegram doesn't send file_size for resends?
# self.assertEqual(videonote.file_size, self.file_size)
self.assertEqual(videonote.file_id, self.videonote.file_id)
self.assertEqual(videonote.length, self.videonote.length)
self.assertEqual(videonote.duration, self.videonote.duration)
self.assertEqual(videonote.thumb, self.videonote.thumb)
self.assertEqual(videonote.file_size, self.videonote.file_size)
def test_videonote_de_json(self):
videonote = telegram.VideoNote.de_json(self.json_dict, self._bot)
self.assertEqual(videonote.file_id, self.videonote_file_id)
self.assertEqual(videonote.duration, self.duration)
self.assertEqual(videonote.thumb, self.thumb)
self.assertEqual(videonote.length, self.length)
self.assertEqual(videonote.file_size, self.file_size)
self.assertEqual(videonote, self.videonote)
def test_videonote_to_json(self):
videonote = telegram.VideoNote.de_json(self.json_dict, self._bot)
self.assertTrue(self.is_json(videonote.to_json()))
self.assertTrue(self.is_json(self.videonote.to_json()))
def test_videonote_to_dict(self):
videonote = telegram.VideoNote.de_json(self.json_dict, self._bot)
videonote = self.videonote.to_dict()
self.assertTrue(self.is_dict(videonote.to_dict()))
self.assertEqual(videonote['file_id'], self.videonote_file_id)
self.assertEqual(videonote['duration'], self.duration)
self.assertEqual(videonote['length'], self.length)
self.assertEqual(videonote['file_size'], self.file_size)
self.assertTrue(self.is_dict(videonote))
self.assertEqual(videonote['file_id'], self.videonote.file_id)
self.assertEqual(videonote['duration'], self.videonote.duration)
self.assertEqual(videonote['length'], self.videonote.length)
self.assertEqual(videonote['file_size'], self.videonote.file_size)
@flaky(3, 1)
@timeout(10)
@ -146,10 +139,8 @@ class VideoNoteTest(BaseTest, unittest.TestCase):
del (json_dict['file_id'])
json_dict['video_note'] = open(os.devnull, 'rb')
self.assertRaises(telegram.TelegramError,
lambda: self._bot.sendVideoNote(chat_id=self._chat_id,
timeout=10,
**json_dict))
with self.assertRaises(telegram.TelegramError):
self._bot.sendVideoNote(chat_id=self._chat_id, timeout=10, **json_dict)
@flaky(3, 1)
@timeout(10)
@ -159,27 +150,24 @@ class VideoNoteTest(BaseTest, unittest.TestCase):
del (json_dict['file_id'])
json_dict['video_note'] = ''
self.assertRaises(telegram.TelegramError,
lambda: self._bot.sendVideoNote(chat_id=self._chat_id,
timeout=10,
**json_dict))
with self.assertRaises(telegram.TelegramError):
self._bot.sendVideoNote(chat_id=self._chat_id, timeout=10, **json_dict)
@flaky(3, 1)
@timeout(10)
def test_reply_videonote(self):
"""Test for Message.reply_videonote"""
message = self._bot.sendMessage(self._chat_id, '.')
# Length is needed... see first test
message = message.reply_video_note(self.videonote_file, length=self.length)
message = message.reply_video_note(self.videonote_file)
self.assertNotEqual(message.video_note.file_id, None)
def test_equality(self):
a = telegram.VideoNote(self.videonote_file_id, self.length, self.duration)
b = telegram.VideoNote(self.videonote_file_id, self.length, self.duration)
c = telegram.VideoNote(self.videonote_file_id, 0, 0, 0)
d = telegram.VideoNote("", self.length, self.duration)
e = telegram.Voice(self.videonote_file_id, self.duration)
a = telegram.VideoNote(self.videonote.file_id, self.videonote.length, self.videonote.duration)
b = telegram.VideoNote(self.videonote.file_id, self.videonote.length, self.videonote.duration)
c = telegram.VideoNote(self.videonote.file_id, 0, 0, 0)
d = telegram.VideoNote("", self.videonote.length, self.videonote.duration)
e = telegram.Voice(self.videonote.file_id, self.videonote.duration)
self.assertEqual(a, b)
self.assertEqual(hash(a), hash(b))
@ -194,5 +182,6 @@ class VideoNoteTest(BaseTest, unittest.TestCase):
self.assertNotEqual(a, e)
self.assertNotEqual(hash(a), hash(e))
if __name__ == '__main__':
unittest.main()

View file

@ -18,50 +18,55 @@
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents Tests for Telegram Voice"""
import sys
import unittest
import os
import unittest
from flaky import flaky
sys.path.append('.')
import telegram
from tests.base import BaseTest, timeout
from tests.bots import get_bot
class VoiceTest(BaseTest, unittest.TestCase):
"""This object represents Tests for Telegram Voice."""
@classmethod
def setUpClass(cls):
cls.voice_file_url = 'https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/tests/data/telegram.ogg'
cls.caption = u"Test voice"
voice_file = open('tests/data/telegram.ogg', 'rb')
bot_info = get_bot()
cls._chat_id = bot_info['chat_id']
cls._bot = telegram.Bot(bot_info['token'])
voice = cls._bot.send_voice(cls._chat_id, voice=voice_file).voice
cls.voice = voice
# Make sure file has been uploaded.
# Simple assertions PY2 Only
assert isinstance(cls.voice, telegram.Voice)
assert isinstance(cls.voice.file_id, str)
assert cls.voice.file_id is not ''
def setUp(self):
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 = 3
self.caption = "Test voice"
self.mime_type = 'audio/ogg'
self.file_size = 9199
self.json_dict = {
'file_id': self.voice_file_id,
'duration': self.duration,
'file_id': self.voice.file_id,
'duration': self.voice.duration,
'caption': self.caption,
'mime_type': self.mime_type,
'file_size': self.file_size
'mime_type': self.voice.mime_type,
'file_size': self.voice.file_size
}
@flaky(3, 1)
@timeout(10)
def test_send_voice_required_args_only(self):
message = self._bot.sendVoice(self._chat_id, self.voice_file)
voice = message.voice
self.assertTrue(isinstance(voice.file_id, str))
self.assertNotEqual(voice.file_id, '')
self.assertEqual(voice.duration, self.duration)
self.assertEqual(voice.mime_type, self.mime_type)
self.assertEqual(voice.file_size, self.file_size)
def test_expected_values(self):
self.assertEqual(self.voice.duration, 3)
self.assertEqual(self.voice.mime_type, 'audio/ogg')
self.assertEqual(self.voice.file_size, 9199)
@flaky(3, 1)
@timeout(10)
@ -69,73 +74,48 @@ class VoiceTest(BaseTest, unittest.TestCase):
message = self._bot.sendVoice(
self._chat_id,
self.voice_file,
duration=self.duration,
duration=self.voice.duration,
caption=self.caption,
mime_type=self.mime_type,
file_size=self.file_size)
mime_type=self.voice.mime_type,
file_size=self.voice.file_size,
disable_notification=False)
self.assertEqual(message.caption, self.caption)
voice = message.voice
self.assertTrue(isinstance(voice.file_id, str))
self.assertIsInstance(voice.file_id, str)
self.assertNotEqual(voice.file_id, '')
self.assertEqual(voice.duration, self.duration)
self.assertEqual(voice.mime_type, self.mime_type)
self.assertEqual(voice.file_size, self.file_size)
self.assertEqual(voice.duration, self.voice.duration)
self.assertEqual(voice.mime_type, self.voice.mime_type)
self.assertEqual(voice.file_size, self.voice.file_size)
@flaky(3, 1)
@timeout(10)
def test_send_voice_ogg_file(self):
message = self._bot.sendVoice(
chat_id=self._chat_id,
voice=self.voice_file,
duration=self.duration,
caption=self.caption)
def test_get_and_download_voice(self):
new_file = self._bot.getFile(self.voice.file_id)
self.assertEqual(message.caption, self.caption)
self.assertEqual(new_file.file_size, self.voice.file_size)
self.assertEqual(new_file.file_id, self.voice.file_id)
self.assertTrue(new_file.file_path.startswith('https://'))
voice = message.voice
new_file.download('telegram.ogg')
self.assertTrue(isinstance(voice.file_id, str))
self.assertNotEqual(voice.file_id, '')
self.assertEqual(voice.duration, self.duration)
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):
message = self._bot.sendVoice(
chat_id=self._chat_id,
voice=self.voice_file,
duration=self.duration,
caption=self.caption,
filename='telegram_custom.ogg')
self.assertEqual(message.caption, self.caption)
voice = message.voice
self.assertTrue(isinstance(voice.file_id, str))
self.assertNotEqual(voice.file_id, '')
self.assertEqual(voice.duration, self.duration)
self.assertEqual(voice.mime_type, self.mime_type)
self.assertEqual(voice.file_size, self.file_size)
self.assertTrue(os.path.isfile('telegram.ogg'))
@flaky(3, 1)
@timeout(10)
def test_send_voice_ogg_url_file(self):
message = self._bot.sendVoice(
chat_id=self._chat_id, voice=self.voice_file_url, duration=self.duration)
chat_id=self._chat_id, voice=self.voice_file_url, duration=self.voice.duration)
voice = message.voice
self.assertTrue(isinstance(voice.file_id, str))
self.assertIsInstance(voice.file_id, str)
self.assertNotEqual(voice.file_id, '')
self.assertEqual(voice.duration, self.duration)
self.assertEqual(voice.mime_type, self.mime_type)
self.assertEqual(voice.file_size, self.file_size)
self.assertEqual(voice.duration, self.voice.duration)
self.assertEqual(voice.mime_type, self.voice.mime_type)
self.assertEqual(voice.file_size, self.voice.file_size)
@flaky(3, 1)
@timeout(10)
@ -143,57 +123,48 @@ class VoiceTest(BaseTest, unittest.TestCase):
message = self._bot.sendVoice(
chat_id=self._chat_id,
voice=self.voice_file_url,
duration=self.duration,
duration=self.voice.duration,
caption=self.caption)
self.assertEqual(message.caption, self.caption)
voice = message.voice
self.assertTrue(isinstance(voice.file_id, str))
self.assertIsInstance(voice.file_id, str)
self.assertNotEqual(voice.file_id, '')
self.assertEqual(voice.duration, self.duration)
self.assertEqual(voice.mime_type, self.mime_type)
self.assertEqual(voice.file_size, self.file_size)
self.assertEqual(voice.duration, self.voice.duration)
self.assertEqual(voice.mime_type, self.voice.mime_type)
self.assertEqual(voice.file_size, self.voice.file_size)
@flaky(3, 1)
@timeout(10)
def test_send_voice_resend(self):
message = self._bot.sendVoice(
chat_id=self._chat_id,
voice=self.voice_file_id,
duration=self.duration,
caption=self.caption)
self.assertEqual(message.caption, self.caption)
voice=self.voice.file_id)
voice = message.voice
self.assertEqual(voice.file_id, self.voice_file_id)
self.assertEqual(voice.duration, 0)
self.assertEqual(voice.mime_type, self.mime_type)
self.assertEqual(voice.file_id, self.voice.file_id)
self.assertEqual(voice.duration, self.voice.duration)
self.assertEqual(voice.mime_type, self.voice.mime_type)
def test_voice_de_json(self):
voice = telegram.Voice.de_json(self.json_dict, self._bot)
self.assertEqual(voice.file_id, self.voice_file_id)
self.assertEqual(voice.duration, self.duration)
self.assertEqual(voice.mime_type, self.mime_type)
self.assertEqual(voice.file_size, self.file_size)
self.assertEqual(voice, self.voice)
def test_voice_to_json(self):
voice = telegram.Voice.de_json(self.json_dict, self._bot)
self.assertTrue(self.is_json(voice.to_json()))
self.assertTrue(self.is_json(self.voice.to_json()))
def test_voice_to_dict(self):
voice = telegram.Voice.de_json(self.json_dict, self._bot)
voice = self.voice.to_dict()
self.assertTrue(self.is_dict(voice.to_dict()))
self.assertEqual(voice['file_id'], self.voice_file_id)
self.assertEqual(voice['duration'], self.duration)
self.assertEqual(voice['mime_type'], self.mime_type)
self.assertEqual(voice['file_size'], self.file_size)
self.assertTrue(self.is_dict(voice))
self.assertEqual(voice['file_id'], self.voice.file_id)
self.assertEqual(voice['duration'], self.voice.duration)
self.assertEqual(voice['mime_type'], self.voice.mime_type)
self.assertEqual(voice['file_size'], self.voice.file_size)
@flaky(3, 1)
@timeout(10)
@ -203,9 +174,8 @@ class VoiceTest(BaseTest, unittest.TestCase):
del (json_dict['file_id'])
json_dict['voice'] = open(os.devnull, 'rb')
self.assertRaises(
telegram.TelegramError,
lambda: self._bot.sendVoice(chat_id=self._chat_id, **json_dict))
with self.assertRaises(telegram.TelegramError):
self._bot.sendVoice(chat_id=self._chat_id, **json_dict)
@flaky(3, 1)
@timeout(10)
@ -215,9 +185,8 @@ class VoiceTest(BaseTest, unittest.TestCase):
del (json_dict['file_id'])
json_dict['voice'] = ''
self.assertRaises(
telegram.TelegramError,
lambda: self._bot.sendVoice(chat_id=self._chat_id, **json_dict))
with self.assertRaises(telegram.TelegramError):
self._bot.sendVoice(chat_id=self._chat_id, **json_dict)
@flaky(3, 1)
@timeout(10)
@ -225,11 +194,9 @@ class VoiceTest(BaseTest, unittest.TestCase):
json_dict = self.json_dict
del (json_dict['file_id'])
del (json_dict['duration'])
self.assertRaises(
TypeError,
lambda: self._bot.sendVoice(chat_id=self._chat_id, **json_dict))
with self.assertRaises(TypeError):
self._bot.sendVoice(chat_id=self._chat_id, **json_dict)
@flaky(3, 1)
@timeout(10)
@ -241,11 +208,11 @@ class VoiceTest(BaseTest, unittest.TestCase):
self.assertNotEqual(message.voice.file_id, '')
def test_equality(self):
a = telegram.Voice(self.voice_file_id, self.duration)
b = telegram.Voice(self.voice_file_id, self.duration)
c = telegram.Voice(self.voice_file_id, 0)
d = telegram.Voice("", self.duration)
e = telegram.Audio(self.voice_file_id, self.duration)
a = telegram.Voice(self.voice.file_id, self.voice.duration)
b = telegram.Voice(self.voice.file_id, self.voice.duration)
c = telegram.Voice(self.voice.file_id, 0)
d = telegram.Voice("", self.voice.duration)
e = telegram.Audio(self.voice.file_id, self.voice.duration)
self.assertEqual(a, b)
self.assertEqual(hash(a), hash(b))