Skip to content

Commit

Permalink
Fix division by zero.
Browse files Browse the repository at this point in the history
Also add a regression test.
  • Loading branch information
porridge committed Nov 30, 2022
1 parent cdd1930 commit bf39ad1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ledgerhelpers/journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def harvest_accounts_and_last_commodities(self):
for subpost in post.xact.posts():
if str(subpost.account) not in accts:
accts.append(str(subpost.account))
comm = subpost.amount / subpost.amount
comm = ledger.Amount(1).with_commodity(subpost.amount.commodity)
comm.commodity = comm.commodity.strip_annotations()
commos[str(subpost.account)] = str(comm)
amts[str(comm)] = True
Expand Down
6 changes: 6 additions & 0 deletions tests/test_ledgerhelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ def test_transactions_with_payee_match(self):
ts = journal.transactions_with_payee("beer", j.internal_parsing())
self.assertEqual(ts[0].payee, "beer")

def test_transaction_with_zero_posting(self):
c = base.datapath("zero.dat")
j = journal.Journal.from_file(c, None)
_, commos = j.accounts_and_last_commodity_for_account()
self.assertEqual(commos["rest"], "1 USD")


class TestGenerateRecord(T):

Expand Down
4 changes: 4 additions & 0 deletions tests/testdata/zero.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
2022-01-01 zero
ac1 1 USD
ac2 -1 USD
rest

0 comments on commit bf39ad1

Please sign in to comment.