X Tutup
Skip to content

Commit 65a2248

Browse files
committed
Much better, such wow Audio/Voice/Video tests
1 parent 0218aa2 commit 65a2248

File tree

3 files changed

+224
-11
lines changed

3 files changed

+224
-11
lines changed

tests/test_audio.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,21 @@ def test_send_audio_required_args_only(self):
5353
print('Testing bot.sendAudio - With required arguments only')
5454

5555
message = self._bot.sendAudio(self._chat_id,
56-
self.audio_file,
57-
self.duration)
56+
self.audio_file)
5857

5958
audio = message.audio
6059

6160
self.assertTrue(isinstance(audio.file_id, str))
6261
self.assertNotEqual(audio.file_id, '')
63-
self.assertEqual(audio.duration, self.duration)
62+
self.assertEqual(audio.duration, 4)
63+
self.assertEqual(audio.performer, '')
64+
self.assertEqual(audio.title, '')
6465
self.assertEqual(audio.mime_type, self.mime_type)
6566
self.assertEqual(audio.file_size, self.file_size)
6667

6768
def test_send_audio_all_args(self):
6869
"""Test telegram.Bot sendAudio method"""
69-
print('Testing bot.sendAudio - With all required arguments')
70+
print('Testing bot.sendAudio - With all arguments')
7071

7172
message = self._bot.sendAudio(self._chat_id,
7273
self.audio_file,
@@ -113,16 +114,16 @@ def test_send_audio_resend(self):
113114
message = self._bot.sendAudio(chat_id=self._chat_id,
114115
audio=self.audio_file_id,
115116
duration=self.duration,
116-
performer=self.performer, # bug: backwards compatibility with Audio
117-
title=self.title) # bug: backwards compatibility with Audio
117+
performer=self.performer,
118+
title=self.title)
118119

119120
audio = message.audio
120121

121122
self.assertEqual(audio.file_id, self.audio_file_id)
122123
self.assertEqual(audio.duration, self.duration)
123124
self.assertEqual(audio.performer, self.performer)
124125
self.assertEqual(audio.title, self.title)
125-
self.assertEqual(audio.mime_type, '') # bug: empty
126+
self.assertEqual(audio.mime_type, '')
126127
self.assertEqual(audio.file_size, self.file_size)
127128

128129
def test_audio_de_json(self):

