Skip to content

Commit

Permalink
feat: add command for migrating groups
Browse files Browse the repository at this point in the history
  • Loading branch information
angela-tran committed Apr 4, 2024
1 parent adb8efd commit ee4943a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
18 changes: 18 additions & 0 deletions littlepay/commands/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def groups(args: Namespace = None) -> int:
elif command == "unlink":
for group in groups:
return_code += unlink_product(client, group.id, args.product_id)
elif command == "migrate":
for group in groups:
return_code += migrate_group(client, group.id)

groups = list(groups)
if csv_output and command != "products":
Expand Down Expand Up @@ -141,3 +144,18 @@ def unlink_product(client: Client, group_id: str, product_id: str) -> int:
return_code = RESULT_FAILURE

return return_code


def migrate_group(client: Client, group_id: str) -> 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

return return_code
2 changes: 2 additions & 0 deletions littlepay/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ def _maincmd(name, help):
groups_link = _subcmd(groups_commands, "link", help="Link one or more concession groups to a product")
groups_link.add_argument("product_id", help="The ID of the product to link to")

_subcmd(groups_commands, "migrate", help="Migrate a group from the old Customer Group format to the current format")

groups_products = _subcmd(groups_commands, "products", help="List products for one or more concession groups")
groups_products.add_argument(
"--csv", action="store_true", default=False, help="Output results in simple CSV format", dest="csv"
Expand Down
26 changes: 26 additions & 0 deletions tests/commands/test_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,29 @@ def test_groups_group_command__unlink_HTTPError(mock_client, capfd):
assert "Unlinking group <-> product" in capture.out
assert "Error" in capture.out
assert "Unlinked" not in capture.out


def test_groups_group_command__migrate(mock_client, capfd):
args = Namespace(group_command="migrate")
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 "Migrating group" in capture.out
assert "Migrated" in capture.out


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

args = Namespace(group_command="migrate")
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

0 comments on commit ee4943a

Please sign in to comment.