diff --git a/bot/handlers/rm.py b/bot/handlers/rm.py index 1b08605..eca4ef6 100644 --- a/bot/handlers/rm.py +++ b/bot/handlers/rm.py @@ -11,26 +11,30 @@ router = Router() -def _parse_rm_command(args: str) -> Tuple[str, str] | None: - try: - command_type, category = args.split() - except ValueError: - return None +class WrongRmCommand(ValueError): + pass + + +def _parse_rm_command(args: str | None) -> Tuple[str, str]: + if not args: + raise WrongRmCommand + command_type, _, category = args.partition(' ') + category = category.strip("'").strip('"') if command_type != 'category': - return None - return command_type, category.upper() + raise WrongRmCommand + return command_type, category async def _delete_category(message: Message, session: Session, command: CommandObject): - parsed = _parse_rm_command(command.args) - if not parsed: + try: + _, category = _parse_rm_command(command.args) + except WrongRmCommand: return await message.answer(f'Неверная команда {command.args!r}') - _, category = parsed try: rm_service.rm_empty_category( user_id=message.chat.id, - item_name=category, + item_name=category.upper(), session=session, ) except rm_service.WrongCategoryError: diff --git a/tests/rm_test.py b/tests/rm_test.py index a3636b8..e486329 100644 --- a/tests/rm_test.py +++ b/tests/rm_test.py @@ -1,10 +1,38 @@ import pytest +from handlers.rm import _parse_rm_command +from handlers.rm import WrongRmCommand from services import rm_service from testing.util import fill_in_db from testing.util import get_random_user +@pytest.mark.parametrize( + 'args,command_type,category', + [ + ('category slava', 'category', 'slava'), + ('category slava slava', 'category', 'slava slava'), + ('category SLAVA slava', 'category', 'SLAVA slava'), + ('category "slava"', 'category', 'slava'), + ("category 'slava'", 'category', 'slava'), + ] +) +def test_parse_rm_command(args, command_type, category): + assert _parse_rm_command(args) == (command_type, category) + + +@pytest.mark.parametrize( + 'args', + [ + 'cat slava', + '', + ] +) +def test_parse_rm_command_raise(args): + with pytest.raises(WrongRmCommand): + _parse_rm_command(args) + + def test_raise_wrong_category(db): fill_in_db(session=db, users_number=2, items_number=0) user = get_random_user(db)