Skip to content

Commit

Permalink
Consolidate test_arguments code using parametrization
Browse files Browse the repository at this point in the history
  • Loading branch information
AvlWx2014 committed Aug 17, 2021
1 parent 8d747c9 commit f16e5fa
Showing 1 changed file with 24 additions and 65 deletions.
89 changes: 24 additions & 65 deletions tests/shared/test_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,81 +5,40 @@

from pubtools._pulp.arguments import SplitAndExtend

FAKE_OPTION_VALUES = ["value1", "value2", "value3"]


@pytest.fixture
def parser():
return ArgumentParser()


@pytest.fixture(params=[",", ".", "-", "/"])
def delimiter(request):
"""Provide parameterization for testing different delimiters."""
return request.param


def test_split_and_extend_single_delimited_instance(parser, delimiter):
"""Test a single option instance, with all values in a delimited list."""
# test one instance, all delimited using different delimiters
parser.add_argument("--option", type=str, action=SplitAndExtend, split_on=delimiter)

sys.argv = ["command", "--option", delimiter.join(FAKE_OPTION_VALUES)]

args = parser.parse_args()
assert args.option == FAKE_OPTION_VALUES


def test_split_and_extend_multiple_option_instances(parser):
"""Test multiple option instances."""
@pytest.mark.parametrize(
"argv, expected",
[
(["--option", "a"], ["a"]),
(["--option", "a,"], ["a", ""]),
(["--option", "a,b"], ["a", "b"]),
(["--option", "a,b,"], ["a", "b", ""]),
(["--option", ",a,b"], ["", "a", "b"]),
(["--option", "a,,b"], ["a", "", "b"]),
(["--option", "a", "--option", "b"], ["a", "b"]),
(["--option", "a,b", "--option", "c"], ["a", "b", "c"]),
(["--option", "a", "--option", "b,c"], ["a", "b", "c"]),
(["--option", "a,,b", "--option", ",c,"], ["a", "", "b", "", "c", ""]),
],
)
def test_split_and_extend(parser, argv, expected):
"""Test SplitAndExtend argparse Action."""
parser.add_argument("--option", type=str, action=SplitAndExtend)

# test multiple, individual instances
sys.argv = ["command"]
for value in FAKE_OPTION_VALUES:
sys.argv.extend(["--option", value])

sys.argv = ["command"] + argv
args = parser.parse_args()
assert args.option == FAKE_OPTION_VALUES
assert args.option == expected


def test_split_and_extend_multiple_mix_and_match_instance(parser, delimiter):
"""Test multiple option instances, some with delimited lists."""
# test mix-and-match, delimited-and-not, using different delimiters
@pytest.mark.parametrize("delimiter", [",", ".", "-", "/"])
def test_split_and_extend_varying_delimiters(parser, delimiter):
"""Test using different delimiters using a single option instance."""
expected = ["a", "b", "x", "y"]
parser.add_argument("--option", type=str, action=SplitAndExtend, split_on=delimiter)

sys.argv = [
"command",
"--option",
delimiter.join(FAKE_OPTION_VALUES[:-1]),
"--option",
FAKE_OPTION_VALUES[-1],
]

args = parser.parse_args()
assert args.option == FAKE_OPTION_VALUES


def test_split_and_extend_multiple_instances_with_trailing_delimiters(parser):
"""Test multiple instances, each with an extra trailing delimiter."""

parser.add_argument("--option", type=str, action=SplitAndExtend)

# test multiple, individual instances with extra
# delimiters e.g. `--option value1,` or `--option value1,,value2`
sys.argv = [
"command",
"--option",
"value0,",
"--option",
",value1,value2,,",
"--option",
"value3,,value4",
"--option",
",,,value5",
]

expected = ["value{}".format(i) for i in range(6)]

sys.argv = ["command", "--option", delimiter.join(expected)]
args = parser.parse_args()
assert args.option == expected

0 comments on commit f16e5fa

Please sign in to comment.