Skip to content

Commit ac0793c

Browse files
committed
test_sticker modified
* cleanup assertions in test_video * modify test_sticker to same standard as the others.
1 parent 28836b7 commit ac0793c

File tree

2 files changed

+104
-63
lines changed

2 files changed

+104
-63
lines changed

tests/test_sticker.py

Lines changed: 102 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -18,90 +18,136 @@
1818
# along with this program. If not, see [http://www.gnu.org/licenses/].
1919
"""This module contains an object that represents Tests for Telegram Sticker"""
2020

21-
import sys
22-
import unittest
2321
import os
22+
import unittest
2423

2524
from flaky import flaky
2625
from future.utils import PY2
2726

28-
sys.path.append('.')
29-
3027
import telegram
3128
from tests.base import BaseTest, timeout
29+
from tests.bots import get_bot
3230

3331

3432
class StickerTest(BaseTest, unittest.TestCase):
3533
"""This object represents Tests for Telegram Sticker."""
3634

37-
def setUp(self):
38-
self.sticker_file_id = 'CAADAQADHAADyIsGAAFZfq1bphjqlgI'
39-
self.width = 510
40-
self.height = 512
41-
self.thumb = {
42-
'width': 90,
43-
'height': 90,
44-
'file_id': 'BQADAQADoQADHyP1B0mzJMVyzcB0Ag',
45-
'file_size': 2364
46-
}
47-
self.emoji = telegram.Emoji.FLEXED_BICEPS
48-
self.file_size = 39518
35+
@classmethod
36+
def setUpClass(cls):
37+
cls.emoji = telegram.Emoji.FLEXED_BICEPS
38+
cls.sticker_file_url = "https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/tests/data/telegram.webp" # noqa
39+
40+
bot_info = get_bot()
41+
cls._chat_id = bot_info['chat_id']
42+
cls._bot = telegram.Bot(bot_info['token'])
43+
44+
sticker_file = open('tests/data/telegram.webp', 'rb')
45+
sticker = cls._bot.send_sticker(cls._chat_id, sticker=sticker_file, timeout=10).sticker
46+
cls.sticker = sticker
47+
cls.thumb = sticker.thumb
48+
49+
# Make sure file has been uploaded.
50+
# Simple assertions PY2 Only
51+
assert isinstance(cls.sticker, telegram.Sticker)
52+
assert isinstance(cls.sticker.file_id, str)
53+
assert cls.sticker.file_id is not ''
54+
assert isinstance(cls.thumb, telegram.PhotoSize)
55+
assert isinstance(cls.thumb.file_id, str)
56+
assert cls.thumb.file_id is not ''
4957

58+
def setUp(self):
59+
self.sticker_file = open('tests/data/telegram.webp', 'rb')
5060
self.json_dict = {
51-
'file_id': self.sticker_file_id,
52-
'width': self.width,
53-
'height': self.height,
54-
'thumb': self.thumb,
61+
'file_id': self.sticker.file_id,
62+
'width': self.sticker.width,
63+
'height': self.sticker.height,
64+
'thumb': self.thumb.to_dict(),
5565
'emoji': self.emoji,
56-
'file_size': self.file_size
66+
'file_size': self.sticker.file_size
5767
}
5868

69+
def test_expected_values(self):
70+
self.assertEqual(self.sticker.width, 510)
71+
self.assertEqual(self.sticker.height, 512)
72+
self.assertEqual(self.sticker.file_size, 39518)
73+
self.assertEqual(self.thumb.width, 90)
74+
self.assertEqual(self.thumb.height, 90)
75+
self.assertEqual(self.thumb.file_size, 3672)
76+
5977
@flaky(3, 1)
6078
@timeout(10)
61-
def test_send_sticker_file(self):
62-
pass
79+
def test_send_sticker_all_args(self):
80+
message = self._bot.sendSticker(chat_id=self._chat_id, sticker=self.sticker.file_id, disable_notification=False)
81+
sticker = message.sticker
82+
83+
self.assertEqual(sticker, self.sticker)
6384

6485
@flaky(3, 1)
6586
@timeout(10)
6687
def test_send_sticker_resend(self):
67-
message = self._bot.sendSticker(chat_id=self._chat_id, sticker=self.sticker_file_id)
88+
message = self._bot.sendSticker(chat_id=self._chat_id, sticker=self.sticker.file_id)
6889

