Add some unit-tests for numeric
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing

This commit is contained in:
Jan Bader 2022-02-23 21:52:36 +00:00
parent 28c20aacd3
commit ea6d198bff
2 changed files with 104 additions and 3 deletions

View File

@ -17,6 +17,19 @@ func Zero() Numeric {
return Numeric{pgtype.Numeric{Exp: 0, Int: big.NewInt(0), Status: pgtype.Present, NaN: false}} return Numeric{pgtype.Numeric{Exp: 0, Int: big.NewInt(0), Status: pgtype.Present, NaN: false}}
} }
func FromInt64(value int64) Numeric {
num := Numeric{}
num.Set(value)
return num
}
func FromInt64WithExp(value int64, exp int32) Numeric {
num := Numeric{}
num.Set(value)
num.Exp = exp
return num
}
func (n Numeric) GetFloat64() float64 { func (n Numeric) GetFloat64() float64 {
if n.Status != pgtype.Present { if n.Status != pgtype.Present {
return 0 return 0
@ -168,10 +181,16 @@ func (n Numeric) MarshalJSON() ([]byte, error) {
return bytesWithSeparator, nil return bytesWithSeparator, nil
} }
func Parse(text string) (Numeric, error) { func MustParse(text string) Numeric {
// Remove trailing currency num, err := Parse(text)
text = trimLastChar(text) if err != nil {
panic(err)
}
return num
}
func Parse(text string) (Numeric, error) {
// Unify decimal separator // Unify decimal separator
text = strings.Replace(text, ",", ".", 1) text = strings.Replace(text, ",", ".", 1)
@ -184,6 +203,13 @@ func Parse(text string) (Numeric, error) {
return num, nil return num, nil
} }
func ParseCurrency(text string) (Numeric, error) {
// Remove trailing currency
text = trimLastChar(text)
return Parse(text)
}
func trimLastChar(s string) string { func trimLastChar(s string) string {
r, size := utf8.DecodeLastRuneInString(s) r, size := utf8.DecodeLastRuneInString(s)
if r == utf8.RuneError && (size == 0 || size == 1) { if r == utf8.RuneError && (size == 0 || size == 1) {

View File

@ -0,0 +1,75 @@
package numeric_test
import (
"testing"
"git.javil.eu/jacob1123/budgeteer/postgres/numeric"
)
type TestCaseMarshalJSON struct {
Value numeric.Numeric
Result string
}
func TestMarshalJSON(t *testing.T) {
tests := []TestCaseMarshalJSON{
{numeric.Zero(), `0`},
{numeric.MustParse("1.23"), "1.23"},
{numeric.MustParse("1,24"), "1.24"},
{numeric.MustParse("123456789.12345"), "123456789.12345"},
}
for _, test := range tests {
t.Run(test.Result, func(t *testing.T) {
z := test.Value
result, err := z.MarshalJSON()
if err != nil {
t.Error(err)
return
}
if string(result) != test.Result {
t.Errorf("Expected %s, got %s", test.Result, string(result))
return
}
})
}
}
type TestCaseParse struct {
Result numeric.Numeric
Value string
}
func TestParse(t *testing.T) {
tests := []TestCaseParse{
{numeric.Zero(), `0`},
{numeric.FromInt64(1), `1`},
{numeric.FromInt64WithExp(1, 1), `10`},
{numeric.FromInt64WithExp(1, 2), `100`},
{numeric.MustParse("1.23"), "1.23"},
{numeric.MustParse("1,24"), "1.24"},
{numeric.MustParse("123456789.12345"), "123456789.12345"},
}
for _, test := range tests {
t.Run(test.Value, func(t *testing.T) {
result, err := numeric.Parse(test.Value)
if err != nil {
t.Error(err)
return
}
if test.Result.Int.Int64() != result.Int.Int64() {
t.Errorf("Expected int %d, got %d", test.Result.Int, result.Int)
return
}
if test.Result.Exp != result.Exp {
t.Errorf("Expected exp %d, got %d", test.Result.Exp, result.Exp)
return
}
// if string(result) != test.Result {
// return
//}
})
}
}