|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# A library that provides a Python interface to the Telegram Bot API |
| 4 | +# Copyright (C) 2015 Leandro Toledo de Souza <leandrotoeldodesouza@gmail.com> |
| 5 | +# |
| 6 | +# This program is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU General Public License as published by |
| 8 | +# the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# This program is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU General Public License |
| 17 | +# along with this program. If not, see [http://www.gnu.org/licenses/]. |
| 18 | + |
| 19 | +"""This module contains a object that represents Tests for Telegram Video""" |
| 20 | + |
| 21 | +import os |
| 22 | +import unittest |
| 23 | +import sys |
| 24 | +sys.path.append('.') |
| 25 | + |
| 26 | +import telegram |
| 27 | +from tests.base import BaseTest |
| 28 | + |
| 29 | + |
| 30 | +class VideoTest(BaseTest, unittest.TestCase): |
| 31 | + """This object represents Tests for Telegram Video.""" |
| 32 | + |
| 33 | + def setUp(self): |
| 34 | + self.video_file = open('tests/data/telegram.mp4', 'rb') |
| 35 | + self.video_file_id = 'BAADAQADXwADHyP1BwJFTcmY2RYCAg' |
| 36 | + self.width = 360 |
| 37 | + self.height = 640 |
| 38 | + self.duration = 4 |
| 39 | + self.thumb = telegram.PhotoSize.de_json({}) |
| 40 | + self.mime_type = 'video/mp4' |
| 41 | + self.file_size = 326534 |
| 42 | + |
| 43 | + # caption is part of sendVideo method but not Video object |
| 44 | + self.caption = u'VideoTest - Caption' |
| 45 | + |
| 46 | + self.json_dict = { |
| 47 | + 'file_id': self.video_file_id, |
| 48 | + 'width': self.width, |
| 49 | + 'height': self.height, |
| 50 | + 'duration': self.duration, |
| 51 | + 'thumb': self.thumb, |
| 52 | + 'mime_type': self.mime_type, |
| 53 | + 'file_size': self.file_size |
| 54 | + } |
| 55 | + |
| 56 | + def test_send_video_required_args_only(self): |
| 57 | + """Test telegram.Bot sendVideo method""" |
| 58 | + print('Testing bot.sendVideo - With required arguments only') |
| 59 | + |
| 60 | + message = self._bot.sendVideo(self._chat_id, |
| 61 | + self.video_file) |
| 62 | + |
| 63 | + video = message.video |
| 64 | + |
| 65 | + self.assertTrue(isinstance(video.file_id, str)) |
| 66 | + self.assertNotEqual(video.file_id, '') |
| 67 | + self.assertEqual(video.width, 0) |
| 68 | + self.assertEqual(video.height, 0) |
| 69 | + self.assertEqual(video.duration, 0) |
| 70 | + self.assertEqual(video.thumb, None) |
| 71 | + self.assertEqual(video.mime_type, '') |
| 72 | + self.assertEqual(video.file_size, self.file_size) |
| 73 | + |
| 74 | + def test_send_video_all_args(self): |
| 75 | + """Test telegram.Bot sendAudio method""" |
| 76 | + print('Testing bot.sendVideo - With all arguments') |
| 77 | + |
| 78 | + message = self._bot.sendVideo(self._chat_id, |
| 79 | + self.video_file, |
| 80 | + duration=self.duration, |
| 81 | + caption=self.caption) |
| 82 | + |
| 83 | + video = message.video |
| 84 | + |
| 85 | + self.assertTrue(isinstance(video.file_id, str)) |
| 86 | + self.assertNotEqual(video.file_id, '') |
| 87 | + self.assertEqual(video.width, 0) |
| 88 | + self.assertEqual(video.height, 0) |
| 89 | + self.assertEqual(video.duration, self.duration) |
| 90 | + self.assertEqual(video.thumb, None) |
| 91 | + self.assertEqual(video.mime_type, '') |
| 92 | + self.assertEqual(video.file_size, self.file_size) |
| 93 | + |
| 94 | + self.assertEqual(message.caption, self.caption) |
| 95 | + |
| 96 | + def test_send_video_mp4_file(self): |
| 97 | + """Test telegram.Bot sendVideo method""" |
| 98 | + print('Testing bot.sendVideo - MP4 File') |
| 99 | + |
| 100 | + message = self._bot.sendVideo(chat_id=self._chat_id, |
| 101 | + video=self.video_file, |
| 102 | + duration=self.duration, |
| 103 | + caption=self.caption) |
| 104 | + |
| 105 | + video = message.video |
| 106 | + |
| 107 | + self.assertTrue(isinstance(video.file_id, str)) |
| 108 | + self.assertNotEqual(video.file_id, '') |
| 109 | + self.assertEqual(video.width, 0) |
| 110 | + self.assertEqual(video.height, 0) |
| 111 | + self.assertEqual(video.duration, self.duration) |
| 112 | + self.assertEqual(video.thumb, None) |
| 113 | + self.assertEqual(video.mime_type, '') |
| 114 | + self.assertEqual(video.file_size, self.file_size) |
| 115 | + |
| 116 | + self.assertEqual(message.caption, self.caption) |
| 117 | + |
| 118 | + def test_send_video_resend(self): |
| 119 | + """Test telegram.Bot sendVideo method""" |
| 120 | + print('Testing bot.sendVideo - Resend by file_id') |
| 121 | + |
| 122 | + message = self._bot.sendVideo(chat_id=self._chat_id, |
| 123 | + video=self.video_file_id, |
| 124 | + duration=self.duration, |
| 125 | + caption=self.caption) |
| 126 | + |
| 127 | + video = message.video |
| 128 | + |
| 129 | + self.assertEqual(video.file_id, self.video_file_id) |
| 130 | + self.assertEqual(video.duration, 0) |
| 131 | + self.assertEqual(video.thumb, None) |
| 132 | + self.assertEqual(video.mime_type, '') |
| 133 | + self.assertEqual(video.file_size, self.file_size) |
| 134 | + |
| 135 | + self.assertEqual(message.caption, self.caption) |
| 136 | + |
| 137 | + def test_video_de_json(self): |
| 138 | + """Test Video.de_json() method""" |
| 139 | + print('Testing Video.de_json()') |
| 140 | + |
| 141 | + video = telegram.Video.de_json(self.json_dict) |
| 142 | + |
| 143 | + self.assertEqual(video.file_id, self.video_file_id) |
| 144 | + self.assertEqual(video.width, self.width) |
| 145 | + self.assertEqual(video.height, self.height) |
| 146 | + self.assertEqual(video.duration, self.duration) |
| 147 | + self.assertEqual(video.thumb, None) |
| 148 | + self.assertEqual(video.mime_type, self.mime_type) |
| 149 | + self.assertEqual(video.file_size, self.file_size) |
| 150 | + |
| 151 | + def test_video_to_json(self): |
| 152 | + """Test Video.to_json() method""" |
| 153 | + print('Testing Video.to_json()') |
| 154 | + |
| 155 | + video = telegram.Video.de_json(self.json_dict) |
| 156 | + |
| 157 | + self.assertTrue(self.is_json(video.to_json())) |
| 158 | + |
| 159 | + def test_video_to_dict(self): |
| 160 | + """Test Video.to_dict() method""" |
| 161 | + print('Testing Video.to_dict()') |
| 162 | + |
| 163 | + video = telegram.Video.de_json(self.json_dict) |
| 164 | + |
| 165 | + self.assertTrue(self.is_dict(video.to_dict())) |
| 166 | + self.assertEqual(video['file_id'], self.video_file_id) |
| 167 | + self.assertEqual(video['width'], self.width) |
| 168 | + self.assertEqual(video['height'], self.height) |
| 169 | + self.assertEqual(video['duration'], self.duration) |
| 170 | + self.assertEqual(video['mime_type'], self.mime_type) |
| 171 | + self.assertEqual(video['file_size'], self.file_size) |
| 172 | + |
| 173 | + def test_error_send_video_empty_file(self): |
| 174 | + print('Testing bot.sendVideo - Null file') |
| 175 | + |
| 176 | + json_dict = self.json_dict |
| 177 | + |
| 178 | + del(json_dict['file_id']) |
| 179 | + json_dict['video'] = open(os.devnull, 'rb') |
| 180 | + |
| 181 | + self.assertRaises(telegram.TelegramError, |
| 182 | + lambda: self._bot.sendVideo(chat_id=self._chat_id, |
| 183 | + **json_dict)) |
| 184 | + |
| 185 | + def test_error_send_video_empty_file_id(self): |
| 186 | + print('Testing bot.sendVideo - Empty file_id') |
| 187 | + |
| 188 | + json_dict = self.json_dict |
| 189 | + |
| 190 | + del(json_dict['file_id']) |
| 191 | + json_dict['video'] = '' |
| 192 | + |
| 193 | + self.assertRaises(telegram.TelegramError, |
| 194 | + lambda: self._bot.sendVideo(chat_id=self._chat_id, |
| 195 | + **json_dict)) |
| 196 | + |
| 197 | + def test_error_video_without_required_args(self): |
| 198 | + print('Testing bot.sendVideo - Without required arguments') |
| 199 | + |
| 200 | + json_dict = self.json_dict |
| 201 | + |
| 202 | + del(json_dict['file_id']) |
| 203 | + del(json_dict['duration']) |
| 204 | + |
| 205 | + self.assertRaises(TypeError, |
| 206 | + lambda: self._bot.sendVideo(chat_id=self._chat_id, |
| 207 | + **json_dict)) |
| 208 | + |
| 209 | +if __name__ == '__main__': |
| 210 | + unittest.main() |
0 commit comments