Skip to content

Commit

Permalink
matching.py: Fix incorrect fuzzy amount loop bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
ankurdave committed Nov 1, 2023
1 parent e790b64 commit e422bda
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
3 changes: 2 additions & 1 deletion beancount_import/matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
45 changes: 45 additions & 0 deletions beancount_import/matching_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit e422bda

Please sign in to comment.