From 600c9d9d6f1d17ce40e59a79ea9c7d56ca37f7a5 Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Sun, 5 Apr 2020 15:29:04 -0400 Subject: [PATCH] meta_receipt: Correctly fall back to other metadata on zero-value postings. RT#10633. --- conservancy_beancount/plugin/meta_receipt.py | 13 +++---------- tests/test_meta_receipt.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/conservancy_beancount/plugin/meta_receipt.py b/conservancy_beancount/plugin/meta_receipt.py index 54cbae3..475c845 100644 --- a/conservancy_beancount/plugin/meta_receipt.py +++ b/conservancy_beancount/plugin/meta_receipt.py @@ -59,23 +59,16 @@ class MetaReceipt(core._RequireLinksPostingMetadataHook): else: return - if not post.units.number: - post_amount = 0 - elif post.units.number > 0: - post_amount = 1 - else: - post_amount = -1 - check_method: _CheckMethod = self._check_links if post.account.is_checking(): - if post_amount == -1: + if post.is_debit(): check_method = self._check_check_id fallback_key = 'check-id' else: fallback_key = 'check' - elif post.account.is_credit_card() and post_amount == -1: + elif post.account.is_credit_card() and not post.is_credit(): fallback_key = 'invoice' - elif post.account.is_under('Assets:PayPal') and post_amount == 1: + elif post.account.is_under('Assets:PayPal') and not post.is_debit(): fallback_key = 'paypal-id' else: yield receipt_error diff --git a/tests/test_meta_receipt.py b/tests/test_meta_receipt.py index 519a313..0871b60 100644 --- a/tests/test_meta_receipt.py +++ b/tests/test_meta_receipt.py @@ -326,3 +326,20 @@ def test_bad_type_check_id_on_txn(hook, test_acct, other_acct, value): def test_fallback_not_accepted_on_other_accounts(hook, test_acct, other_acct, key, value): check(hook, test_acct, other_acct, {test_acct.missing_message()}, post_meta={key: value}) + + +@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values( + ACCOUNTS_WITH_LINK_FALLBACK, + NOT_REQUIRED_ACCOUNTS, + testutil.LINK_METADATA_STRINGS, +)) +def test_fallback_on_zero_amount_postings(hook, test_acct, other_acct, value): + # Unfortunately it does happen that we get donations that go 100% to + # banking fees, and our importer writes a zero-amount posting to the + # Assets account. + txn = testutil.Transaction(postings=[ + ('Income:Donations', '-.1'), + ('Expenses:BankingFees', '.1'), + (test_acct.name, 0, {test_acct.fallback_meta: value}), + ]) + assert not list(hook.run(txn))