python-telegram-bot/tests/test_sticker.py

153 lines
4.9 KiB
Python
Raw Normal View History

2015-09-07 17:10:29 +02: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>
2015-09-07 17:10:29 +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 Sticker"""
2015-09-07 17:10:29 +02:00
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-05-12 08:31:47 +02:00
from future.utils import PY2
2016-02-21 12:52:47 +01:00
2015-09-07 17:10:29 +02:00
sys.path.append('.')
import telegram
from tests.base import BaseTest, timeout
2015-09-07 17:10:29 +02:00
class StickerTest(BaseTest, unittest.TestCase):
"""This object represents Tests for Telegram Sticker."""
def setUp(self):
2017-02-05 23:10:48 +01:00
self.sticker_file_id = 'CAADAQADHAADyIsGAAFZfq1bphjqlgI'
2015-09-07 17:10:29 +02:00
self.width = 510
self.height = 512
2016-10-12 22:56:57 +02:00
self.thumb = {
'width': 90,
'height': 90,
'file_id': 'BQADAQADoQADHyP1B0mzJMVyzcB0Ag',
'file_size': 2364
}
2016-05-11 23:16:36 +02:00
self.emoji = telegram.Emoji.FLEXED_BICEPS
2015-09-07 17:10:29 +02:00
self.file_size = 39518
self.json_dict = {
'file_id': self.sticker_file_id,
'width': self.width,
'height': self.height,
'thumb': self.thumb,
2016-05-11 23:16:36 +02:00
'emoji': self.emoji,
2015-09-07 17:10:29 +02:00
'file_size': self.file_size
}
@flaky(3, 1)
@timeout(10)
2015-09-07 17:10:29 +02:00
def test_send_sticker_file(self):
pass
@flaky(3, 1)
@timeout(10)
2015-09-07 17:10:29 +02:00
def test_send_sticker_resend(self):
2016-05-15 02:52:35 +02:00
message = self._bot.sendSticker(chat_id=self._chat_id, sticker=self.sticker_file_id)
2015-09-07 17:10:29 +02:00
sticker = message.sticker
self.assertEqual(sticker.file_id, self.sticker_file_id)
self.assertEqual(sticker.width, self.width)
self.assertEqual(sticker.height, self.height)
self.assertTrue(isinstance(sticker.thumb, telegram.PhotoSize))
2016-05-12 08:31:47 +02:00
if PY2:
self.assertEqual(sticker.emoji, self.emoji.decode('utf-8'))
else:
self.assertEqual(sticker.emoji, self.emoji)
# self.assertEqual(sticker.file_size, self.file_size) # TODO
2015-09-07 17:10:29 +02:00
def test_sticker_de_json(self):
sticker = telegram.Sticker.de_json(self.json_dict, self._bot)
2015-09-07 17:10:29 +02:00
self.assertEqual(sticker.file_id, self.sticker_file_id)
self.assertEqual(sticker.width, self.width)
self.assertEqual(sticker.height, self.height)
self.assertTrue(isinstance(sticker.thumb, telegram.PhotoSize))
2016-05-11 23:16:36 +02:00
self.assertEqual(sticker.emoji, self.emoji)
2015-09-07 17:10:29 +02:00
self.assertEqual(sticker.file_size, self.file_size)
def test_sticker_to_json(self):
sticker = telegram.Sticker.de_json(self.json_dict, self._bot)
2015-09-07 17:10:29 +02:00
self.assertTrue(self.is_json(sticker.to_json()))
def test_sticker_to_dict(self):
sticker = telegram.Sticker.de_json(self.json_dict, self._bot)
2015-09-07 17:10:29 +02:00
self.assertEqual(sticker['file_id'], self.sticker_file_id)
self.assertEqual(sticker['width'], self.width)
self.assertEqual(sticker['height'], self.height)
self.assertTrue(isinstance(sticker['thumb'], telegram.PhotoSize))
2016-05-11 23:16:36 +02:00
self.assertEqual(sticker['emoji'], self.emoji)
2015-09-07 17:10:29 +02:00
self.assertEqual(sticker['file_size'], self.file_size)
@flaky(3, 1)
@timeout(10)
2015-09-07 17:10:29 +02:00
def test_error_send_sticker_empty_file(self):
json_dict = self.json_dict
2016-04-24 04:11:25 +02:00
del (json_dict['file_id'])
2015-09-07 17:10:29 +02:00
json_dict['sticker'] = open(os.devnull, 'rb')
2016-05-15 02:52:35 +02:00
self.assertRaises(
telegram.TelegramError,
lambda: self._bot.sendSticker(chat_id=self._chat_id, **json_dict))
2015-09-07 17:10:29 +02:00
@flaky(3, 1)
@timeout(10)
2015-09-07 17:10:29 +02:00
def test_error_send_sticker_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 17:10:29 +02:00
json_dict['sticker'] = ''
2016-05-15 02:52:35 +02:00
self.assertRaises(
telegram.TelegramError,
lambda: self._bot.sendSticker(chat_id=self._chat_id, **json_dict))
2015-09-07 17:10:29 +02:00
@flaky(3, 1)
@timeout(10)
2015-09-07 17:10:29 +02:00
def test_error_sticker_without_required_args(self):
json_dict = self.json_dict
2016-04-24 04:11:25 +02:00
del (json_dict['file_id'])
2015-09-07 17:10:29 +02:00
2016-05-15 02:52:35 +02:00
self.assertRaises(
TypeError,
lambda: self._bot.sendSticker(chat_id=self._chat_id, **json_dict))
2016-04-24 04:11:25 +02:00
@flaky(3, 1)
@timeout(10)
def test_reply_sticker(self):
"""Test for Message.reply_sticker"""
message = self._bot.sendMessage(self._chat_id, '.')
message = message.reply_sticker(self.sticker_file_id)
self.assertNotEqual(message.sticker.file_id, '')
2015-09-07 17:10:29 +02:00
if __name__ == '__main__':
unittest.main()