;valuation: more tests; document default amount style issue

[ci skip]
This commit is contained in:
Simon Michael 2019-06-14 18:52:13 -07:00
parent 2da50875ee
commit 33d03284c3
3 changed files with 149 additions and 1 deletions

View File

@ -81,13 +81,23 @@ amountApplyValuation prices styles periodend today ismultiperiod v a =
-- given valuation date. (The default valuation commodity is the
-- commodity of the latest applicable market price before the
-- valuation date.)
--
-- The returned amount will have its commodity's canonical style applied,
-- but with the precision adjusted to show all significant decimal digits
-- up to a maximum of 8. (experimental)
--
-- If the market prices available on that date are not sufficient to
-- calculate this value, the amount is left unchanged.
amountValueAtDate :: [PriceDirective] -> M.Map CommoditySymbol AmountStyle -> Maybe CommoditySymbol -> Day -> Amount -> Amount
amountValueAtDate pricedirectives styles mto d a =
case priceLookup pricedirectives d (acommodity a) mto of
Nothing -> a
Just (comm, rate) -> styleAmount styles $ amount{acommodity=comm, aquantity=rate * aquantity a}
Just (comm, rate) ->
-- setNaturalPrecisionUpTo 8 $ -- XXX force higher precision in case amount appears to be zero ?
-- Make default display style use precision 2 instead of 0 ?
-- Leave as is for now; mentioned in manual.
styleAmount styles
amount{acommodity=comm, aquantity=rate * aquantity a}
------------------------------------------------------------------------------
-- Building a price graph

View File

@ -644,6 +644,42 @@ $ hledger -f- print --value=2000-01-15
```
You may need to explicitly set a commodity's display style, when reverse prices are used.
Eg this output might be surprising:
```
P 2000-01-01 A 2B
2000-01-01
a 1B
b
```
```
$ hledger print -x -X A
2000/01/01
a 0
b 0
```
Explanation: because there's no amount or commodity directive specifying a display style
for A, 0.5A gets the default style, which shows no decimal digits. Because the displayed
amount looks like zero, the commodity symbol and minus sign are not displayed either.
Adding a commodity directive sets a more useful display style for A:
```
P 2000-01-01 A 2B
commodity 0.00A
2000-01-01
a 1B
b
```
```
$ hledger print -X A
2000/01/01
a 0.50A
b -0.50A
```
#### Effect of --value on reports
Below is how `--value` affects each of hledger's reports, currently.

View File

@ -0,0 +1,102 @@
; More valuation tests. See also tests/journal/valuation.test.
; some market prices
P 2019-01-01 B 10 A
P 2019-01-01 C 2 B
P 2019-01-01 A 100 D
P 2019-01-01 E 3 D
; a transaction with both amounts in B
2019-06-01
a 1 B
b
; tests follow. This comment directive makes this file readable
; by hledger, as well as shelltest; useful when troubleshooting.
comment
# 1. normal unvalued output
$ hledger -f- print -x
2019/06/01
a 1 B
b -1 B
>=
# 2. current market value in default valuation commodity
$ hledger -f- print -x -V
2019/06/01
a 10 A
b -10 A
>=
# 3. same as above, but request commodity A explicitly
$ hledger -f- print -x --value=now,A
2019/06/01
a 10 A
b -10 A
>=
# 4. request commodity B - no effect
$ hledger -f- print -x --value=now,B
2019/06/01
a 1 B
b -1 B
>=
# 5. request commodity we don't have prices for - no effect
$ hledger -f- print -x --value=now,Z
2019/06/01
a 1 B
b -1 B
>=
# 6. request commodity C - uses reverse of C->B price.
# There's nothing setting C display style, so the default style is used,
# which shows no decimal digits.
# And because that makes it display as zero, the commodity symbol
# and sign are not shown either.
$ hledger -f- print -x --value=now,C
2019/06/01
a 0
b 0
>=
# # There's nothing setting C display style, so the default style is used,
# # but the precision is increased to show the decimal digit
# # (otherwise it would show C0).
# $ hledger -f- print -x --value=now,C
# 2019/06/01
# a C0.5
# b C-0.5
#
# >=
# 7. request commodity D - chains B->A, A->D prices
$ hledger -f- print -x --value=now,D
2019/06/01
a 1000 D
b -1000 D
>=
# 8. request commodity E - chains B->A, A->D, reverse of D->E prices.
# As with C above, E gets the default display style, with precision 0.
$ hledger -f- print -x --value=now,E
2019/06/01
a E333
b E-333
>=
# # As with C above, E gets the default display style, but with the precision
# # increased to show the decimal digits, but no more than 8.
# $ hledger -f- print -x --value=now,E
# 2019/06/01
# a E333.33333333
# b E-333.33333333
#
# >=