Skip to content

Commit

Permalink
CR fix: add tests, fix union
Browse files Browse the repository at this point in the history
  • Loading branch information
bomzheg committed Jul 8, 2022
1 parent 7f83208 commit 4aac637
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion aiogram/dispatcher/filters/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Command(Filter):
By default this filter is registered for messages and edited messages handlers.
"""

def __init__(self, commands: Union[Iterable[str], Iterable[BotCommand], str, BotCommand],
def __init__(self, commands: Union[Iterable[Union[str, BotCommand]], str, BotCommand],
prefixes: Union[Iterable, str] = '/',
ignore_case: bool = True,
ignore_mention: bool = False,
Expand Down
37 changes: 34 additions & 3 deletions tests/test_dispatcher/test_filters/test_builtin.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from typing import Set
from typing import Set, Union, Iterable
from datetime import datetime

import pytest

from aiogram.dispatcher.filters.builtin import (
Text,
extract_chat_ids,
ChatIDArgumentType, ForwardedMessageFilter, IDFilter,
ChatIDArgumentType, ForwardedMessageFilter, IDFilter, Command,
)
from aiogram.types import Message
from aiogram.types import Message, BotCommand
from tests.types.dataset import MESSAGE, MESSAGE_FROM_CHANNEL


Expand Down Expand Up @@ -108,3 +108,34 @@ async def test_chat_id_for_channels(self):
filter = IDFilter(chat_id=message_from_channel.chat.id)

assert await filter.check(message_from_channel)


@pytest.mark.parametrize("command", [
"/start",
"/start some args",
])
@pytest.mark.parametrize("cmd_filter", [
"start",
("start",),
BotCommand(command="start", description="my desc"),
(BotCommand(command="start", description="bar"),),
(BotCommand(command="start", description="foo"), "help"),
])
@pytest.mark.asyncio
async def test_commands_filter(command: str, cmd_filter: Union[Iterable[Union[str, BotCommand]], str, BotCommand]):
message_with_command = Message(**MESSAGE)
message_with_command.text = command

start_filter = Command(commands=cmd_filter)

assert await start_filter.check(message_with_command)


@pytest.mark.asyncio
async def test_commands_filter_not_checked():
message_with_command = Message(**MESSAGE)
message_with_command.text = "/start"

start_filter = Command(commands=["help", BotCommand("about", "my desc")])

assert not await start_filter.check(message_with_command)

0 comments on commit 4aac637

Please sign in to comment.