2015-09-07 14:56:41 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2017-05-14 12:18:29 +02:00
|
|
|
# Copyright (C) 2015-2017
|
2016-01-05 14:12:03 +01:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
2015-09-07 14:56:41 +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/].
|
2016-10-17 00:22:40 +02:00
|
|
|
"""This module contains an object that represents Tests for Telegram Audio"""
|
2015-09-07 14:56:41 +02:00
|
|
|
|
2016-04-24 04:11:25 +02:00
|
|
|
import os
|
2017-06-21 17:59:35 +02:00
|
|
|
import unittest
|
2016-04-27 00:28:21 +02:00
|
|
|
|
2016-02-21 12:52:47 +01:00
|
|
|
from flaky import flaky
|
|
|
|
|
2015-09-07 14:56:41 +02:00
|
|
|
import telegram
|
2016-02-22 00:38:26 +01:00
|
|
|
from tests.base import BaseTest, timeout
|
2017-06-21 17:59:35 +02:00
|
|
|
from tests.bots import get_bot
|
2015-09-07 14:56:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
class AudioTest(BaseTest, unittest.TestCase):
|
|
|
|
"""This object represents Tests for Telegram Audio."""
|
|
|
|
|
2017-06-21 17:59:35 +02:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
cls.caption = "Test audio"
|
|
|
|
cls.performer = 'Leandro Toledo'
|
|
|
|
cls.title = 'Teste'
|
2017-06-23 20:10:43 +02:00
|
|
|
# cls.audio_file_url = 'https://python-telegram-bot.org/static/testfiles/telegram.mp3'
|
|
|
|
# Shortened link, the above one is cached with the wrong duration.
|
|
|
|
cls.audio_file_url = "https://goo.gl/3En24v"
|
2017-06-21 17:59:35 +02:00
|
|
|
|
|
|
|
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 ''
|
|
|
|
|
2015-09-07 14:56:41 +02:00
|
|
|
def setUp(self):
|
|
|
|
self.audio_file = open('tests/data/telegram.mp3', 'rb')
|
|
|
|
self.json_dict = {
|
2017-06-21 17:59:35 +02:00
|
|
|
'file_id': self.audio.file_id,
|
|
|
|
'duration': self.audio.duration,
|
2015-09-07 14:56:41 +02:00
|
|
|
'performer': self.performer,
|
|
|
|
'title': self.title,
|
2016-10-05 17:50:48 +02:00
|
|
|
'caption': self.caption,
|
2017-06-21 17:59:35 +02:00
|
|
|
'mime_type': self.audio.mime_type,
|
|
|
|
'file_size': self.audio.file_size
|
2015-09-07 14:56:41 +02:00
|
|
|
}
|
|
|
|
|
2017-06-21 17:59:35 +02:00
|
|
|
def test_expected_values(self):
|
2017-06-21 18:46:50 +02:00
|
|
|
self.assertEqual(self.audio.duration, 3)
|
2017-06-21 17:59:35 +02:00
|
|
|
self.assertEqual(self.audio.performer, None)
|
|
|
|
self.assertEqual(self.audio.title, None)
|
2017-06-21 18:46:50 +02:00
|
|
|
self.assertEqual(self.audio.mime_type, 'audio/mpeg')
|
2017-06-21 19:01:20 +02:00
|
|
|
self.assertEqual(self.audio.file_size, 122920)
|
2015-09-07 14:56:41 +02:00
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2015-09-07 14:56:41 +02:00
|
|
|
def test_send_audio_all_args(self):
|
2016-08-26 11:17:05 +02:00
|
|
|
message = self._bot.sendAudio(
|
|
|
|
self._chat_id,
|
|
|
|
self.audio_file,
|
2016-10-05 17:50:48 +02:00
|
|
|
caption=self.caption,
|
2017-06-21 17:59:35 +02:00
|
|
|
duration=self.audio.duration,
|
2016-08-26 11:17:05 +02:00
|
|
|
performer=self.performer,
|
2016-10-05 17:50:48 +02:00
|
|
|
title=self.title,
|
2017-06-21 17:59:35 +02:00
|
|
|
disable_notification=False)
|
2016-10-05 17:50:48 +02:00
|
|
|
|
|
|
|
self.assertEqual(message.caption, self.caption)
|
2015-09-07 14:56:41 +02:00
|
|
|
|
|
|
|
audio = message.audio
|
|
|
|
|
2017-06-21 17:59:35 +02:00
|
|
|
self.assertIsInstance(audio, telegram.Audio)
|
|
|
|
self.assertIsInstance(audio.file_id, str)
|
2017-02-27 15:23:48 +01:00
|
|
|
self.assertNotEqual(audio.file_id, None)
|
2017-06-21 17:59:35 +02:00
|
|
|
self.assertEqual(audio.duration, self.audio.duration)
|
2015-09-07 14:56:41 +02:00
|
|
|
self.assertEqual(audio.performer, self.performer)
|
|
|
|
self.assertEqual(audio.title, self.title)
|
2017-06-21 17:59:35 +02:00
|
|
|
self.assertEqual(audio.mime_type, self.audio.mime_type)
|
|
|
|
self.assertEqual(audio.file_size, self.audio.file_size)
|
2015-09-08 20:52:10 +02:00
|
|
|
|
2017-06-22 18:29:21 +02:00
|
|
|
@flaky(3, 1)
|
|
|
|
@timeout(10)
|
|
|
|
def test_get_and_download_audio(self):
|
|
|
|
new_file = self._bot.getFile(self.audio.file_id)
|
|
|
|
|
|
|
|
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://'))
|
|
|
|
|
|
|
|
new_file.download('telegram.mp3')
|
|
|
|
|
|
|
|
self.assertTrue(os.path.isfile('telegram.mp3'))
|
|
|
|
|
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_audio_mp3_url_file(self):
|
2017-06-21 17:59:35 +02:00
|
|
|
message = self._bot.sendAudio(chat_id=self._chat_id, audio=self.audio_file_url)
|
2016-01-11 23:19:55 +01:00
|
|
|
|
|
|
|
audio = message.audio
|
|
|
|
|
2017-06-21 17:59:35 +02:00
|
|
|
self.assertIsInstance(audio, telegram.Audio)
|
|
|
|
self.assertIsInstance(audio.file_id, str)
|
2017-02-27 15:23:48 +01:00
|
|
|
self.assertNotEqual(audio.file_id, None)
|
2017-06-21 19:01:20 +02:00
|
|
|
self.assertEqual(audio.duration, self.audio.duration)
|
2017-06-21 17:59:35 +02:00
|
|
|
self.assertEqual(audio.mime_type, self.audio.mime_type)
|
|
|
|
self.assertEqual(audio.file_size, self.audio.file_size)
|
2016-01-11 23:19:55 +01:00
|
|
|
|
2016-10-05 17:50:48 +02:00
|
|
|
@flaky(3, 1)
|
|
|
|
@timeout(10)
|
|
|
|
def test_send_audio_mp3_url_file_with_caption(self):
|
|
|
|
message = self._bot.sendAudio(
|
|
|
|
chat_id=self._chat_id,
|
|
|
|
audio=self.audio_file_url,
|
|
|
|
caption=self.caption)
|
|
|
|
|
|
|
|
self.assertEqual(message.caption, self.caption)
|
|
|
|
|
|
|
|
audio = message.audio
|
|
|
|
|
2017-06-21 17:59:35 +02:00
|
|
|
self.assertIsInstance(audio, telegram.Audio)
|
|
|
|
self.assertIsInstance(audio.file_id, str)
|
2017-02-27 15:23:48 +01:00
|
|
|
self.assertNotEqual(audio.file_id, None)
|
2017-06-21 19:01:20 +02:00
|
|
|
self.assertEqual(audio.duration, self.audio.duration)
|
2017-06-21 17:59:35 +02:00
|
|
|
self.assertEqual(audio.mime_type, self.audio.mime_type)
|
|
|
|
self.assertEqual(audio.file_size, self.audio.file_size)
|
2016-10-05 17:50:48 +02:00
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2015-09-07 14:56:41 +02:00
|
|
|
def test_send_audio_resend(self):
|
2016-08-26 11:17:05 +02:00
|
|
|
message = self._bot.sendAudio(
|
|
|
|
chat_id=self._chat_id,
|
2017-06-21 17:59:35 +02:00
|
|
|
audio=self.audio.file_id)
|
2015-09-07 14:56:41 +02:00
|
|
|
|
|
|
|
audio = message.audio
|
|
|
|
|
2017-06-21 17:59:35 +02:00
|
|
|
self.assertEqual(audio, self.audio)
|
2017-07-25 00:35:22 +02:00
|
|
|
|
|
|
|
@flaky(3, 1)
|
|
|
|
@timeout(10)
|
|
|
|
def test_send_audio_with_audio(self):
|
|
|
|
message = self._bot.send_audio(audio=self.audio, chat_id=self._chat_id)
|
|
|
|
audio = message.audio
|
|
|
|
|
|
|
|
self.assertEqual(audio, self.audio)
|
2015-09-07 14:56:41 +02:00
|
|
|
|
|
|
|
def test_audio_de_json(self):
|
2016-09-20 06:36:55 +02:00
|
|
|
audio = telegram.Audio.de_json(self.json_dict, self._bot)
|
2015-09-07 14:56:41 +02:00
|
|
|
|
2017-06-21 17:59:35 +02:00
|
|
|
self.assertIsInstance(audio, telegram.Audio)
|
|
|
|
self.assertEqual(audio.file_id, self.audio.file_id)
|
|
|
|
self.assertEqual(audio.duration, self.audio.duration)
|
2015-09-07 14:56:41 +02:00
|
|
|
self.assertEqual(audio.performer, self.performer)
|
|
|
|
self.assertEqual(audio.title, self.title)
|
2017-06-21 17:59:35 +02:00
|
|
|
self.assertEqual(audio.mime_type, self.audio.mime_type)
|
|
|
|
self.assertEqual(audio.file_size, self.audio.file_size)
|
2015-09-07 14:56:41 +02:00
|
|
|
|
|
|
|
def test_audio_to_json(self):
|
2017-06-21 17:59:35 +02:00
|
|
|
self.assertTrue(self.is_json(self.audio.to_json()))
|
2015-09-07 14:56:41 +02:00
|
|
|
|
|
|
|
def test_audio_to_dict(self):
|
2017-06-21 17:59:35 +02:00
|
|
|
audio = self.audio.to_dict()
|
2015-09-07 14:56:41 +02:00
|
|
|
|
2017-06-21 17:59:35 +02:00
|
|
|
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)
|
2015-09-07 14:56:41 +02:00
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2015-09-07 14:56:41 +02:00
|
|
|
def test_error_send_audio_empty_file(self):
|
|
|
|
json_dict = self.json_dict
|
|
|
|
|
2016-04-24 04:11:25 +02:00
|
|
|
del (json_dict['file_id'])
|
2015-09-07 14:56:41 +02:00
|
|
|
json_dict['audio'] = open(os.devnull, 'rb')
|
|
|
|
|
2017-06-21 17:59:35 +02:00
|
|
|
with self.assertRaises(telegram.TelegramError):
|
|
|
|
self._bot.sendAudio(chat_id=self._chat_id, **json_dict)
|
2015-09-07 14:56:41 +02:00
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2015-09-07 14:56:41 +02:00
|
|
|
def test_error_send_audio_empty_file_id(self):
|
|
|
|
json_dict = self.json_dict
|
|
|
|
|
2016-04-24 04:11:25 +02:00
|
|
|
del (json_dict['file_id'])
|
2015-09-07 14:56:41 +02:00
|
|
|
json_dict['audio'] = ''
|
|
|
|
|
2017-06-21 17:59:35 +02:00
|
|
|
with self.assertRaises(telegram.TelegramError):
|
|
|
|
self._bot.sendAudio(chat_id=self._chat_id, **json_dict)
|
2015-09-07 14:56:41 +02:00
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2015-09-07 14:56:41 +02:00
|
|
|
def test_error_audio_without_required_args(self):
|
|
|
|
json_dict = self.json_dict
|
|
|
|
|
2016-04-24 04:11:25 +02:00
|
|
|
del (json_dict['file_id'])
|
|
|
|
del (json_dict['duration'])
|
2015-09-07 14:56:41 +02:00
|
|
|
|
2017-06-21 17:59:35 +02:00
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
self._bot.sendAudio(chat_id=self._chat_id, **json_dict)
|
2015-09-07 14:56:41 +02:00
|
|
|
|
2016-09-20 06:36:55 +02:00
|
|
|
@flaky(3, 1)
|
|
|
|
@timeout(10)
|
|
|
|
def test_reply_audio(self):
|
|
|
|
"""Test for Message.reply_audio"""
|
|
|
|
message = self._bot.sendMessage(self._chat_id, '.')
|
2017-06-21 17:59:35 +02:00
|
|
|
audio = message.reply_audio(self.audio_file).audio
|
2016-09-20 06:36:55 +02:00
|
|
|
|
2017-06-21 17:59:35 +02:00
|
|
|
self.assertIsInstance(audio, telegram.Audio)
|
|
|
|
self.assertIsInstance(audio.file_id, str)
|
|
|
|
self.assertNotEqual(audio.file_id, None)
|
2016-09-20 06:36:55 +02:00
|
|
|
|
2017-05-14 23:29:31 +02:00
|
|
|
def test_equality(self):
|
2017-06-21 17:59:35 +02:00
|
|
|
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)
|
2017-05-14 23:29:31 +02:00
|
|
|
|
|
|
|
self.assertEqual(a, b)
|
|
|
|
self.assertEqual(hash(a), hash(b))
|
|
|
|
self.assertIsNot(a, b)
|
|
|
|
|
|
|
|
self.assertEqual(a, c)
|
|
|
|
self.assertEqual(hash(a), hash(c))
|
|
|
|
|
|
|
|
self.assertNotEqual(a, d)
|
|
|
|
self.assertNotEqual(hash(a), hash(d))
|
|
|
|
|
|
|
|
self.assertNotEqual(a, e)
|
|
|
|
self.assertNotEqual(hash(a), hash(e))
|
|
|
|
|
2016-04-24 04:11:25 +02:00
|
|
|
|
2015-09-07 14:56:41 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|