data.Account: is_under accepts multiple arguments.

This commit is contained in:
Brett Smith 2020-03-19 09:47:10 -04:00
parent f1c115de49
commit 9b8563f3f0
2 changed files with 24 additions and 8 deletions

View file

@ -36,18 +36,21 @@ class Account(str):
SEP = bc_account.sep SEP = bc_account.sep
def is_real_asset(self) -> bool: def is_real_asset(self) -> bool:
return ( return bool(
self.is_under('Assets:') self.is_under('Assets:')
and not self.is_under('Assets:PrepaidExpenses') and not self.is_under('Assets:PrepaidExpenses')
and not self.is_under('Assets:PrepaidVacation') and not self.is_under('Assets:PrepaidVacation')
) )
def is_under(self, acct_s: str) -> bool: def is_under(self, *acct_seq: str) -> Optional[str]:
return self.startswith(acct_s) and ( for prefix in acct_seq:
acct_s.endswith(self.SEP) if self.startswith(prefix) and (
or self == acct_s prefix.endswith(self.SEP)
or self[len(acct_s)] == self.SEP or self == prefix
) or self[len(prefix)] == self.SEP
):
return prefix
return None
class PostingMeta(collections.abc.MutableMapping): class PostingMeta(collections.abc.MutableMapping):

View file

@ -29,9 +29,22 @@ from conservancy_beancount import data
('Expenses:Tax:Sales', 'Accrued:', False), ('Expenses:Tax:Sales', 'Accrued:', False),
('Expenses:Tax:Sales', 'Accrued', False), ('Expenses:Tax:Sales', 'Accrued', False),
]) ])
def test_is_under(acct_name, under_arg, expected): def test_is_under_one_arg(acct_name, under_arg, expected):
expected = under_arg if expected else None
assert data.Account(acct_name).is_under(under_arg) == expected assert data.Account(acct_name).is_under(under_arg) == expected
@pytest.mark.parametrize('acct_name,expected', [
('Income:Other', 'Income'),
('UnearnedIncome:Other', 'UnearnedIncome'),
('Accrued:AccountsPayable', None),
('Expenses:General', None),
])
def test_is_under_multi_arg(acct_name, expected):
assert data.Account(acct_name).is_under('Income', 'UnearnedIncome') == expected
if expected:
expected += ':'
assert data.Account(acct_name).is_under('Income:', 'UnearnedIncome:') == expected
@pytest.mark.parametrize('acct_name,expected', [ @pytest.mark.parametrize('acct_name,expected', [
('Accrued:AccountsPayable', False), ('Accrued:AccountsPayable', False),
('Accrued:AccountsReceivable', False), ('Accrued:AccountsReceivable', False),