From 9e5d81d8f5de69cc65868fc6bdfe5e1f7bb59eae Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Fri, 3 Mar 2017 17:59:26 -0600 Subject: [PATCH] Add test for pgtype.Int2.AssignTo --- pgtype/bool_test.go | 2 -- pgtype/int2_test.go | 70 +++++++++++++++++++++++++++++++++++++++++-- pgtype/pgtype_test.go | 5 ++++ 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/pgtype/bool_test.go b/pgtype/bool_test.go index 74140b5e..374f07da 100644 --- a/pgtype/bool_test.go +++ b/pgtype/bool_test.go @@ -7,8 +7,6 @@ import ( "github.com/jackc/pgx/pgtype" ) -type _bool bool - func TestBoolTranscode(t *testing.T) { testSuccessfulTranscode(t, "bool", []interface{}{ pgtype.Bool{Bool: false, Status: pgtype.Present}, diff --git a/pgtype/int2_test.go b/pgtype/int2_test.go index a8493a16..1074c9b5 100644 --- a/pgtype/int2_test.go +++ b/pgtype/int2_test.go @@ -2,6 +2,7 @@ package pgtype_test import ( "math" + "reflect" "testing" "github.com/jackc/pgx/pgtype" @@ -19,8 +20,6 @@ func TestInt2Transcode(t *testing.T) { } func TestInt2ConvertFrom(t *testing.T) { - type _int8 int8 - successfulTests := []struct { source interface{} result pgtype.Int2 @@ -53,3 +52,70 @@ func TestInt2ConvertFrom(t *testing.T) { } } } + +func TestInt2AssignTo(t *testing.T) { + var i8 int8 + var i16 int16 + var i32 int32 + var i64 int64 + var i int + var ui8 uint8 + var ui16 uint16 + var ui32 uint32 + var ui64 uint64 + var ui uint + var pi8 *int8 + var _i8 _int8 + var _pi8 *_int8 + + simpleTests := []struct { + src pgtype.Int2 + dst interface{} + expected interface{} + }{ + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &i8, expected: int8(42)}, + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &i16, expected: int16(42)}, + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &i32, expected: int32(42)}, + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &i64, expected: int64(42)}, + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &i, expected: int(42)}, + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &ui8, expected: uint8(42)}, + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &ui16, expected: uint16(42)}, + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &ui32, expected: uint32(42)}, + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &ui64, expected: uint64(42)}, + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &ui, expected: uint(42)}, + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &_i8, expected: _int8(42)}, + {src: pgtype.Int2{Int: 0, Status: pgtype.Null}, dst: &pi8, expected: ((*int8)(nil))}, + {src: pgtype.Int2{Int: 0, Status: pgtype.Null}, dst: &_pi8, expected: ((*_int8)(nil))}, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + pointerAllocTests := []struct { + src pgtype.Int2 + dst interface{} + expected interface{} + }{ + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &pi8, expected: int8(42)}, + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &_pi8, expected: _int8(42)}, + } + + for i, tt := range pointerAllocTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } +} diff --git a/pgtype/pgtype_test.go b/pgtype/pgtype_test.go index a1a575f7..32ebebfe 100644 --- a/pgtype/pgtype_test.go +++ b/pgtype/pgtype_test.go @@ -11,6 +11,11 @@ import ( "github.com/jackc/pgx/pgtype" ) +// Test for renamed types +type _bool bool +type _int8 int8 +type _int16 int16 + func mustConnectPgx(t testing.TB) *pgx.Conn { config, err := pgx.ParseURI(os.Getenv("DATABASE_URL")) if err != nil {