6990
sticker = message.sticker
7091

71-
self.assertEqual(sticker.file_id, self.sticker_file_id)
72-
self.assertEqual(sticker.width, self.width)
73-
self.assertEqual(sticker.height, self.height)
74-
self.assertTrue(isinstance(sticker.thumb, telegram.PhotoSize))
92+
self.assertEqual(sticker.file_id, self.sticker.file_id)
93+
self.assertEqual(sticker.width, self.sticker.width)
94+
self.assertEqual(sticker.height, self.sticker.height)
95+
self.assertIsInstance(sticker.thumb, telegram.PhotoSize)
96+
self.assertEqual(sticker.file_size, self.sticker.file_size)
97+
98+
@flaky(3, 1)
99+
@timeout(10)
100+
def test_sticker_on_server_emoji(self):
101+
server_file_id = "CAADAQADHAADyIsGAAFZfq1bphjqlgI"
102+
message = self._bot.sendSticker(chat_id=self._chat_id, sticker=server_file_id)
103+
sticker = message.sticker
75104
if PY2:
76105
self.assertEqual(sticker.emoji, self.emoji.decode('utf-8'))
77106
else:
78107
self.assertEqual(sticker.emoji, self.emoji)
79-
# self.assertEqual(sticker.file_size, self.file_size) # TODO
108+
109+
@flaky(3, 1)
110+
@timeout(10)
111+
def test_send_sticker_from_url(self):
112+
message = self._bot.sendSticker(chat_id=self._chat_id, sticker=self.sticker_file_url)
113+
sticker = message.sticker
114+
115+
self.assertIsInstance(sticker, telegram.Sticker)
116+
self.assertIsInstance(sticker.file_id, str)
117+
self.assertNotEqual(sticker.file_id, '')
118+
self.assertEqual(sticker.file_size, self.sticker.file_size)
119+
self.assertEqual(sticker.height, self.sticker.height)
120+
self.assertEqual(sticker.width, self.sticker.width)
121+
thumb = sticker.thumb
122+
self.assertIsInstance(thumb, telegram.PhotoSize)
123+
self.assertIsInstance(thumb.file_id, str)
124+
self.assertNotEqual(thumb.file_id, '')
125+
self.assertEqual(thumb.file_size, self.thumb.file_size)
126+
self.assertEqual(thumb.width, self.thumb.width)
127+
self.assertEqual(thumb.height, self.thumb.height)
80128

81129
def test_sticker_de_json(self):
82130
sticker = telegram.Sticker.de_json(self.json_dict, self._bot)
83131

84-
self.assertEqual(sticker.file_id, self.sticker_file_id)
85-
self.assertEqual(sticker.width, self.width)
86-
self.assertEqual(sticker.height, self.height)
87-
self.assertTrue(isinstance(sticker.thumb, telegram.PhotoSize))
132+
self.assertEqual(sticker.file_id, self.sticker.file_id)
133+
self.assertEqual(sticker.width, self.sticker.width)
134+
self.assertEqual(sticker.height, self.sticker.height)
135+
self.assertIsInstance(sticker.thumb, telegram.PhotoSize)
88136
self.assertEqual(sticker.emoji, self.emoji)
89-
self.assertEqual(sticker.file_size, self.file_size)
137+
self.assertEqual(sticker.file_size, self.sticker.file_size)
90138

91139
def test_sticker_to_json(self):
92-
sticker = telegram.Sticker.de_json(self.json_dict, self._bot)
93-
94-
self.assertTrue(self.is_json(sticker.to_json()))
140+
self.assertTrue(self.is_json(self.sticker.to_json()))
95141

96142
def test_sticker_to_dict(self):
97-
sticker = telegram.Sticker.de_json(self.json_dict, self._bot)
143+
sticker = self.sticker.to_dict()
98144

