mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-12-22 22:45:09 +01:00
Much better, such wow Audio/Voice/Video tests
This commit is contained in:
parent
0218aa2f4e
commit
65a224884f
3 changed files with 224 additions and 11 deletions
|
@ -53,20 +53,21 @@ class AudioTest(BaseTest, unittest.TestCase):
|
||||||
print('Testing bot.sendAudio - With required arguments only')
|
print('Testing bot.sendAudio - With required arguments only')
|
||||||
|
|
||||||
message = self._bot.sendAudio(self._chat_id,
|
message = self._bot.sendAudio(self._chat_id,
|
||||||
self.audio_file,
|
self.audio_file)
|
||||||
self.duration)
|
|
||||||
|
|
||||||
audio = message.audio
|
audio = message.audio
|
||||||
|
|
||||||
self.assertTrue(isinstance(audio.file_id, str))
|
self.assertTrue(isinstance(audio.file_id, str))
|
||||||
self.assertNotEqual(audio.file_id, '')
|
self.assertNotEqual(audio.file_id, '')
|
||||||
self.assertEqual(audio.duration, self.duration)
|
self.assertEqual(audio.duration, 4)
|
||||||
|
self.assertEqual(audio.performer, '')
|
||||||
|
self.assertEqual(audio.title, '')
|
||||||
self.assertEqual(audio.mime_type, self.mime_type)
|
self.assertEqual(audio.mime_type, self.mime_type)
|
||||||
self.assertEqual(audio.file_size, self.file_size)
|
self.assertEqual(audio.file_size, self.file_size)
|
||||||
|
|
||||||
def test_send_audio_all_args(self):
|
def test_send_audio_all_args(self):
|
||||||
"""Test telegram.Bot sendAudio method"""
|
"""Test telegram.Bot sendAudio method"""
|
||||||
print('Testing bot.sendAudio - With all required arguments')
|
print('Testing bot.sendAudio - With all arguments')
|
||||||
|
|
||||||
message = self._bot.sendAudio(self._chat_id,
|
message = self._bot.sendAudio(self._chat_id,
|
||||||
self.audio_file,
|
self.audio_file,
|
||||||
|
@ -113,8 +114,8 @@ class AudioTest(BaseTest, unittest.TestCase):
|
||||||
message = self._bot.sendAudio(chat_id=self._chat_id,
|
message = self._bot.sendAudio(chat_id=self._chat_id,
|
||||||
audio=self.audio_file_id,
|
audio=self.audio_file_id,
|
||||||
duration=self.duration,
|
duration=self.duration,
|
||||||
performer=self.performer, # bug: backwards compatibility with Audio
|
performer=self.performer,
|
||||||
title=self.title) # bug: backwards compatibility with Audio
|
title=self.title)
|
||||||
|
|
||||||
audio = message.audio
|
audio = message.audio
|
||||||
|
|
||||||
|
@ -122,7 +123,7 @@ class AudioTest(BaseTest, unittest.TestCase):
|
||||||
self.assertEqual(audio.duration, self.duration)
|
self.assertEqual(audio.duration, self.duration)
|
||||||
self.assertEqual(audio.performer, self.performer)
|
self.assertEqual(audio.performer, self.performer)
|
||||||
self.assertEqual(audio.title, self.title)
|
self.assertEqual(audio.title, self.title)
|
||||||
self.assertEqual(audio.mime_type, '') # bug: empty
|
self.assertEqual(audio.mime_type, '')
|
||||||
self.assertEqual(audio.file_size, self.file_size)
|
self.assertEqual(audio.file_size, self.file_size)
|
||||||
|
|
||||||
def test_audio_de_json(self):
|
def test_audio_de_json(self):
|
||||||
|
|
210
tests/test_video.py
Normal file
210
tests/test_video.py
Normal file
|
@ -0,0 +1,210 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
#
|
||||||
|
# A library that provides a Python interface to the Telegram Bot API
|
||||||
|
# Copyright (C) 2015 Leandro Toledo de Souza <leandrotoeldodesouza@gmail.com>
|
||||||
|
#
|
||||||
|
# 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
|
||||||
|
sys.path.append('.')
|
||||||
|
|
||||||
|
import telegram
|
||||||
|
from tests.base import BaseTest
|
||||||
|
|
||||||
|
|
||||||
|
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'
|
||||||
|
self.width = 360
|
||||||
|
self.height = 640
|
||||||
|
self.duration = 4
|
||||||
|
self.thumb = telegram.PhotoSize.de_json({})
|
||||||
|
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,
|
||||||
|
'mime_type': self.mime_type,
|
||||||
|
'file_size': self.file_size
|
||||||
|
}
|
||||||
|
|
||||||
|
def test_send_video_required_args_only(self):
|
||||||
|
"""Test telegram.Bot sendVideo method"""
|
||||||
|
print('Testing bot.sendVideo - With required arguments only')
|
||||||
|
|
||||||
|
message = self._bot.sendVideo(self._chat_id,
|
||||||
|
self.video_file)
|
||||||
|
|
||||||
|
video = message.video
|
||||||
|
|
||||||
|
self.assertTrue(isinstance(video.file_id, str))
|
||||||
|
self.assertNotEqual(video.file_id, '')
|
||||||
|
self.assertEqual(video.width, 0)
|
||||||
|
self.assertEqual(video.height, 0)
|
||||||
|
self.assertEqual(video.duration, 0)
|
||||||
|
self.assertEqual(video.thumb, None)
|
||||||
|
self.assertEqual(video.mime_type, '')
|
||||||
|
self.assertEqual(video.file_size, self.file_size)
|
||||||
|
|
||||||
|
def test_send_video_all_args(self):
|
||||||
|
"""Test telegram.Bot sendAudio method"""
|
||||||
|
print('Testing bot.sendVideo - With all arguments')
|
||||||
|
|
||||||
|
message = self._bot.sendVideo(self._chat_id,
|
||||||
|
self.video_file,
|
||||||
|
duration=self.duration,
|
||||||
|
caption=self.caption)
|
||||||
|
|
||||||
|
video = message.video
|
||||||
|
|
||||||
|
self.assertTrue(isinstance(video.file_id, str))
|
||||||
|
self.assertNotEqual(video.file_id, '')
|
||||||
|
self.assertEqual(video.width, 0)
|
||||||
|
self.assertEqual(video.height, 0)
|
||||||
|
self.assertEqual(video.duration, self.duration)
|
||||||
|
self.assertEqual(video.thumb, None)
|
||||||
|
self.assertEqual(video.mime_type, '')
|
||||||
|
self.assertEqual(video.file_size, self.file_size)
|
||||||
|
|
||||||
|
self.assertEqual(message.caption, self.caption)
|
||||||
|
|
||||||
|
def test_send_video_mp4_file(self):
|
||||||
|
"""Test telegram.Bot sendVideo method"""
|
||||||
|
print('Testing bot.sendVideo - MP4 File')
|
||||||
|
|
||||||
|
message = self._bot.sendVideo(chat_id=self._chat_id,
|
||||||
|
video=self.video_file,
|
||||||
|
duration=self.duration,
|
||||||
|
caption=self.caption)
|
||||||
|
|
||||||
|
video = message.video
|
||||||
|
|
||||||
|
self.assertTrue(isinstance(video.file_id, str))
|
||||||
|
self.assertNotEqual(video.file_id, '')
|
||||||
|
self.assertEqual(video.width, 0)
|
||||||
|
self.assertEqual(video.height, 0)
|
||||||
|
self.assertEqual(video.duration, self.duration)
|
||||||
|
self.assertEqual(video.thumb, None)
|
||||||
|
self.assertEqual(video.mime_type, '')
|
||||||
|
self.assertEqual(video.file_size, self.file_size)
|
||||||
|
|
||||||
|
self.assertEqual(message.caption, self.caption)
|
||||||
|
|
||||||
|
def test_send_video_resend(self):
|
||||||
|
"""Test telegram.Bot sendVideo method"""
|
||||||
|
print('Testing bot.sendVideo - Resend by file_id')
|
||||||
|
|
||||||
|
message = self._bot.sendVideo(chat_id=self._chat_id,
|
||||||
|
video=self.video_file_id,
|
||||||
|
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(video.file_size, self.file_size)
|
||||||
|
|
||||||
|
self.assertEqual(message.caption, self.caption)
|
||||||
|
|
||||||
|
def test_video_de_json(self):
|
||||||
|
"""Test Video.de_json() method"""
|
||||||
|
print('Testing Video.de_json()')
|
||||||
|
|
||||||
|
video = telegram.Video.de_json(self.json_dict)
|
||||||
|
|
||||||
|
self.assertEqual(video.file_id, self.video_file_id)
|
||||||
|
self.assertEqual(video.width, self.width)
|
||||||
|
self.assertEqual(video.height, self.height)
|
||||||
|
self.assertEqual(video.duration, self.duration)
|
||||||
|
self.assertEqual(video.thumb, None)
|
||||||
|
self.assertEqual(video.mime_type, self.mime_type)
|
||||||
|
self.assertEqual(video.file_size, self.file_size)
|
||||||
|
|
||||||
|
def test_video_to_json(self):
|
||||||
|
"""Test Video.to_json() method"""
|
||||||
|
print('Testing Video.to_json()')
|
||||||
|
|
||||||
|
video = telegram.Video.de_json(self.json_dict)
|
||||||
|
|
||||||
|
self.assertTrue(self.is_json(video.to_json()))
|
||||||
|
|
||||||
|
def test_video_to_dict(self):
|
||||||
|
"""Test Video.to_dict() method"""
|
||||||
|
print('Testing Video.to_dict()')
|
||||||
|
|
||||||
|
video = telegram.Video.de_json(self.json_dict)
|
||||||
|
|
||||||
|
self.assertTrue(self.is_dict(video.to_dict()))
|
||||||
|
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)
|
||||||
|
|
||||||
|
def test_error_send_video_empty_file(self):
|
||||||
|
print('Testing bot.sendVideo - Null file')
|
||||||
|
|
||||||
|
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,
|
||||||
|
**json_dict))
|
||||||
|
|
||||||
|
def test_error_send_video_empty_file_id(self):
|
||||||
|
print('Testing bot.sendVideo - Empty file_id')
|
||||||
|
|
||||||
|
json_dict = self.json_dict
|
||||||
|
|
||||||
|
del(json_dict['file_id'])
|
||||||
|
json_dict['video'] = ''
|
||||||
|
|
||||||
|
self.assertRaises(telegram.TelegramError,
|
||||||
|
lambda: self._bot.sendVideo(chat_id=self._chat_id,
|
||||||
|
**json_dict))
|
||||||
|
|
||||||
|
def test_error_video_without_required_args(self):
|
||||||
|
print('Testing bot.sendVideo - Without required arguments')
|
||||||
|
|
||||||
|
json_dict = self.json_dict
|
||||||
|
|
||||||
|
del(json_dict['file_id'])
|
||||||
|
del(json_dict['duration'])
|
||||||
|
|
||||||
|
self.assertRaises(TypeError,
|
||||||
|
lambda: self._bot.sendVideo(chat_id=self._chat_id,
|
||||||
|
**json_dict))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
|
@ -59,9 +59,9 @@ class VoiceTest(BaseTest, unittest.TestCase):
|
||||||
self.assertEqual(voice.mime_type, self.mime_type)
|
self.assertEqual(voice.mime_type, self.mime_type)
|
||||||
self.assertEqual(voice.file_size, self.file_size)
|
self.assertEqual(voice.file_size, self.file_size)
|
||||||
|
|
||||||
def test_send_audio_all_args(self):
|
def test_send_voice_all_args(self):
|
||||||
"""Test telegram.Bot sendAudio method"""
|
"""Test telegram.Bot sendAudio method"""
|
||||||
print('Testing bot.sendVoice - With all required arguments')
|
print('Testing bot.sendVoice - With all arguments')
|
||||||
|
|
||||||
message = self._bot.sendVoice(self._chat_id,
|
message = self._bot.sendVoice(self._chat_id,
|
||||||
self.voice_file,
|
self.voice_file,
|
||||||
|
@ -82,7 +82,8 @@ class VoiceTest(BaseTest, unittest.TestCase):
|
||||||
print('Testing bot.sendVoice - Ogg File')
|
print('Testing bot.sendVoice - Ogg File')
|
||||||
|
|
||||||
message = self._bot.sendVoice(chat_id=self._chat_id,
|
message = self._bot.sendVoice(chat_id=self._chat_id,
|
||||||
voice=self.voice_file)
|
voice=self.voice_file,
|
||||||
|
duration=self.duration)
|
||||||
|
|
||||||
voice = message.voice
|
voice = message.voice
|
||||||
|
|
||||||
|
@ -97,7 +98,8 @@ class VoiceTest(BaseTest, unittest.TestCase):
|
||||||
print('Testing bot.sendVoice - Resend by file_id')
|
print('Testing bot.sendVoice - Resend by file_id')
|
||||||
|
|
||||||
message = self._bot.sendVoice(chat_id=self._chat_id,
|
message = self._bot.sendVoice(chat_id=self._chat_id,
|
||||||
voice=self.voice_file_id)
|
voice=self.voice_file_id,
|
||||||
|
duration=self.duration)
|
||||||
|
|
||||||
voice = message.voice
|
voice = message.voice
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue