accrual: Use EnumArgument for ReportType.

This commit is contained in:
Brett Smith 2021-02-18 15:05:56 -05:00
parent bd1a5fc9de
commit b142b3e521
2 changed files with 5 additions and 34 deletions

View file

@ -623,13 +623,6 @@ class ReportType(enum.Enum):
OUT = OUTGOING
OUTGOINGS = OUTGOING
@classmethod
def by_name(cls, name: str) -> 'ReportType':
try:
return cls[name.upper()]
except KeyError:
raise ValueError(f"unknown report type {name!r}") from None
def filter_search(postings: Iterable[data.Posting],
search_terms: Iterable[cliutil.SearchTerm],
@ -644,12 +637,14 @@ def parse_arguments(arglist: Optional[Sequence[str]]=None) -> argparse.Namespace
parser = argparse.ArgumentParser(prog=PROGNAME)
cliutil.add_version_argument(parser)
cliutil.add_rewrite_rules_argument(parser)
report_type_arg = cliutil.EnumArgument(ReportType)
parser.add_argument(
'--report-type', '-t',
metavar='NAME',
type=ReportType.by_name,
help="""The type of report to generate, one of `aging`, `balance`, or
`outgoing`. If not specified, the default is `aging` when no search terms are
type=report_type_arg.enum_type,
help=f"""The type of report to generate.
Choices are {report_type_arg.choices_str()}.
If not specified, the default is `aging` when no search terms are
given, `outgoing` for search terms that return a single outstanding payable,
and `balance` any other time.
""")

View file

@ -242,30 +242,6 @@ def test_filter_search(accrual_postings, search_terms, expect_count, check_func)
for post in actual:
assert check_func(post)
@pytest.mark.parametrize('arg,expected', [
('aging', accrual.AgingReport),
('balance', accrual.BalanceReport),
('outgoing', accrual.OutgoingReport),
('age', accrual.AgingReport),
('bal', accrual.BalanceReport),
('out', accrual.OutgoingReport),
('outgoings', accrual.OutgoingReport),
])
def test_report_type_by_name(arg, expected):
assert accrual.ReportType.by_name(arg.lower()).value is expected
assert accrual.ReportType.by_name(arg.title()).value is expected
assert accrual.ReportType.by_name(arg.upper()).value is expected
@pytest.mark.parametrize('arg', [
'unknown',
'blance',
'outgong',
])
def test_report_type_by_unknown_name(arg):
# Raising ValueError helps argparse generate good messages.
with pytest.raises(ValueError):
accrual.ReportType.by_name(arg)
@pytest.mark.parametrize('acct_name,invoice,day', testutil.combine_values(
INVOICE_ACCOUNTS,
['FIXME', '', None, *testutil.NON_STRING_METADATA_VALUES],