diff options
author | Meng-Ying Yang <garfield@dexon.org> | 2018-12-25 16:51:08 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@byzantine-lab.io> | 2019-06-12 17:27:20 +0800 |
commit | a96cea486d4a74b7bd8ada1f004b219e7d8203c4 (patch) | |
tree | 9c91a0919037eab407e23ef462e006258c239dfd /common/big_test.go | |
parent | 9437d34cb57503309b25ea92808705da1d78ac44 (diff) | |
download | go-tangerine-a96cea486d4a74b7bd8ada1f004b219e7d8203c4.tar go-tangerine-a96cea486d4a74b7bd8ada1f004b219e7d8203c4.tar.gz go-tangerine-a96cea486d4a74b7bd8ada1f004b219e7d8203c4.tar.bz2 go-tangerine-a96cea486d4a74b7bd8ada1f004b219e7d8203c4.tar.lz go-tangerine-a96cea486d4a74b7bd8ada1f004b219e7d8203c4.tar.xz go-tangerine-a96cea486d4a74b7bd8ada1f004b219e7d8203c4.tar.zst go-tangerine-a96cea486d4a74b7bd8ada1f004b219e7d8203c4.zip |
core: add database/sql support for more types (#102)
* core: types: add database/sql support for BlockNonce
* common: add database/sql support with Big
New Big type is declared to let big.Int support database/sql by
implementing Scan() and Value() on new type.
Diffstat (limited to 'common/big_test.go')
-rw-r--r-- | common/big_test.go | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/common/big_test.go b/common/big_test.go new file mode 100644 index 000000000..0339b4760 --- /dev/null +++ b/common/big_test.go @@ -0,0 +1,95 @@ +package common + +import ( + "database/sql/driver" + "math/big" + "reflect" + "testing" +) + +func TestBig_Scan(t *testing.T) { + type args struct { + src interface{} + } + tests := []struct { + name string + args args + value Big + wantErr bool + }{ + { + name: "scan int64", + args: args{src: int64(-10)}, + value: Big(*big.NewInt(-10)), + wantErr: false, + }, + { + name: "scan uint64", + args: args{src: uint64(10)}, + value: Big(*big.NewInt(10)), + wantErr: false, + }, + { + name: "scan bytes", + args: args{src: []byte{0x0a}}, + value: Big(*big.NewInt(10)), + wantErr: false, + }, + { + name: "scan string", + args: args{src: "-10"}, + value: Big(*big.NewInt(-10)), + wantErr: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := &Big{} + if err := b.Scan(tt.args.src); (err != nil) != tt.wantErr { + t.Errorf("Big.Scan() error = %v, wantErr %v", err, tt.wantErr) + } + + if !tt.wantErr { + if !reflect.DeepEqual(*b, tt.value) { + t.Errorf( + "Big.Scan() wrong value (got: %v, want: %v)", + *b, tt.value, + ) + } + } + }) + } + +} +func TestBig_Value(t *testing.T) { + r := "12345" + b := Big(*big.NewInt(12345)) + tests := []struct { + name string + b Big + want driver.Value + wantErr bool + }{ + { + name: "working value", + b: b, + want: r, + wantErr: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tt.b.Value() + if (err != nil) != tt.wantErr { + t.Errorf("Big.Value() error = %v, wantErr %v", err, tt.wantErr) + return + } + + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("Hash.Value() = %v, want %v", got, tt.want) + } + }) + } +} |