From 0a757e07b1469e4eb989658a8bf85b65054059dc Mon Sep 17 00:00:00 2001 From: Andy Mikhaylenko Date: Fri, 13 Oct 2023 21:54:22 +0200 Subject: [PATCH] test: ensure 100% coverage --- src/argh/decorators.py | 2 +- tests/test_assembling.py | 4 +- tests/test_decorators.py | 2 +- tests/test_dto.py | 125 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 tests/test_dto.py diff --git a/src/argh/decorators.py b/src/argh/decorators.py index 37de20b..6c0d845 100644 --- a/src/argh/decorators.py +++ b/src/argh/decorators.py @@ -23,7 +23,7 @@ ATTR_WRAPPED_EXCEPTIONS_PROCESSOR, ) from argh.dto import ParserAddArgumentSpec -from argh.utils import naive_guess_func_arg_name, CliArgToFuncArgGuessingError +from argh.utils import CliArgToFuncArgGuessingError, naive_guess_func_arg_name __all__ = ["aliases", "named", "arg", "wrap_errors", "expects_obj"] diff --git a/tests/test_assembling.py b/tests/test_assembling.py index cc2f7af..5215a39 100644 --- a/tests/test_assembling.py +++ b/tests/test_assembling.py @@ -121,7 +121,9 @@ def func(foo: str) -> str: parser_mock = MagicMock(spec=argparse.ArgumentParser) parser_mock.add_help = False - parser_mock.add_argument.side_effect = argparse.ArgumentError(None, "my hat's on fire!") + parser_mock.add_argument.side_effect = argparse.ArgumentError( + None, "my hat's on fire!" + ) with pytest.raises(argh.AssemblingError): argh.set_default_command(parser_mock, func) diff --git a/tests/test_decorators.py b/tests/test_decorators.py index 08ca7ae..7ac1d84 100644 --- a/tests/test_decorators.py +++ b/tests/test_decorators.py @@ -104,7 +104,7 @@ def test_naive_guess_func_arg_name() -> None: assert naive_guess_func_arg_name(("-x", "--foo", "--bar")) == "foo" with pytest.raises(CliArgToFuncArgGuessingError): - naive_guess_func_arg_name(("-x", '-y')) + naive_guess_func_arg_name(("-x", "-y")) # mixed (errors) with pytest.raises(MixedPositionalAndOptionalArgsError): diff --git a/tests/test_dto.py b/tests/test_dto.py new file mode 100644 index 0000000..6a9f136 --- /dev/null +++ b/tests/test_dto.py @@ -0,0 +1,125 @@ +""" +Unit Tests For the Argument DTO +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +""" +from argh.dto import ParserAddArgumentSpec + + +def test_update_empty_dto() -> None: + + def stub_completer(): + ... + + dto = ParserAddArgumentSpec( + func_arg_name="foo", + cli_arg_names=["-f"], + ) + other_dto = ParserAddArgumentSpec( + func_arg_name="bar", + cli_arg_names=["-f", "--foo"], + is_required=True, + default_value=123, + nargs="+", + other_add_parser_kwargs={"knights": "Ni!"}, + completer=stub_completer, + ) + + dto.update(other_dto) + + assert dto == ParserAddArgumentSpec( + func_arg_name="foo", + cli_arg_names=["-f", "--foo"], + is_required=True, + default_value=123, + nargs="+", + other_add_parser_kwargs={"knights": "Ni!"}, + completer=stub_completer, + ) + + +def test_update_full_dto() -> None: + + def stub_completer_one(): + ... + + def stub_completer_two(): + ... + + dto = ParserAddArgumentSpec( + func_arg_name="foo", + cli_arg_names=["-f"], + nargs="?", + is_required=True, + default_value=123, + other_add_parser_kwargs={"'tis but a": "scratch"}, + completer=stub_completer_one, + ) + other_dto = ParserAddArgumentSpec( + func_arg_name="bar", + cli_arg_names=["-f", "--foo"], + nargs="+", + is_required=False, + default_value=None, + other_add_parser_kwargs={"knights": "Ni!"}, + completer=stub_completer_two, + ) + + dto.update(other_dto) + + assert dto == ParserAddArgumentSpec( + func_arg_name="foo", + cli_arg_names=["-f", "--foo"], + is_required=False, + default_value=None, + nargs="+", + other_add_parser_kwargs={"knights": "Ni!", "'tis but a": "scratch"}, + completer=stub_completer_two, + ) + + +class TestGetAllKwargs: + ... + + +def test_make_from_kwargs_minimal() -> None: + dto = ParserAddArgumentSpec.make_from_kwargs("foo", ["-f", "--foo"], {}) + + assert dto == ParserAddArgumentSpec( + func_arg_name="foo", cli_arg_names=["-f", "--foo"] + ) + + +def test_make_from_kwargs_full() -> None: + dto = ParserAddArgumentSpec.make_from_kwargs( + "foo", + ["-f", "--foo"], + { + "action": "some action", + "nargs": "?", + "default": None, + "type": str, + "choices": [1, 2, 3], + "required": False, + "help": "some help", + "metavar": "FOOOOO", + "dest": "foo_dest", + "some arbitrary key": "and its value", + }, + ) + + assert dto == ParserAddArgumentSpec( + func_arg_name="foo", + cli_arg_names=["-f", "--foo"], + is_required=False, + default_value=None, + nargs="?", + other_add_parser_kwargs={ + "action": "some action", + "type": str, + "choices": [1, 2, 3], + "help": "some help", + "metavar": "FOOOOO", + "dest": "foo_dest", + "some arbitrary key": "and its value", + }, + )