99-
self.assertEqual(sticker['file_id'], self.sticker_file_id)
100-
self.assertEqual(sticker['width'], self.width)
101-
self.assertEqual(sticker['height'], self.height)
102-
self.assertTrue(isinstance(sticker['thumb'], telegram.PhotoSize))
103-
self.assertEqual(sticker['emoji'], self.emoji)
104-
self.assertEqual(sticker['file_size'], self.file_size)
145+
self.is_dict(sticker)
146+
self.assertEqual(sticker['file_id'], self.sticker.file_id)
147+
self.assertEqual(sticker['width'], self.sticker.width)
148+
self.assertEqual(sticker['height'], self.sticker.height)
149+
self.assertIsInstance(sticker['thumb'], telegram.PhotoSize)
150+
self.assertEqual(sticker['file_size'], self.sticker.file_size)
105151

106152
@flaky(3, 1)
107153
@timeout(10)
@@ -111,9 +157,8 @@ def test_error_send_sticker_empty_file(self):
111157
del (json_dict['file_id'])
112158
json_dict['sticker'] = open(os.devnull, 'rb')
113159

114-
self.assertRaises(
115-
telegram.TelegramError,
116-
lambda: self._bot.sendSticker(chat_id=self._chat_id, **json_dict))
160+
with self.assertRaises(telegram.TelegramError):
161+
self._bot.sendSticker(chat_id=self._chat_id, **json_dict)
117162

118163
@flaky(3, 1)
119164
@timeout(10)
@@ -123,9 +168,8 @@ def test_error_send_sticker_empty_file_id(self):
123168
del (json_dict['file_id'])
124169
json_dict['sticker'] = ''
125170

126-
self.assertRaises(
127-
telegram.TelegramError,
128-
lambda: self._bot.sendSticker(chat_id=self._chat_id, **json_dict))
171+
with self.assertRaises(telegram.TelegramError):
172+
self._bot.sendSticker(chat_id=self._chat_id, **json_dict)
129173

130174
@flaky(3, 1)
131175
@timeout(10)
@@ -134,25 +178,24 @@ def test_error_sticker_without_required_args(self):
134178

135179
del (json_dict['file_id'])
136180

137-
self.assertRaises(
138-
TypeError,
139-
lambda: self._bot.sendSticker(chat_id=self._chat_id, **json_dict))
181+
with self.assertRaises(TypeError):
182+
self._bot.sendSticker(chat_id=self._chat_id, **json_dict)
140183

141184
@flaky(3, 1)
142185
@timeout(10)
143186
def test_reply_sticker(self):
144187
"""Test for Message.reply_sticker"""
145188
message = self._bot.sendMessage(self._chat_id, '.')
146-
message = message.reply_sticker(self.sticker_file_id)
189+
message = message.reply_sticker(self.sticker.file_id)
147190

148191
self.assertNotEqual(message.sticker.file_id, '')
149192

150193
def test_equality(self):
151-
a = telegram.Sticker(self.sticker_file_id, self.width, self.height)
152-
b = telegram.Sticker(self.sticker_file_id, self.width, self.height)
153-
c = telegram.Sticker(self.sticker_file_id, 0, 0)
154-
d = telegram.Sticker("", self.width, self.height)
155-
e = telegram.PhotoSize(self.sticker_file_id, self.width, self.height)
194+
a = telegram.Sticker(self.sticker.file_id, self.sticker.width, self.sticker.height)
195+
b = telegram.Sticker(self.sticker.file_id, self.sticker.width, self.sticker.height)
196+
c = telegram.Sticker(self.sticker.file_id, 0, 0)
197+
d = telegram.Sticker("", self.sticker.width, self.sticker.height)
198+
e = telegram.PhotoSize(self.sticker.file_id, self.sticker.width, self.sticker.height)
156199

157200
self.assertEqual(a, b)
158201
self.assertEqual(hash(a), hash(b))

tests/test_video.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def test_send_video_mp4_file_url(self):
104104

105105
video = message.video
106106

107-
self.assertTrue(isinstance(video.file_id, str))
107+
self.assertIsInstance(video.file_id, str)
108108
self.assertNotEqual(video.file_id, None)
109109
self.assertEqual(video.height, self.video.height)
110110
self.assertEqual(video.duration, self.video.duration)
@@ -122,9 +122,7 @@ def test_send_video_resend(self):
122122
message = self._bot.sendVideo(
123123
chat_id=self._chat_id,
124124
video=self.video.file_id,
125-
timeout=10,
126-
duration=self.video.duration,
127-
caption=self.caption)
125+
timeout=10)
128126

129127
video = message.video
130128

0 commit comments

Comments
 (0)