github.com/jackc/pgx/v5@v5.5.5/pgtype/int_test.go (about) 1 // Do not edit. Generated from pgtype/int_test.go.erb 2 package pgtype_test 3 4 import ( 5 "context" 6 "math" 7 "testing" 8 9 "github.com/jackc/pgx/v5/pgtype" 10 "github.com/jackc/pgx/v5/pgxtest" 11 ) 12 13 func TestInt2Codec(t *testing.T) { 14 pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "int2", []pgxtest.ValueRoundTripTest{ 15 {int8(1), new(int16), isExpectedEq(int16(1))}, 16 {int16(1), new(int16), isExpectedEq(int16(1))}, 17 {int32(1), new(int16), isExpectedEq(int16(1))}, 18 {int64(1), new(int16), isExpectedEq(int16(1))}, 19 {uint8(1), new(int16), isExpectedEq(int16(1))}, 20 {uint16(1), new(int16), isExpectedEq(int16(1))}, 21 {uint32(1), new(int16), isExpectedEq(int16(1))}, 22 {uint64(1), new(int16), isExpectedEq(int16(1))}, 23 {int(1), new(int16), isExpectedEq(int16(1))}, 24 {uint(1), new(int16), isExpectedEq(int16(1))}, 25 {pgtype.Int2{Int16: 1, Valid: true}, new(int16), isExpectedEq(int16(1))}, 26 {int32(-1), new(pgtype.Int2), isExpectedEq(pgtype.Int2{Int16: -1, Valid: true})}, 27 {1, new(int8), isExpectedEq(int8(1))}, 28 {1, new(int16), isExpectedEq(int16(1))}, 29 {1, new(int32), isExpectedEq(int32(1))}, 30 {1, new(int64), isExpectedEq(int64(1))}, 31 {1, new(uint8), isExpectedEq(uint8(1))}, 32 {1, new(uint16), isExpectedEq(uint16(1))}, 33 {1, new(uint32), isExpectedEq(uint32(1))}, 34 {1, new(uint64), isExpectedEq(uint64(1))}, 35 {1, new(int), isExpectedEq(int(1))}, 36 {1, new(uint), isExpectedEq(uint(1))}, 37 {-1, new(int8), isExpectedEq(int8(-1))}, 38 {-1, new(int16), isExpectedEq(int16(-1))}, 39 {-1, new(int32), isExpectedEq(int32(-1))}, 40 {-1, new(int64), isExpectedEq(int64(-1))}, 41 {-1, new(int), isExpectedEq(int(-1))}, 42 {math.MinInt16, new(int16), isExpectedEq(int16(math.MinInt16))}, 43 {-1, new(int16), isExpectedEq(int16(-1))}, 44 {0, new(int16), isExpectedEq(int16(0))}, 45 {1, new(int16), isExpectedEq(int16(1))}, 46 {math.MaxInt16, new(int16), isExpectedEq(int16(math.MaxInt16))}, 47 {1, new(pgtype.Int2), isExpectedEq(pgtype.Int2{Int16: 1, Valid: true})}, 48 {"1", new(string), isExpectedEq("1")}, 49 {pgtype.Int2{}, new(pgtype.Int2), isExpectedEq(pgtype.Int2{})}, 50 {nil, new(*int16), isExpectedEq((*int16)(nil))}, 51 }) 52 } 53 54 func TestInt2MarshalJSON(t *testing.T) { 55 successfulTests := []struct { 56 source pgtype.Int2 57 result string 58 }{ 59 {source: pgtype.Int2{Int16: 0}, result: "null"}, 60 {source: pgtype.Int2{Int16: 1, Valid: true}, result: "1"}, 61 } 62 for i, tt := range successfulTests { 63 r, err := tt.source.MarshalJSON() 64 if err != nil { 65 t.Errorf("%d: %v", i, err) 66 } 67 68 if string(r) != tt.result { 69 t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r)) 70 } 71 } 72 } 73 74 func TestInt2UnmarshalJSON(t *testing.T) { 75 successfulTests := []struct { 76 source string 77 result pgtype.Int2 78 }{ 79 {source: "null", result: pgtype.Int2{Int16: 0}}, 80 {source: "1", result: pgtype.Int2{Int16: 1, Valid: true}}, 81 } 82 for i, tt := range successfulTests { 83 var r pgtype.Int2 84 err := r.UnmarshalJSON([]byte(tt.source)) 85 if err != nil { 86 t.Errorf("%d: %v", i, err) 87 } 88 89 if r != tt.result { 90 t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) 91 } 92 } 93 } 94 95 func TestInt4Codec(t *testing.T) { 96 pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "int4", []pgxtest.ValueRoundTripTest{ 97 {int8(1), new(int32), isExpectedEq(int32(1))}, 98 {int16(1), new(int32), isExpectedEq(int32(1))}, 99 {int32(1), new(int32), isExpectedEq(int32(1))}, 100 {int64(1), new(int32), isExpectedEq(int32(1))}, 101 {uint8(1), new(int32), isExpectedEq(int32(1))}, 102 {uint16(1), new(int32), isExpectedEq(int32(1))}, 103 {uint32(1), new(int32), isExpectedEq(int32(1))}, 104 {uint64(1), new(int32), isExpectedEq(int32(1))}, 105 {int(1), new(int32), isExpectedEq(int32(1))}, 106 {uint(1), new(int32), isExpectedEq(int32(1))}, 107 {pgtype.Int4{Int32: 1, Valid: true}, new(int32), isExpectedEq(int32(1))}, 108 {int32(-1), new(pgtype.Int4), isExpectedEq(pgtype.Int4{Int32: -1, Valid: true})}, 109 {1, new(int8), isExpectedEq(int8(1))}, 110 {1, new(int16), isExpectedEq(int16(1))}, 111 {1, new(int32), isExpectedEq(int32(1))}, 112 {1, new(int64), isExpectedEq(int64(1))}, 113 {1, new(uint8), isExpectedEq(uint8(1))}, 114 {1, new(uint16), isExpectedEq(uint16(1))}, 115 {1, new(uint32), isExpectedEq(uint32(1))}, 116 {1, new(uint64), isExpectedEq(uint64(1))}, 117 {1, new(int), isExpectedEq(int(1))}, 118 {1, new(uint), isExpectedEq(uint(1))}, 119 {-1, new(int8), isExpectedEq(int8(-1))}, 120 {-1, new(int16), isExpectedEq(int16(-1))}, 121 {-1, new(int32), isExpectedEq(int32(-1))}, 122 {-1, new(int64), isExpectedEq(int64(-1))}, 123 {-1, new(int), isExpectedEq(int(-1))}, 124 {math.MinInt32, new(int32), isExpectedEq(int32(math.MinInt32))}, 125 {-1, new(int32), isExpectedEq(int32(-1))}, 126 {0, new(int32), isExpectedEq(int32(0))}, 127 {1, new(int32), isExpectedEq(int32(1))}, 128 {math.MaxInt32, new(int32), isExpectedEq(int32(math.MaxInt32))}, 129 {1, new(pgtype.Int4), isExpectedEq(pgtype.Int4{Int32: 1, Valid: true})}, 130 {"1", new(string), isExpectedEq("1")}, 131 {pgtype.Int4{}, new(pgtype.Int4), isExpectedEq(pgtype.Int4{})}, 132 {nil, new(*int32), isExpectedEq((*int32)(nil))}, 133 }) 134 } 135 136 func TestInt4MarshalJSON(t *testing.T) { 137 successfulTests := []struct { 138 source pgtype.Int4 139 result string 140 }{ 141 {source: pgtype.Int4{Int32: 0}, result: "null"}, 142 {source: pgtype.Int4{Int32: 1, Valid: true}, result: "1"}, 143 } 144 for i, tt := range successfulTests { 145 r, err := tt.source.MarshalJSON() 146 if err != nil { 147 t.Errorf("%d: %v", i, err) 148 } 149 150 if string(r) != tt.result { 151 t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r)) 152 } 153 } 154 } 155 156 func TestInt4UnmarshalJSON(t *testing.T) { 157 successfulTests := []struct { 158 source string 159 result pgtype.Int4 160 }{ 161 {source: "null", result: pgtype.Int4{Int32: 0}}, 162 {source: "1", result: pgtype.Int4{Int32: 1, Valid: true}}, 163 } 164 for i, tt := range successfulTests { 165 var r pgtype.Int4 166 err := r.UnmarshalJSON([]byte(tt.source)) 167 if err != nil { 168 t.Errorf("%d: %v", i, err) 169 } 170 171 if r != tt.result { 172 t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) 173 } 174 } 175 } 176 177 func TestInt8Codec(t *testing.T) { 178 pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "int8", []pgxtest.ValueRoundTripTest{ 179 {int8(1), new(int64), isExpectedEq(int64(1))}, 180 {int16(1), new(int64), isExpectedEq(int64(1))}, 181 {int32(1), new(int64), isExpectedEq(int64(1))}, 182 {int64(1), new(int64), isExpectedEq(int64(1))}, 183 {uint8(1), new(int64), isExpectedEq(int64(1))}, 184 {uint16(1), new(int64), isExpectedEq(int64(1))}, 185 {uint32(1), new(int64), isExpectedEq(int64(1))}, 186 {uint64(1), new(int64), isExpectedEq(int64(1))}, 187 {int(1), new(int64), isExpectedEq(int64(1))}, 188 {uint(1), new(int64), isExpectedEq(int64(1))}, 189 {pgtype.Int8{Int64: 1, Valid: true}, new(int64), isExpectedEq(int64(1))}, 190 {int32(-1), new(pgtype.Int8), isExpectedEq(pgtype.Int8{Int64: -1, Valid: true})}, 191 {1, new(int8), isExpectedEq(int8(1))}, 192 {1, new(int16), isExpectedEq(int16(1))}, 193 {1, new(int32), isExpectedEq(int32(1))}, 194 {1, new(int64), isExpectedEq(int64(1))}, 195 {1, new(uint8), isExpectedEq(uint8(1))}, 196 {1, new(uint16), isExpectedEq(uint16(1))}, 197 {1, new(uint32), isExpectedEq(uint32(1))}, 198 {1, new(uint64), isExpectedEq(uint64(1))}, 199 {1, new(int), isExpectedEq(int(1))}, 200 {1, new(uint), isExpectedEq(uint(1))}, 201 {-1, new(int8), isExpectedEq(int8(-1))}, 202 {-1, new(int16), isExpectedEq(int16(-1))}, 203 {-1, new(int32), isExpectedEq(int32(-1))}, 204 {-1, new(int64), isExpectedEq(int64(-1))}, 205 {-1, new(int), isExpectedEq(int(-1))}, 206 {math.MinInt64, new(int64), isExpectedEq(int64(math.MinInt64))}, 207 {-1, new(int64), isExpectedEq(int64(-1))}, 208 {0, new(int64), isExpectedEq(int64(0))}, 209 {1, new(int64), isExpectedEq(int64(1))}, 210 {math.MaxInt64, new(int64), isExpectedEq(int64(math.MaxInt64))}, 211 {1, new(pgtype.Int8), isExpectedEq(pgtype.Int8{Int64: 1, Valid: true})}, 212 {"1", new(string), isExpectedEq("1")}, 213 {pgtype.Int8{}, new(pgtype.Int8), isExpectedEq(pgtype.Int8{})}, 214 {nil, new(*int64), isExpectedEq((*int64)(nil))}, 215 }) 216 } 217 218 func TestInt8MarshalJSON(t *testing.T) { 219 successfulTests := []struct { 220 source pgtype.Int8 221 result string 222 }{ 223 {source: pgtype.Int8{Int64: 0}, result: "null"}, 224 {source: pgtype.Int8{Int64: 1, Valid: true}, result: "1"}, 225 } 226 for i, tt := range successfulTests { 227 r, err := tt.source.MarshalJSON() 228 if err != nil { 229 t.Errorf("%d: %v", i, err) 230 } 231 232 if string(r) != tt.result { 233 t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r)) 234 } 235 } 236 } 237 238 func TestInt8UnmarshalJSON(t *testing.T) { 239 successfulTests := []struct { 240 source string 241 result pgtype.Int8 242 }{ 243 {source: "null", result: pgtype.Int8{Int64: 0}}, 244 {source: "1", result: pgtype.Int8{Int64: 1, Valid: true}}, 245 } 246 for i, tt := range successfulTests { 247 var r pgtype.Int8 248 err := r.UnmarshalJSON([]byte(tt.source)) 249 if err != nil { 250 t.Errorf("%d: %v", i, err) 251 } 252 253 if r != tt.result { 254 t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) 255 } 256 } 257 }