File size: 2,691 Bytes
b93364a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
"""Test the Argparse Translator."""
from argparse import ArgumentParser
import pytest
from openbb_cli.argparse_translator.argparse_argument import (
ArgparseArgumentGroupModel,
ArgparseArgumentModel,
)
from openbb_cli.argparse_translator.argparse_translator import (
ArgparseTranslator,
)
# pylint: disable=protected-access
def test_custom_argument_action_validation():
"""Test that CustomArgument raises an error for invalid actions."""
with pytest.raises(ValueError) as excinfo:
ArgparseArgumentModel(
name="test",
type=bool,
dest="test",
default=False,
required=True,
action="store",
help="Test argument",
nargs=None,
choices=None,
)
assert 'action must be "store_true"' in str(excinfo.value)
def test_custom_argument_remove_props_on_store_true():
"""Test that CustomArgument removes type, nargs, and choices on store_true."""
argument = ArgparseArgumentModel(
name="verbose",
type=None,
dest="verbose",
default=None,
required=False,
action="store_true",
help="Verbose output",
nargs=None,
choices=None,
)
assert argument.type is None
assert argument.nargs is None
assert argument.choices is None
def test_custom_argument_group():
"""Test the CustomArgumentGroup class."""
args = [
ArgparseArgumentModel(
name="test",
type=int,
dest="test",
default=1,
required=True,
action="store",
help="Test argument",
nargs=None,
choices=None,
)
]
group = ArgparseArgumentGroupModel(name="Test Group", arguments=args)
assert group.name == "Test Group"
assert len(group.arguments) == 1
assert group.arguments[0].name == "test"
def test_argparse_translator_setup():
"""Test the ArgparseTranslator setup."""
def test_function(test_arg: int):
"""A test function."""
return test_arg * 2
translator = ArgparseTranslator(func=test_function)
parser = translator.parser
assert isinstance(parser, ArgumentParser)
assert "--test_arg" in parser._option_string_actions
def test_argparse_translator_execution():
"""Test the ArgparseTranslator execution."""
def test_function(test_arg: int) -> int:
"""A test function."""
return test_arg * 2
translator = ArgparseTranslator(func=test_function)
parsed_args = translator.parser.parse_args(["--test_arg", "3"])
result = translator.execute_func(parsed_args)
assert result == 6
|