|
| 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 Document""" |
| 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 DocumentTest(BaseTest, unittest.TestCase): |
| 31 | + """This object represents Tests for Telegram Document.""" |
| 32 | + |
| 33 | + def setUp(self): |
| 34 | + self.document_file = open('tests/data/telegram.png', 'rb') |
| 35 | + self.document_file_id = 'BQADAQADpAADHyP1B04ipZxJTe2BAg' |
| 36 | + self.document_file_url = 'http://dummyimage.com/600x400/000/fff.gif&text=telegram' |
| 37 | + self.thumb = {'width': 90, |
| 38 | + 'height': 90, |
| 39 | + 'file_id': 'BQADAQADoQADHyP1B0mzJMVyzcB0Ag', |
| 40 | + 'file_size': 2364} |
| 41 | + self.file_name = 'telegram.png' |
| 42 | + self.mime_type = 'image/png' |
| 43 | + self.file_size = 12948 |
| 44 | + |
| 45 | + self.json_dict = { |
| 46 | + 'file_id': self.document_file_id, |
| 47 | + 'thumb': self.thumb, |
| 48 | + 'file_name': self.file_name, |
| 49 | + 'mime_type': self.mime_type, |
| 50 | + 'file_size': self.file_size |
| 51 | + } |
| 52 | + |
| 53 | + def test_send_document_png_file(self): |
| 54 | + """Test telegram.Bot sendDocument method""" |
| 55 | + print('Testing bot.sendDocument - PNG File') |
| 56 | + |
| 57 | + message = self._bot.sendDocument(self._chat_id, |
| 58 | + self.document_file) |
| 59 | + |
| 60 | + document = message.document |
| 61 | + |
| 62 | + self.assertTrue(isinstance(document.file_id, str)) |
| 63 | + self.assertNotEqual(document.file_id, '') |
| 64 | + self.assertTrue(isinstance(document.thumb, telegram.PhotoSize)) |
| 65 | + self.assertEqual(document.file_name, self.file_name) |
| 66 | + self.assertEqual(document.mime_type, self.mime_type) |
| 67 | + self.assertEqual(document.file_size, self.file_size) |
| 68 | + |
| 69 | + def test_send_document_url_gif_file(self): |
| 70 | + """Test telegram.Bot sendDocument method""" |
| 71 | + print('Testing bot.sendDocument - GIF File by URL') |
| 72 | + |
| 73 | + message = self._bot.sendDocument(self._chat_id, |
| 74 | + self.document_file_url) |
| 75 | + |
| 76 | + document = message.document |
| 77 | + |
| 78 | + self.assertTrue(isinstance(document.file_id, str)) |
| 79 | + self.assertNotEqual(document.file_id, '') |
| 80 | + self.assertTrue(isinstance(document.thumb, telegram.PhotoSize)) |
| 81 | + self.assertEqual(document.file_name, 'image.gif') |
| 82 | + self.assertEqual(document.mime_type, 'image/gif') |
| 83 | + self.assertEqual(document.file_size, 3878) |
| 84 | + |
| 85 | + def test_send_document_resend(self): |
| 86 | + """Test telegram.Bot sendDocument method""" |
| 87 | + print('Testing bot.sendDocument - Resend by file_id') |
| 88 | + |
| 89 | + message = self._bot.sendDocument(chat_id=self._chat_id, |
| 90 | + document=self.document_file_id) |
| 91 | + |
| 92 | + document = message.document |
| 93 | + |
| 94 | + self.assertEqual(document.file_id, self.document_file_id) |
| 95 | + self.assertTrue(isinstance(document.thumb, telegram.PhotoSize)) |
| 96 | + self.assertEqual(document.file_name, self.file_name) |
| 97 | + self.assertEqual(document.mime_type, self.mime_type) |
| 98 | + self.assertEqual(document.file_size, self.file_size) |
| 99 | + |
| 100 | + def test_document_de_json(self): |
| 101 | + """Test Document.de_json() method""" |
| 102 | + print('Testing Document.de_json()') |
| 103 | + |
| 104 | + document = telegram.Document.de_json(self.json_dict) |
| 105 | + |
| 106 | + self.assertEqual(document.file_id, self.document_file_id) |
| 107 | + self.assertTrue(isinstance(document.thumb, telegram.PhotoSize)) |
| 108 | + self.assertEqual(document.file_name, self.file_name) |
| 109 | + self.assertEqual(document.mime_type, self.mime_type) |
| 110 | + self.assertEqual(document.file_size, self.file_size) |
| 111 | + |
| 112 | + def test_document_to_json(self): |
| 113 | + """Test Document.to_json() method""" |
| 114 | + print('Testing Document.to_json()') |
| 115 | + |
| 116 | + document = telegram.Document.de_json(self.json_dict) |
| 117 | + |
| 118 | + self.assertTrue(self.is_json(document.to_json())) |
| 119 | + |
| 120 | + def test_document_to_dict(self): |
| 121 | + """Test Document.to_dict() method""" |
| 122 | + print('Testing Document.to_dict()') |
| 123 | + |
| 124 | + document = telegram.Document.de_json(self.json_dict) |
| 125 | + |
| 126 | + self.assertTrue(self.is_dict(document.to_dict())) |
| 127 | + self.assertEqual(document['file_id'], self.document_file_id) |
| 128 | + self.assertTrue(isinstance(document['thumb'], telegram.PhotoSize)) |
| 129 | + self.assertEqual(document['file_name'], self.file_name) |
| 130 | + self.assertEqual(document['mime_type'], self.mime_type) |
| 131 | + self.assertEqual(document['file_size'], self.file_size) |
| 132 | + |
| 133 | + def test_error_send_document_empty_file(self): |
| 134 | + print('Testing bot.sendDocument - Null file') |
| 135 | + |
| 136 | + json_dict = self.json_dict |
| 137 | + |
| 138 | + del(json_dict['file_id']) |
| 139 | + json_dict['document'] = open(os.devnull, 'rb') |
| 140 | + |
| 141 | + self.assertRaises(telegram.TelegramError, |
| 142 | + lambda: self._bot.sendDocument(chat_id=self._chat_id, |
| 143 | + **json_dict)) |
| 144 | + |
| 145 | + def test_error_send_document_empty_file_id(self): |
| 146 | + print('Testing bot.sendDocument - Empty file_id') |
| 147 | + |
| 148 | + json_dict = self.json_dict |
| 149 | + |
| 150 | + del(json_dict['file_id']) |
| 151 | + json_dict['document'] = '' |
| 152 | + |
| 153 | + self.assertRaises(telegram.TelegramError, |
| 154 | + lambda: self._bot.sendDocument(chat_id=self._chat_id, |
| 155 | + **json_dict)) |
| 156 | + |
| 157 | + def test_error_document_without_required_args(self): |
| 158 | + print('Testing bot.sendDocument - Without required arguments') |
| 159 | + |
| 160 | + json_dict = self.json_dict |
| 161 | + |
| 162 | + del(json_dict['file_id']) |
| 163 | + |
| 164 | + self.assertRaises(TypeError, |
| 165 | + lambda: self._bot.sendDocument(chat_id=self._chat_id, |
| 166 | + **json_dict)) |
| 167 | + |
| 168 | +if __name__ == '__main__': |
| 169 | + unittest.main() |
0 commit comments