This commit is contained in:
Lunny Xiao 2024-03-18 14:02:02 +08:00
parent 63f54e633a
commit c4b5433155
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
1 changed files with 8 additions and 7 deletions

View File

@ -635,12 +635,14 @@ func (d *ZDecimal) FromDB(data []byte) error {
func (d ZDecimal) ToDB() ([]byte, error) { func (d ZDecimal) ToDB() ([]byte, error) {
return []byte(fmt.Sprintf("%d", (d.value).Int64())), nil return []byte(fmt.Sprintf("%d", (d.value).Int64())), nil
} }
func (d ZDecimal) IsZero() bool { func (d ZDecimal) IsZero() bool {
if d.value == nil { if d.value == nil {
return true return true
} }
return d.value.Sign() == 0 return d.value.Sign() == 0
} }
func (d ZDecimal) String() string { func (d ZDecimal) String() string {
if d.value == nil { if d.value == nil {
return "0" return "0"
@ -649,16 +651,16 @@ func (d ZDecimal) String() string {
} }
func TestZDecimal(t *testing.T) { func TestZDecimal(t *testing.T) {
type MyMoney struct { type ZMyMoney struct {
Id int64 Id int64
Account string Account string
Amount ZDecimal Amount ZDecimal
} }
assert.NoError(t, PrepareEngine()) assert.NoError(t, PrepareEngine())
assertSync(t, new(MyMoney)) assertSync(t, new(ZMyMoney))
_, err := testEngine.Insert(&MyMoney{ _, err := testEngine.Insert(&ZMyMoney{
Account: "test", Account: "test",
Amount: ZDecimal{ Amount: ZDecimal{
value: big.NewInt(10000000000000000), value: big.NewInt(10000000000000000),
@ -666,19 +668,19 @@ func TestZDecimal(t *testing.T) {
}) })
assert.NoError(t, err) assert.NoError(t, err)
var m = MyMoney{ m := ZMyMoney{
Id: 1, Id: 1,
} }
has, err := testEngine.Get(&m) has, err := testEngine.Get(&m)
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, has) assert.True(t, has)
_, err = testEngine.Update(&MyMoney{ _, err = testEngine.Update(&ZMyMoney{
Id: 1, Id: 1,
Account: "test2", Account: "test2",
}) })
assert.NoError(t, err) assert.NoError(t, err)
var m2 = MyMoney{ m2 := ZMyMoney{
Id: 1, Id: 1,
} }
has, err = testEngine.Get(&m2) has, err = testEngine.Get(&m2)
@ -688,5 +690,4 @@ func TestZDecimal(t *testing.T) {
assert.Equal(t, "test2", "test2") assert.Equal(t, "test2", "test2")
assert.False(t, m.Amount.IsZero()) assert.False(t, m.Amount.IsZero())
assert.Equal(t, "10000000000000000", m.Amount.String()) assert.Equal(t, "10000000000000000", m.Amount.String())
} }