Skip to content

Commit f6a98d7

Browse files
committed
test_photo done
* Changed to new method of non-static file_id * removed obsolete tests.
1 parent e7b839b commit f6a98d7

File tree

1 file changed

+92
-93
lines changed

1 file changed

+92
-93
lines changed

tests/test_photo.py

Lines changed: 92 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
from flaky import flaky
2727

28+
from tests.bots import get_bot
29+
2830
sys.path.append('.')
2931

3032
import telegram
@@ -34,95 +36,88 @@
3436
class PhotoTest(BaseTest, unittest.TestCase):
3537
"""This object represents Tests for Telegram Photo."""
3638

39+
@classmethod
40+
def setUpClass(cls):
41+
cls.caption = u'PhotoTest - Caption'
42+
cls.photo_file_url = 'https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/tests/data/telegram.jpg'
43+
44+
bot_info = get_bot()
45+
cls._chat_id = bot_info['chat_id']
46+
cls._bot = telegram.Bot(bot_info['token'])
47+
48+
photo_file = open('tests/data/telegram.jpg', 'rb')
49+
photo = cls._bot.send_photo(cls._chat_id, photo=photo_file, timeout=10).photo
50+
cls.thumb, cls.photo = photo
51+
52+
# Make sure file has been uploaded.
53+
# Simple assertions PY2 Only
54+
assert isinstance(cls.photo, telegram.PhotoSize)
55+
assert isinstance(cls.thumb, telegram.PhotoSize)
56+
assert isinstance(cls.photo.file_id, str)
57+
assert isinstance(cls.thumb.file_id, str)
58+
assert cls.photo.file_id is not ''
59+
assert cls.thumb.file_id is not ''
60+
3761
def setUp(self):
3862
self.photo_file = open('tests/data/telegram.jpg', 'rb')
39-
self.photo_file_id = 'AgADAQADgEsyGx8j9QfmDMmwkPBrFcKRzy8ABHW8ul9nW7FoNHYBAAEC'
40-
self.photo_file_url = 'https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/tests/data/telegram.jpg'
4163
self.photo_bytes_jpg_no_standard = 'tests/data/telegram_no_standard_header.jpg'
42-
self.width = 300
43-
self.height = 300
44-
self.thumb = {
45-
'width': 90,
46-
'height': 90,
47-
'file_id': 'AgADAQADgEsyGx8j9QeYW9oDz2mKRsKRzy8ABD64nkFkjujeNXYBAAEC',
48-
'file_size': 1478
49-
}
50-
self.file_size = 10209
51-
52-
# caption is part of sendPhoto method but not Photo object
53-
self.caption = u'PhotoTest - Caption'
54-
5564
self.json_dict = {
56-
'file_id': self.photo_file_id,
57-
'width': self.width,
58-
'height': self.height,
59-
'file_size': self.file_size
65+
'file_id': self.photo.file_id,
66+
'width': self.photo.width,
67+
'height': self.photo.height,
68+
'file_size': self.photo.file_size
6069
}
6170

71+
def test_expected_values(self):
72+
self.assertEqual(self.photo.width, 300)
73+
self.assertEqual(self.photo.height, 300)
74+
self.assertEqual(self.photo.file_size, 10209)
75+
self.assertEqual(self.thumb.width, 90)
76+
self.assertEqual(self.thumb.height, 90)
77+
self.assertEqual(self.thumb.file_size, 1478)
78+
6279
@flaky(3, 1)
6380
@timeout(10)
6481
def test_sendphotoo_all_args(self):
65-
message = self._bot.sendPhoto(self._chat_id, self.photo_file, caption=self.caption)
66-
82+
message = self._bot.sendPhoto(self._chat_id, self.photo_file, caption=self.caption, disable_notification=False)
6783
thumb, photo = message.photo
6884

85+
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
6986
self.assertTrue(isinstance(thumb.file_id, str))
7087
self.assertNotEqual(thumb.file_id, '')
71-
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
72-
self.assertEqual(thumb.width, self.thumb['width'])
73-
self.assertEqual(thumb.height, self.thumb['height'])
74-
self.assertEqual(thumb.file_size, self.thumb['file_size'])
88+
self.assertEqual(thumb.width, self.thumb.width)
89+
self.assertEqual(thumb.height, self.thumb.height)
90+
self.assertEqual(thumb.file_size, self.thumb.file_size)
7591

92+
self.assertTrue(isinstance(photo, telegram.PhotoSize))
7693
self.assertTrue(isinstance(photo.file_id, str))
7794
self.assertNotEqual(photo.file_id, '')
78-
self.assertTrue(isinstance(photo, telegram.PhotoSize))
79-
self.assertEqual(photo.width, self.width)
80-
self.assertEqual(photo.height, self.height)
81-
self.assertEqual(photo.file_size, self.file_size)
95+
self.assertEqual(photo.width, self.photo.width)
96+
self.assertEqual(photo.height, self.photo.height)
97+
self.assertEqual(photo.file_size, self.photo.file_size)
8298

