1package pgtype_test 2 3import ( 4 "math" 5 "reflect" 6 "testing" 7 8 "github.com/jackc/pgtype" 9 "github.com/jackc/pgtype/testutil" 10) 11 12func TestQCharTranscode(t *testing.T) { 13 testutil.TestPgxSuccessfulTranscodeEqFunc(t, `"char"`, []interface{}{ 14 &pgtype.QChar{Int: math.MinInt8, Status: pgtype.Present}, 15 &pgtype.QChar{Int: -1, Status: pgtype.Present}, 16 &pgtype.QChar{Int: 0, Status: pgtype.Present}, 17 &pgtype.QChar{Int: 1, Status: pgtype.Present}, 18 &pgtype.QChar{Int: math.MaxInt8, Status: pgtype.Present}, 19 &pgtype.QChar{Int: 0, Status: pgtype.Null}, 20 }, func(a, b interface{}) bool { 21 return reflect.DeepEqual(a, b) 22 }) 23} 24 25func TestQCharSet(t *testing.T) { 26 successfulTests := []struct { 27 source interface{} 28 result pgtype.QChar 29 }{ 30 {source: int8(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}}, 31 {source: int16(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}}, 32 {source: int32(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}}, 33 {source: int64(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}}, 34 {source: int8(-1), result: pgtype.QChar{Int: -1, Status: pgtype.Present}}, 35 {source: int16(-1), result: pgtype.QChar{Int: -1, Status: pgtype.Present}}, 36 {source: int32(-1), result: pgtype.QChar{Int: -1, Status: pgtype.Present}}, 37 {source: int64(-1), result: pgtype.QChar{Int: -1, Status: pgtype.Present}}, 38 {source: uint8(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}}, 39 {source: uint16(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}}, 40 {source: uint32(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}}, 41 {source: uint64(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}}, 42 {source: "1", result: pgtype.QChar{Int: 1, Status: pgtype.Present}}, 43 {source: _int8(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}}, 44 } 45 46 for i, tt := range successfulTests { 47 var r pgtype.QChar 48 err := r.Set(tt.source) 49 if err != nil { 50 t.Errorf("%d: %v", i, err) 51 } 52 53 if r != tt.result { 54 t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) 55 } 56 } 57} 58 59func TestQCharAssignTo(t *testing.T) { 60 var i8 int8 61 var i16 int16 62 var i32 int32 63 var i64 int64 64 var i int 65 var ui8 uint8 66 var ui16 uint16 67 var ui32 uint32 68 var ui64 uint64 69 var ui uint 70 var pi8 *int8 71 var _i8 _int8 72 var _pi8 *_int8 73 74 simpleTests := []struct { 75 src pgtype.QChar 76 dst interface{} 77 expected interface{} 78 }{ 79 {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &i8, expected: int8(42)}, 80 {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &i16, expected: int16(42)}, 81 {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &i32, expected: int32(42)}, 82 {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &i64, expected: int64(42)}, 83 {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &i, expected: int(42)}, 84 {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &ui8, expected: uint8(42)}, 85 {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &ui16, expected: uint16(42)}, 86 {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &ui32, expected: uint32(42)}, 87 {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &ui64, expected: uint64(42)}, 88 {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &ui, expected: uint(42)}, 89 {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &_i8, expected: _int8(42)}, 90 {src: pgtype.QChar{Int: 0, Status: pgtype.Null}, dst: &pi8, expected: ((*int8)(nil))}, 91 {src: pgtype.QChar{Int: 0, Status: pgtype.Null}, dst: &_pi8, expected: ((*_int8)(nil))}, 92 } 93 94 for i, tt := range simpleTests { 95 err := tt.src.AssignTo(tt.dst) 96 if err != nil { 97 t.Errorf("%d: %v", i, err) 98 } 99 100 if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected { 101 t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) 102 } 103 } 104 105 pointerAllocTests := []struct { 106 src pgtype.QChar 107 dst interface{} 108 expected interface{} 109 }{ 110 {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &pi8, expected: int8(42)}, 111 {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &_pi8, expected: _int8(42)}, 112 } 113 114 for i, tt := range pointerAllocTests { 115 err := tt.src.AssignTo(tt.dst) 116 if err != nil { 117 t.Errorf("%d: %v", i, err) 118 } 119 120 if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected { 121 t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) 122 } 123 } 124 125 errorTests := []struct { 126 src pgtype.QChar 127 dst interface{} 128 }{ 129 {src: pgtype.QChar{Int: -1, Status: pgtype.Present}, dst: &ui8}, 130 {src: pgtype.QChar{Int: -1, Status: pgtype.Present}, dst: &ui16}, 131 {src: pgtype.QChar{Int: -1, Status: pgtype.Present}, dst: &ui32}, 132 {src: pgtype.QChar{Int: -1, Status: pgtype.Present}, dst: &ui64}, 133 {src: pgtype.QChar{Int: -1, Status: pgtype.Present}, dst: &ui}, 134 {src: pgtype.QChar{Int: 0, Status: pgtype.Null}, dst: &i16}, 135 } 136 137 for i, tt := range errorTests { 138 err := tt.src.AssignTo(tt.dst) 139 if err == nil { 140 t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) 141 } 142 } 143} 144