X Tutup
Skip to content

Commit 84b59c8

Browse files
committed
unittest sending non standard jpg photo with bytesio stream
1 parent b46bdf5 commit 84b59c8

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed
44.3 KB
Loading

tests/test_photo.py

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

21+
from io import BytesIO
2122
import sys
2223
import unittest
2324
import os
@@ -37,6 +38,7 @@ def setUp(self):
3738
self.photo_file = open('tests/data/telegram.jpg', 'rb')
3839
self.photo_file_id = 'AgADAQADgEsyGx8j9QfmDMmwkPBrFcKRzy8ABHW8ul9nW7FoNHYBAAEC'
3940
self.photo_file_url = 'https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/tests/data/telegram.jpg'
41+
self.photo_bytes_jpg_no_standard = 'tests/data/telegram_no_standard_header.jpg'
4042
self.width = 300
4143
self.height = 300
4244
self.thumb = {
@@ -122,6 +124,32 @@ def test_send_photo_url_jpg_file(self):
122124
self.assertEqual(photo.height, self.height)
123125
self.assertEqual(photo.file_size, self.file_size)
124126

127+
@flaky(3, 1)
128+
@timeout(10)
129+
def test_send_photo_bytesio_jpg_file(self):
130+
from telegram.inputfile import InputFile
131+
# raw image bytes
132+
raw_bytes = BytesIO(open(self.photo_bytes_jpg_no_standard, 'rb').read())
133+
inputfile = InputFile({"photo": raw_bytes})
134+
self.assertEqual(inputfile.mimetype, 'application/octet-stream')
135+
136+
# raw image bytes with name info
137+
raw_bytes = BytesIO(open(self.photo_bytes_jpg_no_standard, 'rb').read())
138+
raw_bytes.name = self.photo_bytes_jpg_no_standard
139+
inputfile = InputFile({"photo": raw_bytes})
140+
self.assertEqual(inputfile.mimetype, 'image/jpeg')
141+
142+
# send raw photo
143+
raw_bytes = BytesIO(open(self.photo_bytes_jpg_no_standard, 'rb').read())
144+
message = self._bot.sendPhoto(self._chat_id, photo=raw_bytes)
145+
photo = message.photo[-1]
146+
self.assertTrue(isinstance(photo.file_id, str))
147+
self.assertNotEqual(photo.file_id, '')
148+
self.assertTrue(isinstance(photo, telegram.PhotoSize))
149+
self.assertEqual(photo.width, 1920)
150+
self.assertEqual(photo.height, 1080)
151+
self.assertEqual(photo.file_size, 30907)
152+
125153
@flaky(3, 1)
126154
@timeout(10)
127155
def test_send_photo_resend(self):

0 commit comments

Comments
 (0)
X Tutup