Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #128: Maintain original posting order. #129

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"