2016-04-27 00:08:35 +02:00
|
|
|
#!/usr/bin/env python
|
2016-04-26 23:42:50 +02:00
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2017-05-14 12:18:29 +02:00
|
|
|
# Copyright (C) 2015-2017
|
2016-04-26 23:42:50 +02:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
|
|
|
#
|
|
|
|
# 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/].
|
|
|
|
"""
|
2016-10-19 12:35:50 +02:00
|
|
|
This module contains an object that represents Tests for Filters for use with MessageHandler.
|
2016-04-26 23:42:50 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import sys
|
2016-04-27 00:28:21 +02:00
|
|
|
import unittest
|
2016-04-26 23:42:50 +02:00
|
|
|
from datetime import datetime
|
2016-09-13 20:47:43 +02:00
|
|
|
import functools
|
2016-04-27 00:28:21 +02:00
|
|
|
|
2016-04-26 23:42:50 +02:00
|
|
|
sys.path.append('.')
|
|
|
|
|
2016-09-13 20:47:43 +02:00
|
|
|
from telegram import Message, User, Chat, MessageEntity
|
2016-10-15 22:59:41 +02:00
|
|
|
from telegram.ext import Filters, BaseFilter
|
2016-04-26 23:42:50 +02:00
|
|
|
from tests.base import BaseTest
|
|
|
|
|
|
|
|
|
|
|
|
class FiltersTest(BaseTest, unittest.TestCase):
|
|
|
|
"""This object represents Tests for MessageHandler.Filters"""
|
|
|
|
|
|
|
|
def setUp(self):
|
2016-05-15 02:52:35 +02:00
|
|
|
self.message = Message(0, User(0, "Testuser"), datetime.now(), Chat(0, 'private'))
|
2016-09-24 18:56:54 +02:00
|
|
|
self.e = functools.partial(MessageEntity, offset=0, length=0)
|
2016-04-26 23:42:50 +02:00
|
|
|
|
|
|
|
def test_filters_text(self):
|
|
|
|
self.message.text = 'test'
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertTrue(Filters.text(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
self.message.text = '/test'
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertFalse(Filters.text(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
|
|
|
|
def test_filters_command(self):
|
|
|
|
self.message.text = 'test'
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertFalse(Filters.command(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
self.message.text = '/test'
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertTrue(Filters.command(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
|
2016-12-11 22:45:51 +01:00
|
|
|
def test_filters_reply(self):
|
|
|
|
another_message = Message(1, User(1, "TestOther"), datetime.now(), Chat(0, 'private'))
|
|
|
|
self.message.text = 'test'
|
|
|
|
self.assertFalse(Filters.reply(self.message))
|
|
|
|
self.message.reply_to_message = another_message
|
|
|
|
self.assertTrue(Filters.reply(self.message))
|
|
|
|
|
2016-04-26 23:42:50 +02:00
|
|
|
def test_filters_audio(self):
|
|
|
|
self.message.audio = 'test'
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertTrue(Filters.audio(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
self.message.audio = None
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertFalse(Filters.audio(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
|
|
|
|
def test_filters_document(self):
|
|
|
|
self.message.document = 'test'
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertTrue(Filters.document(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
self.message.document = None
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertFalse(Filters.document(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
|
|
|
|
def test_filters_photo(self):
|
|
|
|
self.message.photo = 'test'
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertTrue(Filters.photo(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
self.message.photo = None
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertFalse(Filters.photo(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
|
|
|
|
def test_filters_sticker(self):
|
|
|
|
self.message.sticker = 'test'
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertTrue(Filters.sticker(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
self.message.sticker = None
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertFalse(Filters.sticker(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
|
|
|
|
def test_filters_video(self):
|
|
|
|
self.message.video = 'test'
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertTrue(Filters.video(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
self.message.video = None
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertFalse(Filters.video(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
|
|
|
|
def test_filters_voice(self):
|
|
|
|
self.message.voice = 'test'
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertTrue(Filters.voice(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
self.message.voice = None
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertFalse(Filters.voice(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
|
|
|
|
def test_filters_contact(self):
|
|
|
|
self.message.contact = 'test'
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertTrue(Filters.contact(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
self.message.contact = None
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertFalse(Filters.contact(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
|
|
|
|
def test_filters_location(self):
|
|
|
|
self.message.location = 'test'
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertTrue(Filters.location(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
self.message.location = None
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertFalse(Filters.location(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
|
|
|
|
def test_filters_venue(self):
|
|
|
|
self.message.venue = 'test'
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertTrue(Filters.venue(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
self.message.venue = None
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertFalse(Filters.venue(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
|
2016-10-07 22:37:29 +02:00
|
|
|
def test_filters_game(self):
|
|
|
|
self.message.game = 'test'
|
|
|
|
self.assertTrue(Filters.game(self.message))
|
|
|
|
self.message.game = None
|
|
|
|
self.assertFalse(Filters.game(self.message))
|
|
|
|
|
2017-05-22 17:39:28 +02:00
|
|
|
def test_filters_successful_payment(self):
|
|
|
|
self.message.successful_payment = 'test'
|
2017-05-22 18:13:30 +02:00
|
|
|
self.assertTrue(Filters.successful_payment(self.message))
|
2017-05-22 17:39:28 +02:00
|
|
|
self.message.successful_payment = None
|
|
|
|
self.assertFalse(Filters.successful_payment(self.message))
|
|
|
|
|
2017-06-02 19:48:30 +02:00
|
|
|
def test_filters_invoice(self):
|
|
|
|
self.message.invoice = 'test'
|
|
|
|
self.assertTrue(Filters.invoice(self.message))
|
|
|
|
self.message.invoice = None
|
2017-06-02 20:05:26 +02:00
|
|
|
self.assertFalse(Filters.invoice(self.message))
|
2017-06-02 19:48:30 +02:00
|
|
|
|
2016-04-26 23:42:50 +02:00
|
|
|
def test_filters_status_update(self):
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertFalse(Filters.status_update(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
|
2017-05-25 14:45:47 +02:00
|
|
|
self.message.new_chat_members = ['test']
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertTrue(Filters.status_update(self.message))
|
2017-06-01 04:10:09 +02:00
|
|
|
self.assertTrue(Filters.status_update.new_chat_members(self.message))
|
2017-05-25 14:45:47 +02:00
|
|
|
self.message.new_chat_members = None
|
2016-04-26 23:42:50 +02:00
|
|
|
|
|
|
|
self.message.left_chat_member = 'test'
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertTrue(Filters.status_update(self.message))
|
2017-06-01 04:10:09 +02:00
|
|
|
self.assertTrue(Filters.status_update.left_chat_member(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
self.message.left_chat_member = None
|
|
|
|
|
|
|
|
self.message.new_chat_title = 'test'
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertTrue(Filters.status_update(self.message))
|
2017-06-01 04:10:09 +02:00
|
|
|
self.assertTrue(Filters.status_update.new_chat_title(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
self.message.new_chat_title = ''
|
|
|
|
|
|
|
|
self.message.new_chat_photo = 'test'
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertTrue(Filters.status_update(self.message))
|
2017-06-01 04:10:09 +02:00
|
|
|
self.assertTrue(Filters.status_update.new_chat_photo(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
self.message.new_chat_photo = None
|
|
|
|
|
|
|
|
self.message.delete_chat_photo = True
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertTrue(Filters.status_update(self.message))
|
2017-06-01 04:10:09 +02:00
|
|
|
self.assertTrue(Filters.status_update.delete_chat_photo(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
self.message.delete_chat_photo = False
|
|
|
|
|
|
|
|
self.message.group_chat_created = True
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertTrue(Filters.status_update(self.message))
|
2017-06-01 04:10:09 +02:00
|
|
|
self.assertTrue(Filters.status_update.chat_created(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
self.message.group_chat_created = False
|
|
|
|
|
|
|
|
self.message.supergroup_chat_created = True
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertTrue(Filters.status_update(self.message))
|
2017-06-01 04:10:09 +02:00
|
|
|
self.assertTrue(Filters.status_update.chat_created(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
self.message.supergroup_chat_created = False
|
|
|
|
|
|
|
|
self.message.migrate_to_chat_id = 100
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertTrue(Filters.status_update(self.message))
|
2017-06-01 04:10:09 +02:00
|
|
|
self.assertTrue(Filters.status_update.migrate(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
self.message.migrate_to_chat_id = 0
|
|
|
|
|
|
|
|
self.message.migrate_from_chat_id = 100
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertTrue(Filters.status_update(self.message))
|
2017-06-01 04:10:09 +02:00
|
|
|
self.assertTrue(Filters.status_update.migrate(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
self.message.migrate_from_chat_id = 0
|
|
|
|
|
|
|
|
self.message.channel_chat_created = True
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertTrue(Filters.status_update(self.message))
|
2017-06-01 04:10:09 +02:00
|
|
|
self.assertTrue(Filters.status_update.chat_created(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
self.message.channel_chat_created = False
|
|
|
|
|
|
|
|
self.message.pinned_message = 'test'
|
2016-05-28 02:14:55 +02:00
|
|
|
self.assertTrue(Filters.status_update(self.message))
|
2017-06-01 04:10:09 +02:00
|
|
|
self.assertTrue(Filters.status_update.pinned_message(self.message))
|
2016-04-26 23:42:50 +02:00
|
|
|
self.message.pinned_message = None
|
|
|
|
|
2016-09-13 20:47:43 +02:00
|
|
|
def test_entities_filter(self):
|
2016-09-24 18:56:54 +02:00
|
|
|
self.message.entities = [self.e(MessageEntity.MENTION)]
|
2016-09-24 13:38:56 +02:00
|
|
|
self.assertTrue(Filters.entity(MessageEntity.MENTION)(self.message))
|
2016-09-13 20:47:43 +02:00
|
|
|
|
|
|
|
self.message.entities = []
|
2016-09-24 13:38:56 +02:00
|
|
|
self.assertFalse(Filters.entity(MessageEntity.MENTION)(self.message))
|
2016-09-13 20:47:43 +02:00
|
|
|
|
2016-09-24 18:56:54 +02:00
|
|
|
self.message.entities = [self.e(MessageEntity.BOLD)]
|
2016-09-24 13:38:56 +02:00
|
|
|
self.assertFalse(Filters.entity(MessageEntity.MENTION)(self.message))
|
2016-09-13 20:47:43 +02:00
|
|
|
|
2016-09-24 18:56:54 +02:00
|
|
|
self.message.entities = [self.e(MessageEntity.BOLD), self.e(MessageEntity.MENTION)]
|
2016-09-24 13:38:56 +02:00
|
|
|
self.assertTrue(Filters.entity(MessageEntity.MENTION)(self.message))
|
2016-09-13 20:47:43 +02:00
|
|
|
|
2017-04-23 23:22:05 +02:00
|
|
|
def test_private_filter(self):
|
|
|
|
self.assertTrue(Filters.private(self.message))
|
|
|
|
self.message.chat.type = "group"
|
|
|
|
self.assertFalse(Filters.private(self.message))
|
|
|
|
|
|
|
|
def test_group_fileter(self):
|
|
|
|
self.assertFalse(Filters.group(self.message))
|
|
|
|
self.message.chat.type = "group"
|
|
|
|
self.assertTrue(Filters.group(self.message))
|
|
|
|
self.message.chat.type = "supergroup"
|
|
|
|
self.assertTrue(Filters.group(self.message))
|
|
|
|
|
2016-09-14 19:29:15 +02:00
|
|
|
def test_and_filters(self):
|
|
|
|
self.message.text = 'test'
|
|
|
|
self.message.forward_date = True
|
|
|
|
self.assertTrue((Filters.text & Filters.forwarded)(self.message))
|
|
|
|
self.message.text = '/test'
|
|
|
|
self.assertFalse((Filters.text & Filters.forwarded)(self.message))
|
|
|
|
self.message.text = 'test'
|
|
|
|
self.message.forward_date = None
|
|
|
|
self.assertFalse((Filters.text & Filters.forwarded)(self.message))
|
|
|
|
|
2016-09-24 18:56:54 +02:00
|
|
|
self.message.text = 'test'
|
|
|
|
self.message.forward_date = True
|
|
|
|
self.message.entities = [self.e(MessageEntity.MENTION)]
|
|
|
|
self.assertTrue((Filters.text & Filters.forwarded & Filters.entity(MessageEntity.MENTION))(
|
|
|
|
self.message))
|
|
|
|
self.message.entities = [self.e(MessageEntity.BOLD)]
|
|
|
|
self.assertFalse((Filters.text & Filters.forwarded & Filters.entity(MessageEntity.MENTION)
|
|
|
|
)(self.message))
|
|
|
|
|
2016-09-14 19:29:15 +02:00
|
|
|
def test_or_filters(self):
|
|
|
|
self.message.text = 'test'
|
|
|
|
self.assertTrue((Filters.text | Filters.status_update)(self.message))
|
|
|
|
self.message.group_chat_created = True
|
|
|
|
self.assertTrue((Filters.text | Filters.status_update)(self.message))
|
|
|
|
self.message.text = None
|
|
|
|
self.assertTrue((Filters.text | Filters.status_update)(self.message))
|
|
|
|
self.message.group_chat_created = False
|
|
|
|
self.assertFalse((Filters.text | Filters.status_update)(self.message))
|
|
|
|
|
2016-09-24 18:56:54 +02:00
|
|
|
def test_and_or_filters(self):
|
|
|
|
self.message.text = 'test'
|
|
|
|
self.message.forward_date = True
|
|
|
|
self.assertTrue((Filters.text & (Filters.forwarded | Filters.entity(MessageEntity.MENTION))
|
|
|
|
)(self.message))
|
|
|
|
self.message.forward_date = False
|
|
|
|
self.assertFalse((Filters.text & (Filters.forwarded | Filters.entity(MessageEntity.MENTION)
|
|
|
|
))(self.message))
|
|
|
|
self.message.entities = [self.e(MessageEntity.MENTION)]
|
|
|
|
self.assertTrue((Filters.text & (Filters.forwarded | Filters.entity(MessageEntity.MENTION))
|
|
|
|
)(self.message))
|
|
|
|
|
2016-10-16 12:48:45 +02:00
|
|
|
self.assertRegexpMatches(
|
|
|
|
str((Filters.text & (Filters.forwarded | Filters.entity(MessageEntity.MENTION)))),
|
2016-10-16 13:19:42 +02:00
|
|
|
r"<telegram.ext.filters.MergedFilter consisting of <telegram.ext.filters.(Filters.)?_"
|
|
|
|
r"Text object at .*?> and <telegram.ext.filters.MergedFilter consisting of "
|
|
|
|
r"<telegram.ext.filters.(Filters.)?_Forwarded object at .*?> or "
|
|
|
|
r"<telegram.ext.filters.(Filters.)?entity object at .*?>>>")
|
2016-10-15 22:59:41 +02:00
|
|
|
|
2017-03-28 18:38:44 +02:00
|
|
|
def test_inverted_filters(self):
|
|
|
|
self.message.text = '/test'
|
|
|
|
self.assertTrue((Filters.command)(self.message))
|
|
|
|
self.assertFalse((~Filters.command)(self.message))
|
|
|
|
self.message.text = 'test'
|
|
|
|
self.assertFalse((Filters.command)(self.message))
|
|
|
|
self.assertTrue((~Filters.command)(self.message))
|
|
|
|
|
|
|
|
def test_inverted_and_filters(self):
|
|
|
|
self.message.text = '/test'
|
|
|
|
self.message.forward_date = 1
|
|
|
|
self.assertTrue((Filters.forwarded & Filters.command)(self.message))
|
|
|
|
self.assertFalse((~Filters.forwarded & Filters.command)(self.message))
|
|
|
|
self.assertFalse((Filters.forwarded & ~Filters.command)(self.message))
|
|
|
|
self.assertFalse((~(Filters.forwarded & Filters.command))(self.message))
|
|
|
|
self.message.forward_date = None
|
|
|
|
self.assertFalse((Filters.forwarded & Filters.command)(self.message))
|
|
|
|
self.assertTrue((~Filters.forwarded & Filters.command)(self.message))
|
|
|
|
self.assertFalse((Filters.forwarded & ~Filters.command)(self.message))
|
|
|
|
self.assertTrue((~(Filters.forwarded & Filters.command))(self.message))
|
|
|
|
self.message.text = 'test'
|
|
|
|
self.assertFalse((Filters.forwarded & Filters.command)(self.message))
|
|
|
|
self.assertFalse((~Filters.forwarded & Filters.command)(self.message))
|
|
|
|
self.assertFalse((Filters.forwarded & ~Filters.command)(self.message))
|
|
|
|
self.assertTrue((~(Filters.forwarded & Filters.command))(self.message))
|
|
|
|
|
2016-10-15 22:59:41 +02:00
|
|
|
def test_faulty_custom_filter(self):
|
|
|
|
|
|
|
|
class _CustomFilter(BaseFilter):
|
|
|
|
pass
|
|
|
|
|
|
|
|
custom = _CustomFilter()
|
|
|
|
|
|
|
|
with self.assertRaises(NotImplementedError):
|
|
|
|
(custom & Filters.text)(self.message)
|
|
|
|
|
2017-05-21 14:00:53 +02:00
|
|
|
def test_language_filter_single(self):
|
|
|
|
self.message.from_user.language_code = 'en_US'
|
|
|
|
self.assertTrue((Filters.language('en_US'))(self.message))
|
|
|
|
self.assertTrue((Filters.language('en'))(self.message))
|
|
|
|
self.assertFalse((Filters.language('en_GB'))(self.message))
|
|
|
|
self.assertFalse((Filters.language('da'))(self.message))
|
|
|
|
self.message.from_user.language_code = 'en_GB'
|
|
|
|
self.assertFalse((Filters.language('en_US'))(self.message))
|
|
|
|
self.assertTrue((Filters.language('en'))(self.message))
|
|
|
|
self.assertTrue((Filters.language('en_GB'))(self.message))
|
|
|
|
self.assertFalse((Filters.language('da'))(self.message))
|
|
|
|
self.message.from_user.language_code = 'da'
|
|
|
|
self.assertFalse((Filters.language('en_US'))(self.message))
|
|
|
|
self.assertFalse((Filters.language('en'))(self.message))
|
|
|
|
self.assertFalse((Filters.language('en_GB'))(self.message))
|
|
|
|
self.assertTrue((Filters.language('da'))(self.message))
|
|
|
|
|
|
|
|
def test_language_filter_multiple(self):
|
|
|
|
f = Filters.language(['en_US', 'da'])
|
|
|
|
self.message.from_user.language_code = 'en_US'
|
|
|
|
self.assertTrue(f(self.message))
|
|
|
|
self.message.from_user.language_code = 'en_GB'
|
|
|
|
self.assertFalse(f(self.message))
|
|
|
|
self.message.from_user.language_code = 'da'
|
|
|
|
self.assertTrue(f(self.message))
|
|
|
|
|
2016-04-27 00:08:35 +02:00
|
|
|
|
2016-04-26 23:42:50 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|