github.com/seeker-insurance/kit@v0.0.13/db/null/int_test.go (about) 1 package null 2 3 import ( 4 "encoding/json" 5 "math" 6 "strconv" 7 "testing" 8 ) 9 10 var ( 11 intJSON = []byte(`12345`) 12 intStringJSON = []byte(`"12345"`) 13 nullIntJSON = []byte(`{"Int64":12345,"Valid":true}`) 14 ) 15 16 func TestIntFrom(t *testing.T) { 17 i := IntFrom(12345) 18 assertInt(t, i, "IntFrom()") 19 20 zero := IntFrom(0) 21 if !zero.Valid { 22 t.Error("IntFrom(0)", "is invalid, but should be valid") 23 } 24 } 25 26 func TestIntFromPtr(t *testing.T) { 27 n := int64(12345) 28 iptr := &n 29 i := IntFromPtr(iptr) 30 assertInt(t, i, "IntFromPtr()") 31 32 null := IntFromPtr(nil) 33 assertNullInt(t, null, "IntFromPtr(nil)") 34 } 35 36 func TestUnmarshalInt(t *testing.T) { 37 var i Int 38 err := json.Unmarshal(intJSON, &i) 39 maybePanic(err) 40 assertInt(t, i, "int json") 41 42 var si Int 43 err = json.Unmarshal(intStringJSON, &si) 44 maybePanic(err) 45 assertInt(t, si, "int string json") 46 47 var ni Int 48 err = json.Unmarshal(nullIntJSON, &ni) 49 maybePanic(err) 50 assertInt(t, ni, "sql.NullInt64 json") 51 52 var bi Int 53 err = json.Unmarshal(floatBlankJSON, &bi) 54 maybePanic(err) 55 assertNullInt(t, bi, "blank json string") 56 57 var null Int 58 err = json.Unmarshal(nullJSON, &null) 59 maybePanic(err) 60 assertNullInt(t, null, "null json") 61 62 var badType Int 63 err = json.Unmarshal(boolJSON, &badType) 64 if err == nil { 65 panic("err should not be nil") 66 } 67 assertNullInt(t, badType, "wrong type json") 68 69 var invalid Int 70 err = invalid.UnmarshalJSON(invalidJSON) 71 if _, ok := err.(*json.SyntaxError); !ok { 72 t.Errorf("expected json.SyntaxError, not %T", err) 73 } 74 assertNullInt(t, invalid, "invalid json") 75 } 76 77 func TestUnmarshalNonIntegerNumber(t *testing.T) { 78 var i Int 79 err := json.Unmarshal(floatJSON, &i) 80 if err == nil { 81 panic("err should be present; non-integer number coerced to int") 82 } 83 } 84 85 func TestUnmarshalInt64Overflow(t *testing.T) { 86 int64Overflow := uint64(math.MaxInt64) 87 88 // Max int64 should decode successfully 89 var i Int 90 err := json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i) 91 maybePanic(err) 92 93 // Attempt to overflow 94 int64Overflow++ 95 err = json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i) 96 if err == nil { 97 panic("err should be present; decoded value overflows int64") 98 } 99 } 100 101 func TestTextUnmarshalInt(t *testing.T) { 102 var i Int 103 err := i.UnmarshalText([]byte("12345")) 104 maybePanic(err) 105 assertInt(t, i, "UnmarshalText() int") 106 107 var blank Int 108 err = blank.UnmarshalText([]byte("")) 109 maybePanic(err) 110 assertNullInt(t, blank, "UnmarshalText() empty int") 111 112 var null Int 113 err = null.UnmarshalText([]byte("null")) 114 maybePanic(err) 115 assertNullInt(t, null, `UnmarshalText() "null"`) 116 } 117 118 func TestMarshalInt(t *testing.T) { 119 i := IntFrom(12345) 120 data, err := json.Marshal(i) 121 maybePanic(err) 122 assertJSONEquals(t, data, "12345", "non-empty json marshal") 123 124 // invalid values should be encoded as null 125 null := NewInt(0, false) 126 data, err = json.Marshal(null) 127 maybePanic(err) 128 assertJSONEquals(t, data, "null", "null json marshal") 129 } 130 131 func TestMarshalIntText(t *testing.T) { 132 i := IntFrom(12345) 133 data, err := i.MarshalText() 134 maybePanic(err) 135 assertJSONEquals(t, data, "12345", "non-empty text marshal") 136 137 // invalid values should be encoded as null 138 null := NewInt(0, false) 139 data, err = null.MarshalText() 140 maybePanic(err) 141 assertJSONEquals(t, data, "", "null text marshal") 142 } 143 144 func TestIntPointer(t *testing.T) { 145 i := IntFrom(12345) 146 ptr := i.Ptr() 147 if *ptr != 12345 { 148 t.Errorf("bad %s int: %#v ≠ %d\n", "pointer", ptr, 12345) 149 } 150 151 null := NewInt(0, false) 152 ptr = null.Ptr() 153 if ptr != nil { 154 t.Errorf("bad %s int: %#v ≠ %s\n", "nil pointer", ptr, "nil") 155 } 156 } 157 158 func TestIntIsZero(t *testing.T) { 159 i := IntFrom(12345) 160 if i.IsZero() { 161 t.Errorf("IsZero() should be false") 162 } 163 164 null := NewInt(0, false) 165 if !null.IsZero() { 166 t.Errorf("IsZero() should be true") 167 } 168 169 zero := NewInt(0, true) 170 if zero.IsZero() { 171 t.Errorf("IsZero() should be false") 172 } 173 } 174 175 func TestIntSetValid(t *testing.T) { 176 change := NewInt(0, false) 177 assertNullInt(t, change, "SetValid()") 178 change.SetValid(12345) 179 assertInt(t, change, "SetValid()") 180 } 181 182 func TestIntScan(t *testing.T) { 183 var i Int 184 err := i.Scan(12345) 185 maybePanic(err) 186 assertInt(t, i, "scanned int") 187 188 var null Int 189 err = null.Scan(nil) 190 maybePanic(err) 191 assertNullInt(t, null, "scanned null") 192 } 193 194 func TestIntValueOrZero(t *testing.T) { 195 valid := NewInt(12345, true) 196 if valid.ValueOrZero() != 12345 { 197 t.Error("unexpected ValueOrZero", valid.ValueOrZero()) 198 } 199 200 invalid := NewInt(12345, false) 201 if invalid.ValueOrZero() != 0 { 202 t.Error("unexpected ValueOrZero", invalid.ValueOrZero()) 203 } 204 } 205 206 func TestIntAuto(t *testing.T) { 207 valid := NewIntAuto(5) 208 if !valid.Valid { 209 t.Error("unexpected ValueOrZero", valid.ValueOrZero()) 210 } 211 212 invalid := NewIntAuto(0) 213 if invalid.Valid { 214 t.Error("unexpected valid", invalid.Valid) 215 } 216 } 217 218 func assertInt(t *testing.T, i Int, from string) { 219 if i.Int64 != 12345 { 220 t.Errorf("bad %s int: %d ≠ %d\n", from, i.Int64, 12345) 221 } 222 if !i.Valid { 223 t.Error(from, "is invalid, but should be valid") 224 } 225 } 226 227 func assertNullInt(t *testing.T, i Int, from string) { 228 if i.Valid { 229 t.Error(from, "is valid, but should be invalid") 230 } 231 }