From e3394c169345575da704e06c223fa789e3b693e7 Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Thu, 30 Mar 2023 12:31:04 +0200 Subject: [PATCH] Extract fetching list ID Also don't check if account is found, that function alredy raises a ConsoleError. --- tests/integration/test_lists.py | 17 ++++++++++++++- toot/commands.py | 37 ++++++++++++--------------------- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/tests/integration/test_lists.py b/tests/integration/test_lists.py index c9fd392..dc2e3f5 100644 --- a/tests/integration/test_lists.py +++ b/tests/integration/test_lists.py @@ -1,6 +1,7 @@ -from toot import api +import pytest from tests.integration.conftest import register_account +from toot.exceptions import ConsoleError def test_lists_empty(run): @@ -35,6 +36,10 @@ def test_list_create_delete(run): out = run("lists") assert out == "You have no lists defined." + with pytest.raises(ConsoleError) as exc: + run("list_delete", "mango") + assert str(exc.value) == "List not found" + def test_list_add_remove(run, app): acc = register_account(app) @@ -51,6 +56,16 @@ def test_list_add_remove(run, app): out = run("list_accounts", "foo") assert acc.username in out + # Account doesn't exist + with pytest.raises(ConsoleError) as exc: + run("list_add", "foo", "does_not_exist") + assert str(exc.value) == "Account not found" + + # List doesn't exist + with pytest.raises(ConsoleError) as exc: + run("list_add", "does_not_exist", acc.username) + assert str(exc.value) == "List not found" + out = run("list_remove", "foo", acc.username) assert out == f'✓ Removed account "{acc.username}"' diff --git a/toot/commands.py b/toot/commands.py index ce34e58..b17cf72 100644 --- a/toot/commands.py +++ b/toot/commands.py @@ -434,11 +434,7 @@ def lists(app, user, args): def list_accounts(app, user, args): - list_id = args.id if args.id else api.find_list_id(app, user, args.title) - if not list_id: - print_out("List not found") - return - + list_id = _get_list_id(app, user, args) response = api.get_list_accounts(app, user, list_id) print_list_accounts(response) @@ -449,24 +445,15 @@ def list_create(app, user, args): def list_delete(app, user, args): - list_id = args.id if args.id else api.find_list_id(app, user, args.title) - if not list_id: - print_out("List not found") - return - + list_id = _get_list_id(app, user, args) api.delete_list(app, user, list_id) print_out(f"✓ List \"{args.title if args.title else args.id}\" deleted.") def list_add(app, user, args): - list_id = args.id if args.id else api.find_list_id(app, user, args.title) - if not list_id: - print_out("List not found") - return + list_id = _get_list_id(app, user, args) account = find_account(app, user, args.account) - if not account: - print_out("Account not found") - return + try: api.add_accounts_to_list(app, user, list_id, [account['id']]) except Exception as ex: @@ -484,22 +471,24 @@ def list_add(app, user, args): else: print_out(f"{ex}") return + print_out(f"✓ Added account \"{args.account}\"") def list_remove(app, user, args): - list_id = args.id if args.id else api.find_list_id(app, user, args.title) - if not list_id: - print_out("List not found") - return + list_id = _get_list_id(app, user, args) account = find_account(app, user, args.account) - if not account: - print_out("Account not found") - return api.remove_accounts_from_list(app, user, list_id, [account['id']]) print_out(f"✓ Removed account \"{args.account}\"") +def _get_list_id(app, user, args): + list_id = args.id or api.find_list_id(app, user, args.title) + if not list_id: + raise ConsoleError("List not found") + return list_id + + def mute(app, user, args): account = find_account(app, user, args.account) api.mute(app, user, account['id'])