From 71f50a6cf8643df127195877bec4149839663a80 2020-12-29 17:20:53 From: Brett Smith Date: 2020-12-29 17:20:53 Subject: [PATCH] data: Bugfix is_opening_balance_txn() for donations from equity. RT#13516 Opening balance transactions should only include opening equity accounts and non-equity accounts. Reflect that in the test. --- diff --git a/conservancy_beancount/data.py b/conservancy_beancount/data.py index a7f4ba704448f6dcb4755edbf3bd8dc95953b008..05079bac9e300ce25daab599e7ff85564759ff7f 100644 --- a/conservancy_beancount/data.py +++ b/conservancy_beancount/data.py @@ -687,10 +687,10 @@ def is_opening_balance_txn(txn: Transaction) -> bool: if not opening_equity.currency: retval = False else: - rest = balance_of(txn, lambda acct: not acct.is_opening_equity()) - if not rest.currency: - retval = False - else: - retval = abs(opening_equity.number + rest.number) < decimal.Decimal('.01') + rest = balance_of(txn, lambda acct: not acct.is_under(*EQUITY_ACCOUNTS)) + retval = ( + opening_equity.currency == rest.currency + and abs(opening_equity.number + rest.number) < decimal.Decimal('.01') + ) _opening_balance_cache[key] = retval return retval diff --git a/setup.py b/setup.py index 81a16dc5d246115f5882b34ec8de08b7d0338735..d50de594f14d9f012e99c07ed824f5f5cfba5219 100755 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ from setuptools import setup setup( name='conservancy_beancount', description="Plugin, library, and reports for reading Conservancy's books", - version='1.14.2', + version='1.14.3', author='Software Freedom Conservancy', author_email='info@sfconservancy.org', license='GNU AGPLv3+', diff --git a/tests/test_data_is_opening_balance_txn.py b/tests/test_data_is_opening_balance_txn.py index 5dba3c8a23a41c24d59fedb843b1e51e33c2ff1d..70d49bd11286ead3a38f33e47244403029bc1223 100644 --- a/tests/test_data_is_opening_balance_txn.py +++ b/tests/test_data_is_opening_balance_txn.py @@ -50,6 +50,8 @@ def test_opening_with_fx(): ('Expenses:Other', 'Equity:Retained:Costs', 300), # Release from restriction ('Equity:Funds:Unrestricted', 'Equity:Funds:Restricted', 400), + # Donation from project fund + ('Equity:Funds:Restricted', 'Income:Donations', 500), ]) def test_not_opening_balance(acct1, acct2, number): txn = testutil.Transaction(postings=[ diff --git a/tests/test_meta_entity.py b/tests/test_meta_entity.py index f8b012238e5c8e0ac8404c8df1e46fc693f24a59..c8d5b54f990f1b21ccdb359e8342dd859810c005 100644 --- a/tests/test_meta_entity.py +++ b/tests/test_meta_entity.py @@ -188,6 +188,19 @@ def test_invalid_payee_but_valid_metadata(hook, payee, src_value): ]) assert not any(hook.run(txn)) +def test_mixed_sources(hook): + txn = testutil.Transaction(payee='Payee', postings=[ + ('Income:Donations', -5), + ('Equity:Funds:Restricted', 5, {TEST_KEY: 'Entity'}), + ]) + assert not any(hook.run(txn)) + assert txn.postings[-1].meta[TEST_KEY] == 'Entity' + assert txn.meta[TEST_KEY] == 'Payee' + try: + assert txn.postings[0].meta[TEST_KEY] == 'Payee' + except (KeyError, TypeError): + pass + @pytest.mark.parametrize('account,required', [ ('Assets:Bank:Checking', False), ('Assets:Cash', False),