tests/test_video.py

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
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 Video"""
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 VideoTest(BaseTest, unittest.TestCase):
31+
"""This object represents Tests for Telegram Video."""
32+
33+
def setUp(self):
34+
self.video_file = open('tests/data/telegram.mp4', 'rb')
35+
self.video_file_id = 'BAADAQADXwADHyP1BwJFTcmY2RYCAg'
36+
self.width = 360
37+
self.height = 640
38+
self.duration = 4
39+
self.thumb = telegram.PhotoSize.de_json({})
40+
self.mime_type = 'video/mp4'
41+
self.file_size = 326534
42+
43+
# caption is part of sendVideo method but not Video object
44+
self.caption = u'VideoTest - Caption'
45+
46+
self.json_dict = {
47+
'file_id': self.video_file_id,
48+
'width': self.width,
49+
'height': self.height,
50+
'duration': self.duration,
51+
'thumb': self.thumb,
52+
'mime_type': self.mime_type,
53+
'file_size': self.file_size
54+
}
55+
56+
def test_send_video_required_args_only(self):
57+
"""Test telegram.Bot sendVideo method"""
58+
print('Testing bot.sendVideo - With required arguments only')
59+
60+
message = self._bot.sendVideo(self._chat_id,
61+
self.video_file)
62+
63+
video = message.video
64+
65+
self.assertTrue(isinstance(video.file_id, str))
66+
self.assertNotEqual(video.file_id, '')
67+
self.assertEqual(video.width, 0)
68+
self.assertEqual(video.height, 0)
69+
self.assertEqual(video.duration, 0)
70+
self.assertEqual(video.thumb, None)
71+
self.assertEqual(video.mime_type, '')
72+
self.assertEqual(video.file_size, self.file_size)
73+
74+
def test_send_video_all_args(self):
75+
"""Test telegram.Bot sendAudio method"""
76+
print('Testing bot.sendVideo - With all arguments')
77+
78+
message = self._bot.sendVideo(self._chat_id,
79+
self.video_file,
80+
duration=self.duration,
81+
caption=self.caption)
82+
83+
video = message.video
84+
85+
self.assertTrue(isinstance(video.file_id, str))
86+
self.assertNotEqual(video.file_id, '')
87+
self.assertEqual(video.width, 0)
88+
self.assertEqual(video.height, 0)
89+
self.assertEqual(video.duration, self.duration)
90+
self.assertEqual(video.thumb, None)
91+
self.assertEqual(video.mime_type, '')
92+
self.assertEqual(video.file_size, self.file_size)
93+
94+
self.assertEqual(message.caption, self.caption)
95+
96+
def test_send_video_mp4_file(self):
97+
"""Test telegram.Bot sendVideo method"""
98+
print('Testing bot.sendVideo - MP4 File')
99+
100+
message = self._bot.sendVideo(chat_id=self._chat_id,
101+
video=self.video_file,
102+
duration=self.duration,
103+
caption=self.caption)
104+
105+
video = message.video
106+
107+
self.assertTrue(isinstance(video.file_id, str))
108+
self.assertNotEqual(video.file_id, '')
109+
self.assertEqual(video.width, 0)
110+
self.assertEqual(video.height, 0)
111+
self.assertEqual(video.duration, self.duration)
112+
self.assertEqual(video.thumb, None)
113+
self.assertEqual(video.mime_type, '')
114+
self.assertEqual(video.file_size, self.file_size)
115+
116+
self.assertEqual(message.caption, self.caption)
117+
118+
def test_send_video_resend(self):
119+
"""Test telegram.Bot sendVideo method"""
120+
print('Testing bot.sendVideo - Resend by file_id')
121+
122+
message = self._bot.sendVideo(chat_id=self._chat_id,
123+
video=self.video_file_id,
124+
duration=self.duration,
125+
caption=self.caption)
126+
127+
video = message.video
128+
129+
self.assertEqual(video.file_id, self.video_file_id)
130+
self.assertEqual(video.duration, 0)
131+
self.assertEqual(video.thumb, None)
132+
self.assertEqual(video.mime_type, '')
133+
self.assertEqual(video.file_size, self.file_size)
134+
135+
self.assertEqual(message.caption, self.caption)
136+
137+
def test_video_de_json(self):
138+
"""Test Video.de_json() method"""
139+
print('Testing Video.de_json()')
140+
141+
video = telegram.Video.de_json(self.json_dict)
142+
143+
self.assertEqual(video.file_id, self.video_file_id)
144+
self.assertEqual(video.width, self.width)
145+
self.assertEqual(video.height, self.height)
146+
self.assertEqual(video.duration, self.duration)
147+
self.assertEqual(video.thumb, None)
148+
self.assertEqual(video.mime_type, self.mime_type)
149+
self.assertEqual(video.file_size, self.file_size)
150+
151+
def test_video_to_json(self):
152+
"""Test Video.to_json() method"""
153+
print('Testing Video.to_json()')
154+
155+
video = telegram.Video.de_json(self.json_dict)
156+
157+
self.assertTrue(self.is_json(video.to_json()))
158+
159+
def test_video_to_dict(self):
160+
"""Test Video.to_dict() method"""
161+
print('Testing Video.to_dict()')
162+
163+
video = telegram.Video.de_json(self.json_dict)
164+
165+
self.assertTrue(self.is_dict(video.to_dict()))
166+
self.assertEqual(video['file_id'], self.video_file_id)
167+
self.assertEqual(video['width'], self.width)
168+
self.assertEqual(video['height'], self.height)
169+
self.assertEqual(video['duration'], self.duration)
170+
self.assertEqual(video['mime_type'], self.mime_type)
171+
self.assertEqual(video['file_size'], self.file_size)
172+
173+
def test_error_send_video_empty_file(self):
174+
print('Testing bot.sendVideo - Null file')
175+
176+
json_dict = self.json_dict
177+
178+
del(json_dict['file_id'])
179+
json_dict['video'] = open(os.devnull, 'rb')
180+
181+
self.assertRaises(telegram.TelegramError,
182+
lambda: self._bot.sendVideo(chat_id=self._chat_id,
183+
**json_dict))
184+
185+
def test_error_send_video_empty_file_id(self):
186+
print('Testing bot.sendVideo - Empty file_id')
187+
188+
json_dict = self.json_dict
189+
190+
del(json_dict['file_id'])
191+
json_dict['video'] = ''
192+
193+
self.assertRaises(telegram.TelegramError,
194+
lambda: self._bot.sendVideo(chat_id=self._chat_id,
195+
**json_dict))
196+
197+
def test_error_video_without_required_args(self):
198+
print('Testing bot.sendVideo - Without required arguments')
199+
200+
json_dict = self.json_dict
201+
202+
del(json_dict['file_id'])
203+
del(json_dict['duration'])
204+
205+
self.assertRaises(TypeError,
206+
lambda: self._bot.sendVideo(chat_id=self._chat_id,
207+
**json_dict))
208+
209+
if __name__ == '__main__':
210+
unittest.main()

tests/test_voice.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ def test_send_voice_required_args_only(self):
5959
self.assertEqual(voice.mime_type, self.mime_type)
6060
self.assertEqual(voice.file_size, self.file_size)
6161

62-
def test_send_audio_all_args(self):
62+
def test_send_voice_all_args(self):
6363
"""Test telegram.Bot sendAudio method"""
64-
print('Testing bot.sendVoice - With all required arguments')
64+
print('Testing bot.sendVoice - With all arguments')
6565

6666
message = self._bot.sendVoice(self._chat_id,
6767
self.voice_file,
@@ -82,7 +82,8 @@ def test_send_voice_ogg_file(self):
8282
print('Testing bot.sendVoice - Ogg File')
8383

8484
message = self._bot.sendVoice(chat_id=self._chat_id,
85-
voice=self.voice_file)
85+
voice=self.voice_file,
86+
duration=self.duration)
8687

8788
voice = message.voice
8889

@@ -97,7 +98,8 @@ def test_send_voice_resend(self):
9798
print('Testing bot.sendVoice - Resend by file_id')
9899

99100
message = self._bot.sendVoice(chat_id=self._chat_id,
100-
voice=self.voice_file_id)
101+
voice=self.voice_file_id,
102+
duration=self.duration)
101103

102104
voice = message.voice
103105

0 commit comments

Comments
 (0)
X Tutup