Skip to content

Commit 4cd2a4f

Browse files
committed
refactor(switch): update arg format and passing
remove unused constant
1 parent 0ae1c93 commit 4cd2a4f

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

littlepay/config.py

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"envs": {ENV_QA: DEFAULT_ENV, ENV_PROD: DEFAULT_ENV},
1919
"participants": {"cst": {ENV_QA: DEFAULT_CREDENTIALS, ENV_PROD: DEFAULT_CREDENTIALS}},
2020
}
21-
CONFIG_TYPES = list(DEFAULT_ACTIVE.keys())
2221

2322

2423
def _ensure_current_exists() -> bool:

littlepay/main.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from littlepay.commands.groups import groups
88
from littlepay.commands.products import products
99
from littlepay.commands.switch import switch
10-
from littlepay.config import CONFIG_TYPES, Config
10+
from littlepay.config import Config
1111

1212

1313
def _subcmd(subparsers: _SubParsersAction, name: str, help: str) -> ArgumentParser:
@@ -113,10 +113,10 @@ def _maincmd(name, help):
113113
products_unlink = _subcmd(products_commands, "unlink", help="Unlink a concession group from one or more products")
114114
products_unlink.add_argument("group_id", help="The ID of the concession group to unlink")
115115

116-
# littlepay switch {env, participant} VALUE
116+
# littlepay switch [[--env VALUE], [--participant VALUE]]
117117
switch_parser = _maincmd("switch", help="Switch the active environment or participant")
118-
switch_parser.add_argument("switch_type", choices=CONFIG_TYPES, help="The type of object to switch", metavar="TYPE")
119-
switch_parser.add_argument("switch_arg", help="The new object value", metavar="VALUE")
118+
switch_parser.add_argument("-e", "--env", help="The environment to switch to")
119+
switch_parser.add_argument("-p", "--participant", help="The participant to switch to")
120120

121121
args = main_parser.parse_args(argv)
122122

@@ -127,7 +127,7 @@ def _maincmd(name, help):
127127
elif args.command == "products":
128128
return products(args)
129129
elif args.command == "switch":
130-
return switch(args.switch_type, args.switch_arg)
130+
return switch(args.env, args.participant)
131131
else:
132132
main_parser.print_help()
133133
return RESULT_FAILURE

tests/test_main.py

+25-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pytest
66

77
from littlepay.commands import RESULT_FAILURE, RESULT_SUCCESS
8-
from littlepay.config import CONFIG_TYPES, Config
8+
from littlepay.config import Config
99
import littlepay.main
1010
from littlepay.main import main, __name__ as MODULE
1111

@@ -270,24 +270,38 @@ def test_main_products_status_unrecognized(mock_commands_products, status_flag):
270270
assert mock_commands_products.call_count == 0
271271

272272

273-
@pytest.mark.parametrize("switch_type", CONFIG_TYPES)
274-
def test_main_switch_recognized_type(mock_commands_switch, switch_type):
275-
result = main(argv=["switch", switch_type, "new_value"])
273+
@pytest.mark.parametrize("switch_arg", ["-e", "--env"])
274+
def test_main_switch_env(mock_commands_switch, switch_arg):
275+
result = main(argv=["switch", switch_arg, "new_value"])
276276

277277
assert result == RESULT_SUCCESS
278-
mock_commands_switch.assert_called_once_with(switch_type, "new_value")
278+
mock_commands_switch.assert_called_once_with("new_value", None)
279279

280280

281-
def test_main_switch_missing_value(mock_commands_switch):
282-
with pytest.raises(SystemExit):
283-
main(argv=["switch", "env"])
281+
@pytest.mark.parametrize("switch_arg", ["-p", "--participant"])
282+
def test_main_switch_participant(mock_commands_switch, switch_arg):
283+
result = main(argv=["switch", switch_arg, "new_value"])
284284

285-
assert mock_commands_switch.call_count == 0
285+
assert result == RESULT_SUCCESS
286+
mock_commands_switch.assert_called_once_with(None, "new_value")
287+
288+
289+
@pytest.mark.parametrize(
290+
"switch_args",
291+
[["-e", "env", "-p", "participant"], ["--env", "env", "--participant", "participant"]],
292+
)
293+
def test_main_switch_both(mock_commands_switch, switch_args):
294+
argv = ["switch"] + switch_args
295+
result = main(argv=argv)
296+
297+
assert result == RESULT_SUCCESS
298+
mock_commands_switch.assert_called_once_with("env", "participant")
286299

287300

288-
def test_main_switch_unrecognized_type(mock_commands_switch):
301+
@pytest.mark.parametrize("switch_type", ["-e", "--env", "-p", "--participant"])
302+
def test_main_switch_missing_value(mock_commands_switch, switch_type):
289303
with pytest.raises(SystemExit):
290-
main(argv=["switch", "unrecognized", "new_value"])
304+
main(argv=["switch", switch_type])
291305

292306
assert mock_commands_switch.call_count == 0
293307

0 commit comments

Comments
 (0)