Add tests for big.Int usage

This commit is contained in:
Lunny Xiao 2021-06-06 15:31:42 +08:00
parent d0219c37a8
commit 6d2e256c61
1 changed files with 48 additions and 0 deletions

View File

@ -7,6 +7,8 @@ package integrations
import ( import (
"errors" "errors"
"fmt" "fmt"
"math/big"
"strconv"
"testing" "testing"
"xorm.io/xorm" "xorm.io/xorm"
@ -402,3 +404,49 @@ func TestUnsigned(t *testing.T) {
assert.False(t, true, "Unsigned is not implemented") assert.False(t, true, "Unsigned is not implemented")
} }
} }
type MyDecimal big.Int
func (d *MyDecimal) FromDB(data []byte) error {
i, _ := strconv.ParseInt(string(data), 10, 64)
if d == nil {
d = (*MyDecimal)(big.NewInt(i))
} else {
(*big.Int)(d).SetInt64(i)
}
return nil
}
func (d *MyDecimal) ToDB() ([]byte, error) {
return []byte(fmt.Sprintf("%d", (*big.Int)(d).Int64())), nil
}
func (d *MyDecimal) AsBigInt() *big.Int {
return (*big.Int)(d)
}
func (d *MyDecimal) AsInt64() int64 {
return d.AsBigInt().Int64()
}
func TestDecimal(t *testing.T) {
type MyMoney struct {
Id int64
Account *MyDecimal
}
assert.NoError(t, PrepareEngine())
assertSync(t, new(MyMoney))
_, err := testEngine.Insert(&MyMoney{
Account: (*MyDecimal)(big.NewInt(10000000000000000)),
})
assert.NoError(t, err)
var m MyMoney
has, err := testEngine.Get(&m)
assert.NoError(t, err)
assert.True(t, has)
assert.NotNil(t, m.Account)
assert.EqualValues(t, 10000000000000000, m.Account.AsInt64())
}