mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 10:11:52 +00:00
ignore_case fix for aiogram.filters.command.Command() (#1107)
* ignore_case fix * Create 1106.bugfix.rst * better fix, added tests * Update 1106.bugfix.rst * fix, attempt to satisfy the linter * not on purpose, single quotes in tests
This commit is contained in:
parent
fad45c66aa
commit
3428924d63
3 changed files with 32 additions and 2 deletions
1
CHANGES/1106.bugfix.rst
Normal file
1
CHANGES/1106.bugfix.rst
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
Bug fix, which made ignore_case parameter not work in aiogram.filters.command.Command
|
||||||
|
|
@ -87,6 +87,8 @@ class Command(Filter):
|
||||||
"Command filter only supports str, re.Pattern, BotCommand object"
|
"Command filter only supports str, re.Pattern, BotCommand object"
|
||||||
" or their Iterable"
|
" or their Iterable"
|
||||||
)
|
)
|
||||||
|
if ignore_case and isinstance(command, str):
|
||||||
|
command = command.casefold()
|
||||||
items.append(command)
|
items.append(command)
|
||||||
|
|
||||||
if not items:
|
if not items:
|
||||||
|
|
@ -164,7 +166,12 @@ class Command(Filter):
|
||||||
result = allowed_command.match(command.command)
|
result = allowed_command.match(command.command)
|
||||||
if result:
|
if result:
|
||||||
return replace(command, regexp_match=result)
|
return replace(command, regexp_match=result)
|
||||||
elif command.command == allowed_command: # String
|
|
||||||
|
command_name = command.command
|
||||||
|
if self.ignore_case:
|
||||||
|
command_name = command_name.casefold()
|
||||||
|
|
||||||
|
if command_name == allowed_command: # String
|
||||||
return command
|
return command
|
||||||
raise CommandException("Command did not match pattern")
|
raise CommandException("Command did not match pattern")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,23 @@ class TestCommandFilter:
|
||||||
assert cmd.commands[0] == "start"
|
assert cmd.commands[0] == "start"
|
||||||
# assert cmd == Command(commands=["start"])
|
# assert cmd == Command(commands=["start"])
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"commands,checklist",
|
||||||
|
[
|
||||||
|
[("Test1", "tEst2", "teSt3"), ("test1", "test2", "test3")],
|
||||||
|
[("12TeSt", "3t4Est", "5TE6sT"), ("12test", "3t4est", "5te6st")],
|
||||||
|
[[BotCommand(command="Test", description="Test1")], ("test",)],
|
||||||
|
[[BotCommand(command="tEsT", description="Test2")], ("test",)],
|
||||||
|
[(re.compile(r"test(\d+)"), "TeSt"), (re.compile(r"test(\d+)"), "test")],
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_init_casefold(self, commands, checklist):
|
||||||
|
command = Command(*commands, ignore_case=True)
|
||||||
|
assert command.commands == checklist
|
||||||
|
|
||||||
|
command = Command(*commands, ignore_case=False)
|
||||||
|
assert command.commands != checklist
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"text,command,result",
|
"text,command,result",
|
||||||
[
|
[
|
||||||
|
|
@ -72,10 +89,15 @@ class TestCommandFilter:
|
||||||
["/start test", CommandStart(deep_link=True), True],
|
["/start test", CommandStart(deep_link=True), True],
|
||||||
["/start test", CommandStart(deep_link=True, deep_link_encoded=True), False],
|
["/start test", CommandStart(deep_link=True, deep_link_encoded=True), False],
|
||||||
["/start dGVzdA", CommandStart(deep_link=True, deep_link_encoded=True), True],
|
["/start dGVzdA", CommandStart(deep_link=True, deep_link_encoded=True), True],
|
||||||
|
["/TeSt", Command("test", ignore_case=True), True],
|
||||||
|
["/TeSt", Command("TeSt", ignore_case=True), True],
|
||||||
|
["/test", Command("TeSt", ignore_case=True), True],
|
||||||
|
["/TeSt", Command("test", ignore_case=False), False],
|
||||||
|
["/test", Command("TeSt", ignore_case=False), False],
|
||||||
|
["/TeSt", Command("TeSt", ignore_case=False), True],
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
async def test_parse_command(self, bot: MockedBot, text: str, result: bool, command: Command):
|
async def test_parse_command(self, bot: MockedBot, text: str, result: bool, command: Command):
|
||||||
# TODO: test ignore case
|
|
||||||
# TODO: test ignore mention
|
# TODO: test ignore mention
|
||||||
|
|
||||||
message = Message(
|
message = Message(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue