hledger/hledger/test/forecast.test
Stephen Morgan c07ad29a87 imp!: forecast: Implements more intuitive logic for the forecast interval. (#1648)
The forecast period begins on:
- the start date supplied to the `--forecast` argument, if present
- otherwise, the later of
  - the report start date if specified with -b/-p/date:
  - the day after the latest normal (non-periodic) transaction in the journal, if any
- otherwise today.
It ends on:
- the end date supplied to the `--forecast` argument, if present
- otherwise the report end date if specified with -e/-p/date:
- otherwise 180 days (6 months) from today.

Note that the previous behaviour did not quite match the documentation,
so this also acts as a bug fix for #1665.
2021-08-26 20:32:30 -10:00

346 lines
12 KiB
Plaintext

# Test generation of periodic transactions with --forecast.
# Note periodic transaction tests should include a regular transaction
# to pin the start date of forecasted transactions.
<
2016/12/31
expenses:housing $600
assets:cash
~ monthly from 2016/1
income $-1000
expenses:food $20
expenses:leisure $15
expenses:grocery $30
assets:cash
# 1. A balance report with forecasted transactions.
$ hledger bal -M -b 2016-11 -e 2017-02 -f - --forecast
Balance changes in 2016-11-01..2017-01-31:
|| 2016-11 2016-12 2017-01
==================++===========================
assets:cash || 0 $-600 $935
expenses:food || 0 0 $20
expenses:grocery || 0 0 $30
expenses:housing || 0 $600 0
expenses:leisure || 0 0 $15
income || 0 0 $-1000
------------------++---------------------------
|| 0 0 0
>=0
<
2016/12/31
expenses:housing $600
assets:cash
~ monthly from 2016/1 * marked cleared, and with a description
income $-1000
expenses:food $20
expenses:leisure $15
expenses:grocery $30
assets:cash
# 2. print forecasted transactions, with status and description.
$ hledger print -b 2016-11 -e 2017-02 -f - --forecast
2016-12-31
expenses:housing $600
assets:cash
2017-01-01 * marked cleared, and with a description
; generated-transaction: ~ monthly from 2016/1
income $-1000
expenses:food $20
expenses:leisure $15
expenses:grocery $30
assets:cash
>=0
<
2016/12/31
expenses:housing $600
assets:cash
~ monthly from 2016/1 ; or a comment, maybe with a:tag
income $-1000
expenses:food $20
expenses:leisure $15
expenses:grocery $30
assets:cash
# 3. A register with forecasted transactions, with a comment and tag.
$ hledger register -b 2016-11 -e 2017-02 -f - --forecast tag:a
2017-01-01 income $-1000 $-1000
expenses:food $20 $-980
expenses:leisure $15 $-965
expenses:grocery $30 $-935
assets:cash $935 0
>=0
<
2016/01/01
expenses:fun $10 ; more fireworks
assets:cash
2016/12/02
expenses:housing $600
assets:cash
~ yearly from 2016
income $-10000 ; bonus
assets:cash
# 4. Check that --forecast generates transactions only after last transaction date in journal.
$ hledger register -b 2015-12 -e 2017-02 -f - assets:cash --forecast
2016-01-01 assets:cash $-10 $-10
2016-12-02 assets:cash $-600 $-610
2017-01-01 assets:cash $10000 $9390
>=0
<
Y 2000
~ 2/1 forecast
; a real transaction to set the start of the forecast window
2000/1/1 real
# 5. Y affects M/D partial dates in periodic transactions.
# The recur tag shows the original period expression and is not modified.
$ hledger -f - print --forecast desc:forecast
2000-02-01 forecast
; generated-transaction: ~ 2/1
>=0
<
Y 2000
~ 15 forecast
; a real transaction to set the start of the forecast window
2000/1/1 real
# 6. Y also sets the month to 1, affecting D dates:
$ hledger -f - print --forecast desc:forecast
2000-01-15 forecast
; generated-transaction: ~ 15
>=0
<
Y 2000
~ next month forecast
; a real transaction to set the start of the forecast window
2000/1/1 real
# 7. Y also sets the day to 1, affecting relative dates:
$ hledger -f - print --forecast desc:forecast
2000-02-01 forecast
; generated-transaction: ~ next month
>=0
<
2016/12/31
expenses:housing $600
assets:cash
~ monthly from 2016/1 salary
income $-1000
assets:cash
# 8. A balance report with forecast-begin enabling transaction before report end
$ hledger bal -M -b 2016-10 -e 2017-02 -f - --forecast=20160801-
Balance changes in 2016-10-01..2017-01-31:
|| 2016-10 2016-11 2016-12 2017-01
==================++====================================
assets:cash || $1000 $1000 $400 $1000
expenses:housing || 0 0 $600 0
income || $-1000 $-1000 $-1000 $-1000
------------------++------------------------------------
|| 0 0 0 0
>=0
# 9. Parse error in malformed forecast period expression
$ hledger bal -M -b 2016-10 -e 2017-02 -f - --forecast=20160801-foobar
>
>2
hledger: could not parse forecast period : 1:10:
|
1 | 20160801-foobar
| ^
unexpected 'f'
expecting end of input
(use -h to see usage)
>=1
<
commodity 1,000.00 USD
2020-01-01
(a) 1,000.00 USD
~ every 28th day
(a) 1000.00 USD
# 10. Amount display style is applied to forecasted transactions.
$ hledger -f - reg --forecast date:202001
2020-01-01 (a) 1,000.00 USD 1,000.00 USD
2020-01-28 (a) 1,000.00 USD 2,000.00 USD
>=0
<
2021-09-01 Normal Balance Assertion Works
Checking = -60
Costs
~ 2021-10-01 explicit forecasted assertion
Checking = -100
Costs 40
~ 2021-10-02 auto-deduced forecasted assertion
Checking = -120
Costs
# 11. Forecast transactions work with balance assignments
$ hledger -f - print -x --forecast -e 2021-11
2021-09-01 Normal Balance Assertion Works
Checking -60 = -60
Costs 60
2021-10-01 explicit forecasted assertion
; generated-transaction: ~ 2021-10-01
Checking -40 = -100
Costs 40
2021-10-02 auto-deduced forecasted assertion
; generated-transaction: ~ 2021-10-02
Checking -20 = -120
Costs 20
>=0
<
2021-08-01
(a) 0
~ every weekday
income:client1 -10 USD
assets:receivables:contractor1
# 12. Generated forecast for weekday transactions
$ hledger -f - reg --forecast -b "2021-09-01" -e "2021-09-15" --forecast -w 100
2021-09-01 income:client1 -10 USD -10 USD
assets:receivables:contractor1 10 USD 0
2021-09-02 income:client1 -10 USD -10 USD
assets:receivables:contractor1 10 USD 0
2021-09-03 income:client1 -10 USD -10 USD
assets:receivables:contractor1 10 USD 0
2021-09-06 income:client1 -10 USD -10 USD
assets:receivables:contractor1 10 USD 0
2021-09-07 income:client1 -10 USD -10 USD
assets:receivables:contractor1 10 USD 0
2021-09-08 income:client1 -10 USD -10 USD
assets:receivables:contractor1 10 USD 0
2021-09-09 income:client1 -10 USD -10 USD
assets:receivables:contractor1 10 USD 0
2021-09-10 income:client1 -10 USD -10 USD
assets:receivables:contractor1 10 USD 0
2021-09-13 income:client1 -10 USD -10 USD
assets:receivables:contractor1 10 USD 0
2021-09-14 income:client1 -10 USD -10 USD
assets:receivables:contractor1 10 USD 0
>=0
<
2021-08-01
(a) 0
~ every weekendday
income:client1 -10 USD
assets:receivables:contractor1
# 13. Generated forecast for weekend transactions
$ hledger -f - reg --forecast -b "2021-09-01" -e "2021-09-15" --forecast -w 100
2021-09-04 income:client1 -10 USD -10 USD
assets:receivables:contractor1 10 USD 0
2021-09-05 income:client1 -10 USD -10 USD
assets:receivables:contractor1 10 USD 0
2021-09-11 income:client1 -10 USD -10 USD
assets:receivables:contractor1 10 USD 0
2021-09-12 income:client1 -10 USD -10 USD
assets:receivables:contractor1 10 USD 0
>=0
<
2021-01-01
(a) 1000
~ daily
(a) 1
# 14. Arguments to --forecast take precedence over anything. Only generate up to the day before the end date.
$ hledger -f - reg --forecast="2020-01-01..2020-01-05" -b 2019-12-01 -e 2020-02-01 -H
2020-01-01 (a) 1 1
2020-01-02 (a) 1 2
2020-01-03 (a) 1 3
2020-01-04 (a) 1 4
>=0
# 15. With no arguments to --forecast, we use the report start date if it's after the journal end date.
$ hledger -f - reg --forecast -b 2021-02-01 -e 2021-02-05 -H
2021-02-01 (a) 1 1001
2021-02-02 (a) 1 1002
2021-02-03 (a) 1 1003
2021-02-04 (a) 1 1004
>=0
# 16. With no arguments to --forecast, we use journal end date if it's after the report start date.
$ hledger -f - reg --forecast -b 2020-12-01 -e 2021-01-05 -H
2021-01-01 (a) 1000 1000
2021-01-02 (a) 1 1001
2021-01-03 (a) 1 1002
2021-01-04 (a) 1 1003
>=0
# 17. With no arguments to --forecast, and no report start, generate from journal end to 180 days from today.
# We use here the fact that we are at least 180 days from 2021-01-01. This test will fail if you travel back in time!
$ hledger -f - reg --forecast -H
> /1 1360/
>=0
<
~ daily
(a) 1
# 18. No real transactions.
# Arguments to --forecast take precedence over anything. Only generate up to the day before the end date.
$ hledger -f - reg --forecast="2020-01-01..2020-01-05" -b 2019-12-01 -e 2020-01-05 -H
2020-01-01 (a) 1 1
2020-01-02 (a) 1 2
2020-01-03 (a) 1 3
2020-01-04 (a) 1 4
>=0
# 19. No real transactions.
# With no arguments to --forecast, we use the report start date.
$ hledger -f - reg --forecast -b 2021-02-01 -e 2021-02-05 -H
2021-02-01 (a) 1 1
2021-02-02 (a) 1 2
2021-02-03 (a) 1 3
2021-02-04 (a) 1 4
>=0
# 20. No real transactions.
# With no arguments to --forecast, and no report start, generate from today to 180 days from today.
$ hledger -f - reg --forecast -H
> /1 180/
>=0