Skip to content

Commit

Permalink
Merge pull request #25 from HeySlava/fix-parse-command
Browse files Browse the repository at this point in the history
Delete category with multiple words
  • Loading branch information
HeySlava authored Jan 20, 2024
2 parents 8437db9 + 4b32fa1 commit 3391a29
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
26 changes: 15 additions & 11 deletions bot/handlers/rm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
28 changes: 28 additions & 0 deletions tests/rm_test.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down

0 comments on commit 3391a29

Please sign in to comment.