a16aecfa96
Why: big.Float is not an ideal type for dealing with monetary amounts, because no matter how high the precision, some non-integer decimal values can not be represented exactly in base-2 floating point. Also, storing gob-encoded big.Float values in the database makes it very hard to use those values in meaningful queries, making it difficult to do any sort of analysis on billing. For better accuracy, then, we can just represent monetary values as integers (in whatever base units are appropriate for the currency). For example, STORJ tokens or Bitcoins can not be split into pieces smaller than 10^-8, so we can store amounts of STORJ or BTC with precision simply by moving the decimal point 8 digits to the right. For USD values (assuming we don't want to deal with fractional cents), we can move the decimal point 2 digits to the right. To make it easier and less error-prone to deal with the math involved, I introduce here a new type, monetary.Amount, instances of which have an associated value _and_ a currency. Change-Id: I03395d52f0e2473cf301361f6033722b54640265
100 lines
3.6 KiB
Go
100 lines
3.6 KiB
Go
// Copyright (C) 2021 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package monetary
|
|
|
|
import (
|
|
"math/big"
|
|
"testing"
|
|
|
|
"github.com/shopspring/decimal"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
var (
|
|
// If we use more than 18 decimal places, baseUnits values will overflow int64
|
|
// even if there is only one digit to the left of the decimal point.
|
|
manyDigitsCurrency = &Currency{name: "manyDigitsCurrency", symbol: "mdc", decimalPlaces: 18}
|
|
noDigitsCurrency = &Currency{name: "noDigitsCurrency", symbol: "ndc", decimalPlaces: 0}
|
|
)
|
|
|
|
func TestAmountFromBigFloatAndAmountAsBigFloat(t *testing.T) {
|
|
parseFloat := func(s string) *big.Float {
|
|
bf, _, err := big.ParseFloat(s, 10, 256, big.ToNearestEven)
|
|
if err != nil {
|
|
t.Fatalf("failed to parse %q as float: %v", s, err)
|
|
}
|
|
return bf
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
floatValue *big.Float
|
|
currency *Currency
|
|
baseUnits int64
|
|
wantErr bool
|
|
}{
|
|
{"zero", big.NewFloat(0), StorjToken, 0, false},
|
|
{"one", big.NewFloat(1), USDollars, 100, false},
|
|
{"negative", big.NewFloat(-1), Bitcoin, -100000000, false},
|
|
{"smallest", big.NewFloat(1e-8), StorjToken, 1, false},
|
|
{"minus smallest", big.NewFloat(-1e-8), StorjToken, -1, false},
|
|
{"one+delta", parseFloat("1.000000000000000001"), manyDigitsCurrency, 1000000000000000001, false},
|
|
{"minus one+delta", parseFloat("-1.000000000000000001"), manyDigitsCurrency, -1000000000000000001, false},
|
|
{"large number", parseFloat("4611686018427387904.0"), noDigitsCurrency, 4611686018427387904, false},
|
|
{"infinity", parseFloat("Inf"), StorjToken, 0, true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := AmountFromBigFloat(tt.floatValue, tt.currency)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("AmountFromBigFloat() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if tt.wantErr {
|
|
return
|
|
}
|
|
want := Amount{baseUnits: tt.baseUnits, currency: tt.currency}
|
|
assert.Equal(t, want, got)
|
|
assert.Equal(t, tt.baseUnits, got.BaseUnits())
|
|
|
|
fullPrecValue, err := tt.floatValue.MarshalText()
|
|
require.NoError(t, err)
|
|
gotAsFloat := got.AsBigFloatWithPrecision(tt.floatValue.Prec())
|
|
fullPrecGot, err := gotAsFloat.MarshalText()
|
|
require.NoError(t, err)
|
|
|
|
assert.Truef(t, tt.floatValue.Cmp(gotAsFloat) == 0,
|
|
"(expected) %v != (got) %v", string(fullPrecValue), string(fullPrecGot))
|
|
})
|
|
}
|
|
}
|
|
func TestAmountFromDecimalAndAmountAsDecimal(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
decimalValue decimal.Decimal
|
|
currency *Currency
|
|
baseUnits int64
|
|
wantErr bool
|
|
}{
|
|
{"zero", decimal.Decimal{}, StorjToken, 0, false},
|
|
{"one", decimal.NewFromInt(1), USDollars, 100, false},
|
|
{"negative", decimal.NewFromInt(-1), Bitcoin, -100000000, false},
|
|
{"smallest", decimal.NewFromFloat(1e-8), StorjToken, 1, false},
|
|
{"one+delta", decimal.RequireFromString("1.000000000000000001"), manyDigitsCurrency, 1000000000000000001, false},
|
|
{"minus one+delta", decimal.RequireFromString("-1.000000000000000001"), manyDigitsCurrency, -1000000000000000001, false},
|
|
{"large number", decimal.RequireFromString("4611686018427387904.0"), noDigitsCurrency, 4611686018427387904, false},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := AmountFromDecimal(tt.decimalValue, tt.currency)
|
|
want := Amount{baseUnits: tt.baseUnits, currency: tt.currency}
|
|
assert.Equal(t, want, got)
|
|
assert.Equal(t, tt.baseUnits, got.BaseUnits())
|
|
assert.Truef(t, tt.decimalValue.Equal(got.AsDecimal()),
|
|
"%v != %v", tt.decimalValue, got.AsDecimal())
|
|
})
|
|
}
|
|
}
|