mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
(Attempt to) fix failing Travis CI tests.
This commit is contained in:
@@ -1,200 +1,184 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
import pytest
|
||||||
sys.path.append('../')
|
|
||||||
from telebot import types
|
|
||||||
from telebot import apihelper
|
|
||||||
import telebot
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
TOKEN = os.environ['TOKEN']
|
import telebot
|
||||||
CHAT_ID = os.environ['CHAT_ID']
|
from telebot import types
|
||||||
|
from telebot import apihelper
|
||||||
|
|
||||||
|
sys.path.append('../')
|
||||||
|
|
||||||
def test_message_listener():
|
should_skip = 'TOKEN' and 'CHAT_ID' not in os.environ
|
||||||
msg_list = []
|
|
||||||
for x in range(100):
|
|
||||||
msg_list.append(create_text_message('Message ' + str(x)))
|
|
||||||
|
|
||||||
def listener(messages):
|
if not should_skip:
|
||||||
assert len(messages) == 100
|
TOKEN = os.environ['TOKEN']
|
||||||
|
CHAT_ID = os.environ['CHAT_ID']
|
||||||
|
|
||||||
tb = telebot.TeleBot('')
|
@pytest.mark.skipif(should_skip, reason="No environment variables configured")
|
||||||
tb.set_update_listener(listener)
|
class TestTeleBot:
|
||||||
|
|
||||||
|
def test_message_listener(self):
|
||||||
|
msg_list = []
|
||||||
|
for x in range(100):
|
||||||
|
msg_list.append(self.create_text_message('Message ' + str(x)))
|
||||||
|
|
||||||
def test_message_handler():
|
def listener(messages):
|
||||||
tb = telebot.TeleBot('')
|
assert len(messages) == 100
|
||||||
msg = create_text_message('/help')
|
|
||||||
|
|
||||||
@tb.message_handler(commands=['help', 'start'])
|
tb = telebot.TeleBot('')
|
||||||
def command_handler(message):
|
tb.set_update_listener(listener)
|
||||||
message.text = 'got'
|
|
||||||
|
|
||||||
tb.process_new_messages([msg])
|
def test_message_handler(self):
|
||||||
time.sleep(1)
|
tb = telebot.TeleBot('')
|
||||||
assert msg.text == 'got'
|
msg = self.create_text_message('/help')
|
||||||
|
|
||||||
|
@tb.message_handler(commands=['help', 'start'])
|
||||||
|
def command_handler(message):
|
||||||
|
message.text = 'got'
|
||||||
|
|
||||||
def test_message_handler_reg():
|
tb.process_new_messages([msg])
|
||||||
bot = telebot.TeleBot('')
|
time.sleep(1)
|
||||||
msg = create_text_message(r'https://web.telegram.org/')
|
assert msg.text == 'got'
|
||||||
|
|
||||||
@bot.message_handler(regexp='((https?):((//)|(\\\\))+([\w\d:#@%/;$()~_?\+-=\\\.&](#!)?)*)')
|
def test_message_handler_reg(self):
|
||||||
def command_url(message):
|
bot = telebot.TeleBot('')
|
||||||
msg.text = 'got'
|
msg = self.create_text_message(r'https://web.telegram.org/')
|
||||||
|
|
||||||
bot.process_new_messages([msg])
|
@bot.message_handler(regexp='((https?):((//)|(\\\\))+([\w\d:#@%/;$()~_?\+-=\\\.&](#!)?)*)')
|
||||||
time.sleep(1)
|
def command_url(message):
|
||||||
assert msg.text == 'got'
|
msg.text = 'got'
|
||||||
|
|
||||||
|
bot.process_new_messages([msg])
|
||||||
|
time.sleep(1)
|
||||||
|
assert msg.text == 'got'
|
||||||
|
|
||||||
def test_message_handler_reg_fail():
|
def test_message_handler_reg_fail(self):
|
||||||
bot = telebot.TeleBot('')
|
bot = telebot.TeleBot('')
|
||||||
msg = create_text_message(r'web.telegram.org/')
|
msg = self.create_text_message(r'web.telegram.org/')
|
||||||
|
|
||||||
@bot.message_handler(regexp='((https?):((//)|(\\\\))+([\w\d:#@%/;$()~_?\+-=\\\.&](#!)?)*)')
|
@bot.message_handler(regexp='((https?):((//)|(\\\\))+([\w\d:#@%/;$()~_?\+-=\\\.&](#!)?)*)')
|
||||||
def command_url(message):
|
def command_url(message):
|
||||||
msg.text = 'got'
|
msg.text = 'got'
|
||||||
|
|
||||||
bot.process_new_messages([msg])
|
bot.process_new_messages([msg])
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
assert not msg.text == 'got'
|
assert not msg.text == 'got'
|
||||||
|
|
||||||
|
def test_send_file_by_id(self):
|
||||||
|
file_id = 'BQADBQADjAIAAsYifgbvqwq1he9REAI'
|
||||||
|
tb = telebot.TeleBot(TOKEN)
|
||||||
|
ret_msg = tb.send_document(CHAT_ID, file_id)
|
||||||
|
assert ret_msg.message_id
|
||||||
|
|
||||||
def test_send_file_by_id():
|
def test_send_file(self):
|
||||||
file_id = 'BQADBQADjAIAAsYifgbvqwq1he9REAI'
|
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = telebot.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_document(CHAT_ID, file_id)
|
ret_msg = tb.send_document(CHAT_ID, file_data)
|
||||||
assert ret_msg.message_id
|
assert ret_msg.message_id
|
||||||
|
|
||||||
|
def test_send_video(self):
|
||||||
|
file_data = open('./test_data/test_video.mp4', 'rb')
|
||||||
|
tb = telebot.TeleBot(TOKEN)
|
||||||
|
ret_msg = tb.send_video(CHAT_ID, file_data)
|
||||||
|
assert ret_msg.message_id
|
||||||
|
|
||||||
def test_send_file():
|
def test_send_video_more_params(self):
|
||||||
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
file_data = open('./test_data/test_video.mp4', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = telebot.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_document(CHAT_ID, file_data)
|
ret_msg = tb.send_video(CHAT_ID, file_data, 1)
|
||||||
assert ret_msg.message_id
|
assert ret_msg.message_id
|
||||||
|
|
||||||
|
def test_send_file_exception(self):
|
||||||
|
tb = telebot.TeleBot(TOKEN)
|
||||||
|
try:
|
||||||
|
tb.send_document(CHAT_ID, None)
|
||||||
|
assert False
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
assert True
|
||||||
|
|
||||||
def test_send_video():
|
def test_send_photo_by_id(self):
|
||||||
file_data = open('./test_data/test_video.mp4', 'rb')
|
photo_id = 'AgADBQADTKgxG8YifgbcWQAB7Da9yYIx1rEyAAT-HYJ3CrJEqdA2AQABAg'
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = telebot.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_video(CHAT_ID, file_data)
|
ret_msg = tb.send_photo(CHAT_ID, photo_id)
|
||||||
assert ret_msg.message_id
|
assert ret_msg.message_id
|
||||||
|
|
||||||
|
def test_send_photo(self):
|
||||||
|
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
||||||
|
tb = telebot.TeleBot(TOKEN)
|
||||||
|
ret_msg = tb.send_photo(CHAT_ID, file_data)
|
||||||
|
assert ret_msg.message_id
|
||||||
|
|
||||||
def test_send_video_more_params():
|
def test_send_audio(self):
|
||||||
file_data = open('./test_data/test_video.mp4', 'rb')
|
file_data = open('./test_data/record.mp3', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = telebot.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_video(CHAT_ID, file_data, 1)
|
ret_msg = tb.send_audio(CHAT_ID, file_data, 1, 'eternnoir', 'pyTelegram')
|
||||||
assert ret_msg.message_id
|
assert ret_msg.content_type == 'audio'
|
||||||
|
assert ret_msg.audio.performer == 'eternnoir'
|
||||||
|
assert ret_msg.audio.title == 'pyTelegram'
|
||||||
|
|
||||||
|
def test_send_voice(self):
|
||||||
|
file_data = open('./test_data/record.ogg', 'rb')
|
||||||
|
tb = telebot.TeleBot(TOKEN)
|
||||||
|
ret_msg = tb.send_voice(CHAT_ID, file_data)
|
||||||
|
assert ret_msg.voice.mime_type == 'audio/ogg'
|
||||||
|
|
||||||
def test_send_file_exception():
|
def test_send_message(self):
|
||||||
tb = telebot.TeleBot(TOKEN)
|
text = 'CI Test Message'
|
||||||
try:
|
tb = telebot.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_document(CHAT_ID, None)
|
ret_msg = tb.send_message(CHAT_ID, text)
|
||||||
assert False
|
assert ret_msg.message_id
|
||||||
except Exception as e:
|
|
||||||
print(e)
|
|
||||||
assert True
|
|
||||||
|
|
||||||
|
def test_forward_message(self):
|
||||||
|
text = 'CI forward_message Test Message'
|
||||||
|
tb = telebot.TeleBot(TOKEN)
|
||||||
|
msg = tb.send_message(CHAT_ID, text)
|
||||||
|
ret_msg = tb.forward_message(CHAT_ID, CHAT_ID, msg.message_id)
|
||||||
|
assert ret_msg.forward_from
|
||||||
|
|
||||||
def test_send_photo_by_id():
|
def test_reply_to(self):
|
||||||
photo_id = 'AgADBQADTKgxG8YifgbcWQAB7Da9yYIx1rEyAAT-HYJ3CrJEqdA2AQABAg'
|
text = 'CI reply_to Test Message'
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = telebot.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_photo(CHAT_ID, photo_id)
|
msg = tb.send_message(CHAT_ID, text)
|
||||||
assert ret_msg.message_id
|
ret_msg = tb.reply_to(msg, text + ' REPLY')
|
||||||
|
assert ret_msg.reply_to_message.message_id == msg.message_id
|
||||||
|
|
||||||
|
def test_register_for_reply(self):
|
||||||
|
text = 'CI reply_to Test Message'
|
||||||
|
tb = telebot.TeleBot(TOKEN)
|
||||||
|
msg = tb.send_message(CHAT_ID, text, reply_markup=types.ForceReply())
|
||||||
|
reply_msg = tb.reply_to(msg, text + ' REPLY')
|
||||||
|
|
||||||
def test_send_photo():
|
def process_reply(message):
|
||||||
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
assert msg.message_id == message.reply_to_message.message_id
|
||||||
tb = telebot.TeleBot(TOKEN)
|
|
||||||
ret_msg = tb.send_photo(CHAT_ID, file_data)
|
|
||||||
assert ret_msg.message_id
|
|
||||||
|
|
||||||
|
tb.register_for_reply(msg, process_reply)
|
||||||
|
|
||||||
def test_send_audio():
|
tb.process_new_messages([reply_msg])
|
||||||
file_data = open('./test_data/record.mp3', 'rb')
|
|
||||||
tb = telebot.TeleBot(TOKEN)
|
|
||||||
ret_msg = tb.send_audio(CHAT_ID, file_data, 1, 'eternnoir', 'pyTelegram')
|
|
||||||
assert ret_msg.content_type == 'audio'
|
|
||||||
assert ret_msg.audio.performer == 'eternnoir'
|
|
||||||
assert ret_msg.audio.title == 'pyTelegram'
|
|
||||||
|
|
||||||
|
def test_send_location(self):
|
||||||
|
tb = telebot.TeleBot(TOKEN)
|
||||||
|
lat = 26.3875591
|
||||||
|
lon = -161.2901042
|
||||||
|
ret_msg = tb.send_location(CHAT_ID, lat, lon)
|
||||||
|
assert int(ret_msg.location.longitude) == int(lon)
|
||||||
|
assert int(ret_msg.location.latitude) == int(lat)
|
||||||
|
|
||||||
def test_send_voice():
|
def create_text_message(self, text):
|
||||||
file_data = open('./test_data/record.ogg', 'rb')
|
params = {'text': text}
|
||||||
tb = telebot.TeleBot(TOKEN)
|
chat = types.User(11, 'test')
|
||||||
ret_msg = tb.send_voice(CHAT_ID, file_data)
|
return types.Message(1, None, None, chat, 'text', params)
|
||||||
assert ret_msg.voice.mime_type == 'audio/ogg'
|
|
||||||
|
|
||||||
|
def test_is_string_unicode(self):
|
||||||
|
s1 = u'string'
|
||||||
|
assert apihelper.is_string(s1)
|
||||||
|
|
||||||
def test_send_message():
|
def test_is_string_string(self):
|
||||||
text = 'CI Test Message'
|
s1 = 'string'
|
||||||
tb = telebot.TeleBot(TOKEN)
|
assert apihelper.is_string(s1)
|
||||||
ret_msg = tb.send_message(CHAT_ID, text)
|
|
||||||
assert ret_msg.message_id
|
|
||||||
|
|
||||||
|
def test_not_string(self):
|
||||||
def test_forward_message():
|
i1 = 10
|
||||||
text = 'CI forward_message Test Message'
|
assert not apihelper.is_string(i1)
|
||||||
tb = telebot.TeleBot(TOKEN)
|
|
||||||
msg = tb.send_message(CHAT_ID, text)
|
|
||||||
ret_msg = tb.forward_message(CHAT_ID, CHAT_ID, msg.message_id)
|
|
||||||
assert ret_msg.forward_from
|
|
||||||
|
|
||||||
|
|
||||||
def test_reply_to():
|
|
||||||
text = 'CI reply_to Test Message'
|
|
||||||
tb = telebot.TeleBot(TOKEN)
|
|
||||||
msg = tb.send_message(CHAT_ID, text)
|
|
||||||
ret_msg = tb.reply_to(msg, text + ' REPLY')
|
|
||||||
assert ret_msg.reply_to_message.message_id == msg.message_id
|
|
||||||
|
|
||||||
|
|
||||||
def test_register_for_reply():
|
|
||||||
text = 'CI reply_to Test Message'
|
|
||||||
tb = telebot.TeleBot(TOKEN)
|
|
||||||
msg = tb.send_message(CHAT_ID, text, reply_markup=types.ForceReply())
|
|
||||||
reply_msg = tb.reply_to(msg, text + ' REPLY')
|
|
||||||
|
|
||||||
def process_reply(message):
|
|
||||||
assert msg.message_id == message.reply_to_message.message_id
|
|
||||||
|
|
||||||
tb.register_for_reply(msg, process_reply)
|
|
||||||
|
|
||||||
tb.process_new_messages([reply_msg])
|
|
||||||
|
|
||||||
|
|
||||||
def test_send_location():
|
|
||||||
tb = telebot.TeleBot(TOKEN)
|
|
||||||
lat = 26.3875591
|
|
||||||
lon = -161.2901042
|
|
||||||
ret_msg = tb.send_location(CHAT_ID, lat, lon)
|
|
||||||
assert int(ret_msg.location.longitude) == int(lon)
|
|
||||||
assert int(ret_msg.location.latitude) == int(lat)
|
|
||||||
|
|
||||||
|
|
||||||
def create_text_message(text):
|
|
||||||
params = {'text': text}
|
|
||||||
chat = types.User(11, 'test')
|
|
||||||
return types.Message(1, None, None, chat, 'text', params)
|
|
||||||
|
|
||||||
|
|
||||||
def test_is_string_unicode():
|
|
||||||
s1 = u'string'
|
|
||||||
assert apihelper.is_string(s1)
|
|
||||||
|
|
||||||
|
|
||||||
def test_is_string_string():
|
|
||||||
s1 = 'string'
|
|
||||||
assert apihelper.is_string(s1)
|
|
||||||
|
|
||||||
|
|
||||||
def test_not_string():
|
|
||||||
i1 = 10
|
|
||||||
assert not apihelper.is_string(i1)
|
|
||||||
|
|
||||||
test_send_voice()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user