2015-09-07 16:22:25 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2016-01-05 14:12:03 +01:00
|
|
|
# Copyright (C) 2015-2016
|
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
2015-09-07 16:22:25 +02:00
|
|
|
#
|
|
|
|
# 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/].
|
|
|
|
|
|
|
|
"""This module contains a object that represents Tests for Telegram Video"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import unittest
|
|
|
|
import sys
|
2016-02-21 12:52:47 +01:00
|
|
|
from flaky import flaky
|
|
|
|
|
2015-09-07 16:22:25 +02:00
|
|
|
sys.path.append('.')
|
|
|
|
|
|
|
|
import telegram
|
2016-02-22 00:38:26 +01:00
|
|
|
from tests.base import BaseTest, timeout
|
2015-09-07 16:22:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
class VideoTest(BaseTest, unittest.TestCase):
|
|
|
|
"""This object represents Tests for Telegram Video."""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.video_file = open('tests/data/telegram.mp4', 'rb')
|
|
|
|
self.video_file_id = 'BAADAQADXwADHyP1BwJFTcmY2RYCAg'
|
2016-01-12 00:11:10 +01:00
|
|
|
self.video_file_url = 'https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/tests/data/telegram.mp4'
|
2015-09-07 16:22:25 +02:00
|
|
|
self.width = 360
|
|
|
|
self.height = 640
|
2016-02-27 21:49:13 +01:00
|
|
|
self.duration = 5
|
|
|
|
self.thumb = telegram.PhotoSize.de_json(
|
|
|
|
{'file_id': 'AAQBABOMsecvAAQqqoY1Pee_MqcyAAIC',
|
|
|
|
'file_size': 645,
|
|
|
|
'height': 90,
|
|
|
|
'width': 51})
|
2015-09-07 16:22:25 +02:00
|
|
|
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,
|
2016-02-27 21:49:13 +01:00
|
|
|
'thumb': self.thumb.to_dict(),
|
2015-09-07 16:22:25 +02:00
|
|
|
'mime_type': self.mime_type,
|
|
|
|
'file_size': self.file_size
|
|
|
|
}
|
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2015-09-07 16:22:25 +02:00
|
|
|
def test_send_video_required_args_only(self):
|
|
|
|
message = self._bot.sendVideo(self._chat_id,
|
2016-02-27 22:01:22 +01:00
|
|
|
self.video_file,
|
|
|
|
timeout=10)
|
2015-09-07 16:22:25 +02:00
|
|
|
|
|
|
|
video = message.video
|
|
|
|
|
|
|
|
self.assertTrue(isinstance(video.file_id, str))
|
|
|
|
self.assertNotEqual(video.file_id, '')
|
2016-02-27 21:49:13 +01:00
|
|
|
self.assertEqual(video.width, self.width)
|
|
|
|
self.assertEqual(video.height, self.height)
|
|
|
|
self.assertEqual(video.duration, self.duration)
|
|
|
|
self.assertEqual(video.thumb, self.thumb)
|
2015-09-07 16:22:25 +02:00
|
|
|
self.assertEqual(video.mime_type, '')
|
|
|
|
self.assertEqual(video.file_size, self.file_size)
|
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2015-09-07 16:22:25 +02:00
|
|
|
def test_send_video_all_args(self):
|
|
|
|
message = self._bot.sendVideo(self._chat_id,
|
|
|
|
self.video_file,
|
2016-02-27 22:01:22 +01:00
|
|
|
timeout=10,
|
2015-09-07 16:22:25 +02:00
|
|
|
duration=self.duration,
|
|
|
|
caption=self.caption)
|
|
|
|
|
|
|
|
video = message.video
|
|
|
|
|
|
|
|
self.assertTrue(isinstance(video.file_id, str))
|
|
|
|
self.assertNotEqual(video.file_id, '')
|
2016-02-27 21:49:13 +01:00
|
|
|
self.assertEqual(video.width, self.width)
|
|
|
|
self.assertEqual(video.height, self.height)
|
2015-09-07 16:22:25 +02:00
|
|
|
self.assertEqual(video.duration, self.duration)
|
2016-02-27 21:49:13 +01:00
|
|
|
self.assertEqual(video.thumb, self.thumb)
|
2015-09-07 16:22:25 +02:00
|
|
|
self.assertEqual(video.mime_type, '')
|
|
|
|
self.assertEqual(video.file_size, self.file_size)
|
|
|
|
|
|
|
|
self.assertEqual(message.caption, self.caption)
|
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2015-09-07 16:22:25 +02:00
|
|
|
def test_send_video_mp4_file(self):
|
|
|
|
message = self._bot.sendVideo(chat_id=self._chat_id,
|
|
|
|
video=self.video_file,
|
2016-02-27 22:01:22 +01:00
|
|
|
timeout=10,
|
2015-09-07 16:22:25 +02:00
|
|
|
duration=self.duration,
|
|
|
|
caption=self.caption)
|
|
|
|
|
|
|
|
video = message.video
|
|
|
|
|
|
|
|
self.assertTrue(isinstance(video.file_id, str))
|
|
|
|
self.assertNotEqual(video.file_id, '')
|
2016-02-27 21:49:13 +01:00
|
|
|
self.assertEqual(video.width, self.width)
|
|
|
|
self.assertEqual(video.height, self.height)
|
2015-09-07 16:22:25 +02:00
|
|
|
self.assertEqual(video.duration, self.duration)
|
2016-02-27 21:49:13 +01:00
|
|
|
self.assertEqual(video.thumb, self.thumb)
|
2015-09-07 16:22:25 +02:00
|
|
|
self.assertEqual(video.mime_type, '')
|
|
|
|
self.assertEqual(video.file_size, self.file_size)
|
|
|
|
|
|
|
|
self.assertEqual(message.caption, self.caption)
|
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2015-09-08 20:52:10 +02:00
|
|
|
def test_send_video_mp4_file_with_custom_filename(self):
|
|
|
|
message = self._bot.sendVideo(chat_id=self._chat_id,
|
|
|
|
video=self.video_file,
|
2016-02-27 22:01:22 +01:00
|
|
|
timeout=10,
|
2015-09-08 20:52:10 +02:00
|
|
|
duration=self.duration,
|
|
|
|
caption=self.caption,
|
|
|
|
filename='telegram_custom.mp4')
|
|
|
|
|
|
|
|
video = message.video
|
|
|
|
|
|
|
|
self.assertTrue(isinstance(video.file_id, str))
|
|
|
|
self.assertNotEqual(video.file_id, '')
|
2016-02-27 21:49:13 +01:00
|
|
|
self.assertEqual(video.width, self.width)
|
|
|
|
self.assertEqual(video.height, self.height)
|
2015-09-08 20:52:10 +02:00
|
|
|
self.assertEqual(video.duration, self.duration)
|
2016-02-27 21:49:13 +01:00
|
|
|
self.assertEqual(video.thumb, self.thumb)
|
2015-09-08 20:52:10 +02:00
|
|
|
self.assertEqual(video.mime_type, '')
|
|
|
|
self.assertEqual(video.file_size, self.file_size)
|
|
|
|
|
|
|
|
self.assertEqual(message.caption, self.caption)
|
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2016-01-11 23:19:55 +01:00
|
|
|
def test_send_video_mp4_file_url(self):
|
|
|
|
message = self._bot.sendVideo(chat_id=self._chat_id,
|
|
|
|
video=self.video_file_url,
|
2016-02-27 22:01:22 +01:00
|
|
|
timeout=10,
|
2016-01-11 23:19:55 +01:00
|
|
|
duration=self.duration,
|
|
|
|
caption=self.caption)
|
|
|
|
|
|
|
|
video = message.video
|
|
|
|
|
|
|
|
self.assertTrue(isinstance(video.file_id, str))
|
|
|
|
self.assertNotEqual(video.file_id, '')
|
2016-02-27 21:49:13 +01:00
|
|
|
self.assertEqual(video.height, self.height)
|
2016-01-11 23:19:55 +01:00
|
|
|
self.assertEqual(video.duration, self.duration)
|
2016-02-27 21:49:13 +01:00
|
|
|
self.assertEqual(video.thumb, self.thumb)
|
2016-01-11 23:19:55 +01:00
|
|
|
self.assertEqual(video.mime_type, '')
|
2016-01-11 23:33:17 +01:00
|
|
|
self.assertEqual(video.file_size, self.file_size)
|
2016-01-11 23:19:55 +01:00
|
|
|
|
|
|
|
self.assertEqual(message.caption, self.caption)
|
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2015-09-07 16:22:25 +02:00
|
|
|
def test_send_video_resend(self):
|
|
|
|
message = self._bot.sendVideo(chat_id=self._chat_id,
|
|
|
|
video=self.video_file_id,
|
2016-02-27 22:01:22 +01:00
|
|
|
timeout=10,
|
2015-09-07 16:22:25 +02:00
|
|
|
duration=self.duration,
|
|
|
|
caption=self.caption)
|
|
|
|
|
|
|
|
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, '')
|
|
|
|
|
|
|
|
self.assertEqual(message.caption, self.caption)
|
|
|
|
|
|
|
|
def test_video_de_json(self):
|
|
|
|
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)
|
2016-02-27 21:49:13 +01:00
|
|
|
self.assertEqual(video.thumb, self.thumb)
|
2015-09-07 16:22:25 +02:00
|
|
|
self.assertEqual(video.mime_type, self.mime_type)
|
|
|
|
self.assertEqual(video.file_size, self.file_size)
|
|
|
|
|
|
|
|
def test_video_to_json(self):
|
|
|
|
video = telegram.Video.de_json(self.json_dict)
|
|
|
|
|
|
|
|
self.assertTrue(self.is_json(video.to_json()))
|
|
|
|
|
|
|
|
def test_video_to_dict(self):
|
|
|
|
video = telegram.Video.de_json(self.json_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)
|
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2015-09-07 16:22:25 +02:00
|
|
|
def test_error_send_video_empty_file(self):
|
|
|
|
json_dict = self.json_dict
|
|
|
|
|
|
|
|
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,
|
2016-02-27 22:01:22 +01:00
|
|
|
timeout=10,
|
2015-09-07 16:22:25 +02:00
|
|
|
**json_dict))
|
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2015-09-07 16:22:25 +02:00
|
|
|
def test_error_send_video_empty_file_id(self):
|
|
|
|
json_dict = self.json_dict
|
|
|
|
|
|
|
|
del(json_dict['file_id'])
|
|
|
|
json_dict['video'] = ''
|
|
|
|
|
|
|
|
self.assertRaises(telegram.TelegramError,
|
|
|
|
lambda: self._bot.sendVideo(chat_id=self._chat_id,
|
2016-02-27 22:01:22 +01:00
|
|
|
timeout=10,
|
2015-09-07 16:22:25 +02:00
|
|
|
**json_dict))
|
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2015-09-07 16:22:25 +02:00
|
|
|
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,
|
2016-02-27 22:01:22 +01:00
|
|
|
timeout=10,
|
2015-09-07 16:22:25 +02:00
|
|
|
**json_dict))
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|