| 1 |
package schema |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"math" |
| 6 |
"reflect" |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
) |
| 10 |
|
| 11 |
func TestUnixSecondSerializer_Value(t *testing.T) { |
| 12 |
var ( |
| 13 |
intValue = math.MaxInt64 |
| 14 |
int8Value = int8(math.MaxInt8) |
| 15 |
int16Value = int16(math.MaxInt16) |
| 16 |
int32Value = int32(math.MaxInt32) |
| 17 |
int64Value = int64(math.MaxInt64) |
| 18 |
uintValue = uint(math.MaxInt64) |
| 19 |
uint8Value = uint8(math.MaxUint8) |
| 20 |
uint16Value = uint16(math.MaxUint16) |
| 21 |
uint32Value = uint32(math.MaxUint32) |
| 22 |
uint64Value = uint64(math.MaxInt64) |
| 23 |
maxInt64Plus1 = uint64(math.MaxInt64 + 1) |
| 24 |
|
| 25 |
intPtrValue = &intValue |
| 26 |
int8PtrValue = &int8Value |
| 27 |
int16PtrValue = &int16Value |
| 28 |
int32PtrValue = &int32Value |
| 29 |
int64PtrValue = &int64Value |
| 30 |
uintPtrValue = &uintValue |
| 31 |
uint8PtrValue = &uint8Value |
| 32 |
uint16PtrValue = &uint16Value |
| 33 |
uint32PtrValue = &uint32Value |
| 34 |
uint64PtrValue = &uint64Value |
| 35 |
maxInt64Plus1Ptr = &maxInt64Plus1 |
| 36 |
) |
| 37 |
tests := []struct { |
| 38 |
name string |
| 39 |
value interface{} |
| 40 |
want interface{} |
| 41 |
wantErr bool |
| 42 |
}{ |
| 43 |
{ |
| 44 |
name: "int", |
| 45 |
value: intValue, |
| 46 |
want: time.Unix(int64(intValue), 0).UTC(), |
| 47 |
wantErr: false, |
| 48 |
}, |
| 49 |
{ |
| 50 |
name: "int8", |
| 51 |
value: int8Value, |
| 52 |
want: time.Unix(int64(int8Value), 0).UTC(), |
| 53 |
wantErr: false, |
| 54 |
}, |
| 55 |
{ |
| 56 |
name: "int16", |
| 57 |
value: int16Value, |
| 58 |
want: time.Unix(int64(int16Value), 0).UTC(), |
| 59 |
wantErr: false, |
| 60 |
}, |
| 61 |
{ |
| 62 |
name: "int32", |
| 63 |
value: int32Value, |
| 64 |
want: time.Unix(int64(int32Value), 0).UTC(), |
| 65 |
wantErr: false, |
| 66 |
}, |
| 67 |
{ |
| 68 |
name: "int64", |
| 69 |
value: int64Value, |
| 70 |
want: time.Unix(int64Value, 0).UTC(), |
| 71 |
wantErr: false, |
| 72 |
}, |
| 73 |
{ |
| 74 |
name: "uint", |
| 75 |
value: uintValue, |
| 76 |
want: time.Unix(int64(uintValue), 0).UTC(), //nolint:gosec |
| 77 |
wantErr: false, |
| 78 |
}, |
| 79 |
{ |
| 80 |
name: "uint8", |
| 81 |
value: uint8Value, |
| 82 |
want: time.Unix(int64(uint8Value), 0).UTC(), |
| 83 |
wantErr: false, |
| 84 |
}, |
| 85 |
{ |
| 86 |
name: "uint16", |
| 87 |
value: uint16Value, |
| 88 |
want: time.Unix(int64(uint16Value), 0).UTC(), |
| 89 |
wantErr: false, |
| 90 |
}, |
| 91 |
{ |
| 92 |
name: "uint32", |
| 93 |
value: uint32Value, |
| 94 |
want: time.Unix(int64(uint32Value), 0).UTC(), |
| 95 |
wantErr: false, |
| 96 |
}, |
| 97 |
{ |
| 98 |
name: "uint64", |
| 99 |
value: uint64Value, |
| 100 |
want: time.Unix(int64(uint64Value), 0).UTC(), //nolint:gosec |
| 101 |
wantErr: false, |
| 102 |
}, |
| 103 |
{ |
| 104 |
name: "maxInt64+1", |
| 105 |
value: maxInt64Plus1, |
| 106 |
want: nil, |
| 107 |
wantErr: true, |
| 108 |
}, |
| 109 |
{ |
| 110 |
name: "*int", |
| 111 |
value: intPtrValue, |
| 112 |
want: time.Unix(int64(*intPtrValue), 0).UTC(), |
| 113 |
wantErr: false, |
| 114 |
}, |
| 115 |
{ |
| 116 |
name: "*int8", |
| 117 |
value: int8PtrValue, |
| 118 |
want: time.Unix(int64(*int8PtrValue), 0).UTC(), |
| 119 |
wantErr: false, |
| 120 |
}, |
| 121 |
{ |
| 122 |
name: "*int16", |
| 123 |
value: int16PtrValue, |
| 124 |
want: time.Unix(int64(*int16PtrValue), 0).UTC(), |
| 125 |
wantErr: false, |
| 126 |
}, |
| 127 |
{ |
| 128 |
name: "*int32", |
| 129 |
value: int32PtrValue, |
| 130 |
want: time.Unix(int64(*int32PtrValue), 0).UTC(), |
| 131 |
wantErr: false, |
| 132 |
}, |
| 133 |
{ |
| 134 |
name: "*int64", |
| 135 |
value: int64PtrValue, |
| 136 |
want: time.Unix(*int64PtrValue, 0).UTC(), |
| 137 |
wantErr: false, |
| 138 |
}, |
| 139 |
{ |
| 140 |
name: "*uint", |
| 141 |
value: uintPtrValue, |
| 142 |
want: time.Unix(int64(*uintPtrValue), 0).UTC(), //nolint:gosec |
| 143 |
wantErr: false, |
| 144 |
}, |
| 145 |
{ |
| 146 |
name: "*uint8", |
| 147 |
value: uint8PtrValue, |
| 148 |
want: time.Unix(int64(*uint8PtrValue), 0).UTC(), |
| 149 |
wantErr: false, |
| 150 |
}, |
| 151 |
{ |
| 152 |
name: "*uint16", |
| 153 |
value: uint16PtrValue, |
| 154 |
want: time.Unix(int64(*uint16PtrValue), 0).UTC(), |
| 155 |
wantErr: false, |
| 156 |
}, |
| 157 |
{ |
| 158 |
name: "*uint32", |
| 159 |
value: uint32PtrValue, |
| 160 |
want: time.Unix(int64(*uint32PtrValue), 0).UTC(), |
| 161 |
wantErr: false, |
| 162 |
}, |
| 163 |
{ |
| 164 |
name: "*uint64", |
| 165 |
value: uint64PtrValue, |
| 166 |
want: time.Unix(int64(*uint64PtrValue), 0).UTC(), //nolint:gosec |
| 167 |
wantErr: false, |
| 168 |
}, |
| 169 |
{ |
| 170 |
name: "pointer to maxInt64+1", |
| 171 |
value: maxInt64Plus1Ptr, |
| 172 |
want: nil, |
| 173 |
wantErr: true, |
| 174 |
}, |
| 175 |
{ |
| 176 |
name: "nil pointer", |
| 177 |
value: (*int)(nil), |
| 178 |
want: nil, |
| 179 |
wantErr: false, |
| 180 |
}, |
| 181 |
{ |
| 182 |
name: "invalid type", |
| 183 |
value: "invalid", |
| 184 |
want: nil, |
| 185 |
wantErr: true, |
| 186 |
}, |
| 187 |
} |
| 188 |
for _, tt := range tests { |
| 189 |
t.Run(tt.name, func(t *testing.T) { |
| 190 |
got, err := UnixSecondSerializer{}.Value(context.Background(), nil, reflect.Value{}, tt.value) |
| 191 |
if (err != nil) != tt.wantErr { |
| 192 |
t.Fatalf("UnixSecondSerializer.Value() error = %v, wantErr %v", err, tt.wantErr) |
| 193 |
} |
| 194 |
if err != nil { |
| 195 |
return |
| 196 |
} |
| 197 |
if tt.want == nil && got == nil { |
| 198 |
return |
| 199 |
} |
| 200 |
if tt.want == nil { |
| 201 |
t.Fatalf("UnixSecondSerializer.Value() = %v, want nil", got) |
| 202 |
} |
| 203 |
if got == nil { |
| 204 |
t.Fatalf("UnixSecondSerializer.Value() = nil, want %v", tt.want) |
| 205 |
} |
| 206 |
if gotTime, ok := got.(time.Time); !ok { |
| 207 |
t.Errorf("UnixSecondSerializer.Value() returned %T, expected time.Time", got) |
| 208 |
} else if !tt.want.(time.Time).Equal(gotTime) { |
| 209 |
t.Errorf("UnixSecondSerializer.Value() = %v, want %v", got, tt.want) |
| 210 |
} |
| 211 |
}) |
| 212 |
} |
| 213 |
} |