mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 10:11:52 +00:00
Combining filters (again) (#1018)
* Added explicit logic filters, added slots to all other filters * Update translation files * Update docs
This commit is contained in:
parent
1a3e2a8991
commit
b0f251a8b8
26 changed files with 823 additions and 361 deletions
|
|
@ -3,7 +3,6 @@ from typing import Awaitable
|
|||
import pytest
|
||||
|
||||
from aiogram.filters import Filter
|
||||
from aiogram.filters.base import _InvertFilter
|
||||
|
||||
try:
|
||||
from asynctest import CoroutineMock, patch
|
||||
|
|
@ -32,20 +31,3 @@ class TestBaseFilter:
|
|||
call = my_filter(event="test")
|
||||
await call
|
||||
mocked_call.assert_awaited_with(event="test")
|
||||
|
||||
async def test_invert(self):
|
||||
my_filter = MyFilter()
|
||||
my_inverted_filter = ~my_filter
|
||||
|
||||
assert str(my_inverted_filter) == f"~{str(my_filter)}"
|
||||
|
||||
assert isinstance(my_inverted_filter, _InvertFilter)
|
||||
|
||||
with patch(
|
||||
"tests.test_filters.test_base.MyFilter.__call__",
|
||||
new_callable=CoroutineMock,
|
||||
) as mocked_call:
|
||||
call = my_inverted_filter(event="test")
|
||||
result = await call
|
||||
mocked_call.assert_awaited_with(event="test")
|
||||
assert not result
|
||||
|
|
|
|||
38
tests/test_filters/test_logic.py
Normal file
38
tests/test_filters/test_logic.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
import pytest
|
||||
|
||||
from aiogram.filters import Text, and_f, invert_f, or_f
|
||||
from aiogram.filters.logic import _AndFilter, _InvertFilter, _OrFilter
|
||||
|
||||
|
||||
class TestLogic:
|
||||
@pytest.mark.parametrize(
|
||||
"obj,case,result",
|
||||
[
|
||||
[True, and_f(lambda t: t is True, lambda t: t is True), True],
|
||||
[True, and_f(lambda t: t is True, lambda t: t is False), False],
|
||||
[True, and_f(lambda t: t is False, lambda t: t is False), False],
|
||||
[True, and_f(lambda t: {"t": t}, lambda t: t is False), False],
|
||||
[True, and_f(lambda t: {"t": t}, lambda t: t is True), {"t": True}],
|
||||
[True, or_f(lambda t: t is True, lambda t: t is True), True],
|
||||
[True, or_f(lambda t: t is True, lambda t: t is False), True],
|
||||
[True, or_f(lambda t: t is False, lambda t: t is False), False],
|
||||
[True, or_f(lambda t: t is False, lambda t: t is True), True],
|
||||
[True, or_f(lambda t: t is False, lambda t: {"t": t}), {"t": True}],
|
||||
[True, or_f(lambda t: {"t": t}, lambda t: {"a": 42}), {"t": True}],
|
||||
[True, invert_f(lambda t: t is False), True],
|
||||
],
|
||||
)
|
||||
async def test_logic(self, obj, case, result):
|
||||
assert await case(obj) == result
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"case,type_",
|
||||
[
|
||||
[or_f(Text(text="test"), Text(text="test")), _OrFilter],
|
||||
[and_f(Text(text="test"), Text(text="test")), _AndFilter],
|
||||
[invert_f(Text(text="test")), _InvertFilter],
|
||||
[~Text(text="test"), _InvertFilter],
|
||||
],
|
||||
)
|
||||
def test_dunder_methods(self, case, type_):
|
||||
assert isinstance(case, type_)
|
||||
Loading…
Add table
Add a link
Reference in a new issue