Merge pull request #175 from Birdi7/fix-empty-text-filter

Fix empty text filter
This commit is contained in:
Alex Root Junior 2019-07-23 13:20:53 +03:00 committed by GitHub
commit 2aa01b3c6f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 10 deletions

View file

@ -221,13 +221,13 @@ class Text(Filter):
:param ignore_case: case insensitive
"""
# Only one mode can be used. check it.
check = sum(map(bool, (equals, contains, startswith, endswith)))
check = sum(map(lambda s: s is not None, (equals, contains, startswith, endswith)))
if check > 1:
args = "' and '".join([arg[0] for arg in [('equals', equals),
('contains', contains),
('startswith', startswith),
('endswith', endswith)
] if arg[1]])
] if arg[1] is not None])
raise ValueError(f"Arguments '{args}' cannot be used together.")
elif check == 0:
raise ValueError(f"No one mode is specified!")
@ -266,22 +266,22 @@ class Text(Filter):
if self.ignore_case:
text = text.lower()
if self.equals:
if self.equals is not None:
self.equals = str(self.equals)
if self.ignore_case:
self.equals = self.equals.lower()
return text == self.equals
elif self.contains:
elif self.contains is not None:
self.contains = str(self.contains)
if self.ignore_case:
self.contains = self.contains.lower()
return self.contains in text
elif self.startswith:
elif self.startswith is not None:
self.startswith = str(self.startswith)
if self.ignore_case:
self.startswith = self.startswith.lower()
return text.startswith(self.startswith)
elif self.endswith:
elif self.endswith is not None:
self.endswith = str(self.endswith)
if self.ignore_case:
self.endswith = self.endswith.lower()

View file

@ -7,7 +7,12 @@ from aiogram.types import Message, CallbackQuery, InlineQuery, Poll
class TestTextFilter:
@pytest.mark.asyncio
@pytest.mark.parametrize("test_prefix, test_text, ignore_case",
[('example_string', 'example_string', True),
[('', '', True),
('', 'exAmple_string', True),
('', '', False),
('', 'exAmple_string', False),
('example_string', 'example_string', True),
('example_string', 'exAmple_string', True),
('exAmple_string', 'example_string', True),
@ -52,7 +57,12 @@ class TestTextFilter:
@pytest.mark.asyncio
@pytest.mark.parametrize("test_postfix, test_text, ignore_case",
[('example_string', 'example_string', True),
[('', '', True),
('', 'exAmple_string', True),
('', '', False),
('', 'exAmple_string', False),
('example_string', 'example_string', True),
('example_string', 'exAmple_string', True),
('exAmple_string', 'example_string', True),
@ -97,7 +107,12 @@ class TestTextFilter:
@pytest.mark.asyncio
@pytest.mark.parametrize("test_string, test_text, ignore_case",
[('example_string', 'example_string', True),
[('', '', True),
('', 'exAmple_string', True),
('', '', False),
('', 'exAmple_string', False),
('example_string', 'example_string', True),
('example_string', 'exAmple_string', True),
('exAmple_string', 'example_string', True),
@ -142,7 +157,12 @@ class TestTextFilter:
@pytest.mark.asyncio
@pytest.mark.parametrize("test_filter_text, test_text, ignore_case",
[('example_string', 'example_string', True),
[('', '', True),
('', 'exAmple_string', True),
('', '', False),
('', 'exAmple_string', False),
('example_string', 'example_string', True),
('example_string', 'exAmple_string', True),
('exAmple_string', 'example_string', True),