Changeset - c712105bed3c
[Not reviewed]
0 18 0
Brett Smith - 4 years ago 2020-04-03 14:34:10
brettcsmith@brettcsmith.org
Revise chart of accounts used throughout.

The main impetus of this change is to rename accounts that were outside
Beancount's accepted five root accounts, to move them into that
structure. This includes:

Accrued:*Payable: → Liabilities:Payable:*
Accrued:*Receivable: → Assets:Receivable:*
UneanedIncome:* → Liabilities:UnearnedIncome:*

Note the last change did inspire in a change to our validation rules. We no
longer require income-type on unearned income, because it's no longer
considered income at all. Once it's earned and converted to an Income
account, that has an income-type of course.

This did inspire another rename that was not required, but
provided more consistency with the other account names above:

Assets:Prepaid* → Assets:Prepaid:*

Where applicable, I have generally extended tests to make sure one of each
of the five account types is tested. (This mostly meant adding an Equity
account to the tests.) I also added tests for key parts of the hierarchy,
like Assets:Receivable and Liabilities:Payable, where applicable.

As part of this change, Account.is_real_asset() got renamed to
Account.is_cash_equivalent(), to better self-document its purpose.
18 files changed with 155 insertions and 114 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/data.py
Show inline comments
...
 
@@ -49,43 +49,43 @@ LINK_METADATA = frozenset([
 
    'approval',
 
    'check',
 
    'contract',
 
    'invoice',
 
    'purchase-order',
 
    'receipt',
 
    'statement',
 
])
 

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

	
 
    This is a string that names an account, like Accrued:AccountsPayable
 
    This is a string that names an account, like Assets:Bank:Checking
 
    or Income:Donations. This class provides additional methods for common
 
    account name parsing and queries.
 
    """
 
    __slots__ = ()
 

	
 
    SEP = bc_account.sep
 

	
 
    def is_checking(self) -> bool:
 
        return self.is_real_asset() and ':Check' in self
 
    def is_cash_equivalent(self) -> bool:
 
        return (
 
            self.is_under('Assets:') is not None
 
            and self.is_under('Assets:Prepaid', 'Assets:Receivable') is None
 
        )
 

	
 
    def is_income(self) -> bool:
 
        return self.is_under('Income:', 'UnearnedIncome:') is not None
 
    def is_checking(self) -> bool:
 
        return self.is_cash_equivalent() and ':Check' in self
 

	
 
    def is_real_asset(self) -> bool:
 
        return bool(
 
            self.is_under('Assets:')
 
            and not self.is_under('Assets:PrepaidExpenses', 'Assets:PrepaidVacation')
 
        )
 
    def is_credit_card(self) -> bool:
 
        return self.is_under('Liabilities:CreditCard') is not None
 

	
 
    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.
 

	
 
        You can use the return value of this method as a boolean if you don't
 
        care which account string is matched.
 

	
 
        An account is considered to be under itself:
...
 
@@ -239,24 +239,24 @@ class Posting(BasePosting):
 
        return self._compare_amount(operator.pos, threshold, default)
 

	
 
    def is_debit(self,
 
                  threshold: DecimalCompat=0,
 
                  default: Optional[bool]=None,
 
    ) -> Optional[bool]:
 
        return self._compare_amount(operator.neg, threshold, default)
 

	
 
    def is_payment(self,
 
                  threshold: DecimalCompat=0,
 
                  default: Optional[bool]=None,
 
    ) -> Optional[bool]:
 
        return self.account.is_real_asset() and self.is_debit(threshold, default)
 
        return self.account.is_cash_equivalent() and self.is_debit(threshold, default)
 

	
 

	
 
def iter_postings(txn: Transaction) -> Iterator[Posting]:
 
    """Yield an enhanced Posting object for every posting in the transaction"""
 
    for index, source in enumerate(txn.postings):
 
        yield Posting(
 
            Account(source.account),
 
            *source[1:5],
 
            # see rationale above about Posting.meta
 
            PostingMeta(txn, index, source), # type:ignore[arg-type]
 
        )
conservancy_beancount/plugin/meta_entity.py
Show inline comments
...
 
@@ -50,21 +50,26 @@ class MetaEntity(core.TransactionHook):
 

	
 
    def run(self, txn: Transaction) -> errormod.Iter:
 
        txn_entity = txn.meta.get(self.METADATA_KEY)
 
        if txn_entity is None:
 
            txn_entity_ok = None
 
        elif isinstance(txn_entity, str):
 
            txn_entity_ok = bool(self.ENTITY_RE.match(txn_entity))
 
        else:
 
            txn_entity_ok = False
 
        if txn_entity_ok is False:
 
            yield errormod.InvalidMetadataError(txn, self.METADATA_KEY, txn_entity)
 
        for post in data.iter_postings(txn):
 
            if post.account.is_under('Assets', 'Equity', 'Liabilities'):
 
            if not post.account.is_under(
 
                    'Assets:Receivable',
 
                    'Expenses',
 
                    'Income',
 
                    'Liabilities:Payable',
 
            ):
 
                continue
 
            entity = post.meta.get(self.METADATA_KEY)
 
            if entity is None:
 
                yield errormod.InvalidMetadataError(txn, self.METADATA_KEY, entity, post)
 
            elif entity is txn_entity:
 
                pass
 
            elif not self.ENTITY_RE.match(entity):
 
                yield errormod.InvalidMetadataError(txn, self.METADATA_KEY, entity, post)
conservancy_beancount/plugin/meta_income_type.py
Show inline comments
...
 
@@ -21,32 +21,32 @@ from ..beancount_types import (
 
    MetaValueEnum,
 
    Transaction,
 
)
 

	
 
class MetaIncomeType(core._NormalizePostingMetadataHook):
 
    VALUES_ENUM = core.MetadataEnum('income-type', {
 
        'Donations',
 
        'Payable-Derecognition',
 
        'RBI',
 
        'UBTI',
 
    })
 
    DEFAULT_VALUES = {
 
        'Income:Conferences:Registrations': 'RBI',
 
        'Income:Conferences:Sponsorship': 'RBI',
 
        'Income:Donations': 'Donations',
 
        'Income:Honoraria': 'RBI',
 
        'Income:Interest': 'RBI',
 
        'Income:Interest:Dividend': 'RBI',
 
        'Income:Royalties': 'RBI',
 
        'Income:Sales': 'RBI',
 
        'Income:SoftwareDevelopment': 'RBI',
 
        'Income:TrademarkLicensing': 'RBI',
 
        'UnearnedIncome:Conferences:Registrations': 'RBI',
 
        'UnearnedIncome:MatchPledges': 'Donations',
 
    }
 

	
 
    def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool:
 
        return post.account.is_income()
 
        return post.account.is_under('Income') is not None
 

	
 
    def _default_value(self, txn: Transaction, post: data.Posting) -> MetaValueEnum:
 
        try:
 
            return self.DEFAULT_VALUES[post.account]
 
        except KeyError:
 
            raise errormod.InvalidMetadataError(txn, self.METADATA_KEY, None, post) from None
conservancy_beancount/plugin/meta_invoice.py
Show inline comments
...
 
@@ -17,13 +17,16 @@
 
from . import core
 
from .. import data
 
from .. import errors as errormod
 
from ..beancount_types import (
 
    MetaValueEnum,
 
    Transaction,
 
)
 

	
 
class MetaInvoice(core._RequireLinksPostingMetadataHook):
 
    METADATA_KEY = 'invoice'
 

	
 
    def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool:
 
        return post.account.is_under('Accrued') is not None
 
        return post.account.is_under(
 
            'Assets:Receivable',
 
            'Liabilities:Payable',
 
        ) is not None
conservancy_beancount/plugin/meta_project.py
Show inline comments
...
 
@@ -70,22 +70,29 @@ class MetaProject(core._NormalizePostingMetadataHook):
 
            self.VALUES_ENUM = core.MetadataEnum(self.METADATA_KEY, names, aliases)
 

	
 
    def _config_error(self, msg: str, filename: Optional[Path]=None) -> NoReturn:
 
        source = {}
 
        if filename is not None:
 
            source['filename'] = str(filename)
 
        raise errormod.ConfigurationError(
 
            "cannot load project data: " + msg,
 
            source=source,
 
        )
 

	
 
    def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool:
 
        return post.account.is_under('Assets', 'Equity', 'Liabilities') is None
 
        if post.account.is_under('Liabilities'):
 
            return not post.account.is_credit_card()
 
        else:
 
            return post.account.is_under(
 
                'Assets:Receivable',
 
                'Expenses',
 
                'Income',
 
            ) is not None
 

	
 
    def _default_value(self, txn: Transaction, post: data.Posting) -> MetaValueEnum:
 
        if post.account.is_under(
 
                'Accrued:VacationPayable',
 
                'Expenses:Payroll',
 
                'Liabilities:Payable:Vacation',
 
        ):
 
            return self.DEFAULT_PROJECT
 
        else:
 
            raise errormod.InvalidMetadataError(txn, self.METADATA_KEY, None, post)
conservancy_beancount/plugin/meta_receipt.py
Show inline comments
...
 
@@ -20,48 +20,48 @@ from .. import data
 
from .. import errors as errormod
 
from ..beancount_types import (
 
    Transaction,
 
)
 

	
 
class MetaReceipt(core._RequireLinksPostingMetadataHook):
 
    METADATA_KEY = 'receipt'
 

	
 
    def __init__(self, config: configmod.Config) -> None:
 
        self.payment_threshold = abs(config.payment_threshold())
 

	
 
    def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool:
 
        return bool(
 
            (post.account.is_real_asset() or post.account.is_under('Liabilities'))
 
        return (
 
            (post.account.is_cash_equivalent() or post.account.is_credit_card())
 
            and post.units.number is not None
 
            and abs(post.units.number) >= self.payment_threshold
 
        )
 

	
 
    def post_run(self, txn: Transaction, post: data.Posting) -> errormod.Iter:
 
        try:
 
            self._check_links(txn, post, self.METADATA_KEY)
 
        except errormod.InvalidMetadataError as error:
 
            receipt_error = error
 
        else:
 
            return
 

	
 
        if not post.units.number:
 
            post_amount = 0
 
        elif post.units.number > 0:
 
            post_amount = 1
 
        else:
 
            post_amount = -1
 

	
 
        if post.account.is_checking():
 
            fallback_key = 'check'
 
        elif post.account.is_under('Liabilities:CreditCard') and post_amount == -1:
 
        elif post.account.is_credit_card() and post_amount == -1:
 
            fallback_key = 'invoice'
 
        elif post.account.is_under('Assets:PayPal') and post_amount == 1:
 
            fallback_key = 'paypal-id'
 
        else:
 
            yield receipt_error
 
            return
 

	
 
        try:
 
            self._check_links(txn, post, fallback_key)
 
        except errormod.InvalidMetadataError as fallback_error:
 
            if receipt_error.value is None and fallback_error.value is None:
 
                yield errormod.InvalidMetadataError(
conservancy_beancount/plugin/meta_receivable_documentation.py
Show inline comments
...
 
@@ -48,25 +48,25 @@ class MetaReceivableDocumentation(core._RequireLinksPostingMetadataHook):
 
    )
 

	
 
    def __init__(self, config: configmod.Config) -> None:
 
        rt_wrapper = config.rt_wrapper()
 
        # In principle, we could still check for non-RT invoices and enforce
 
        # checks on them without an RT wrapper. In practice, that would
 
        # provide so little utility today it's not worth bothering with.
 
        if rt_wrapper is None:
 
            raise errormod.ConfigurationError("can't log in to RT")
 
        self.rt = rt_wrapper
 

	
 
    def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool:
 
        if not post.account.is_under('Accrued:AccountsReceivable'):
 
        if not post.account.is_under('Assets:Receivable'):
 
            return False
 

	
 
        # Get the first invoice, or return False if it doesn't exist.
 
        try:
 
            invoice_link = post.meta.get_links('invoice')[0]
 
        except (IndexError, TypeError):
 
            return False
 

	
 
        # Get the filename, following an RT link if necessary.
 
        rt_args = self.rt.parse(invoice_link)
 
        if rt_args is not None:
 
            ticket_id, attachment_id = rt_args
tests/test_data_account.py
Show inline comments
...
 
@@ -17,76 +17,84 @@
 
import pytest
 

	
 
from conservancy_beancount import data
 

	
 
@pytest.mark.parametrize('acct_name,under_arg,expected', [
 
    ('Expenses:Tax:Sales', 'Expenses:Tax:Sales:', False),
 
    ('Expenses:Tax:Sales', 'Expenses:Tax:Sales', True),
 
    ('Expenses:Tax:Sales', 'Expenses:Tax:', True),
 
    ('Expenses:Tax:Sales', 'Expenses:Tax', True),
 
    ('Expenses:Tax:Sales', 'Expenses:', True),
 
    ('Expenses:Tax:Sales', 'Expenses', True),
 
    ('Expenses:Tax:Sales', 'Expense', False),
 
    ('Expenses:Tax:Sales', 'Accrued:', False),
 
    ('Expenses:Tax:Sales', 'Accrued', False),
 
    ('Expenses:Tax:Sales', 'Equity:', False),
 
    ('Expenses:Tax:Sales', 'Equity', False),
 
])
 
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
 

	
 
@pytest.mark.parametrize('acct_name,expected', [
 
    ('Income:Other', 'Income'),
 
    ('UnearnedIncome:Other', 'UnearnedIncome'),
 
    ('Accrued:AccountsPayable', None),
 
    ('Expenses:General', None),
 
    ('Assets:Cash', None),
 
    ('Assets:Checking', None),
 
    ('Assets:Prepaid:Expenses', 'Assets:Prepaid'),
 
    ('Assets:Receivable:Accounts', 'Assets:Receivable'),
 
])
 
def test_is_under_multi_arg(acct_name, expected):
 
    assert data.Account(acct_name).is_under('Income', 'UnearnedIncome') == expected
 
    assert expected == data.Account(acct_name).is_under(
 
        'Assets:Prepaid', 'Assets:Receivable',
 
    )
 
    if expected:
 
        expected += ':'
 
    assert data.Account(acct_name).is_under('Income:', 'UnearnedIncome:') == expected
 
    assert expected == data.Account(acct_name).is_under(
 
        'Assets:Prepaid:', 'Assets:Receivable:',
 
    )
 

	
 
@pytest.mark.parametrize('acct_name,expected', [
 
    ('Accrued:AccountsReceivable', False),
 
    ('Assets:Cash', False),
 
    ('Expenses:General', False),
 
    ('Income:Donations', True),
 
    ('Income:Sales', True),
 
    ('Income:Other', True),
 
    ('Liabilities:CreditCard', False),
 
    ('UnearnedIncome:MatchPledges', True),
 
])
 
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:Bank:Checking', True),
 
    ('Assets:Cash', True),
 
    ('Assets:Cash:EUR', True),
 
    ('Assets:PrepaidExpenses', False),
 
    ('Assets:PrepaidVacation', False),
 
    ('Assets:Bank:Checking', True),
 
    ('Expenses:General', False),
 
    ('Income:Donations', False),
 
    ('Assets:Prepaid:Expenses', False),
 
    ('Assets:Prepaid:Vacation', False),
 
    ('Assets:Receivable:Accounts', False),
 
    ('Assets:Receivable:Fraud', False),
 
    ('Expenses:Other', False),
 
    ('Equity:OpeningBalance', False),
 
    ('Income:Other', False),
 
    ('Liabilities:CreditCard', False),
 
])
 
def test_is_real_asset(acct_name, expected):
 
    assert data.Account(acct_name).is_real_asset() == expected
 
def test_is_cash_equivalent(acct_name, expected):
 
    assert data.Account(acct_name).is_cash_equivalent() == 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),
 
    ('Assets:Prepaid:Expenses', False),
 
    ('Assets:Receivable:Accounts', False),
 
    ('Expenses:Other', False),
 
    ('Equity:OpeningBalance', False),
 
    ('Income:Other', False),
 
    ('Liabilities:CreditCard', False),
 
])
 
def test_is_checking(acct_name, expected):
 
    assert data.Account(acct_name).is_checking() == expected
 

	
 
@pytest.mark.parametrize('acct_name,expected', [
 
    ('Assets:Cash', False),
 
    ('Assets:Prepaid:Expenses', False),
 
    ('Assets:Receivable:Accounts', False),
 
    ('Expenses:Other', False),
 
    ('Equity:OpeningBalance', False),
 
    ('Income:Other', False),
 
    ('Liabilities:CreditCard', True),
 
    ('Liabilities:CreditCard:Visa', True),
 
    ('Liabilities:Payable:Accounts', False),
 
    ('Liabilities:UnearnedIncome:Donations', False),
 
])
 
def test_is_credit_card(acct_name, expected):
 
    assert data.Account(acct_name).is_credit_card() == expected
tests/test_data_posting.py
Show inline comments
...
 
@@ -17,35 +17,35 @@
 
import pytest
 

	
 
from . import testutil
 

	
 
from decimal import Decimal
 

	
 
import beancount.core.amount as bc_amount
 

	
 
from conservancy_beancount import data
 

	
 
PAYMENT_ACCOUNTS = {
 
    'Assets:Cash',
 
    'Assets:Checking',
 
    'Assets:Bank:Checking',
 
}
 

	
 
NON_PAYMENT_ACCOUNTS = {
 
    'Accrued:AccountsReceivable',
 
    'Assets:PrepaidExpenses',
 
    'Assets:PrepaidVacation',
 
    'Assets:Prepaid:Expenses',
 
    'Assets:Prepaid:Vacation',
 
    'Assets:Receivable:Accounts',
 
    'Equity:OpeningBalance',
 
    'Expenses:Other',
 
    'Income:Other',
 
    'Liabilities:CreditCard',
 
    'UnearnedIncome:MatchPledges',
 
}
 

	
 
AMOUNTS = [
 
    None,
 
    '-25.50',
 
    0,
 
    '25.75',
 
]
 

	
 
def Posting(account, number,
 
            currency='USD', cost=None, price=None, flag=None,
 
            **meta):
tests/test_meta_approval.py
Show inline comments
...
 
@@ -12,36 +12,36 @@
 
# GNU Affero General Public License for more details.
 
#
 
# You should have received a copy of the GNU Affero General Public License
 
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
 

	
 
import pytest
 

	
 
from . import testutil
 

	
 
from conservancy_beancount.plugin import meta_approval
 

	
 
REQUIRED_ACCOUNTS = {
 
    'Assets:Bank:Checking',
 
    'Assets:Cash',
 
    'Assets:Checking',
 
    'Assets:Savings',
 
}
 

	
 
NON_REQUIRED_ACCOUNTS = {
 
    'Accrued:AccountsPayable',
 
    'Assets:PrepaidExpenses',
 
    'Assets:PrepaidVacation',
 
    'Assets:Prepaid:Expenses',
 
    'Assets:Receivable:Accounts',
 
    'Equity:QpeningBalance',
 
    'Expenses:Other',
 
    'Income:Other',
 
    'UnearnedIncome:Donations',
 
    'Liabilities:Payable:Accounts',
 
}
 

	
 
CREDITCARD_ACCOUNT = 'Liabilities:CreditCard'
 

	
 
TEST_KEY = 'approval'
 

	
 
@pytest.fixture(scope='module')
 
def hook():
 
    config = testutil.TestConfig()
 
    return meta_approval.MetaApproval(config)
 

	
 
@pytest.mark.parametrize('acct1,acct2,value', testutil.combine_values(
tests/test_meta_entity.py
Show inline comments
...
 
@@ -102,32 +102,36 @@ def test_valid_values_on_transactions(hook, src_value):
 
@pytest.mark.parametrize('src_value', INVALID_VALUES)
 
def test_invalid_values_on_transactions(hook, src_value):
 
    txn = testutil.Transaction(**{TEST_KEY: src_value}, postings=[
 
        ('Assets:Cash', -25),
 
        ('Expenses:General', 25),
 
    ])
 
    errors = list(hook.run(txn))
 
    assert 1 <= len(errors) <= 2
 
    assert all(error.message == "transaction has invalid entity: {}".format(src_value)
 
               for error in hook.run(txn))
 

	
 
@pytest.mark.parametrize('account,required', [
 
    ('Accrued:AccountsReceivable', True),
 
    ('Assets:Bank:Checking', False),
 
    ('Assets:Cash', False),
 
    ('Assets:Receivable:Accounts', True),
 
    ('Assets:Receivable:Loans', True),
 
    ('Equity:OpeningBalances', False),
 
    ('Expenses:General', True),
 
    ('Income:Donations', True),
 
    ('Liabilities:CreditCard', False),
 
    ('UnearnedIncome:Donations', True),
 
    ('Liabilities:Payable:Accounts', True),
 
    ('Liabilities:Payable:Vacation', True),
 
    ('Liabilities:UnearnedIncome:Donations', False),
 
])
 
def test_which_accounts_required_on(hook, account, required):
 
    txn = testutil.Transaction(postings=[
 
        ('Assets:Checking', 25),
 
        ('Assets:Checking', -25),
 
        (account, 25),
 
    ])
 
    errors = list(hook.run(txn))
 
    if not required:
 
        assert not errors
 
    else:
 
        assert errors
 
        assert any(error.message == "{} missing entity".format(account)
 
                   for error in errors)
tests/test_meta_expense_allocation.py
Show inline comments
...
 
@@ -74,29 +74,30 @@ def test_valid_values_on_transactions(hook, src_value, set_value):
 

	
 
@pytest.mark.parametrize('src_value', INVALID_VALUES)
 
def test_invalid_values_on_transactions(hook, src_value):
 
    txn = testutil.Transaction(**{TEST_KEY: src_value}, postings=[
 
        ('Assets:Cash', -25),
 
        ('Expenses:General', 25),
 
    ])
 
    errors = list(hook.run(txn))
 
    assert errors
 
    testutil.check_post_meta(txn, None, None)
 

	
 
@pytest.mark.parametrize('account', [
 
    'Accrued:AccountsReceivable',
 
    'Assets:Cash',
 
    'Income:Donations',
 
    'Assets:Receivable:Accounts',
 
    'Equity:OpeningBalance',
 
    'Income:Other',
 
    'Liabilities:CreditCard',
 
    'UnearnedIncome:Donations',
 
    'Liabilities:Payable:Vacation',
 
])
 
def test_non_expense_accounts_skipped(hook, account):
 
    meta = {TEST_KEY: 'program'}
 
    txn = testutil.Transaction(postings=[
 
        (account, -25),
 
        ('Expenses:General', 25, meta.copy()),
 
    ])
 
    errors = list(hook.run(txn))
 
    assert not errors
 
    testutil.check_post_meta(txn, None, meta)
 

	
 
@pytest.mark.parametrize('account,set_value', [
tests/test_meta_income_type.py
Show inline comments
...
 
@@ -74,50 +74,52 @@ def test_valid_values_on_transactions(hook, src_value, set_value):
 

	
 
@pytest.mark.parametrize('src_value', INVALID_VALUES)
 
def test_invalid_values_on_transactions(hook, src_value):
 
    txn = testutil.Transaction(**{TEST_KEY: src_value}, postings=[
 
        ('Assets:Cash', 25),
 
        ('Income:Other', -25),
 
    ])
 
    errors = list(hook.run(txn))
 
    assert errors
 
    testutil.check_post_meta(txn, None, None)
 

	
 
@pytest.mark.parametrize('account', [
 
    'Accrued:AccountsReceivable',
 
    'Assets:Cash',
 
    'Expenses:General',
 
    'Assets:Receivable:Accounts',
 
    'Equity:OpeningBalance',
 
    'Expenses:Other',
 
    'Liabilities:CreditCard',
 
    'Liabilities:Payable:Vacation',
 
])
 
def test_non_income_accounts_skipped(hook, account):
 
    meta = {TEST_KEY: 'RBI'}
 
    txn = testutil.Transaction(postings=[
 
        (account, 25),
 
        ('Income:Other', -25, meta.copy()),
 
    ])
 
    errors = list(hook.run(txn))
 
    assert not errors
 
    testutil.check_post_meta(txn, None, meta)
 

	
 
@pytest.mark.parametrize('account,set_value', [
 
    ('Income:Conferences:Registrations', 'RBI'),
 
    ('Income:Conferences:Sponsorship', 'RBI'),
 
    ('Income:Donations', 'Donations'),
 
    ('Income:Honoraria', 'RBI'),
 
    ('Income:Interest', 'RBI'),
 
    ('Income:Interest:Dividend', 'RBI'),
 
    ('Income:Royalties', 'RBI'),
 
    ('Income:Sales', 'RBI'),
 
    ('Income:SoftwareDevelopment', 'RBI'),
 
    ('Income:TrademarkLicensing', 'RBI'),
 
    ('UnearnedIncome:Conferences:Registrations', 'RBI'),
 
    ('UnearnedIncome:MatchPledges', 'Donations'),
 
])
 
def test_default_values(hook, account, set_value):
 
    txn = testutil.Transaction(postings=[
 
        ('Assets:Cash', 25),
 
        (account, -25),
 
    ])
 
    errors = list(hook.run(txn))
 
    assert not errors
 
    testutil.check_post_meta(txn, None, {TEST_KEY: set_value})
 

	
 
@pytest.mark.parametrize('account', [
 
    'Income:Other',
tests/test_meta_invoice.py
Show inline comments
...
 
@@ -12,34 +12,36 @@
 
# GNU Affero General Public License for more details.
 
#
 
# You should have received a copy of the GNU Affero General Public License
 
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
 

	
 
import pytest
 

	
 
from . import testutil
 

	
 
from conservancy_beancount.plugin import meta_invoice
 

	
 
REQUIRED_ACCOUNTS = {
 
    'Accrued:AccountsPayable',
 
    'Accrued:AccountsReceivable',
 
    'Assets:Receivable:Accounts',
 
    'Assets:Receivable:Loans',
 
    'Liabilities:Payable:Accounts',
 
    'Liabilities:Payable:Vacation',
 
}
 

	
 
NON_REQUIRED_ACCOUNTS = {
 
    'Assets:Cash',
 
    'Equity:OpeningBalance',
 
    'Expenses:Other',
 
    'Income:Other',
 
    'Liabilities:CreditCard',
 
    'UnearnedIncome:Donations',
 
}
 

	
 
TEST_KEY = 'invoice'
 

	
 
@pytest.fixture(scope='module')
 
def hook():
 
    config = testutil.TestConfig()
 
    return meta_invoice.MetaInvoice(config)
 

	
 
@pytest.mark.parametrize('acct1,acct2,value', testutil.combine_values(
 
    REQUIRED_ACCOUNTS,
 
    NON_REQUIRED_ACCOUNTS,
tests/test_meta_project.py
Show inline comments
...
 
@@ -77,65 +77,69 @@ def test_valid_values_on_transactions(hook, src_value, set_value):
 

	
 
@pytest.mark.parametrize('src_value', INVALID_VALUES)
 
def test_invalid_values_on_transactions(hook, src_value):
 
    txn = testutil.Transaction(**{TEST_KEY: src_value}, postings=[
 
        ('Assets:Cash', -25),
 
        ('Expenses:General', 25),
 
    ])
 
    errors = list(hook.run(txn))
 
    assert errors
 
    testutil.check_post_meta(txn, None, None)
 

	
 
@pytest.mark.parametrize('account,required', [
 
    ('Accrued:AccountsReceivable', True),
 
    ('Assets:Cash', False),
 
    ('Equity:Opening-Balances', False),
 
    ('Assets:Receivable:Accounts', True),
 
    ('Assets:Receivable:Loans', True),
 
    ('Equity:OpeningBalance', False),
 
    ('Expenses:General', True),
 
    ('Income:Donations', True),
 
    ('Liabilities:CreditCard', False),
 
    ('UnearnedIncome:Donations', True),
 
    ('Liabilities:Payable:Accounts', True),
 
    # We do want a "project" for Lia:Pay:Vacation but it has a default value
 
    ('Liabilities:Payable:Vacation', False),
 
    ('Liabilities:UnearnedIncome:Donations', True),
 
])
 
def test_which_accounts_required_on(hook, account, required):
 
    txn = testutil.Transaction(postings=[
 
        ('Assets:Checking', 25),
 
        (account, 25),
 
    ])
 
    errors = list(hook.run(txn))
 
    assert required == any(errors)
 

	
 
@pytest.mark.parametrize('account', [
 
    'Accrued:VacationPayable',
 
    'Expenses:Payroll:Salary',
 
    'Expenses:Payroll:Tax',
 
    'Liabilities:Payable:Vacation',
 
])
 
def test_default_values(hook, account):
 
    txn = testutil.Transaction(postings=[
 
        ('Assets:Checking', -25),
 
        (account, 25),
 
    ])
 
    errors = list(hook.run(txn))
 
    assert not errors
 
    testutil.check_post_meta(txn, None, {TEST_KEY: DEFAULT_VALUE})
 

	
 
@pytest.mark.parametrize('date,required', [
 
    (testutil.EXTREME_FUTURE_DATE, False),
 
    (testutil.FUTURE_DATE, True),
 
    (testutil.FY_START_DATE, True),
 
    (testutil.FY_MID_DATE, True),
 
    (testutil.PAST_DATE, None),
 
])
 
def test_default_value_set_in_date_range(hook, date, required):
 
    txn = testutil.Transaction(date=date, postings=[
 
        ('Expenses:Payroll:Benefits', 25),
 
        ('Accrued:VacationPayable', -25),
 
        ('Liabilities:Payable:Vacation', -25),
 
    ])
 
    errors = list(hook.run(txn))
 
    assert not errors
 
    expect_meta = {TEST_KEY: DEFAULT_VALUE} if required else None
 
    testutil.check_post_meta(txn, expect_meta, expect_meta)
 

	
 
@pytest.mark.parametrize('repo_path', [
 
    None,
 
    '..',
 
])
 
def test_missing_project_data(repo_path):
 
    config = testutil.TestConfig(repo_path=repo_path)
tests/test_meta_receipt.py
Show inline comments
...
 
@@ -68,31 +68,31 @@ ACCOUNTS = [AccountForTesting._make(t) for t in [
 

	
 
ACCOUNTS_WITH_FALLBACKS = [acct for acct in ACCOUNTS if acct.fallback_meta]
 
ACCOUNTS_WITHOUT_FALLBACKS = [acct for acct in ACCOUNTS if not acct.fallback_meta]
 
KNOWN_FALLBACKS = {acct.fallback_meta for acct in ACCOUNTS_WITH_FALLBACKS}
 

	
 
# These are mostly fill-in values.
 
# We don't need to run every test on every value for these, just enough to
 
# convince ourselves the hook never reports errors against these accounts.
 
# Making this a iterator rather than a sequence means testutil.combine_values
 
# doesn't require the decorated test to go over every value, which in turn
 
# trims unnecessary test time.
 
NOT_REQUIRED_ACCOUNTS = itertools.cycle([
 
    'Accrued:AccountsPayable',
 
    'Accrued:AccountsReceivable',
 
    'Assets:PrepaidExpenses',
 
    'Assets:PrepaidVacation',
 
    'Assets:Prepaid:Expenses',
 
    'Assets:Receivable:Accounts',
 
    'Equity:OpeningBalance',
 
    'Expenses:Other',
 
    'Income:Other',
 
    'UnearnedIncome:Donations',
 
    'Liabilities:Payable:Accounts',
 
    'Liabilities:UnearnedIncome:Donations',
 
])
 

	
 
def check(hook, test_acct, other_acct, expected, *,
 
          txn_meta={}, post_meta={}, check_type=PostType.BOTH, min_amt=0):
 
    check_type &= test_acct.required_types
 
    assert check_type, "tried to test a non-applicable account"
 
    if check_type == PostType.BOTH:
 
        check(hook, test_acct, other_acct, expected,
 
              txn_meta=txn_meta, post_meta=post_meta, check_type=PostType.CREDIT)
 
        check_type = PostType.DEBIT
 
    amount = decimal.Decimal('{:.02f}'.format(min_amt + random.random() * 100))
 
    if check_type == PostType.DEBIT:
tests/test_meta_receivable_documentation.py
Show inline comments
...
 
@@ -14,25 +14,25 @@
 
# You should have received a copy of the GNU Affero General Public License
 
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
 

	
 
import random
 

	
 
import pytest
 

	
 
from . import testutil
 

	
 
from conservancy_beancount import errors as errormod
 
from conservancy_beancount.plugin import meta_receivable_documentation
 

	
 
TEST_ACCT = 'Accrued:AccountsReceivable'
 
TEST_ACCT = 'Assets:Receivable:Accounts'
 
OTHER_ACCT = 'Income:Donations'
 

	
 
SUPPORTING_METADATA = [
 
    'approval',
 
    'contract',
 
    'purchase-order',
 
]
 

	
 
NON_SUPPORTING_METADATA = [
 
    'check',
 
    'receipt',
 
    'statement',
...
 
@@ -169,20 +169,28 @@ def test_type_errors_reported_with_valid_txn_docs(hook, invoice, support_key, su
 
        for key, value in meta.items()
 
        if key != 'invoice' and  key != support_key
 
    }
 
    check(hook, expected, txn_meta=meta)
 

	
 
@pytest.mark.parametrize('invoice,meta_type', testutil.combine_values(
 
    RECEIVED_INVOICE_LINKS,
 
    ['post_meta', 'txn_meta'],
 
))
 
def test_received_invoices_not_checked(hook, invoice, meta_type):
 
    check(hook, None, **{meta_type: {'invoice': invoice}})
 

	
 
def test_does_not_apply_to_payables(hook):
 
@pytest.mark.parametrize('account', [
 
    'Assets:Bank:Checking',
 
    'Assets:Cash',
 
    'Equity:OpeningBalance',
 
    'Expenses:BankingFees',
 
    'Liabilities:CreditCard',
 
    'Liabilities:Payable:Accounts',
 
])
 
def test_does_not_apply_to_other_accounts(hook, account):
 
    meta = seed_meta()
 
    check(hook, None, 'Accrued:AccountsPayable', 'Expenses:Other', post_meta=meta)
 
    check(hook, None, account, 'Expenses:Other', post_meta=meta)
 

	
 
def test_configuration_error_without_rt():
 
    config = testutil.TestConfig()
 
    with pytest.raises(errormod.ConfigurationError):
 
        meta_receivable_documentation.MetaReceivableDocumentation(config)
tests/test_meta_tax_implication.py
Show inline comments
...
 
@@ -48,85 +48,82 @@ INVALID_VALUES = {
 
}
 

	
 
TEST_KEY = 'tax-implication'
 

	
 
@pytest.fixture(scope='module')
 
def hook():
 
    config = testutil.TestConfig()
 
    return meta_tax_implication.MetaTaxImplication(config)
 

	
 
@pytest.mark.parametrize('src_value,set_value', VALID_VALUES.items())
 
def test_valid_values_on_postings(hook, src_value, set_value):
 
    txn = testutil.Transaction(postings=[
 
        ('Accrued:AccountsPayable', 25),
 
        ('Liabilities:Payable:Accounts', 25),
 
        ('Assets:Cash', -25, {TEST_KEY: src_value}),
 
    ])
 
    errors = list(hook.run(txn))
 
    assert not errors
 
    testutil.check_post_meta(txn, None, {TEST_KEY: set_value})
 

	
 
@pytest.mark.parametrize('src_value', INVALID_VALUES)
 
def test_invalid_values_on_postings(hook, src_value):
 
    txn = testutil.Transaction(postings=[
 
        ('Accrued:AccountsPayable', 25),
 
        ('Liabilities:Payable:Accounts', 25),
 
        ('Assets:Cash', -25, {TEST_KEY: src_value}),
 
    ])
 
    errors = list(hook.run(txn))
 
    assert errors
 
    testutil.check_post_meta(txn, None, {TEST_KEY: src_value})
 

	
 
@pytest.mark.parametrize('src_value,set_value', VALID_VALUES.items())
 
def test_valid_values_on_transactions(hook, src_value, set_value):
 
    txn = testutil.Transaction(**{TEST_KEY: src_value}, postings=[
 
        ('Accrued:AccountsPayable', 25),
 
        ('Liabilities:Payable:Accounts', 25),
 
        ('Assets:Cash', -25),
 
    ])
 
    errors = list(hook.run(txn))
 
    assert not errors
 
    testutil.check_post_meta(txn, None, {TEST_KEY: set_value})
 

	
 
@pytest.mark.parametrize('src_value', INVALID_VALUES)
 
def test_invalid_values_on_transactions(hook, src_value):
 
    txn = testutil.Transaction(**{TEST_KEY: src_value}, postings=[
 
        ('Accrued:AccountsPayable', 25),
 
        ('Liabilities:Payable:Accounts', 25),
 
        ('Assets:Cash', -25),
 
    ])
 
    errors = list(hook.run(txn))
 
    assert errors
 
    testutil.check_post_meta(txn, None, None)
 

	
 
@pytest.mark.parametrize('account', [
 
    'Accrued:AccountsPayable',
 
    'Expenses:General',
 
@pytest.mark.parametrize('count,account', enumerate([
 
    'Assets:Payable:Accounts',
 
    'Assets:Prepaid:Expenses',
 
    'Equity:OpeningBalance',
 
    'Expenses:Other',
 
    'Income:Other',
 
    'Liabilities:CreditCard',
 
])
 
def test_non_asset_accounts_skipped(hook, account):
 
    'Liabilities:Payable:Accounts',
 
    'Liabilities:UnearnedIncome:Donations',
 
], 1))
 
def test_non_payment_accounts_skipped(hook, account, count):
 
    amount = count * 100
 
    meta = {TEST_KEY: 'USA-Corporation'}
 
    txn = testutil.Transaction(postings=[
 
        (account, 25),
 
        ('Assets:Cash', -25, meta.copy()),
 
        (account, amount),
 
        ('Assets:Checking', -amount, meta.copy()),
 
    ])
 
    errors = list(hook.run(txn))
 
    assert not errors
 
    testutil.check_post_meta(txn, None, meta)
 

	
 
def test_prepaid_expenses_skipped(hook, ):
 
    txn = testutil.Transaction(postings=[
 
        ('Expenses:General', 25),
 
        ('Assets:PrepaidExpenses', -25),
 
    ])
 
    errors = list(hook.run(txn))
 
    assert not errors
 
    testutil.check_post_meta(txn, None, None)
 

	
 
def test_asset_credits_skipped(hook, ):
 
    txn = testutil.Transaction(postings=[
 
        ('Income:Donations', -25),
 
        ('Assets:Cash', 25),
 
    ])
 
    errors = list(hook.run(txn))
 
    assert not errors
 
    testutil.check_post_meta(txn, None, None)
 

	
 
@pytest.mark.parametrize('date,need_value', [
 
    (testutil.EXTREME_FUTURE_DATE, False),
 
    (testutil.FUTURE_DATE, True),
0 comments (0 inline, 0 general)