diff --git a/pgtype/date_test.go b/pgtype/date_test.go index 65d743e9..3a473b6a 100644 --- a/pgtype/date_test.go +++ b/pgtype/date_test.go @@ -94,4 +94,20 @@ func TestDateAssignTo(t *testing.T) { t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) } } + + errorTests := []struct { + src pgtype.Date + dst interface{} + }{ + {src: pgtype.Date{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), InfinityModifier: pgtype.Infinity, Status: pgtype.Present}, dst: &tim}, + {src: pgtype.Date{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), InfinityModifier: pgtype.NegativeInfinity, Status: pgtype.Present}, dst: &tim}, + {src: pgtype.Date{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Null}, dst: &tim}, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } } diff --git a/pgtype/int2_test.go b/pgtype/int2_test.go index 1074c9b5..8601309d 100644 --- a/pgtype/int2_test.go +++ b/pgtype/int2_test.go @@ -118,4 +118,24 @@ func TestInt2AssignTo(t *testing.T) { t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) } } + + errorTests := []struct { + src pgtype.Int2 + dst interface{} + }{ + {src: pgtype.Int2{Int: 150, Status: pgtype.Present}, dst: &i8}, + {src: pgtype.Int2{Int: -1, Status: pgtype.Present}, dst: &ui8}, + {src: pgtype.Int2{Int: -1, Status: pgtype.Present}, dst: &ui16}, + {src: pgtype.Int2{Int: -1, Status: pgtype.Present}, dst: &ui32}, + {src: pgtype.Int2{Int: -1, Status: pgtype.Present}, dst: &ui64}, + {src: pgtype.Int2{Int: -1, Status: pgtype.Present}, dst: &ui}, + {src: pgtype.Int2{Int: 0, Status: pgtype.Null}, dst: &i16}, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } } diff --git a/pgtype/int4_test.go b/pgtype/int4_test.go index cd57e2c9..0ac2e5b5 100644 --- a/pgtype/int4_test.go +++ b/pgtype/int4_test.go @@ -118,4 +118,25 @@ func TestInt4AssignTo(t *testing.T) { t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) } } + + errorTests := []struct { + src pgtype.Int4 + dst interface{} + }{ + {src: pgtype.Int4{Int: 150, Status: pgtype.Present}, dst: &i8}, + {src: pgtype.Int4{Int: 40000, Status: pgtype.Present}, dst: &i16}, + {src: pgtype.Int4{Int: -1, Status: pgtype.Present}, dst: &ui8}, + {src: pgtype.Int4{Int: -1, Status: pgtype.Present}, dst: &ui16}, + {src: pgtype.Int4{Int: -1, Status: pgtype.Present}, dst: &ui32}, + {src: pgtype.Int4{Int: -1, Status: pgtype.Present}, dst: &ui64}, + {src: pgtype.Int4{Int: -1, Status: pgtype.Present}, dst: &ui}, + {src: pgtype.Int4{Int: 0, Status: pgtype.Null}, dst: &i32}, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } } diff --git a/pgtype/int8_test.go b/pgtype/int8_test.go index f9d8646f..15762a50 100644 --- a/pgtype/int8_test.go +++ b/pgtype/int8_test.go @@ -118,4 +118,26 @@ func TestInt8AssignTo(t *testing.T) { t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) } } + + errorTests := []struct { + src pgtype.Int8 + dst interface{} + }{ + {src: pgtype.Int8{Int: 150, Status: pgtype.Present}, dst: &i8}, + {src: pgtype.Int8{Int: 40000, Status: pgtype.Present}, dst: &i16}, + {src: pgtype.Int8{Int: 5000000000, Status: pgtype.Present}, dst: &i32}, + {src: pgtype.Int8{Int: -1, Status: pgtype.Present}, dst: &ui8}, + {src: pgtype.Int8{Int: -1, Status: pgtype.Present}, dst: &ui16}, + {src: pgtype.Int8{Int: -1, Status: pgtype.Present}, dst: &ui32}, + {src: pgtype.Int8{Int: -1, Status: pgtype.Present}, dst: &ui64}, + {src: pgtype.Int8{Int: -1, Status: pgtype.Present}, dst: &ui}, + {src: pgtype.Int8{Int: 0, Status: pgtype.Null}, dst: &i64}, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } } diff --git a/pgtype/timestamptz.go b/pgtype/timestamptz.go index 4f08cd2a..721c8084 100644 --- a/pgtype/timestamptz.go +++ b/pgtype/timestamptz.go @@ -55,10 +55,7 @@ func (src *Timestamptz) AssignTo(dst interface{}) error { // if dst is a pointer to pointer, strip the pointer and try again case reflect.Ptr: if src.Status == Null { - if !el.IsNil() { - // if the destination pointer is not nil, nil it out - el.Set(reflect.Zero(el.Type())) - } + el.Set(reflect.Zero(el.Type())) return nil } if el.IsNil() { diff --git a/pgtype/timestamptz_test.go b/pgtype/timestamptz_test.go index adb72620..8f80ca81 100644 --- a/pgtype/timestamptz_test.go +++ b/pgtype/timestamptz_test.go @@ -103,4 +103,20 @@ func TestTimestamptzAssignTo(t *testing.T) { t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) } } + + errorTests := []struct { + src pgtype.Timestamptz + dst interface{} + }{ + {src: pgtype.Timestamptz{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), InfinityModifier: pgtype.Infinity, Status: pgtype.Present}, dst: &tim}, + {src: pgtype.Timestamptz{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), InfinityModifier: pgtype.NegativeInfinity, Status: pgtype.Present}, dst: &tim}, + {src: pgtype.Timestamptz{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Null}, dst: &tim}, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } }