aiogram/tests/test_filters.py

265 lines
10 KiB
Python
Raw Normal View History

2019-07-18 20:52:41 +03:00
import pytest
from aiogram.dispatcher.filters import Text
2019-11-15 12:17:57 +02:00
from aiogram.types import CallbackQuery, InlineQuery, Message, Poll
2019-07-18 20:52:41 +03:00
2019-08-11 23:32:41 +03:00
def data_sample_1():
return [
2019-11-15 12:17:57 +02:00
("", ""),
("", "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"),
2019-08-11 23:32:41 +03:00
]
2019-11-15 12:17:57 +02:00
class TestTextFilter:
2019-08-11 23:32:41 +03:00
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))
2019-07-18 20:52:41 +03:00
@pytest.mark.asyncio
2019-11-15 12:17:57 +02:00
@pytest.mark.parametrize("ignore_case", (True, False))
2019-08-11 23:32:41 +03:00
@pytest.mark.parametrize("test_prefix, test_text", data_sample_1())
2019-07-18 20:52:41 +03:00
async def test_startswith(self, test_prefix, test_text, ignore_case):
test_filter = Text(startswith=test_prefix, ignore_case=ignore_case)
async def check(obj):
result = await test_filter.check(obj)
if ignore_case:
_test_prefix = test_prefix.lower()
_test_text = test_text.lower()
else:
_test_prefix = test_prefix
_test_text = test_text
return result is _test_text.startswith(_test_prefix)
2019-08-11 23:32:41 +03:00
await self._run_check(check, test_text)
2019-07-18 20:52:41 +03:00
2019-07-27 13:26:57 +03:00
@pytest.mark.asyncio
2019-11-15 12:17:57 +02:00
@pytest.mark.parametrize("ignore_case", (True, False))
@pytest.mark.parametrize(
"test_prefix_list, test_text",
[
(["not_example", ""], ""),
(["", "not_example"], "exAmple_string"),
(["not_example", "example_string"], "example_string"),
(["example_string", "not_example"], "exAmple_string"),
(["not_example", "exAmple_string"], "example_string"),
(["not_example", "example_string"], "example_string_dsf"),
(["example_string", "not_example"], "example_striNG_dsf"),
(["not_example", "example_striNG"], "example_string_dsf"),
(["not_example", "example_string"], "not_example_string"),
(["example_string", "not_example"], "not_eXample_string"),
(["not_example", "EXample_string"], "not_example_string"),
],
)
2019-07-27 13:26:57 +03:00
async def test_startswith_list(self, test_prefix_list, test_text, ignore_case):
test_filter = Text(startswith=test_prefix_list, ignore_case=ignore_case)
async def check(obj):
result = await test_filter.check(obj)
if ignore_case:
_test_prefix_list = map(str.lower, test_prefix_list)
_test_text = test_text.lower()
else:
_test_prefix_list = test_prefix_list
_test_text = test_text
return result is any(map(_test_text.startswith, _test_prefix_list))
2019-08-11 23:32:41 +03:00
await self._run_check(check, test_text)
2019-07-27 13:26:57 +03:00
2019-07-18 20:52:41 +03:00
@pytest.mark.asyncio
2019-11-15 12:17:57 +02:00
@pytest.mark.parametrize("ignore_case", (True, False))
2019-08-11 23:32:41 +03:00
@pytest.mark.parametrize("test_postfix, test_text", data_sample_1())
2019-07-18 20:52:41 +03:00
async def test_endswith(self, test_postfix, test_text, ignore_case):
test_filter = Text(endswith=test_postfix, ignore_case=ignore_case)
async def check(obj):
result = await test_filter.check(obj)
if ignore_case:
_test_postfix = test_postfix.lower()
_test_text = test_text.lower()
else:
_test_postfix = test_postfix
_test_text = test_text
return result is _test_text.endswith(_test_postfix)
2019-08-11 23:32:41 +03:00
await self._run_check(check, test_text)
2019-07-18 20:52:41 +03:00
2019-07-27 13:26:57 +03:00
@pytest.mark.asyncio
2019-11-15 12:17:57 +02:00
@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"),
(["not_example", "example_string"], "exAmple_string"),
(["exAmple_string", "not_example"], "example_string"),
(["not_example", "example_string"], "example_string_dsf"),
(["example_string", "not_example"], "example_striNG_dsf"),
(["not_example", "example_striNG"], "example_string_dsf"),
(["not_example", "example_string"], "not_example_string"),
(["example_string", "not_example"], "not_eXample_string"),
(["not_example", "EXample_string"], "not_example_string"),
],
)
2019-07-27 13:26:57 +03:00
async def test_endswith_list(self, test_postfix_list, test_text, ignore_case):
test_filter = Text(endswith=test_postfix_list, ignore_case=ignore_case)
async def check(obj):
result = await test_filter.check(obj)
if ignore_case:
_test_postfix_list = map(str.lower, test_postfix_list)
_test_text = test_text.lower()
else:
_test_postfix_list = test_postfix_list
_test_text = test_text
return result is any(map(_test_text.endswith, _test_postfix_list))
2019-11-15 12:17:57 +02:00
2019-08-11 23:32:41 +03:00
await self._run_check(check, test_text)
2019-07-27 13:26:57 +03:00
2019-07-18 20:52:41 +03:00
@pytest.mark.asyncio
2019-11-15 12:17:57 +02:00
@pytest.mark.parametrize("ignore_case", (True, False))
@pytest.mark.parametrize(
"test_string, test_text",
[
("", ""),
("", "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_strin"),
("example_string", "not_eXample_strin"),
("EXample_string", "not_example_strin"),
],
)
2019-07-18 20:52:41 +03:00
async def test_contains(self, test_string, test_text, ignore_case):
2019-07-18 23:02:03 +03:00
test_filter = Text(contains=test_string, ignore_case=ignore_case)
2019-07-18 20:52:41 +03:00
async def check(obj):
result = await test_filter.check(obj)
if ignore_case:
_test_string = test_string.lower()
_test_text = test_text.lower()
else:
_test_string = test_string
_test_text = test_text
return result is (_test_string in _test_text)
2019-08-11 23:32:41 +03:00
await self._run_check(check, test_text)
2019-07-18 20:52:41 +03:00
2019-07-27 13:26:57 +03:00
@pytest.mark.asyncio
2019-11-15 12:17:57 +02:00
@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"),
],
)
2019-07-27 13:26:57 +03:00
async def test_contains_list(self, test_filter_list, test_text, ignore_case):
test_filter = Text(contains=test_filter_list, ignore_case=ignore_case)
async def check(obj):
result = await test_filter.check(obj)
if ignore_case:
_test_filter_list = list(map(str.lower, test_filter_list))
_test_text = test_text.lower()
else:
_test_filter_list = test_filter_list
_test_text = test_text
return result is all(map(_test_text.__contains__, _test_filter_list))
2019-08-11 23:32:41 +03:00
await self._run_check(check, test_text)
2019-07-27 13:26:57 +03:00
2019-07-18 20:52:41 +03:00
@pytest.mark.asyncio
2019-11-15 12:17:57 +02:00
@pytest.mark.parametrize("ignore_case", (True, False))
@pytest.mark.parametrize(
"test_filter_text, test_text",
[
("", ""),
("", "exAmple_string"),
("example_string", "example_string"),
("example_string", "exAmple_string"),
("exAmple_string", "example_string"),
("example_string", "not_example_string"),
("example_string", "not_eXample_string"),
("EXample_string", "not_example_string"),
],
)
2019-07-18 20:52:41 +03:00
async def test_equals_string(self, test_filter_text, test_text, ignore_case):
2019-07-18 22:57:59 +03:00
test_filter = Text(equals=test_filter_text, ignore_case=ignore_case)
2019-07-18 20:52:41 +03:00
async def check(obj):
result = await test_filter.check(obj)
if ignore_case:
_test_filter_text = test_filter_text.lower()
_test_text = test_text.lower()
else:
_test_filter_text = test_filter_text
_test_text = test_text
return result is (_test_text == _test_filter_text)
2019-08-11 23:32:41 +03:00
await self._run_check(check, test_text)
2019-07-24 11:57:17 +03:00
@pytest.mark.asyncio
2019-11-15 12:17:57 +02:00
@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"),
(["example_string"], "exAmple_string"),
(["exAmple_string"], "example_string"),
(["example_string"], "not_example_string"),
(["example_string"], "not_eXample_string"),
(["EXample_string"], "not_example_string"),
(["example_string", "new_string"], "example_string"),
(["new_string", "example_string"], "exAmple_string"),
(["exAmple_string", "new_string"], "example_string"),
(["example_string", "new_string"], "not_example_string"),
(["new_string", "example_string"], "not_eXample_string"),
(["EXample_string", "new_string"], "not_example_string"),
],
)
2019-07-24 11:57:17 +03:00
async def test_equals_list(self, test_filter_list, test_text, ignore_case):
test_filter = Text(equals=test_filter_list, ignore_case=ignore_case)
async def check(obj):
result = await test_filter.check(obj)
if ignore_case:
_test_filter_list = list(map(str.lower, test_filter_list))
_test_text = test_text.lower()
else:
_test_filter_list = test_filter_list
_test_text = test_text
assert result is (_test_text in _test_filter_list)
await check(Message(text=test_text))
await check(CallbackQuery(data=test_text))
await check(InlineQuery(query=test_text))
await check(Poll(question=test_text))