Changeset - 2d49f7dfbcf4
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-03-30 19:01:25
brettcsmith@brettcsmith.org
data: Add Account.is_checking() method.
2 files changed with 22 insertions and 0 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/data.py
Show inline comments
...
 
@@ -49,32 +49,35 @@ LINK_METADATA = frozenset([
 
    'contract',
 
    'invoice',
 
    'purchase-order',
 
    'receipt',
 
    'statement',
 
])
 

	
 
class Account(str):
 
    """Account name string
 

	
 
    This is a string that names an account, like Accrued:AccountsPayable
 
    or Income:Donations. This class provides additional methods for common
 
    account name parsing and queries.
 
    """
 
    SEP = bc_account.sep
 

	
 
    def is_checking(self) -> bool:
 
        return self.is_real_asset() and ':Check' in self
 

	
 
    def is_income(self) -> bool:
 
        return self.is_under('Income:', 'UnearnedIncome:') is not None
 

	
 
    def is_real_asset(self) -> bool:
 
        return bool(
 
            self.is_under('Assets:')
 
            and not self.is_under('Assets:PrepaidExpenses', 'Assets:PrepaidVacation')
 
        )
 

	
 
    def is_under(self, *acct_seq: str) -> Optional[str]:
 
        """Return a match if this account is "under" a part of the hierarchy
 

	
 
        Pass in any number of account name strings as arguments. If this
 
        account is under one of those strings in the account hierarchy, the
 
        first matching string will be returned. Otherwise, None is returned.
 

	
tests/test_data_account.py
Show inline comments
...
 
@@ -58,16 +58,35 @@ def test_is_under_multi_arg(acct_name, expected):
 
def test_is_income(acct_name, expected):
 
    assert data.Account(acct_name).is_income() == expected
 

	
 
@pytest.mark.parametrize('acct_name,expected', [
 
    ('Accrued:AccountsPayable', False),
 
    ('Accrued:AccountsReceivable', False),
 
    ('Assets:Cash', True),
 
    ('Assets:Cash:EUR', True),
 
    ('Assets:PrepaidExpenses', False),
 
    ('Assets:PrepaidVacation', False),
 
    ('Assets:Bank:Checking', True),
 
    ('Expenses:General', False),
 
    ('Income:Donations', False),
 
])
 
def test_is_real_asset(acct_name, expected):
 
    assert data.Account(acct_name).is_real_asset() == expected
 

	
 
@pytest.mark.parametrize('acct_name,expected', [
 
    ('Accrued:AccountsPayable', False),
 
    ('Accrued:AccountsReceivable', False),
 
    ('Assets:Bank:Check9999', True),
 
    ('Assets:Bank:CheckCard', True),
 
    ('Assets:Bank:Checking', True),
 
    ('Assets:Bank:Savings', False),
 
    ('Assets:Cash', False),
 
    ('Assets:Check9999', True),
 
    ('Assets:CheckCard', True),
 
    ('Assets:Checking', True),
 
    ('Assets:PrepaidExpenses', False),
 
    ('Assets:Savings', False),
 
    ('Expenses:CheckingFees', False),
 
    ('Income:Interest:Checking', False),
 
])
 
def test_is_checking(acct_name, expected):
 
    assert data.Account(acct_name).is_checking() == expected
0 comments (0 inline, 0 general)