|
| 1 | +#!/usr/bin/env python |
| 2 | +# encoding: utf-8 |
| 3 | +# |
| 4 | +# A library that provides a Python interface to the Telegram Bot API |
| 5 | +# Copyright (C) 2015-2016 |
| 6 | +# Leandro Toledo de Souza <devs@python-telegram-bot.org> |
| 7 | +# |
| 8 | +# This program is free software: you can redistribute it and/or modify |
| 9 | +# it under the terms of the GNU General Public License as published by |
| 10 | +# the Free Software Foundation, either version 3 of the License, or |
| 11 | +# (at your option) any later version. |
| 12 | +# |
| 13 | +# This program is distributed in the hope that it will be useful, |
| 14 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +# GNU General Public License for more details. |
| 17 | +# |
| 18 | +# You should have received a copy of the GNU General Public License |
| 19 | +# along with this program. If not, see [http://www.gnu.org/licenses/]. |
| 20 | +"""This module contains a object that represents Tests for Telegram Games""" |
| 21 | + |
| 22 | +import sys |
| 23 | +import unittest |
| 24 | + |
| 25 | +sys.path.append('.') |
| 26 | + |
| 27 | +import telegram |
| 28 | +from tests.base import BaseTest |
| 29 | + |
| 30 | + |
| 31 | +class GameTest(BaseTest, unittest.TestCase): |
| 32 | + """This object represents Tests for Telegram Game.""" |
| 33 | + |
| 34 | + def setUp(self): |
| 35 | + self.title = 'Python-telegram-bot Test Game' |
| 36 | + self.description = 'description' |
| 37 | + self.photo = [telegram.PhotoSize(file_id='Blah', width=640, height=360)] |
| 38 | + self.text = 'Other description' |
| 39 | + self.text_entities = [telegram.MessageEntity( |
| 40 | + type=telegram.MessageEntity.URL, offset=13, length=17)] |
| 41 | + self.animation = telegram.Animation(file_id='Bleh') |
| 42 | + |
| 43 | + self.json_dict = { |
| 44 | + 'title': self.title, |
| 45 | + 'description': self.description, |
| 46 | + 'photo': self.photo, |
| 47 | + 'text': self.text, |
| 48 | + 'text_entities': [e.to_json() for e in self.text_entities], |
| 49 | + 'animation': self.animation.to_json() |
| 50 | + } |
| 51 | + |
| 52 | + def test_game_de_json(self): |
| 53 | + game = telegram.Game.de_json(self.json_dict, self._bot) |
| 54 | + |
| 55 | + self.assertEqual(game.title, self.title) |
| 56 | + self.assertEqual(game.description, self.description) |
| 57 | + self.assertTrue(isinstance(game.photo[0], telegram.PhotoSize)) |
| 58 | + self.assertEqual(game.text, self.text) |
| 59 | + self.assertTrue(isinstance(game.text_entities[0], telegram.MessageEntity)) |
| 60 | + self.assertTrue(isinstance(game.animation, telegram.Animation)) |
| 61 | + |
| 62 | + def test_game_to_json(self): |
| 63 | + game = telegram.Game.de_json(self.json_dict, self._bot) |
| 64 | + |
| 65 | + self.assertTrue(self.is_json(game.to_json())) |
| 66 | + |
| 67 | + def test_game_all_args(self): |
| 68 | + game = telegram.Game( |
| 69 | + title=self.title, |
| 70 | + description=self.description, |
| 71 | + photo=self.photo, |
| 72 | + text=self.text, |
| 73 | + text_entities=self.text_entities, |
| 74 | + animation=self.animation) |
| 75 | + |
| 76 | + self.assertEqual(game.title, self.title) |
| 77 | + self.assertEqual(game.description, self.description) |
| 78 | + self.assertEqual(game.photo, self.photo) |
| 79 | + self.assertEqual(game.text, self.text) |
| 80 | + self.assertEqual(game.text_entities, self.text_entities) |
| 81 | + self.assertEqual(game.animation, self.animation) |
| 82 | + |
| 83 | + def test_parse_entity(self): |
| 84 | + text = (b'\\U0001f469\\u200d\\U0001f469\\u200d\\U0001f467' |
| 85 | + b'\\u200d\\U0001f467\\U0001f431http://google.com').decode('unicode-escape') |
| 86 | + entity = telegram.MessageEntity(type=telegram.MessageEntity.URL, offset=13, length=17) |
| 87 | + game = telegram.Game( |
| 88 | + self.title, self.description, self.photo, text=text, text_entities=[entity]) |
| 89 | + self.assertEqual(game.parse_text_entity(entity), 'http://google.com') |
| 90 | + |
| 91 | + def test_parse_entities(self): |
| 92 | + text = (b'\\U0001f469\\u200d\\U0001f469\\u200d\\U0001f467' |
| 93 | + b'\\u200d\\U0001f467\\U0001f431http://google.com').decode('unicode-escape') |
| 94 | + entity = telegram.MessageEntity(type=telegram.MessageEntity.URL, offset=13, length=17) |
| 95 | + entity_2 = telegram.MessageEntity(type=telegram.MessageEntity.BOLD, offset=13, length=1) |
| 96 | + game = telegram.Game( |
| 97 | + self.title, self.description, self.photo, text=text, text_entities=[entity_2, entity]) |
| 98 | + self.assertDictEqual( |
| 99 | + game.parse_text_entities(telegram.MessageEntity.URL), {entity: 'http://google.com'}) |
| 100 | + self.assertDictEqual(game.parse_text_entities(), {entity: 'http://google.com', |
| 101 | + entity_2: 'h'}) |
| 102 | + |
| 103 | + |
| 104 | +class AnimationTest(BaseTest, unittest.TestCase): |
| 105 | + """This object represents Tests for Telegram Animatiion.""" |
| 106 | + |
| 107 | + def setUp(self): |
| 108 | + self.file_id = 'thisisafileid' |
| 109 | + self.thumb = telegram.PhotoSize(file_id='Blah', width=640, height=360) |
| 110 | + self.file_name = 'File name' |
| 111 | + self.mime_type = 'something/gif' |
| 112 | + self.file_size = 42 |
| 113 | + |
| 114 | + self.json_dict = { |
| 115 | + 'file_id': self.file_id, |
| 116 | + 'thumb': self.thumb, |
| 117 | + 'file_name': self.file_name, |
| 118 | + 'mime_type': self.mime_type, |
| 119 | + 'file_size': self.file_size |
| 120 | + } |
| 121 | + |
| 122 | + def test_animation_de_json(self): |
| 123 | + animation = telegram.Animation.de_json(self.json_dict, self._bot) |
| 124 | + |
| 125 | + self.assertEqual(animation.file_id, self.file_id) |
| 126 | + self.assertEqual(animation.thumb, self.thumb) |
| 127 | + self.assertEqual(animation.file_name, self.file_name) |
| 128 | + self.assertEqual(animation.mime_type, self.mime_type) |
| 129 | + self.assertEqual(animation.file_size, self.file_size) |
| 130 | + |
| 131 | + def test_game_to_json(self): |
| 132 | + animation = telegram.Animation.de_json(self.json_dict, self._bot) |
| 133 | + |
| 134 | + self.assertTrue(self.is_json(animation.to_json())) |
0 commit comments