Skip to content

Commit

Permalink
feat(migrate): use force flag in implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
angela-tran committed Apr 5, 2024
1 parent 056b8f3 commit 4c691b1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 12 deletions.
28 changes: 20 additions & 8 deletions littlepay/commands/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def groups(args: Namespace = None) -> int:
return_code += unlink_product(client, group.id, args.product_id)
elif command == "migrate":
for group in groups:
return_code += migrate_group(client, group.id)
return_code += migrate_group(client, group.id, getattr(args, "force", False))

groups = list(groups)
if csv_output and command != "products":
Expand Down Expand Up @@ -146,16 +146,28 @@ def unlink_product(client: Client, group_id: str, product_id: str) -> int:
return return_code


def migrate_group(client: Client, group_id: str) -> int:
def migrate_group(client: Client, group_id: str, force: bool = False) -> int:
config = Config()
print_active_message(config, "Migrating group", f"[{group_id}]")
return_code = RESULT_SUCCESS

try:
client.migrate_concession_group(group_id)
print("✅ Migrated")
except HTTPError as err:
print(f"❌ Error: {err}")
return_code = RESULT_FAILURE
if force is True:
confirm = "yes"
else:
try:
confirm = input("❔ Are you sure? (yes/no): ")
except EOFError:
confirm = "no"

if confirm.lower().startswith("y"):
print("Migrating group...")
try:
client.migrate_concession_group(group_id)
print("✅ Migrated")
except HTTPError as err:
print(f"❌ Error: {err}")
return_code = RESULT_FAILURE
else:
print("Canceled...")

return return_code
58 changes: 54 additions & 4 deletions tests/commands/test_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,10 @@ def test_groups_group_command__unlink_HTTPError(mock_client, capfd):
assert "Unlinked" not in capture.out


def test_groups_group_command__migrate(mock_client, capfd):
@pytest.mark.parametrize("sample_input", ["y", "Y", "yes", "Yes", "YES"])
def test_groups_group_command__migrate_confirm(mock_client, capfd, mock_input, sample_input):
mock_input(sample_input)

args = Namespace(group_command="migrate")
res = groups(args)
capture = capfd.readouterr()
Expand All @@ -309,16 +312,63 @@ def test_groups_group_command__migrate(mock_client, capfd):
assert res == RESULT_SUCCESS
assert "Migrating group" in capture.out
assert "Migrated" in capture.out
assert "Matching groups" in capture.out


def test_groups_group_command__migrate_HTTPError(mock_client, capfd):
mock_client.migrate_concession_group.side_effect = HTTPError
def test_groups_group_command__migrate_confirm_error(capfd, mock_input):
_input = mock_input(None)
_input.side_effect = EOFError

args = Namespace(group_command="migrate")
res = groups(args)
capture = capfd.readouterr()

assert res == RESULT_SUCCESS
assert "Migrating group" in capture.out
assert "Canceled" in capture.out
assert "Matching groups" in capture.out


@pytest.mark.parametrize("sample_input", ["n", "N", "no", "No", "NO"])
def test_groups_group_command__migrate_decline(capfd, mock_input, sample_input):
mock_input(sample_input)

args = Namespace(group_command="migrate")
res = groups(args)
capture = capfd.readouterr()

assert res == RESULT_SUCCESS
assert "Migrating group" in capture.out
assert "Canceled" in capture.out
assert "Matching groups" in capture.out


def test_groups_group_command__migrate_force(mock_client, capfd, mock_input):
_input = mock_input("no")

args = Namespace(group_command="migrate", force=True)
res = groups(args)
capture = capfd.readouterr()

for group in GROUP_RESPONSES:
mock_client.migrate_concession_group.assert_any_call(group.id)

assert res == RESULT_SUCCESS
assert _input.called is False
assert "Migrating group" in capture.out
assert "Migrated" in capture.out
assert "Matching groups" in capture.out


def test_groups_group_command__migrate_HTTPError(mock_client, capfd, mock_input):
mock_client.migrate_concession_group.side_effect = HTTPError
mock_input("y")

args = Namespace(group_command="migrate", group_id="1234")
res = groups(args)
capture = capfd.readouterr()

assert res == RESULT_FAILURE
assert "Migrating group" in capture.out
assert "Error" in capture.out
assert "Migrated" not in capture.out
assert "Matching groups" in capture.out

0 comments on commit 4c691b1

Please sign in to comment.