Skip to content

Commit

Permalink
Fix beancount#128: Maintain original posting order.
Browse files Browse the repository at this point in the history
  • Loading branch information
awtimmering committed Apr 5, 2024
1 parent 9c9ec14 commit e9f311d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
10 changes: 3 additions & 7 deletions smart_importer/entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ def update_postings(transaction, accounts):
if len(transaction.postings) != 1:
return transaction

new_postings = [
Posting(account, None, None, None, None, None) for account in accounts
new_postings = [transaction.postings[0]] + [
Posting(account, None, None, None, None, None)
for account in accounts if account != transaction.postings[0].account
]
for posting in transaction.postings:
if posting.account in accounts:
new_postings[accounts.index(posting.account)] = posting
else:
new_postings.append(posting)

return transaction._replace(postings=new_postings)

Expand Down
73 changes: 73 additions & 0 deletions tests/entries_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# pylint: disable=missing-docstring
import textwrap

from beancount.parser import parser

from smart_importer.entries import update_postings


def test_update_postings_regular():

test_entries, _, _ = parser.parse_string(
textwrap.dedent("""
2024-04-04 * "Supermarket ABC" "Groceries"
Assets:US:BofA:Checking -100.00 USD
"""))

transaction = test_entries[0]
accounts = "Assets:US:BofA:Checking", "Expenses:Food"

updated_transaction = update_postings(transaction, accounts)

assert len(updated_transaction.postings) == 2

# Check if the first posting is unchanged
assert updated_transaction.postings[0] == transaction.postings[0]

# Check if the remaining postings are created correctly
assert updated_transaction.postings[1].account == "Expenses:Food"


def test_update_postings_inverse():

test_entries, _, _ = parser.parse_string(
textwrap.dedent("""
2024-04-04 * "Supermarket ABC" "Groceries"
Assets:US:BofA:Checking -100.00 USD
"""))

transaction = test_entries[0]
accounts = "Expenses:Food", "Assets:US:BofA:Checking"

updated_transaction = update_postings(transaction, accounts)

assert len(updated_transaction.postings) == 2

# Check if the first posting is unchanged
assert updated_transaction.postings[0] == transaction.postings[0]

# Check if the remaining postings are created correctly
assert updated_transaction.postings[1].account == "Expenses:Food"


def test_update_postings_multi():

test_entries, _, _ = parser.parse_string(
textwrap.dedent("""
2024-04-04 * "Supermarket ABC" "Groceries"
Assets:US:BofA:Checking -100.00 USD
"""))

transaction = test_entries[0]
accounts = "Assets:US:BofA:Checking", "Expenses:Food", "Expenses:Clothing"

updated_transaction = update_postings(transaction, accounts)

assert len(updated_transaction.postings) == 3

# Check if the first posting is unchanged
assert updated_transaction.postings[0] == transaction.postings[0]

# Check if the remaining postings are created correctly
assert updated_transaction.postings[1].account == "Expenses:Food"
assert updated_transaction.postings[2].account == "Expenses:Clothing"

0 comments on commit e9f311d

Please sign in to comment.