def fields_with_expected_tokens() -> FieldsWithTokens: fields = [ FieldFactory(name="name_1", value=Argument(cli_names=True, env_name=False, help="help 1")), FieldFactory(name="name_2", value=Argument(cli_names=False, env_name=True, help="help 2")), FieldFactory(name="name_3", value=Argument(cli_names=True, env_name=True, help="help 3")), FieldFactory(name="name_4", value=Argument(cli_names=["-a", "--arg"], env_name="ARG", help="help 4")), ] expected_tokens = [ ["--name-1", "help 1"], ["NAME_2", "help 2"], ["--name-3", "NAME_3", "help 3"], ["-a", "--arg", "ARG", "help 4"], ] return fields, expected_tokens
class Args(RocketBase): arg_1: str = Argument(cli_names=["-a1", "--my-arg-1"], env_name="MY_ARG_1", help="First argument.") arg_2: str = Argument(cli_names=["-a2", "--my-arg-2"], env_name="MY_ARG_2", help="Second argument.")
def test_provided_arguments_are_correctly_parsed() -> None: fields_data = [ Field(name="name_1", type=str, value=Argument(env_name=True)), Field(name="name_2", type=int, value=Argument(env_name="ARG")), ] with patch_env_args(NAME_1="abcd", ARG="1234"): parsed_args = get_env_args(fields_data) expected_args = {"name_1": "abcd", "name_2": "1234"} assert parsed_args == expected_args
def test_provided_arguments_are_correctly_parsed() -> None: fields_data = [ Field(name="name_1", type=str, value=Argument(cli_names=True)), Field(name="name_2", type=int, value=Argument(cli_names=["--arg"])), Field(name="name_3", type=float, value=Argument(cli_names=["-a"])), ] cli_args = ["--name-1", "abcd", "--arg", "1234", "-a", "12.34"] with patch_cli_args(cli_args): parsed_args = get_cmd_line_args(fields_data) expected_args = {"name_1": "abcd", "name_2": "1234", "name_3": "12.34"} assert parsed_args == expected_args
class FieldFactory(Factory): class Meta: model = Field name: str = Sequence(lambda i: f"name_{i}") type: Any = str value: Argument = Argument()
def test_turned_off_arguments_arent_gathered_from_command_line() -> None: fields_data = [ FieldFactory(name="name", value=Argument(cli_names=False)) ] cli_args = ["--name", "abcd"] with patch_cli_args(cli_args), pytest.raises(SystemExit): get_cmd_line_args(fields_data)
def test_turned_off_arguments_arent_gathered_from_env() -> None: fields_data = [ FieldFactory(name="name", value=Argument(env_name=False)) ] with patch_env_args(NAME="abcd"): parsed_args = get_env_args(fields_data) assert parsed_args == {}
def test_returns_custom_names() -> None: expected_cli_names = ["-a", "--arg"] field_data = FieldFactory(value=Argument( cli_names=expected_cli_names)) assert field_data.cli_names == expected_cli_names
class Args(RocketBase): arg_int: int = Argument(cli_names=False) arg_str: str = Argument(cli_names=False) arg_float: float = Argument(cli_names=False)
class Args(RocketBase): arg: str = Argument(cli_names=["-a", "--arg"])
class Args(RocketBase): arg_1: str = Argument(default="default_value") arg_2: str = Argument(default="default_value") arg_3: str = Argument(default="default_value")
class Args(RocketBase): arg_int: int = Argument(env_name="MY_ARG_INT") arg_float: float = Argument(env_name="MY_ARG_FLOAT") arg_str: str = Argument(env_name="MY_ARG_STR")
def test_returns_none_if_cli_names_are_turned_off() -> None: field_data = FieldFactory(value=Argument(cli_names=False)) assert field_data.cli_names is None
class Args(RocketBase): arg_1: str = Argument( cli_names=["-a1", "--arg-1", "----long-arg-1"]) arg_2: str = Argument( cli_names=["-a2", "--arg-2", "----long-arg-2"])
class Args(RocketBase): arg_int: int = Argument(env_name=False, default=1234) arg_str: str = Argument(env_name=False, default="abcd") arg_float: float = Argument(env_name=False, default=12.34)
def test_generates_name_based_on_field() -> None: field_data = FieldFactory(name="field_name", value=Argument(cli_names=True)) assert field_data.cli_names == ["--field-name"]
def test_returns_custom_name() -> None: expected_name = "ARG" field_data = FieldFactory(value=Argument(env_name=expected_name)) assert field_data.env_name == expected_name
def test_generates_name_based_on_field() -> None: field_data = FieldFactory(name="field_name", value=Argument(env_name=True)) assert field_data.env_name == "FIELD_NAME"
def test_returns_none_if_env_name_is_turned_off() -> None: field_data = FieldFactory(value=Argument(env_name=False)) assert field_data.env_name is None