haskell, hledger
May 28, 2013

Balance assertions, session 4 - working

In today’s session I fixed the bug in balance-assertions. Yes, you need to sort before group:

journalCheckBalanceAssertions j = do
  let postingsByAccount = groupBy (\p1 p2 -> paccount p1 == paccount p2) $
                          sortBy (comparing paccount) $
                          journalPostings j

I wasn’t sure if I could rely on that preserving the order of postings within each account, but the haddock says sort is stable, which I think means yes. Stable ordering is important because assertions depend on the order postings are parsed from the file(s), as in ledger. I think actually it should sort by date too - eg you might include earlier-dated entries from another file, later in parsing - but it will still have to rely on parse order for postings on the same date. For now, it’s entirely based on parse order.

I tested some more, cleaned up the assertion failure output, updated the manual:

Balance Assertions

hledger supports ledger-style balance assertions in journal files. These look like =EXPECTEDBALANCE following a posting’s amount. Eg in this example we assert the expected balance in accounts a and b after each posting:

2013/1/1
  a   $1  =$1
  b       =$-1

2013/1/2
  a   $1  =$2
  b  $-1  =$-2

After reading a journal file, hledger will check all balance assertions and report an error if any of them fail. Balance assertions can protect you from, eg, inadvertently disrupting reconciled balances while cleaning up old entries. Be aware that they may have some impact on parsing performance. Also note, balance assertion amounts currently must have a commodity symbol.

and committed. Balance assertions are in, time for real-world testing.