Add pgtype error cases

v3-numeric-wip
Jack Christensen 2017-03-03 18:39:52 -06:00
parent 5b861d0a5f
commit 6a3b22cee8
6 changed files with 96 additions and 4 deletions

View File

@ -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)
}
}
}

View File

@ -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)
}
}
}

View File

@ -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)
}
}
}

View File

@ -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)
}
}
}

View File

@ -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() {

View File

@ -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)
}
}
}