From e422bdac93370ea7690fcffc9c841b8bab521005 Mon Sep 17 00:00:00 2001 From: Ankur Dave Date: Wed, 1 Nov 2023 19:18:16 -0400 Subject: [PATCH] matching.py: Fix incorrect fuzzy amount loop bounds --- beancount_import/matching.py | 3 ++- beancount_import/matching_test.py | 45 +++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/beancount_import/matching.py b/beancount_import/matching.py index bf2afa41..b1e11697 100644 --- a/beancount_import/matching.py +++ b/beancount_import/matching.py @@ -443,7 +443,8 @@ def _get_matches( if cur_matches is not None: lower_bound = bisect.bisect_left(cur_matches, (lower, tuple(), None, None)) upper_bound = bisect.bisect_right(cur_matches, (upper, (sys.maxsize,), None, None), lo=lower_bound) - for sp in cur_matches[lower_bound-1 if lower_bound > 0 else 0:upper_bound]: + for sp in cur_matches[lower_bound:upper_bound]: + assert abs(sp.number - amount.number) <= self.fuzzy_match_amount posting = sp.mp.posting # Verify that the account is compatible. if not are_accounts_mergeable(account, posting.account): diff --git a/beancount_import/matching_test.py b/beancount_import/matching_test.py index 151b370a..7b26865b 100755 --- a/beancount_import/matching_test.py +++ b/beancount_import/matching_test.py @@ -721,6 +721,33 @@ def test_match_fuzzy_amount(): note: "B" """) +def test_match_fuzzy_amount_upper_bound(): + # We match despite a skew of 0.01 USD, our configured fuzzy_match_amount. + assert_match( + pending_candidate=""" + 2016-01-01 * "Narration" + note: "A" + Income:A -100 USD + note: "A" + Expenses:FIXME 100 USD + """, + pending=""" + 2016-01-01 * "Narration" + note2: "B" + Assets:B 100.01 STOCK { 1.00 USD } + note: "B" + Expenses:FIXME -100.01 USD + """, + matches=""" + 2016-01-01 * "Narration" + note: "A" + note2: "B" + Income:A -100 USD + note: "A" + Assets:B 100.01 STOCK { 1.00 USD } + note: "B" + """) + def test_nonmatch_fuzzy_amount(): # We don't match with a skew of 0.02, beyond our configured fuzzy_match_amount. assert_match( @@ -739,6 +766,24 @@ def test_nonmatch_fuzzy_amount(): Expenses:FIXME -99.98 USD """) +def test_nonmatch_fuzzy_amount_with_dates(): + # We don't match with a skew of 0.02, beyond our configured fuzzy_match_amount. + assert_match( + pending_candidate=""" + 2023-01-01 * "Transaction 0" + Assets:A 20.00 USD + date: 2023-01-01 + cleared: TRUE + Expenses:FIXME -20.00 USD + """, + pending=""" + 2023-01-01 * "Transaction 1" + Assets:B -19.98 USD + date: 2023-01-01 + cleared: TRUE + Expenses:FIXME 19.98 USD + """) + def test_match_grouped_differing_signs(): # Can group postings of differing signs to make a match. assert_match(