test_photo done

* Changed to new method of non-static file_id
* removed obsolete tests.
This commit is contained in:
Eldin 2017-06-21 17:21:18 +02:00
parent e7b839b69e
commit f6a98d7888

View file

@ -25,6 +25,8 @@ import os
from flaky import flaky
from tests.bots import get_bot
sys.path.append('.')
import telegram
@ -34,95 +36,88 @@ from tests.base import BaseTest, timeout
class PhotoTest(BaseTest, unittest.TestCase):
"""This object represents Tests for Telegram Photo."""
@classmethod
def setUpClass(cls):
cls.caption = u'PhotoTest - Caption'
cls.photo_file_url = 'https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/tests/data/telegram.jpg'
bot_info = get_bot()
cls._chat_id = bot_info['chat_id']
cls._bot = telegram.Bot(bot_info['token'])
photo_file = open('tests/data/telegram.jpg', 'rb')
photo = cls._bot.send_photo(cls._chat_id, photo=photo_file, timeout=10).photo
cls.thumb, cls.photo = photo
# Make sure file has been uploaded.
# Simple assertions PY2 Only
assert isinstance(cls.photo, telegram.PhotoSize)
assert isinstance(cls.thumb, telegram.PhotoSize)
assert isinstance(cls.photo.file_id, str)
assert isinstance(cls.thumb.file_id, str)
assert cls.photo.file_id is not ''
assert cls.thumb.file_id is not ''
def setUp(self):
self.photo_file = open('tests/data/telegram.jpg', 'rb')
self.photo_file_id = 'AgADAQADgEsyGx8j9QfmDMmwkPBrFcKRzy8ABHW8ul9nW7FoNHYBAAEC'
self.photo_file_url = 'https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/tests/data/telegram.jpg'
self.photo_bytes_jpg_no_standard = 'tests/data/telegram_no_standard_header.jpg'
self.width = 300
self.height = 300
self.thumb = {
'width': 90,
'height': 90,
'file_id': 'AgADAQADgEsyGx8j9QeYW9oDz2mKRsKRzy8ABD64nkFkjujeNXYBAAEC',
'file_size': 1478
}
self.file_size = 10209
# caption is part of sendPhoto method but not Photo object
self.caption = u'PhotoTest - Caption'
self.json_dict = {
'file_id': self.photo_file_id,
'width': self.width,
'height': self.height,
'file_size': self.file_size
'file_id': self.photo.file_id,
'width': self.photo.width,
'height': self.photo.height,
'file_size': self.photo.file_size
}
def test_expected_values(self):
self.assertEqual(self.photo.width, 300)
self.assertEqual(self.photo.height, 300)
self.assertEqual(self.photo.file_size, 10209)
self.assertEqual(self.thumb.width, 90)
self.assertEqual(self.thumb.height, 90)
self.assertEqual(self.thumb.file_size, 1478)
@flaky(3, 1)
@timeout(10)
def test_sendphotoo_all_args(self):
message = self._bot.sendPhoto(self._chat_id, self.photo_file, caption=self.caption)
message = self._bot.sendPhoto(self._chat_id, self.photo_file, caption=self.caption, disable_notification=False)
thumb, photo = message.photo
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
self.assertTrue(isinstance(thumb.file_id, str))
self.assertNotEqual(thumb.file_id, '')
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
self.assertEqual(thumb.width, self.thumb['width'])
self.assertEqual(thumb.height, self.thumb['height'])
self.assertEqual(thumb.file_size, self.thumb['file_size'])
self.assertEqual(thumb.width, self.thumb.width)
self.assertEqual(thumb.height, self.thumb.height)
self.assertEqual(thumb.file_size, self.thumb.file_size)
self.assertTrue(isinstance(photo, telegram.PhotoSize))
self.assertTrue(isinstance(photo.file_id, str))
self.assertNotEqual(photo.file_id, '')
self.assertTrue(isinstance(photo, telegram.PhotoSize))
self.assertEqual(photo.width, self.width)
self.assertEqual(photo.height, self.height)
self.assertEqual(photo.file_size, self.file_size)
self.assertEqual(photo.width, self.photo.width)
self.assertEqual(photo.height, self.photo.height)
self.assertEqual(photo.file_size, self.photo.file_size)
self.assertEqual(message.caption, self.caption)
@flaky(3, 1)
@timeout(10)
def test_send_photo_jpg_file(self):
message = self._bot.sendPhoto(self._chat_id, self.photo_file)
thumb, photo = message.photo
self.assertTrue(isinstance(thumb.file_id, str))
self.assertNotEqual(thumb.file_id, '')
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
self.assertEqual(thumb.width, self.thumb['width'])
self.assertEqual(thumb.height, self.thumb['height'])
self.assertEqual(thumb.file_size, self.thumb['file_size'])
self.assertTrue(isinstance(photo.file_id, str))
self.assertNotEqual(photo.file_id, '')
self.assertTrue(isinstance(photo, telegram.PhotoSize))
self.assertEqual(photo.width, self.width)
self.assertEqual(photo.height, self.height)
self.assertEqual(photo.file_size, self.file_size)
@flaky(3, 1)
@timeout(10)
def test_send_photo_url_jpg_file(self):
message = self._bot.sendPhoto(self._chat_id, self.photo_file_url)
message = self._bot.sendPhoto(self._chat_id, photo=self.photo_file_url)
thumb, photo = message.photo
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
self.assertTrue(isinstance(thumb.file_id, str))
self.assertNotEqual(thumb.file_id, '')
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
self.assertEqual(thumb.width, self.thumb['width'])
self.assertEqual(thumb.height, self.thumb['height'])
self.assertEqual(thumb.file_size, self.thumb['file_size'])
self.assertEqual(thumb.width, self.thumb.width)
self.assertEqual(thumb.height, self.thumb.height)
self.assertEqual(thumb.file_size, self.thumb.file_size)
self.assertTrue(isinstance(photo, telegram.PhotoSize))
self.assertTrue(isinstance(photo.file_id, str))
self.assertNotEqual(photo.file_id, '')
self.assertTrue(isinstance(photo, telegram.PhotoSize))
self.assertEqual(photo.width, self.width)
self.assertEqual(photo.height, self.height)
self.assertEqual(photo.file_size, self.file_size)
self.assertEqual(photo.width, self.photo.width)
self.assertEqual(photo.height, self.photo.height)
self.assertEqual(photo.file_size, self.photo.file_size)
@flaky(3, 1)
@timeout(10)
@ -153,28 +148,30 @@ class PhotoTest(BaseTest, unittest.TestCase):
@flaky(3, 1)
@timeout(10)
def test_send_photo_resend(self):
message = self._bot.sendPhoto(chat_id=self._chat_id, photo=self.photo_file_id)
message = self._bot.sendPhoto(chat_id=self._chat_id, photo=self.photo.file_id)
thumb, photo = message.photo
self.assertEqual(thumb.file_id, self.thumb['file_id'])
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
self.assertEqual(thumb.width, self.thumb['width'])
self.assertEqual(thumb.height, self.thumb['height'])
self.assertEqual(thumb.file_id, self.thumb.file_id)
self.assertEqual(thumb.width, self.thumb.width)
self.assertEqual(thumb.height, self.thumb.height)
self.assertEqual(thumb.file_size, self.thumb.file_size)
self.assertEqual(photo.file_id, self.photo_file_id)
self.assertTrue(isinstance(photo, telegram.PhotoSize))
self.assertEqual(photo.width, self.width)
self.assertEqual(photo.height, self.height)
self.assertEqual(photo.file_id, self.photo.file_id)
self.assertEqual(photo.width, self.photo.width)
self.assertEqual(photo.height, self.photo.height)
self.assertEqual(photo.file_size, self.photo.file_size)
def test_photo_de_json(self):
photo = telegram.PhotoSize.de_json(self.json_dict, self._bot)
self.assertEqual(photo.file_id, self.photo_file_id)
self.assertTrue(isinstance(photo, telegram.PhotoSize))
self.assertEqual(photo.width, self.width)
self.assertEqual(photo.height, self.height)
self.assertEqual(photo.file_size, self.file_size)
self.assertEqual(photo.file_id, self.photo.file_id)
self.assertEqual(photo.width, self.photo.width)
self.assertEqual(photo.height, self.photo.height)
self.assertEqual(photo.file_size, self.photo.file_size)
def test_photo_to_json(self):
photo = telegram.PhotoSize.de_json(self.json_dict, self._bot)
@ -182,14 +179,13 @@ class PhotoTest(BaseTest, unittest.TestCase):
self.assertTrue(self.is_json(photo.to_json()))
def test_photo_to_dict(self):
photo = telegram.PhotoSize.de_json(self.json_dict, self._bot)
photo = telegram.PhotoSize.de_json(self.json_dict, self._bot).to_dict()
self.assertTrue(self.is_dict(photo.to_dict()))
self.assertEqual(photo['file_id'], self.photo_file_id)
self.assertTrue(isinstance(photo, telegram.PhotoSize))
self.assertEqual(photo['width'], self.width)
self.assertEqual(photo['height'], self.height)
self.assertEqual(photo['file_size'], self.file_size)
self.assertTrue(self.is_dict(photo))
self.assertEqual(photo['file_id'], self.photo.file_id)
self.assertEqual(photo['width'], self.photo.width)
self.assertEqual(photo['height'], self.photo.height)
self.assertEqual(photo['file_size'], self.photo.file_size)
@flaky(3, 1)
@timeout(10)
@ -199,9 +195,8 @@ class PhotoTest(BaseTest, unittest.TestCase):
del (json_dict['file_id'])
json_dict['photo'] = open(os.devnull, 'rb')
self.assertRaises(
telegram.TelegramError,
lambda: self._bot.sendPhoto(chat_id=self._chat_id, **json_dict))
with self.assertRaises(telegram.TelegramError):
self._bot.sendPhoto(chat_id=self._chat_id, **json_dict)
@flaky(3, 1)
@timeout(10)
@ -211,9 +206,8 @@ class PhotoTest(BaseTest, unittest.TestCase):
del (json_dict['file_id'])
json_dict['photo'] = ''
self.assertRaises(
telegram.TelegramError,
lambda: self._bot.sendPhoto(chat_id=self._chat_id, **json_dict))
with self.assertRaises(telegram.TelegramError):
self._bot.sendPhoto(chat_id=self._chat_id, **json_dict)
@flaky(3, 1)
@timeout(10)
@ -224,25 +218,30 @@ class PhotoTest(BaseTest, unittest.TestCase):
del (json_dict['width'])
del (json_dict['height'])
self.assertRaises(
TypeError,
lambda: self._bot.sendPhoto(chat_id=self._chat_id, **json_dict))
with self.assertRaises(TypeError):
self._bot.sendPhoto(chat_id=self._chat_id, **json_dict)
@flaky(3, 1)
@timeout(10)
def test_reply_photo(self):
"""Test for Message.reply_photo"""
message = self._bot.sendMessage(self._chat_id, '.')
message = message.reply_photo(self.photo_file)
thumb, photo = message.reply_photo(self.photo_file).photo
self.assertNotEqual(message.photo[0].file_id, '')
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
self.assertTrue(isinstance(thumb.file_id, str))
self.assertNotEqual(thumb.file_id, '')
self.assertTrue(isinstance(photo, telegram.PhotoSize))
self.assertTrue(isinstance(photo.file_id, str))
self.assertNotEqual(photo.file_id, '')
def test_equality(self):
a = telegram.PhotoSize(self.photo_file_id, self.width, self.height)
b = telegram.PhotoSize(self.photo_file_id, self.width, self.height)
c = telegram.PhotoSize(self.photo_file_id, 0, 0)
d = telegram.PhotoSize("", self.width, self.height)
e = telegram.Sticker(self.photo_file_id, self.width, self.height)
a = telegram.PhotoSize(self.photo.file_id, self.photo.width, self.photo.height)
b = telegram.PhotoSize(self.photo.file_id, self.photo.width, self.photo.height)
c = telegram.PhotoSize(self.photo.file_id, 0, 0)
d = telegram.PhotoSize("", self.photo.width, self.photo.height)
e = telegram.Sticker(self.photo.file_id, self.photo.width, self.photo.height)
self.assertEqual(a, b)
self.assertEqual(hash(a), hash(b))