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