2015-09-07 19:12:58 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2016-01-05 14:12:03 +01:00
|
|
|
# Copyright (C) 2015-2016
|
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
2015-09-07 19:12:58 +02:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
|
|
|
"""This module contains a object that represents a Base class for tests"""
|
|
|
|
|
|
|
|
import os
|
2016-04-24 04:11:25 +02:00
|
|
|
import sys
|
2016-02-22 00:38:26 +01:00
|
|
|
import signal
|
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
from nose.tools import make_decorator
|
2016-02-22 00:38:26 +01:00
|
|
|
|
2015-09-07 19:12:58 +02:00
|
|
|
sys.path.append('.')
|
|
|
|
|
|
|
|
import json
|
|
|
|
import telegram
|
|
|
|
|
|
|
|
|
|
|
|
class BaseTest(object):
|
|
|
|
"""This object represents a Base test and its sets of functions."""
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(BaseTest, self).__init__(*args, **kwargs)
|
|
|
|
|
2016-05-15 02:52:35 +02:00
|
|
|
bot = telegram.Bot(os.environ.get('TOKEN',
|
|
|
|
'133505823:AAHZFMHno3mzVLErU5b5jJvaeG--qUyLyG0'))
|
2016-03-31 20:36:18 +02:00
|
|
|
chat_id = os.environ.get('CHAT_ID', '12173560')
|
2015-09-07 19:12:58 +02:00
|
|
|
|
2016-05-25 01:31:10 +02:00
|
|
|
self._group_id = os.environ.get('GROUP_ID', '-49740850')
|
|
|
|
self._channel_id = os.environ.get('CHANNEL_ID', '@pythontelegrambottests')
|
2015-09-07 19:12:58 +02:00
|
|
|
self._bot = bot
|
|
|
|
self._chat_id = chat_id
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_json(string):
|
|
|
|
try:
|
|
|
|
json.loads(string)
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_dict(dictionary):
|
|
|
|
if isinstance(dictionary, dict):
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
2016-02-22 00:38:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestTimedOut(AssertionError):
|
2016-05-15 02:52:35 +02:00
|
|
|
|
2016-02-22 00:38:26 +01:00
|
|
|
def __init__(self, time_limit, frame):
|
2016-02-22 00:52:31 +01:00
|
|
|
super(TestTimedOut, self).__init__('time_limit={0}'.format(time_limit))
|
2016-02-22 00:38:26 +01:00
|
|
|
self.time_limit = time_limit
|
|
|
|
self.frame = frame
|
|
|
|
|
|
|
|
|
|
|
|
def timeout(time_limit):
|
2016-05-15 02:52:35 +02:00
|
|
|
|
2016-02-22 00:38:26 +01:00
|
|
|
def decorator(func):
|
2016-05-15 02:52:35 +02:00
|
|
|
|
2016-02-22 00:38:26 +01:00
|
|
|
def timed_out(_signum, frame):
|
|
|
|
raise TestTimedOut(time_limit, frame)
|
|
|
|
|
|
|
|
def newfunc(*args, **kwargs):
|
|
|
|
orig_handler = signal.signal(signal.SIGALRM, timed_out)
|
|
|
|
signal.alarm(time_limit)
|
|
|
|
try:
|
|
|
|
rc = func(*args, **kwargs)
|
|
|
|
finally:
|
|
|
|
signal.alarm(0)
|
|
|
|
signal.signal(signal.SIGALRM, orig_handler)
|
|
|
|
return rc
|
|
|
|
|
|
|
|
newfunc = make_decorator(func)(newfunc)
|
|
|
|
return newfunc
|
|
|
|
|
|
|
|
return decorator
|