@@ -4,8 +4,10 @@ import (
44 "encoding/json"
55 "fmt"
66 "math"
7+ "math/big"
78 "testing"
89
10+ "github.com/jackc/pgx/v5/pgtype"
911 "github.com/oasisprotocol/oasis-core/go/common/cbor"
1012 "github.com/stretchr/testify/require"
1113)
@@ -122,3 +124,53 @@ func TestBigDecimalNumeric(t *testing.T) {
122124 require .EqualValues (t , dec , roundTripped , "BigDecimal should match after Numeric conversion for value %s" , tc .value )
123125 }
124126}
127+
128+ func TestNumericToBigInt (t * testing.T ) {
129+ for _ , tc := range []struct {
130+ name string
131+ numeric pgtype.Numeric
132+ expected int64
133+ hasError bool
134+ }{
135+ {
136+ name : "zero exponent" ,
137+ numeric : pgtype.Numeric {Int : big .NewInt (12345 ), Exp : 0 , Valid : true },
138+ expected : 12345 ,
139+ },
140+ {
141+ name : "positive exponent" ,
142+ numeric : pgtype.Numeric {Int : big .NewInt (123 ), Exp : 2 , Valid : true },
143+ expected : 12300 ,
144+ },
145+ {
146+ name : "negative exponent exact division" ,
147+ numeric : pgtype.Numeric {Int : big .NewInt (12300 ), Exp : - 2 , Valid : true },
148+ expected : 123 ,
149+ },
150+ {
151+ name : "negative exponent with remainder" ,
152+ numeric : pgtype.Numeric {Int : big .NewInt (12345 ), Exp : - 2 , Valid : true },
153+ hasError : true ,
154+ },
155+ {
156+ name : "zero value" ,
157+ numeric : pgtype.Numeric {Int : big .NewInt (0 ), Exp : 0 , Valid : true },
158+ expected : 0 ,
159+ },
160+ {
161+ name : "negative value" ,
162+ numeric : pgtype.Numeric {Int : big .NewInt (- 500 ), Exp : 1 , Valid : true },
163+ expected : - 5000 ,
164+ },
165+ } {
166+ t .Run (tc .name , func (t * testing.T ) {
167+ result , err := NumericToBigInt (tc .numeric )
168+ if tc .hasError {
169+ require .Error (t , err )
170+ } else {
171+ require .NoError (t , err )
172+ require .Equal (t , tc .expected , result .Int64 ())
173+ }
174+ })
175+ }
176+ }
0 commit comments