accrual: Use EnumArgument for ReportType.
This commit is contained in:
		
							parent
							
								
									bd1a5fc9de
								
							
						
					
					
						commit
						b142b3e521
					
				
					 2 changed files with 5 additions and 34 deletions
				
			
		| 
						 | 
					@ -623,13 +623,6 @@ class ReportType(enum.Enum):
 | 
				
			||||||
    OUT = OUTGOING
 | 
					    OUT = OUTGOING
 | 
				
			||||||
    OUTGOINGS = 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],
 | 
					def filter_search(postings: Iterable[data.Posting],
 | 
				
			||||||
                  search_terms: Iterable[cliutil.SearchTerm],
 | 
					                  search_terms: Iterable[cliutil.SearchTerm],
 | 
				
			||||||
| 
						 | 
					@ -644,12 +637,14 @@ def parse_arguments(arglist: Optional[Sequence[str]]=None) -> argparse.Namespace
 | 
				
			||||||
    parser = argparse.ArgumentParser(prog=PROGNAME)
 | 
					    parser = argparse.ArgumentParser(prog=PROGNAME)
 | 
				
			||||||
    cliutil.add_version_argument(parser)
 | 
					    cliutil.add_version_argument(parser)
 | 
				
			||||||
    cliutil.add_rewrite_rules_argument(parser)
 | 
					    cliutil.add_rewrite_rules_argument(parser)
 | 
				
			||||||
 | 
					    report_type_arg = cliutil.EnumArgument(ReportType)
 | 
				
			||||||
    parser.add_argument(
 | 
					    parser.add_argument(
 | 
				
			||||||
        '--report-type', '-t',
 | 
					        '--report-type', '-t',
 | 
				
			||||||
        metavar='NAME',
 | 
					        metavar='NAME',
 | 
				
			||||||
        type=ReportType.by_name,
 | 
					        type=report_type_arg.enum_type,
 | 
				
			||||||
        help="""The type of report to generate, one of `aging`, `balance`, or
 | 
					        help=f"""The type of report to generate.
 | 
				
			||||||
`outgoing`. If not specified, the default is `aging` when no search terms are
 | 
					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,
 | 
					given, `outgoing` for search terms that return a single outstanding payable,
 | 
				
			||||||
and `balance` any other time.
 | 
					and `balance` any other time.
 | 
				
			||||||
""")
 | 
					""")
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -242,30 +242,6 @@ def test_filter_search(accrual_postings, search_terms, expect_count, check_func)
 | 
				
			||||||
    for post in actual:
 | 
					    for post in actual:
 | 
				
			||||||
        assert check_func(post)
 | 
					        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(
 | 
					@pytest.mark.parametrize('acct_name,invoice,day', testutil.combine_values(
 | 
				
			||||||
    INVOICE_ACCOUNTS,
 | 
					    INVOICE_ACCOUNTS,
 | 
				
			||||||
    ['FIXME', '', None, *testutil.NON_STRING_METADATA_VALUES],
 | 
					    ['FIXME', '', None, *testutil.NON_STRING_METADATA_VALUES],
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue