mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-06 16:15:51 +00:00
Refactor TextFilter and its tests
This commit is contained in:
parent
f4339d10b0
commit
277eb8b701
2 changed files with 133 additions and 247 deletions
|
|
@ -84,9 +84,9 @@ class Command(Filter):
|
|||
|
||||
if not ignore_mention and mention and (await message.bot.me).username.lower() != mention.lower():
|
||||
return False
|
||||
elif prefix not in prefixes:
|
||||
if prefix not in prefixes:
|
||||
return False
|
||||
elif (command.lower() if ignore_case else command) not in commands:
|
||||
if (command.lower() if ignore_case else command) not in commands:
|
||||
return False
|
||||
|
||||
return {'command': Command.CommandObj(command=command, prefix=prefix, mention=mention)}
|
||||
|
|
@ -271,19 +271,26 @@ class Text(Filter):
|
|||
|
||||
if self.ignore_case:
|
||||
text = text.lower()
|
||||
_pre_process_func = lambda s: str(s).lower()
|
||||
else:
|
||||
_pre_process_func = str
|
||||
|
||||
# now check
|
||||
if self.equals is not None:
|
||||
self.equals = list(map(lambda s: str(s).lower() if self.ignore_case else str(s), self.equals))
|
||||
return text in self.equals
|
||||
elif self.contains is not None:
|
||||
self.contains = list(map(lambda s: str(s).lower() if self.ignore_case else str(s), self.contains))
|
||||
return all(map(text.__contains__, self.contains))
|
||||
elif self.startswith is not None:
|
||||
self.startswith = list(map(lambda s: str(s).lower() if self.ignore_case else str(s), self.startswith))
|
||||
return any(map(text.startswith, self.startswith))
|
||||
elif self.endswith is not None:
|
||||
self.endswith = list(map(lambda s: str(s).lower() if self.ignore_case else str(s), self.endswith))
|
||||
return any(map(text.endswith, self.endswith))
|
||||
equals = list(map(_pre_process_func, self.equals))
|
||||
return text in equals
|
||||
|
||||
if self.contains is not None:
|
||||
contains = list(map(_pre_process_func, self.contains))
|
||||
return all(map(text.__contains__, contains))
|
||||
|
||||
if self.startswith is not None:
|
||||
startswith = list(map(_pre_process_func, self.startswith))
|
||||
return any(map(text.startswith, startswith))
|
||||
|
||||
if self.endswith is not None:
|
||||
endswith = list(map(_pre_process_func, self.endswith))
|
||||
return any(map(text.endswith, endswith))
|
||||
|
||||
return False
|
||||
|
||||
|
|
|
|||
|
|
@ -4,38 +4,35 @@ from aiogram.dispatcher.filters import Text
|
|||
from aiogram.types import Message, CallbackQuery, InlineQuery, Poll
|
||||
|
||||
|
||||
def data_sample_1():
|
||||
return [
|
||||
('', ''),
|
||||
('', 'exAmple_string'),
|
||||
|
||||
('example_string', 'example_string'),
|
||||
('example_string', 'exAmple_string'),
|
||||
('exAmple_string', 'example_string'),
|
||||
|
||||
('example_string', 'example_string_dsf'),
|
||||
('example_string', 'example_striNG_dsf'),
|
||||
('example_striNG', 'example_string_dsf'),
|
||||
|
||||
('example_string', 'not_example_string'),
|
||||
('example_string', 'not_eXample_string'),
|
||||
('EXample_string', 'not_example_string'),
|
||||
]
|
||||
|
||||
class TestTextFilter:
|
||||
|
||||
async def _run_check(self, check, test_text):
|
||||
assert await check(Message(text=test_text))
|
||||
assert await check(CallbackQuery(data=test_text))
|
||||
assert await check(InlineQuery(query=test_text))
|
||||
assert await check(Poll(question=test_text))
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("test_prefix, test_text, ignore_case",
|
||||
[('', '', True),
|
||||
('', 'exAmple_string', True),
|
||||
('', '', False),
|
||||
('', 'exAmple_string', False),
|
||||
|
||||
('example_string', 'example_string', True),
|
||||
('example_string', 'exAmple_string', True),
|
||||
('exAmple_string', 'example_string', True),
|
||||
|
||||
('example_string', 'example_string', False),
|
||||
('example_string', 'exAmple_string', False),
|
||||
('exAmple_string', 'example_string', False),
|
||||
|
||||
('example_string', 'example_string_dsf', True),
|
||||
('example_string', 'example_striNG_dsf', True),
|
||||
('example_striNG', 'example_string_dsf', True),
|
||||
|
||||
('example_string', 'example_string_dsf', False),
|
||||
('example_string', 'example_striNG_dsf', False),
|
||||
('example_striNG', 'example_string_dsf', False),
|
||||
|
||||
('example_string', 'not_example_string', True),
|
||||
('example_string', 'not_eXample_string', True),
|
||||
('EXample_string', 'not_example_string', True),
|
||||
|
||||
('example_string', 'not_example_string', False),
|
||||
('example_string', 'not_eXample_string', False),
|
||||
('EXample_string', 'not_example_string', False),
|
||||
])
|
||||
@pytest.mark.parametrize('ignore_case', (True, False))
|
||||
@pytest.mark.parametrize("test_prefix, test_text", data_sample_1())
|
||||
async def test_startswith(self, test_prefix, test_text, ignore_case):
|
||||
test_filter = Text(startswith=test_prefix, ignore_case=ignore_case)
|
||||
|
||||
|
|
@ -50,42 +47,26 @@ class TestTextFilter:
|
|||
|
||||
return result is _test_text.startswith(_test_prefix)
|
||||
|
||||
assert await check(Message(text=test_text))
|
||||
assert await check(CallbackQuery(data=test_text))
|
||||
assert await check(InlineQuery(query=test_text))
|
||||
assert await check(Poll(question=test_text))
|
||||
await self._run_check(check, test_text)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("test_prefix_list, test_text, ignore_case",
|
||||
[(['not_example', ''], '', True),
|
||||
(['', 'not_example'], 'exAmple_string', True),
|
||||
(['not_example', ''], '', False),
|
||||
(['', 'not_example'], 'exAmple_string', False),
|
||||
@pytest.mark.parametrize('ignore_case', (True, False))
|
||||
@pytest.mark.parametrize("test_prefix_list, test_text", [
|
||||
(['not_example', ''], ''),
|
||||
(['', 'not_example'], 'exAmple_string'),
|
||||
|
||||
(['example_string', 'not_example'], 'example_string', True),
|
||||
(['not_example', 'example_string'], 'exAmple_string', True),
|
||||
(['exAmple_string', 'not_example'], 'example_string', True),
|
||||
(['not_example', 'example_string'], 'example_string'),
|
||||
(['example_string', 'not_example'], 'exAmple_string'),
|
||||
(['not_example', 'exAmple_string'], 'example_string'),
|
||||
|
||||
(['not_example', 'example_string'], 'example_string', False),
|
||||
(['example_string', 'not_example'], 'exAmple_string', False),
|
||||
(['not_example', 'exAmple_string'], 'example_string', False),
|
||||
(['not_example', 'example_string'], 'example_string_dsf'),
|
||||
(['example_string', 'not_example'], 'example_striNG_dsf'),
|
||||
(['not_example', 'example_striNG'], 'example_string_dsf'),
|
||||
|
||||
(['example_string', 'not_example'], 'example_string_dsf', True),
|
||||
(['not_example', 'example_string'], 'example_striNG_dsf', True),
|
||||
(['example_striNG', 'not_example'], 'example_string_dsf', True),
|
||||
|
||||
(['not_example', 'example_string'], 'example_string_dsf', False),
|
||||
(['example_string', 'not_example'], 'example_striNG_dsf', False),
|
||||
(['not_example', 'example_striNG'], 'example_string_dsf', False),
|
||||
|
||||
(['example_string', 'not_example'], 'not_example_string', True),
|
||||
(['not_example', 'example_string'], 'not_eXample_string', True),
|
||||
(['EXample_string', 'not_example'], 'not_example_string', True),
|
||||
|
||||
(['not_example', 'example_string'], 'not_example_string', False),
|
||||
(['example_string', 'not_example'], 'not_eXample_string', False),
|
||||
(['not_example', 'EXample_string'], 'not_example_string', False),
|
||||
])
|
||||
(['not_example', 'example_string'], 'not_example_string'),
|
||||
(['example_string', 'not_example'], 'not_eXample_string'),
|
||||
(['not_example', 'EXample_string'], 'not_example_string'),
|
||||
])
|
||||
async def test_startswith_list(self, test_prefix_list, test_text, ignore_case):
|
||||
test_filter = Text(startswith=test_prefix_list, ignore_case=ignore_case)
|
||||
|
||||
|
|
@ -100,42 +81,11 @@ class TestTextFilter:
|
|||
|
||||
return result is any(map(_test_text.startswith, _test_prefix_list))
|
||||
|
||||
assert await check(Message(text=test_text))
|
||||
assert await check(CallbackQuery(data=test_text))
|
||||
assert await check(InlineQuery(query=test_text))
|
||||
assert await check(Poll(question=test_text))
|
||||
await self._run_check(check, test_text)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("test_postfix, test_text, ignore_case",
|
||||
[('', '', True),
|
||||
('', 'exAmple_string', True),
|
||||
('', '', False),
|
||||
('', 'exAmple_string', False),
|
||||
|
||||
('example_string', 'example_string', True),
|
||||
('example_string', 'exAmple_string', True),
|
||||
('exAmple_string', 'example_string', True),
|
||||
|
||||
('example_string', 'example_string', False),
|
||||
('example_string', 'exAmple_string', False),
|
||||
('exAmple_string', 'example_string', False),
|
||||
|
||||
('example_string', 'example_string_dsf', True),
|
||||
('example_string', 'example_striNG_dsf', True),
|
||||
('example_striNG', 'example_string_dsf', True),
|
||||
|
||||
('example_string', 'example_string_dsf', False),
|
||||
('example_string', 'example_striNG_dsf', False),
|
||||
('example_striNG', 'example_string_dsf', False),
|
||||
|
||||
('example_string', 'not_example_string', True),
|
||||
('example_string', 'not_eXample_string', True),
|
||||
('EXample_string', 'not_eXample_string', True),
|
||||
|
||||
('example_string', 'not_example_string', False),
|
||||
('example_string', 'not_eXample_string', False),
|
||||
('EXample_string', 'not_example_string', False),
|
||||
])
|
||||
@pytest.mark.parametrize('ignore_case', (True, False))
|
||||
@pytest.mark.parametrize("test_postfix, test_text", data_sample_1())
|
||||
async def test_endswith(self, test_postfix, test_text, ignore_case):
|
||||
test_filter = Text(endswith=test_postfix, ignore_case=ignore_case)
|
||||
|
||||
|
|
@ -150,42 +100,26 @@ class TestTextFilter:
|
|||
|
||||
return result is _test_text.endswith(_test_postfix)
|
||||
|
||||
assert await check(Message(text=test_text))
|
||||
assert await check(CallbackQuery(data=test_text))
|
||||
assert await check(InlineQuery(query=test_text))
|
||||
assert await check(Poll(question=test_text))
|
||||
await self._run_check(check, test_text)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("test_postfix_list, test_text, ignore_case",
|
||||
[(['', 'not_example'], '', True),
|
||||
(['not_example', ''], 'exAmple_string', True),
|
||||
(['', 'not_example'], '', False),
|
||||
(['not_example', ''], 'exAmple_string', False),
|
||||
@pytest.mark.parametrize('ignore_case', (True, False))
|
||||
@pytest.mark.parametrize("test_postfix_list, test_text", [
|
||||
(['', 'not_example'], ''),
|
||||
(['not_example', ''], 'exAmple_string'),
|
||||
|
||||
(['example_string', 'not_example'], 'example_string', True),
|
||||
(['not_example', 'example_string'], 'exAmple_string', True),
|
||||
(['exAmple_string', 'not_example'], 'example_string', True),
|
||||
(['example_string', 'not_example'], 'example_string'),
|
||||
(['not_example', 'example_string'], 'exAmple_string'),
|
||||
(['exAmple_string', 'not_example'], 'example_string'),
|
||||
|
||||
(['example_string', 'not_example'], 'example_string', False),
|
||||
(['not_example', 'example_string'], 'exAmple_string', False),
|
||||
(['exAmple_string', 'not_example'], 'example_string', False),
|
||||
(['not_example', 'example_string'], 'example_string_dsf'),
|
||||
(['example_string', 'not_example'], 'example_striNG_dsf'),
|
||||
(['not_example', 'example_striNG'], 'example_string_dsf'),
|
||||
|
||||
(['example_string', 'not_example'], 'example_string_dsf', True),
|
||||
(['not_example', 'example_string'], 'example_striNG_dsf', True),
|
||||
(['example_striNG', 'not_example'], 'example_string_dsf', True),
|
||||
|
||||
(['not_example', 'example_string'], 'example_string_dsf', False),
|
||||
(['example_string', 'not_example'], 'example_striNG_dsf', False),
|
||||
(['not_example', 'example_striNG'], 'example_string_dsf', False),
|
||||
|
||||
(['not_example', 'example_string'], 'not_example_string', True),
|
||||
(['example_string', 'not_example'], 'not_eXample_string', True),
|
||||
(['not_example', 'EXample_string'], 'not_eXample_string', True),
|
||||
|
||||
(['not_example', 'example_string'], 'not_example_string', False),
|
||||
(['example_string', 'not_example'], 'not_eXample_string', False),
|
||||
(['not_example', 'EXample_string'], 'not_example_string', False),
|
||||
])
|
||||
(['not_example', 'example_string'], 'not_example_string'),
|
||||
(['example_string', 'not_example'], 'not_eXample_string'),
|
||||
(['not_example', 'EXample_string'], 'not_example_string'),
|
||||
])
|
||||
async def test_endswith_list(self, test_postfix_list, test_text, ignore_case):
|
||||
test_filter = Text(endswith=test_postfix_list, ignore_case=ignore_case)
|
||||
|
||||
|
|
@ -199,42 +133,26 @@ class TestTextFilter:
|
|||
_test_text = test_text
|
||||
|
||||
return result is any(map(_test_text.endswith, _test_postfix_list))
|
||||
assert await check(Message(text=test_text))
|
||||
assert await check(CallbackQuery(data=test_text))
|
||||
assert await check(InlineQuery(query=test_text))
|
||||
assert await check(Poll(question=test_text))
|
||||
await self._run_check(check, test_text)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("test_string, test_text, ignore_case",
|
||||
[('', '', True),
|
||||
('', 'exAmple_string', True),
|
||||
('', '', False),
|
||||
('', 'exAmple_string', False),
|
||||
@pytest.mark.parametrize('ignore_case', (True, False))
|
||||
@pytest.mark.parametrize("test_string, test_text", [
|
||||
('', ''),
|
||||
('', 'exAmple_string'),
|
||||
|
||||
('example_string', 'example_string', True),
|
||||
('example_string', 'exAmple_string', True),
|
||||
('exAmple_string', 'example_string', True),
|
||||
('example_string', 'example_string'),
|
||||
('example_string', 'exAmple_string'),
|
||||
('exAmple_string', 'example_string'),
|
||||
|
||||
('example_string', 'example_string', False),
|
||||
('example_string', 'exAmple_string', False),
|
||||
('exAmple_string', 'example_string', False),
|
||||
('example_string', 'example_string_dsf'),
|
||||
('example_string', 'example_striNG_dsf'),
|
||||
('example_striNG', 'example_string_dsf'),
|
||||
|
||||
('example_string', 'example_string_dsf', True),
|
||||
('example_string', 'example_striNG_dsf', True),
|
||||
('example_striNG', 'example_string_dsf', True),
|
||||
|
||||
('example_string', 'example_string_dsf', False),
|
||||
('example_string', 'example_striNG_dsf', False),
|
||||
('example_striNG', 'example_string_dsf', False),
|
||||
|
||||
('example_string', 'not_example_strin', True),
|
||||
('example_string', 'not_eXample_strin', True),
|
||||
('EXample_string', 'not_eXample_strin', True),
|
||||
|
||||
('example_string', 'not_example_strin', False),
|
||||
('example_string', 'not_eXample_strin', False),
|
||||
('EXample_string', 'not_example_strin', False),
|
||||
])
|
||||
('example_string', 'not_example_strin'),
|
||||
('example_string', 'not_eXample_strin'),
|
||||
('EXample_string', 'not_example_strin'),
|
||||
])
|
||||
async def test_contains(self, test_string, test_text, ignore_case):
|
||||
test_filter = Text(contains=test_string, ignore_case=ignore_case)
|
||||
|
||||
|
|
@ -249,23 +167,16 @@ class TestTextFilter:
|
|||
|
||||
return result is (_test_string in _test_text)
|
||||
|
||||
assert await check(Message(text=test_text))
|
||||
assert await check(CallbackQuery(data=test_text))
|
||||
assert await check(InlineQuery(query=test_text))
|
||||
assert await check(Poll(question=test_text))
|
||||
await self._run_check(check, test_text)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("test_filter_list, test_text, ignore_case",
|
||||
[(['a', 'ab', 'abc'], 'A', True),
|
||||
(['a', 'ab', 'abc'], 'ab', True),
|
||||
(['a', 'ab', 'abc'], 'aBc', True),
|
||||
(['a', 'ab', 'abc'], 'd', True),
|
||||
|
||||
(['a', 'ab', 'abc'], 'A', False),
|
||||
(['a', 'ab', 'abc'], 'ab', False),
|
||||
(['a', 'ab', 'abc'], 'aBc', False),
|
||||
(['a', 'ab', 'abc'], 'd', False),
|
||||
])
|
||||
@pytest.mark.parametrize('ignore_case', (True, False))
|
||||
@pytest.mark.parametrize("test_filter_list, test_text", [
|
||||
(['a', 'ab', 'abc'], 'A'),
|
||||
(['a', 'ab', 'abc'], 'ab'),
|
||||
(['a', 'ab', 'abc'], 'aBc'),
|
||||
(['a', 'ab', 'abc'], 'd'),
|
||||
])
|
||||
async def test_contains_list(self, test_filter_list, test_text, ignore_case):
|
||||
test_filter = Text(contains=test_filter_list, ignore_case=ignore_case)
|
||||
|
||||
|
|
@ -280,34 +191,22 @@ class TestTextFilter:
|
|||
|
||||
return result is all(map(_test_text.__contains__, _test_filter_list))
|
||||
|
||||
assert await check(Message(text=test_text))
|
||||
assert await check(CallbackQuery(data=test_text))
|
||||
assert await check(InlineQuery(query=test_text))
|
||||
assert await check(Poll(question=test_text))
|
||||
await self._run_check(check, test_text)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("test_filter_text, test_text, ignore_case",
|
||||
[('', '', True),
|
||||
('', 'exAmple_string', True),
|
||||
('', '', False),
|
||||
('', 'exAmple_string', False),
|
||||
@pytest.mark.parametrize('ignore_case', (True, False))
|
||||
@pytest.mark.parametrize("test_filter_text, test_text", [
|
||||
('', ''),
|
||||
('', 'exAmple_string'),
|
||||
|
||||
('example_string', 'example_string', True),
|
||||
('example_string', 'exAmple_string', True),
|
||||
('exAmple_string', 'example_string', True),
|
||||
('example_string', 'example_string'),
|
||||
('example_string', 'exAmple_string'),
|
||||
('exAmple_string', 'example_string'),
|
||||
|
||||
('example_string', 'example_string', False),
|
||||
('example_string', 'exAmple_string', False),
|
||||
('exAmple_string', 'example_string', False),
|
||||
|
||||
('example_string', 'not_example_string', True),
|
||||
('example_string', 'not_eXample_string', True),
|
||||
('EXample_string', 'not_eXample_string', True),
|
||||
|
||||
('example_string', 'not_example_string', False),
|
||||
('example_string', 'not_eXample_string', False),
|
||||
('EXample_string', 'not_example_string', False),
|
||||
])
|
||||
('example_string', 'not_example_string'),
|
||||
('example_string', 'not_eXample_string'),
|
||||
('EXample_string', 'not_example_string'),
|
||||
])
|
||||
async def test_equals_string(self, test_filter_text, test_text, ignore_case):
|
||||
test_filter = Text(equals=test_filter_text, ignore_case=ignore_case)
|
||||
|
||||
|
|
@ -321,50 +220,30 @@ class TestTextFilter:
|
|||
_test_text = test_text
|
||||
return result is (_test_text == _test_filter_text)
|
||||
|
||||
assert await check(Message(text=test_text))
|
||||
assert await check(CallbackQuery(data=test_text))
|
||||
assert await check(InlineQuery(query=test_text))
|
||||
assert await check(Poll(question=test_text))
|
||||
await self._run_check(check, test_text)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("test_filter_list, test_text, ignore_case",
|
||||
[(['', 'new_string'], '', True),
|
||||
(['new_string', ''], 'exAmple_string', True),
|
||||
(['new_string', ''], '', False),
|
||||
(['', 'new_string'], 'exAmple_string', False),
|
||||
@pytest.mark.parametrize('ignore_case', (True, False))
|
||||
@pytest.mark.parametrize("test_filter_list, test_text", [
|
||||
(['new_string', ''], ''),
|
||||
(['', 'new_string'], 'exAmple_string'),
|
||||
|
||||
(['example_string'], 'example_string', True),
|
||||
(['example_string'], 'exAmple_string', True),
|
||||
(['exAmple_string'], 'example_string', True),
|
||||
(['example_string'], 'example_string'),
|
||||
(['example_string'], 'exAmple_string'),
|
||||
(['exAmple_string'], 'example_string'),
|
||||
|
||||
(['example_string'], 'example_string', False),
|
||||
(['example_string'], 'exAmple_string', False),
|
||||
(['exAmple_string'], 'example_string', False),
|
||||
(['example_string'], 'not_example_string'),
|
||||
(['example_string'], 'not_eXample_string'),
|
||||
(['EXample_string'], 'not_example_string'),
|
||||
|
||||
(['example_string'], 'not_example_string', True),
|
||||
(['example_string'], 'not_eXample_string', True),
|
||||
(['EXample_string'], 'not_eXample_string', True),
|
||||
(['example_string', 'new_string'], 'example_string'),
|
||||
(['new_string', 'example_string'], 'exAmple_string'),
|
||||
(['exAmple_string', 'new_string'], 'example_string'),
|
||||
|
||||
(['example_string'], 'not_example_string', False),
|
||||
(['example_string'], 'not_eXample_string', False),
|
||||
(['EXample_string'], 'not_example_string', False),
|
||||
|
||||
(['example_string', 'new_string'], 'example_string', True),
|
||||
(['new_string', 'example_string'], 'exAmple_string', True),
|
||||
(['exAmple_string', 'new_string'], 'example_string', True),
|
||||
|
||||
(['example_string', 'new_string'], 'example_string', False),
|
||||
(['new_string', 'example_string'], 'exAmple_string', False),
|
||||
(['exAmple_string', 'new_string'], 'example_string', False),
|
||||
|
||||
(['example_string', 'new_string'], 'not_example_string', True),
|
||||
(['new_string', 'example_string'], 'not_eXample_string', True),
|
||||
(['EXample_string', 'new_string'], 'not_eXample_string', True),
|
||||
|
||||
(['example_string', 'new_string'], 'not_example_string', False),
|
||||
(['new_string', 'example_string'], 'not_eXample_string', False),
|
||||
(['EXample_string', 'new_string'], 'not_example_string', False),
|
||||
])
|
||||
(['example_string', 'new_string'], 'not_example_string'),
|
||||
(['new_string', 'example_string'], 'not_eXample_string'),
|
||||
(['EXample_string', 'new_string'], 'not_example_string'),
|
||||
])
|
||||
async def test_equals_list(self, test_filter_list, test_text, ignore_case):
|
||||
test_filter = Text(equals=test_filter_list, ignore_case=ignore_case)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue