|
| 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 Voice""" |
| 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 VoiceTest(BaseTest, unittest.TestCase): |
| 31 | + """This object represents Tests for Telegram Voice.""" |
| 32 | + |
| 33 | + def setUp(self): |
| 34 | + self.voice_file = open('tests/data/telegram.ogg', 'rb') |
| 35 | + self.voice_file_id = 'AwADAQADTgADHyP1B_mbw34svXPHAg' |
| 36 | + self.duration = 0 |
| 37 | + self.mime_type = 'audio/ogg' |
| 38 | + self.file_size = 9199 |
| 39 | + |
| 40 | + self.json_dict = { |
| 41 | + 'file_id': self.voice_file_id, |
| 42 | + 'duration': self.duration, |
| 43 | + 'mime_type': self.mime_type, |
| 44 | + 'file_size': self.file_size |
| 45 | + } |
| 46 | + |
| 47 | + def test_send_voice_required_args_only(self): |
| 48 | + """Test telegram.Bot sendVoice method""" |
| 49 | + print('Testing bot.sendVoice - With required arguments only') |
| 50 | + |
| 51 | + message = self._bot.sendVoice(self._chat_id, |
| 52 | + self.voice_file) |
| 53 | + |
| 54 | + voice = message.voice |
| 55 | + |
| 56 | + self.assertTrue(isinstance(voice.file_id, str)) |
| 57 | + self.assertNotEqual(voice.file_id, '') |
| 58 | + self.assertEqual(voice.duration, self.duration) |
| 59 | + self.assertEqual(voice.mime_type, self.mime_type) |
| 60 | + self.assertEqual(voice.file_size, self.file_size) |
| 61 | + |
| 62 | + def test_send_audio_all_args(self): |
| 63 | + """Test telegram.Bot sendAudio method""" |
| 64 | + print('Testing bot.sendVoice - With all required arguments') |
| 65 | + |
| 66 | + message = self._bot.sendVoice(self._chat_id, |
| 67 | + self.voice_file, |
| 68 | + self.duration, |
| 69 | + mime_type=self.mime_type, |
| 70 | + file_size=self.file_size) |
| 71 | + |
| 72 | + voice = message.voice |
| 73 | + |
| 74 | + self.assertTrue(isinstance(voice.file_id, str)) |
| 75 | + self.assertNotEqual(voice.file_id, '') |
| 76 | + self.assertEqual(voice.duration, self.duration) |
| 77 | + self.assertEqual(voice.mime_type, self.mime_type) |
| 78 | + self.assertEqual(voice.file_size, self.file_size) |
| 79 | + |
| 80 | + def test_send_voice_ogg_file(self): |
| 81 | + """Test telegram.Bot sendVoice method""" |
| 82 | + print('Testing bot.sendVoice - Ogg File') |
| 83 | + |
| 84 | + message = self._bot.sendVoice(chat_id=self._chat_id, |
| 85 | + voice=self.voice_file) |
| 86 | + |
| 87 | + voice = message.voice |
| 88 | + |
| 89 | + self.assertTrue(isinstance(voice.file_id, str)) |
| 90 | + self.assertNotEqual(voice.file_id, '') |
| 91 | + self.assertEqual(voice.duration, self.duration) |
| 92 | + self.assertEqual(voice.mime_type, self.mime_type) |
| 93 | + self.assertEqual(voice.file_size, self.file_size) |
| 94 | + |
| 95 | + def test_send_voice_resend(self): |
| 96 | + """Test telegram.Bot sendVoice method""" |
| 97 | + print('Testing bot.sendVoice - Resend by file_id') |
| 98 | + |
| 99 | + message = self._bot.sendVoice(chat_id=self._chat_id, |
| 100 | + voice=self.voice_file_id) |
| 101 | + |
| 102 | + voice = message.voice |
| 103 | + |
| 104 | + self.assertEqual(voice.file_id, self.voice_file_id) |
| 105 | + self.assertEqual(voice.duration, self.duration) |
| 106 | + self.assertEqual(voice.mime_type, self.mime_type) |
| 107 | + self.assertEqual(voice.file_size, self.file_size) |
| 108 | + |
| 109 | + def test_voice_de_json(self): |
| 110 | + """Test Voice.de_json() method""" |
| 111 | + print('Testing Voice.de_json()') |
| 112 | + |
| 113 | + voice = telegram.Voice.de_json(self.json_dict) |
| 114 | + |
| 115 | + self.assertEqual(voice.file_id, self.voice_file_id) |
| 116 | + self.assertEqual(voice.duration, self.duration) |
| 117 | + self.assertEqual(voice.mime_type, self.mime_type) |
| 118 | + self.assertEqual(voice.file_size, self.file_size) |
| 119 | + |
| 120 | + def test_voice_to_json(self): |
| 121 | + """Test Voice.to_json() method""" |
| 122 | + print('Testing Voice.to_json()') |
| 123 | + |
| 124 | + voice = telegram.Voice.de_json(self.json_dict) |
| 125 | + |
| 126 | + self.assertTrue(self.is_json(voice.to_json())) |
| 127 | + |
| 128 | + def test_voice_to_dict(self): |
| 129 | + """Test Voice.to_dict() method""" |
| 130 | + print('Testing Voice.to_dict()') |
| 131 | + |
| 132 | + voice = telegram.Voice.de_json(self.json_dict) |
| 133 | + |
| 134 | + self.assertTrue(self.is_dict(voice.to_dict())) |
| 135 | + self.assertEqual(voice['file_id'], self.voice_file_id) |
| 136 | + self.assertEqual(voice['duration'], self.duration) |
| 137 | + self.assertEqual(voice['mime_type'], self.mime_type) |
| 138 | + self.assertEqual(voice['file_size'], self.file_size) |
| 139 | + |
| 140 | + def test_error_send_voice_empty_file(self): |
| 141 | + print('Testing bot.sendVoice - Null file') |
| 142 | + |
| 143 | + json_dict = self.json_dict |
| 144 | + |
| 145 | + del(json_dict['file_id']) |
| 146 | + json_dict['voice'] = open(os.devnull, 'rb') |
| 147 | + |
| 148 | + self.assertRaises(telegram.TelegramError, |
| 149 | + lambda: self._bot.sendVoice(chat_id=self._chat_id, |
| 150 | + **json_dict)) |
| 151 | + |
| 152 | + def test_error_send_voice_empty_file_id(self): |
| 153 | + print('Testing bot.sendVoice - Empty file_id') |
| 154 | + |
| 155 | + json_dict = self.json_dict |
| 156 | + |
| 157 | + del(json_dict['file_id']) |
| 158 | + json_dict['voice'] = '' |
| 159 | + |
| 160 | + self.assertRaises(telegram.TelegramError, |
| 161 | + lambda: self._bot.sendVoice(chat_id=self._chat_id, |
| 162 | + **json_dict)) |
| 163 | + |
| 164 | + def test_error_voice_without_required_args(self): |
| 165 | + print('Testing bot.sendVoice - Without required arguments') |
| 166 | + |
| 167 | + json_dict = self.json_dict |
| 168 | + |
| 169 | + del(json_dict['file_id']) |
| 170 | + del(json_dict['duration']) |
| 171 | + |
| 172 | + self.assertRaises(TypeError, |
| 173 | + lambda: self._bot.sendVoice(chat_id=self._chat_id, |
| 174 | + **json_dict)) |
| 175 | + |
| 176 | +if __name__ == '__main__': |
| 177 | + unittest.main() |
0 commit comments