-
Notifications
You must be signed in to change notification settings - Fork 0
/
big_int_test.go
63 lines (54 loc) · 1.59 KB
/
big_int_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package sqlutil_test
import (
"database/sql/driver"
"errors"
"math/big"
"reflect"
"testing"
"time"
"github.com/modcloth/sqlutil"
)
func TestBigIntValue(t *testing.T) {
var valueTests = []struct {
n sqlutil.BigInt
expected driver.Value
}{
{sqlutil.BigInt{Int: *big.NewInt(2)}, "2"},
{sqlutil.BigInt{Int: *big.NewInt(1844674407370955)}, "1844674407370955"},
{sqlutil.BigInt{Int: *big.NewInt(-1)}, "-1"},
}
for _, tt := range valueTests {
actual, err := tt.n.Value()
if err != nil {
t.Errorf("%+v.Value(): got error: %+v", tt.n, err)
}
if actual != tt.expected {
t.Errorf("%+v.Value(): expected %s, actual %s", tt.n, tt.expected, actual)
}
}
}
func TestBigIntScan(t *testing.T) {
var tests = []struct {
n driver.Value
expected sqlutil.BigInt
err error
}{
{int64(2), sqlutil.BigInt{Int: *big.NewInt(2)}, nil},
{float64(2.2), sqlutil.BigInt{}, errors.New("couldn't scan float64")},
{true, sqlutil.BigInt{}, errors.New("couldn't scan bool")},
{[]byte("9"), sqlutil.BigInt{Int: *big.NewInt(9)}, nil},
{"2", sqlutil.BigInt{Int: *big.NewInt(2)}, nil},
{time.Now(), sqlutil.BigInt{}, errors.New("couldn't scan time.Time")},
{nil, sqlutil.BigInt{}, errors.New("couldn't scan <nil>")},
}
for _, tt := range tests {
actual := sqlutil.BigInt{}
err := actual.Scan(tt.n)
if !reflect.DeepEqual(err, tt.err) {
t.Errorf("%+v.Scan(%v): expected error %+v, got error: %+v", actual, tt.n, tt.err, err)
}
if actual.Int.Cmp(&tt.expected.Int) != 0 {
t.Errorf("%+v.Scan(%v): expected %+v, actual: %+v", actual, tt.n, tt.expected, actual)
}
}
}