-
-
Notifications
You must be signed in to change notification settings - Fork 341
imp: add: Verify balance assertions on each posting (#2355) #2356
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
Changes from all commits
9123d88
cd003dc
45416fb
b2eaf51
6183f82
8dcacdc
f7cd12a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,7 +114,158 @@ $ rm -f nosuch.journal; hledger -f nosuch.journal add; rm -f nosuch.journal | |
> /Amount 3 \[-0.75\]:/ | ||
>2 // | ||
|
||
## 10. shouldn't add decimals if there aren't any | ||
## 10. Balance assertions with == | ||
|
||
< | ||
2025-05-01 | ||
x | ||
a | ||
50 USD | ||
b | ||
-50 USD == 50 USD | ||
. | ||
$ rm -f nosuch.journal; hledger -f nosuch.journal add; rm -f nosuch.journal | ||
> // | ||
>2 /Balance assertion failed in b/ | ||
|
||
## 11. Balance assertions with = | ||
|
||
< | ||
2025-05-01 | ||
x | ||
a | ||
\$10 | ||
a | ||
10 EUR | ||
a | ||
-10 EUR = 0 EUR | ||
a | ||
\$-10 = $0 | ||
. | ||
$ rm -f nosuch.journal; hledger -f nosuch.journal add; rm -f nosuch.journal | ||
> /Save this transaction to the journal/ | ||
>2 // | ||
|
||
## 12. Assertions with subaccounts | ||
|
||
< | ||
2025-05-01 | ||
x | ||
a:b | ||
1000 JPY | ||
a | ||
-500 JPY ==* 500 JPY | ||
c | ||
-500 JPY | ||
. | ||
$ rm -f nosuch.journal; hledger -f nosuch.journal add; rm -f nosuch.journal | ||
> /Save this transaction to the journal/ | ||
>2 // | ||
|
||
## 13. Assertions with posting dates | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also test add-ing an amount with an assertion and a posting date ? I'm not sure if that's supported. Also what about balance assignments, are they expected to work or be rejected ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! It looks like it's not possible to add an assertion and a posting date such that the assertion would be valid on the day of the posting date. However, this is also not valid in a journal file:
This might be possible to support eventually, but since this is already the existing behavior, I think that's better for a follow-up PR. Balance assignments aren't supported in stock
If we're interested, I could look at adding those next. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I wouldn't expect that journal file to pass. But this should be accepted, shouldn't it ?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Equivalent to this journal:)
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Right you are, current hledger doesn't allow add-ing balance assertions so of course it doesn't allow add-ing balance assignments either. Once it allows add-ing assertions, people might expect assignments too, let's mention in the manual that add-ing those isn't supported yet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
And yes that works as desired, I just checked. (Except it required me to enter the year; ideally it would use the transaction date's year as default.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it, thanks! I've added a test for this case. Not sure why the partial date doesn't work...I'll dig into what the journal parsing does when handling partial dates. |
||
|
||
< | ||
2025-05-01 | ||
x | ||
a | ||
50 USD ; date:2025-05-10 | ||
b | ||
-50 USD | ||
. | ||
y | ||
2025-05-05 | ||
x2 | ||
a | ||
10 USD == 10 USD | ||
c | ||
-10 USD | ||
. | ||
# Check the output with c to make sure we get to the final transaction display | ||
# (anything generic is also in the first transaction) | ||
$ rm -f nosuch.journal; hledger -f nosuch.journal add; rm -f nosuch.journal | ||
> /c[[:space:]]+-10 USD/ | ||
>2 // | ||
|
||
## 14. Multi-commodity subaccount assertions | ||
< | ||
2025-05-01 | ||
x | ||
a:b | ||
50 EUR | ||
a:c | ||
500 MXN | ||
a | ||
-50 EUR =* 0 EUR | ||
a | ||
-500 MXN =* 0 MXN | ||
. | ||
$ rm -f nosuch.journal; hledger -f nosuch.journal add; rm -f nosuch.journal | ||
> /Save this transaction to the journal/ | ||
>2 // | ||
|
||
## 15. Assertion combined with posting date | ||
< | ||
2025-05-10 | ||
x | ||
(a) | ||
10 | ||
. | ||
y | ||
2025-05-10 | ||
x2 | ||
(a) | ||
3 = 3 ; date:2025-05-01 | ||
. | ||
$ rm -f nosuch.journal; hledger -f nosuch.journal add; rm -f nosuch.journal | ||
> /\(a\)[[:space:]]+3 = 3 ; date:2025-05-01/ | ||
>2 // | ||
|
||
## 16. Verify that -I skips assertions | ||
< | ||
2025-05-10 | ||
x | ||
a | ||
10 USD = 20 USD | ||
b | ||
-10 USD | ||
. | ||
$ rm -f nosuch.journal; hledger -f nosuch.journal -I add; rm -f nosuch.journal | ||
> /Save this transaction to the journal/ | ||
>2 // | ||
|
||
## 17. Partial dates in added transaction | ||
< | ||
2025-05-01 | ||
x | ||
a | ||
10 USD ; date:5/2 | ||
b | ||
-10 USD | ||
. | ||
$ rm -f nosuch.journal; hledger -f nosuch.journal add; rm -f nosuch.journal | ||
> /Save this transaction to the journal/ | ||
>2 // | ||
|
||
## 18. add checks existing assertions | ||
< | ||
2025-05-10 | ||
x | ||
a | ||
10 USD == 10 USD | ||
b | ||
-10 USD | ||
. | ||
y | ||
2025-05-01 | ||
x | ||
a | ||
10 USD | ||
$ rm -f nosuch.journal; hledger -f nosuch.journal add; rm -f nosuch.journal | ||
> // | ||
>2 /Balance assertion failed in a/ | ||
|
||
|
||
## 18. shouldn't add decimals if there aren't any | ||
## printf '\n\na\n1\nb\n' | hledger -f /dev/null add | ||
# < | ||
# | ||
|
@@ -124,4 +275,3 @@ $ rm -f nosuch.journal; hledger -f nosuch.journal add; rm -f nosuch.journal | |
# b | ||
# $ hledger -f /dev/null add | ||
# > /amount 2 \[-1\]/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I imagine this could be slow if called for a lot of transactions, but we intend it to be called only for one, the one user has just entered during an
add
session. So, minimal user-visible delay even with a big journal and slow machine, hopefully..There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm. I'm not entirely sure I understand what you're saying, but this does need to be run with the entire journal (since we're checking that the most recent transaction slots into the rest of the journal). For what it's worth, I don't claim to have a huge journal, but
hledger stats
says I have over 5000 transactions, andhledger add
with assertions is still quite snappy on my machine.Perhaps a nice compromise is to only do the assertion checking if there's an actual assertion in the given transaction?
I'll note that one side effect of this PR that I hadn't considered until now is that this will also check that any future assertions still hold as a result of the transaction, and we'd lose that by only entering the assertion logic if there is an assertion.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aiee! Good point, I hadn't thought of that. We should probably mention in the manual, and I should mention in release notes: entering an amount during
add
can now fail if it would break any balance assertion in the journal. (Even if you aren't adding a new assertion.) Also it can fail if there was a pre-existing failing assertion. Though in that case,add
probably wouldn't run - unless you run it with-I
. We should probably make sure that the new assertion check also respects-I
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought that this check would skip any assertions, but a quick test seems to indicate otherwise! I'll check this out as well. I'm happy to update the manual as well, and good call on flagging this in the release notes.
It also appears that I might have been ahead of myself and already implemented the "only check if there's an assertion in the transaction" restriction--let me think a bit more about the ramifications of that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would help to not just use
defbalancingopts
everywhere 🤦♂️There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've corrected it so that
-I
works to ignore assertions. With that in place, I think I'll propose removing the restriction of only checking that assertions pass when there's an assertion in the transaction. I added that for performance, but even with the100ktxns-1kaccts.journal
example file, it takes less than a second to analyze an assertion on my laptop.This would let us always make sure that the
add
ed transaction doesn't break existing assertions, which I think is a nice feature, and with-I
, it can be disabled if it's ever undesirable.