8399
self.assertEqual(message.caption, self.caption)
84100

85-
@flaky(3, 1)
86-
@timeout(10)
87-
def test_send_photo_jpg_file(self):
88-
message = self._bot.sendPhoto(self._chat_id, self.photo_file)
89-
90-
thumb, photo = message.photo
91-
92-
self.assertTrue(isinstance(thumb.file_id, str))
93-
self.assertNotEqual(thumb.file_id, '')
94-
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
95-
self.assertEqual(thumb.width, self.thumb['width'])
96-
self.assertEqual(thumb.height, self.thumb['height'])
97-
self.assertEqual(thumb.file_size, self.thumb['file_size'])
98-
99-
self.assertTrue(isinstance(photo.file_id, str))
100-
self.assertNotEqual(photo.file_id, '')
101-
self.assertTrue(isinstance(photo, telegram.PhotoSize))
102-
self.assertEqual(photo.width, self.width)
103-
self.assertEqual(photo.height, self.height)
104-
self.assertEqual(photo.file_size, self.file_size)
105-
106101
@flaky(3, 1)
107102
@timeout(10)
108103
def test_send_photo_url_jpg_file(self):
109-
message = self._bot.sendPhoto(self._chat_id, self.photo_file_url)
104+
message = self._bot.sendPhoto(self._chat_id, photo=self.photo_file_url)
110105

111106
thumb, photo = message.photo
112107

108+
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
113109
self.assertTrue(isinstance(thumb.file_id, str))
114110
self.assertNotEqual(thumb.file_id, '')
115-
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
116-
self.assertEqual(thumb.width, self.thumb['width'])
117-
self.assertEqual(thumb.height, self.thumb['height'])
118-
self.assertEqual(thumb.file_size, self.thumb['file_size'])
111+
self.assertEqual(thumb.width, self.thumb.width)
112+
self.assertEqual(thumb.height, self.thumb.height)
113+
self.assertEqual(thumb.file_size, self.thumb.file_size)
119114

115+
self.assertTrue(isinstance(photo, telegram.PhotoSize))
120116
self.assertTrue(isinstance(photo.file_id, str))
121117
self.assertNotEqual(photo.file_id, '')
122-
self.assertTrue(isinstance(photo, telegram.PhotoSize))
123-
self.assertEqual(photo.width, self.width)
124-
self.assertEqual(photo.height, self.height)
125-
self.assertEqual(photo.file_size, self.file_size)
118+
self.assertEqual(photo.width, self.photo.width)
119+
self.assertEqual(photo.height, self.photo.height)
120+
self.assertEqual(photo.file_size, self.photo.file_size)
126121

127122
@flaky(3, 1)
128123
@timeout(10)
@@ -153,43 +148,44 @@ def test_send_photo_bytesio_jpg_file(self):
153148
@flaky(3, 1)
154149
@timeout(10)
155150
def test_send_photo_resend(self):
156-
message = self._bot.sendPhoto(chat_id=self._chat_id, photo=self.photo_file_id)
151+
message = self._bot.sendPhoto(chat_id=self._chat_id, photo=self.photo.file_id)
157152

158153
thumb, photo = message.photo
159154

160-
self.assertEqual(thumb.file_id, self.thumb['file_id'])
161155
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
162-
self.assertEqual(thumb.width, self.thumb['width'])
163-
self.assertEqual(thumb.height, self.thumb['height'])
156+
self.assertEqual(thumb.file_id, self.thumb.file_id)
157+
self.assertEqual(thumb.width, self.thumb.width)
158+
self.assertEqual(thumb.height, self.thumb.height)
159+
self.assertEqual(thumb.file_size, self.thumb.file_size)
164160

165-
self.assertEqual(photo.file_id, self.photo_file_id)
166161
self.assertTrue(isinstance(photo, telegram.PhotoSize))
167-
self.assertEqual(photo.width, self.width)
168-
self.assertEqual(photo.height, self.height)
162+
self.assertEqual(photo.file_id, self.photo.file_id)
163+
self.assertEqual(photo.width, self.photo.width)
164+
self.assertEqual(photo.height, self.photo.height)
165+
self.assertEqual(photo.file_size, self.photo.file_size)
169166

170167
def test_photo_de_json(self):
171168
photo = telegram.PhotoSize.de_json(self.json_dict, self._bot)
172169

173-
self.assertEqual(photo.file_id, self.photo_file_id)
174170
self.assertTrue(isinstance(photo, telegram.PhotoSize))
175-
self.assertEqual(photo.width, self.width)
176-
self.assertEqual(photo.height, self.height)
177-
self.assertEqual(photo.file_size, self.file_size)
171+
self.assertEqual(photo.file_id, self.photo.file_id)
172+
self.assertEqual(photo.width, self.photo.width)
173+
self.assertEqual(photo.height, self.photo.height)
174+
self.assertEqual(photo.file_size, self.photo.file_size)
178175

