2016-01-12 01:25:46 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
|
|
|
# Copyright (C) 2015-2016
|
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
|
|
|
#
|
|
|
|
# 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 Photo"""
|
|
|
|
|
|
|
|
import sys
|
2016-04-24 04:11:25 +02:00
|
|
|
import unittest
|
|
|
|
import os
|
2016-04-27 00:28:21 +02:00
|
|
|
|
2016-02-21 12:52:47 +01:00
|
|
|
from flaky import flaky
|
|
|
|
|
2016-01-12 01:25:46 +01:00
|
|
|
sys.path.append('.')
|
|
|
|
|
|
|
|
import telegram
|
2016-02-22 00:38:26 +01:00
|
|
|
from tests.base import BaseTest, timeout
|
2016-01-12 01:25:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
class PhotoTest(BaseTest, unittest.TestCase):
|
|
|
|
"""This object represents Tests for Telegram Photo."""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.photo_file = open('tests/data/telegram.jpg', 'rb')
|
|
|
|
self.photo_file_id = 'AgADAQADvb8xGx8j9QcpZDKxYoFK3bfX1i8ABFX_dgMWoKDuQugAAgI'
|
|
|
|
self.photo_file_url = 'https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/tests/data/telegram.jpg'
|
|
|
|
self.width = 300
|
|
|
|
self.height = 300
|
2016-04-26 23:23:15 +02:00
|
|
|
self.thumb = {
|
|
|
|
'width': 90,
|
|
|
|
'height': 90,
|
|
|
|
'file_id':
|
|
|
|
'AgADAQADvb8xGx8j9QcpZDKxYoFK3bfX1i8ABBxRLXFhLnhIQ-gAAgI',
|
|
|
|
'file_size': 1478
|
|
|
|
}
|
2016-01-12 01:25:46 +01:00
|
|
|
self.file_size = 10209
|
|
|
|
|
|
|
|
# caption is part of sendPhoto method but not Photo object
|
|
|
|
self.caption = u'PhotoTest - Caption'
|
|
|
|
|
|
|
|
self.json_dict = {
|
|
|
|
'file_id': self.photo_file_id,
|
|
|
|
'width': self.width,
|
|
|
|
'height': self.height,
|
|
|
|
'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)
|
2016-01-12 01:25:46 +01:00
|
|
|
def test_sendphotoo_all_args(self):
|
2016-05-15 02:52:35 +02:00
|
|
|
message = self._bot.sendPhoto(self._chat_id, self.photo_file, caption=self.caption)
|
2016-01-12 01:25:46 +01:00
|
|
|
|
|
|
|
thumb, photo = message.photo
|
|
|
|
|
|
|
|
self.assertTrue(isinstance(thumb.file_id, str))
|
|
|
|
self.assertNotEqual(thumb.file_id, '')
|
|
|
|
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
|
|
|
|
self.assertEqual(thumb.width, self.thumb['width'])
|
|
|
|
self.assertEqual(thumb.height, self.thumb['height'])
|
|
|
|
self.assertEqual(thumb.file_size, self.thumb['file_size'])
|
|
|
|
|
|
|
|
self.assertTrue(isinstance(photo.file_id, str))
|
|
|
|
self.assertNotEqual(photo.file_id, '')
|
|
|
|
self.assertTrue(isinstance(photo, telegram.PhotoSize))
|
|
|
|
self.assertEqual(photo.width, self.width)
|
|
|
|
self.assertEqual(photo.height, self.height)
|
|
|
|
self.assertEqual(photo.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-12 01:25:46 +01:00
|
|
|
def test_send_photo_jpg_file(self):
|
2016-04-26 23:23:15 +02:00
|
|
|
message = self._bot.sendPhoto(self._chat_id, self.photo_file)
|
2016-01-12 01:25:46 +01:00
|
|
|
|
|
|
|
thumb, photo = message.photo
|
|
|
|
|
|
|
|
self.assertTrue(isinstance(thumb.file_id, str))
|
|
|
|
self.assertNotEqual(thumb.file_id, '')
|
|
|
|
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
|
|
|
|
self.assertEqual(thumb.width, self.thumb['width'])
|
|
|
|
self.assertEqual(thumb.height, self.thumb['height'])
|
|
|
|
self.assertEqual(thumb.file_size, self.thumb['file_size'])
|
|
|
|
|
|
|
|
self.assertTrue(isinstance(photo.file_id, str))
|
|
|
|
self.assertNotEqual(photo.file_id, '')
|
|
|
|
self.assertTrue(isinstance(photo, telegram.PhotoSize))
|
|
|
|
self.assertEqual(photo.width, self.width)
|
|
|
|
self.assertEqual(photo.height, self.height)
|
|
|
|
self.assertEqual(photo.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)
|
2016-01-12 01:25:46 +01:00
|
|
|
def test_send_photo_url_jpg_file(self):
|
2016-04-26 23:23:15 +02:00
|
|
|
message = self._bot.sendPhoto(self._chat_id, self.photo_file_url)
|
2016-01-12 01:25:46 +01:00
|
|
|
|
|
|
|
thumb, photo = message.photo
|
|
|
|
|
|
|
|
self.assertTrue(isinstance(thumb.file_id, str))
|
|
|
|
self.assertNotEqual(thumb.file_id, '')
|
|
|
|
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
|
|
|
|
self.assertEqual(thumb.width, self.thumb['width'])
|
|
|
|
self.assertEqual(thumb.height, self.thumb['height'])
|
|
|
|
self.assertEqual(thumb.file_size, self.thumb['file_size'])
|
|
|
|
|
|
|
|
self.assertTrue(isinstance(photo.file_id, str))
|
|
|
|
self.assertNotEqual(photo.file_id, '')
|
|
|
|
self.assertTrue(isinstance(photo, telegram.PhotoSize))
|
|
|
|
self.assertEqual(photo.width, self.width)
|
|
|
|
self.assertEqual(photo.height, self.height)
|
|
|
|
self.assertEqual(photo.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)
|
2016-01-12 01:25:46 +01:00
|
|
|
def test_send_photo_resend(self):
|
2016-05-15 02:52:35 +02:00
|
|
|
message = self._bot.sendPhoto(chat_id=self._chat_id, photo=self.photo_file_id)
|
2016-01-12 01:25:46 +01:00
|
|
|
|
|
|
|
thumb, photo = message.photo
|
|
|
|
|
|
|
|
self.assertEqual(thumb.file_id, self.thumb['file_id'])
|
|
|
|
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
|
|
|
|
self.assertEqual(thumb.width, self.thumb['width'])
|
|
|
|
self.assertEqual(thumb.height, self.thumb['height'])
|
|
|
|
|
|
|
|
self.assertEqual(photo.file_id, self.photo_file_id)
|
|
|
|
self.assertTrue(isinstance(photo, telegram.PhotoSize))
|
|
|
|
self.assertEqual(photo.width, self.width)
|
|
|
|
self.assertEqual(photo.height, self.height)
|
|
|
|
|
|
|
|
def test_photo_de_json(self):
|
|
|
|
photo = telegram.PhotoSize.de_json(self.json_dict)
|
|
|
|
|
|
|
|
self.assertEqual(photo.file_id, self.photo_file_id)
|
|
|
|
self.assertTrue(isinstance(photo, telegram.PhotoSize))
|
|
|
|
self.assertEqual(photo.width, self.width)
|
|
|
|
self.assertEqual(photo.height, self.height)
|
|
|
|
self.assertEqual(photo.file_size, self.file_size)
|
|
|
|
|
|
|
|
def test_photo_to_json(self):
|
|
|
|
photo = telegram.PhotoSize.de_json(self.json_dict)
|
|
|
|
|
|
|
|
self.assertTrue(self.is_json(photo.to_json()))
|
|
|
|
|
|
|
|
def test_photo_to_dict(self):
|
|
|
|
photo = telegram.PhotoSize.de_json(self.json_dict)
|
|
|
|
|
|
|
|
self.assertTrue(self.is_dict(photo.to_dict()))
|
|
|
|
self.assertEqual(photo['file_id'], self.photo_file_id)
|
|
|
|
self.assertTrue(isinstance(photo, telegram.PhotoSize))
|
|
|
|
self.assertEqual(photo['width'], self.width)
|
|
|
|
self.assertEqual(photo['height'], self.height)
|
|
|
|
self.assertEqual(photo['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)
|
2016-01-12 01:25:46 +01:00
|
|
|
def test_error_send_photo_empty_file(self):
|
|
|
|
json_dict = self.json_dict
|
|
|
|
|
2016-04-24 04:11:25 +02:00
|
|
|
del (json_dict['file_id'])
|
2016-01-12 01:25:46 +01:00
|
|
|
json_dict['photo'] = open(os.devnull, 'rb')
|
|
|
|
|
2016-05-15 02:52:35 +02:00
|
|
|
self.assertRaises(
|
|
|
|
telegram.TelegramError,
|
|
|
|
lambda: self._bot.sendPhoto(chat_id=self._chat_id, **json_dict))
|
2016-01-12 01:25:46 +01:00
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2016-01-12 01:25:46 +01:00
|
|
|
def test_error_send_photo_empty_file_id(self):
|
|
|
|
json_dict = self.json_dict
|
|
|
|
|
2016-04-24 04:11:25 +02:00
|
|
|
del (json_dict['file_id'])
|
2016-01-12 01:25:46 +01:00
|
|
|
json_dict['photo'] = ''
|
|
|
|
|
2016-05-15 02:52:35 +02:00
|
|
|
self.assertRaises(
|
|
|
|
telegram.TelegramError,
|
|
|
|
lambda: self._bot.sendPhoto(chat_id=self._chat_id, **json_dict))
|
2016-01-12 01:25:46 +01:00
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2016-01-12 01:25:46 +01:00
|
|
|
def test_error_photo_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['width'])
|
|
|
|
del (json_dict['height'])
|
2016-01-12 01:25:46 +01:00
|
|
|
|
2016-05-15 02:52:35 +02:00
|
|
|
self.assertRaises(
|
|
|
|
TypeError,
|
|
|
|
lambda: self._bot.sendPhoto(chat_id=self._chat_id, **json_dict))
|
2016-01-12 01:25:46 +01:00
|
|
|
|
2016-04-24 04:11:25 +02:00
|
|
|
|
2016-01-12 01:25:46 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|