From 3428924d63903ad4b4d3ca9986d4b97d0a4c2242 Mon Sep 17 00:00:00 2001 From: Alex <103587954+Shaonis@users.noreply.github.com> Date: Sat, 4 Feb 2023 19:52:36 +0200 Subject: [PATCH] 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 --- CHANGES/1106.bugfix.rst | 1 + aiogram/filters/command.py | 9 ++++++++- tests/test_filters/test_command.py | 24 +++++++++++++++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 CHANGES/1106.bugfix.rst diff --git a/CHANGES/1106.bugfix.rst b/CHANGES/1106.bugfix.rst new file mode 100644 index 00000000..c3b7154e --- /dev/null +++ b/CHANGES/1106.bugfix.rst @@ -0,0 +1 @@ +Bug fix, which made ignore_case parameter not work in aiogram.filters.command.Command diff --git a/aiogram/filters/command.py b/aiogram/filters/command.py index b590ba67..13997073 100644 --- a/aiogram/filters/command.py +++ b/aiogram/filters/command.py @@ -87,6 +87,8 @@ class Command(Filter): "Command filter only supports str, re.Pattern, BotCommand object" " or their Iterable" ) + if ignore_case and isinstance(command, str): + command = command.casefold() items.append(command) if not items: @@ -164,7 +166,12 @@ class Command(Filter): result = allowed_command.match(command.command) if 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 raise CommandException("Command did not match pattern") diff --git a/tests/test_filters/test_command.py b/tests/test_filters/test_command.py index 1bb7d0a4..ec79098a 100644 --- a/tests/test_filters/test_command.py +++ b/tests/test_filters/test_command.py @@ -35,6 +35,23 @@ class TestCommandFilter: assert cmd.commands[0] == "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( "text,command,result", [ @@ -72,10 +89,15 @@ class TestCommandFilter: ["/start test", CommandStart(deep_link=True), True], ["/start test", CommandStart(deep_link=True, deep_link_encoded=True), False], ["/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): - # TODO: test ignore case # TODO: test ignore mention message = Message(