179176
def test_photo_to_json(self):
180177
photo = telegram.PhotoSize.de_json(self.json_dict, self._bot)
181178

182179
self.assertTrue(self.is_json(photo.to_json()))
183180

184181
def test_photo_to_dict(self):
185-
photo = telegram.PhotoSize.de_json(self.json_dict, self._bot)
182+
photo = telegram.PhotoSize.de_json(self.json_dict, self._bot).to_dict()
186183

187-
self.assertTrue(self.is_dict(photo.to_dict()))
188-
self.assertEqual(photo['file_id'], self.photo_file_id)
189-
self.assertTrue(isinstance(photo, telegram.PhotoSize))
190-
self.assertEqual(photo['width'], self.width)
191-
self.assertEqual(photo['height'], self.height)
192-
self.assertEqual(photo['file_size'], self.file_size)
184+
self.assertTrue(self.is_dict(photo))
185+
self.assertEqual(photo['file_id'], self.photo.file_id)
186+
self.assertEqual(photo['width'], self.photo.width)
187+
self.assertEqual(photo['height'], self.photo.height)
188+
self.assertEqual(photo['file_size'], self.photo.file_size)
193189

194190
@flaky(3, 1)
195191
@timeout(10)
@@ -199,9 +195,8 @@ def test_error_send_photo_empty_file(self):
199195
del (json_dict['file_id'])
200196
json_dict['photo'] = open(os.devnull, 'rb')
201197

202-
self.assertRaises(
203-
telegram.TelegramError,
204-
lambda: self._bot.sendPhoto(chat_id=self._chat_id, **json_dict))
198+
with self.assertRaises(telegram.TelegramError):
199+
self._bot.sendPhoto(chat_id=self._chat_id, **json_dict)
205200

206201
@flaky(3, 1)
207202
@timeout(10)
@@ -211,9 +206,8 @@ def test_error_send_photo_empty_file_id(self):
211206
del (json_dict['file_id'])
212207
json_dict['photo'] = ''
213208

214-
self.assertRaises(
215-
telegram.TelegramError,
216-
lambda: self._bot.sendPhoto(chat_id=self._chat_id, **json_dict))
209+
with self.assertRaises(telegram.TelegramError):
210+
self._bot.sendPhoto(chat_id=self._chat_id, **json_dict)
217211

218212
@flaky(3, 1)
219213
@timeout(10)
@@ -224,25 +218,30 @@ def test_error_photo_without_required_args(self):
224218
del (json_dict['width'])
225219
del (json_dict['height'])
226220

227-
self.assertRaises(
228-
TypeError,
229-
lambda: self._bot.sendPhoto(chat_id=self._chat_id, **json_dict))
221+
with self.assertRaises(TypeError):
222+
self._bot.sendPhoto(chat_id=self._chat_id, **json_dict)
230223

231224
@flaky(3, 1)
232225
@timeout(10)
233226
def test_reply_photo(self):
234227
"""Test for Message.reply_photo"""
235228
message = self._bot.sendMessage(self._chat_id, '.')
236-
message = message.reply_photo(self.photo_file)
229+
thumb, photo = message.reply_photo(self.photo_file).photo
230+
231+
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
232+
self.assertTrue(isinstance(thumb.file_id, str))
233+
self.assertNotEqual(thumb.file_id, '')
237234

238-
self.assertNotEqual(message.photo[0].file_id, '')
235+
self.assertTrue(isinstance(photo, telegram.PhotoSize))
236+
self.assertTrue(isinstance(photo.file_id, str))
237+
self.assertNotEqual(photo.file_id, '')
239238

240239
def test_equality(self):
241-
a = telegram.PhotoSize(self.photo_file_id, self.width, self.height)
242-
b = telegram.PhotoSize(self.photo_file_id, self.width, self.height)
243-
c = telegram.PhotoSize(self.photo_file_id, 0, 0)
244-
d = telegram.PhotoSize("", self.width, self.height)
245-
e = telegram.Sticker(self.photo_file_id, self.width, self.height)
240+
a = telegram.PhotoSize(self.photo.file_id, self.photo.width, self.photo.height)
241+
b = telegram.PhotoSize(self.photo.file_id, self.photo.width, self.photo.height)
242+
c = telegram.PhotoSize(self.photo.file_id, 0, 0)
243+
d = telegram.PhotoSize("", self.photo.width, self.photo.height)
244+
e = telegram.Sticker(self.photo.file_id, self.photo.width, self.photo.height)
246245

247246
self.assertEqual(a, b)
248247
self.assertEqual(hash(a), hash(b))

0 commit comments

Comments
 (0)