github.com/seeker-insurance/kit@v0.0.13/db/null/float_test.go (about) 1 package null 2 3 import ( 4 "encoding/json" 5 "math" 6 "testing" 7 ) 8 9 var ( 10 floatJSON = []byte(`1.2345`) 11 floatStringJSON = []byte(`"1.2345"`) 12 floatBlankJSON = []byte(`""`) 13 nullFloatJSON = []byte(`{"Float64":1.2345,"Valid":true}`) 14 ) 15 16 func TestFloatFrom(t *testing.T) { 17 f := FloatFrom(1.2345) 18 assertFloat(t, f, "FloatFrom()") 19 20 zero := FloatFrom(0) 21 if !zero.Valid { 22 t.Error("FloatFrom(0)", "is invalid, but should be valid") 23 } 24 } 25 26 func TestFloatFromPtr(t *testing.T) { 27 n := float64(1.2345) 28 iptr := &n 29 f := FloatFromPtr(iptr) 30 assertFloat(t, f, "FloatFromPtr()") 31 32 null := FloatFromPtr(nil) 33 assertNullFloat(t, null, "FloatFromPtr(nil)") 34 } 35 36 func TestUnmarshalFloat(t *testing.T) { 37 var f Float 38 err := json.Unmarshal(floatJSON, &f) 39 maybePanic(err) 40 assertFloat(t, f, "float json") 41 42 var sf Float 43 err = json.Unmarshal(floatStringJSON, &sf) 44 maybePanic(err) 45 assertFloat(t, sf, "string float json") 46 47 var nf Float 48 err = json.Unmarshal(nullFloatJSON, &nf) 49 maybePanic(err) 50 assertFloat(t, nf, "sql.NullFloat64 json") 51 52 var null Float 53 err = json.Unmarshal(nullJSON, &null) 54 maybePanic(err) 55 assertNullFloat(t, null, "null json") 56 57 var blank Float 58 err = json.Unmarshal(floatBlankJSON, &blank) 59 maybePanic(err) 60 assertNullFloat(t, blank, "null blank string json") 61 62 var badType Float 63 err = json.Unmarshal(boolJSON, &badType) 64 if err == nil { 65 panic("err should not be nil") 66 } 67 assertNullFloat(t, badType, "wrong type json") 68 69 var invalid Float 70 err = invalid.UnmarshalJSON(invalidJSON) 71 if _, ok := err.(*json.SyntaxError); !ok { 72 t.Errorf("expected json.SyntaxError, not %T", err) 73 } 74 } 75 76 func TestTextUnmarshalFloat(t *testing.T) { 77 var f Float 78 err := f.UnmarshalText([]byte("1.2345")) 79 maybePanic(err) 80 assertFloat(t, f, "UnmarshalText() float") 81 82 var blank Float 83 err = blank.UnmarshalText([]byte("")) 84 maybePanic(err) 85 assertNullFloat(t, blank, "UnmarshalText() empty float") 86 87 var null Float 88 err = null.UnmarshalText([]byte("null")) 89 maybePanic(err) 90 assertNullFloat(t, null, `UnmarshalText() "null"`) 91 } 92 93 func TestMarshalFloat(t *testing.T) { 94 f := FloatFrom(1.2345) 95 data, err := json.Marshal(f) 96 maybePanic(err) 97 assertJSONEquals(t, data, "1.2345", "non-empty json marshal") 98 99 // invalid values should be encoded as null 100 null := NewFloat(0, false) 101 data, err = json.Marshal(null) 102 maybePanic(err) 103 assertJSONEquals(t, data, "null", "null json marshal") 104 } 105 106 func TestMarshalFloatText(t *testing.T) { 107 f := FloatFrom(1.2345) 108 data, err := f.MarshalText() 109 maybePanic(err) 110 assertJSONEquals(t, data, "1.2345", "non-empty text marshal") 111 112 // invalid values should be encoded as null 113 null := NewFloat(0, false) 114 data, err = null.MarshalText() 115 maybePanic(err) 116 assertJSONEquals(t, data, "", "null text marshal") 117 } 118 119 func TestFloatPointer(t *testing.T) { 120 f := FloatFrom(1.2345) 121 ptr := f.Ptr() 122 if *ptr != 1.2345 { 123 t.Errorf("bad %s float: %#v ≠ %v\n", "pointer", ptr, 1.2345) 124 } 125 126 null := NewFloat(0, false) 127 ptr = null.Ptr() 128 if ptr != nil { 129 t.Errorf("bad %s float: %#v ≠ %s\n", "nil pointer", ptr, "nil") 130 } 131 } 132 133 func TestFloatIsZero(t *testing.T) { 134 f := FloatFrom(1.2345) 135 if f.IsZero() { 136 t.Errorf("IsZero() should be false") 137 } 138 139 null := NewFloat(0, false) 140 if !null.IsZero() { 141 t.Errorf("IsZero() should be true") 142 } 143 144 zero := NewFloat(0, true) 145 if zero.IsZero() { 146 t.Errorf("IsZero() should be false") 147 } 148 } 149 150 func TestFloatSetValid(t *testing.T) { 151 change := NewFloat(0, false) 152 assertNullFloat(t, change, "SetValid()") 153 change.SetValid(1.2345) 154 assertFloat(t, change, "SetValid()") 155 } 156 157 func TestFloatScan(t *testing.T) { 158 var f Float 159 err := f.Scan(1.2345) 160 maybePanic(err) 161 assertFloat(t, f, "scanned float") 162 163 var sf Float 164 err = sf.Scan("1.2345") 165 maybePanic(err) 166 assertFloat(t, sf, "scanned string float") 167 168 var null Float 169 err = null.Scan(nil) 170 maybePanic(err) 171 assertNullFloat(t, null, "scanned null") 172 } 173 174 func TestFloatInfNaN(t *testing.T) { 175 nan := NewFloat(math.NaN(), true) 176 _, err := nan.MarshalJSON() 177 if err == nil { 178 t.Error("expected error for NaN, got nil") 179 } 180 181 inf := NewFloat(math.Inf(1), true) 182 _, err = inf.MarshalJSON() 183 if err == nil { 184 t.Error("expected error for Inf, got nil") 185 } 186 } 187 188 func TestFloatValueOrZero(t *testing.T) { 189 valid := NewFloat(1.2345, true) 190 if valid.ValueOrZero() != 1.2345 { 191 t.Error("unexpected ValueOrZero", valid.ValueOrZero()) 192 } 193 194 invalid := NewFloat(1.2345, false) 195 if invalid.ValueOrZero() != 0 { 196 t.Error("unexpected ValueOrZero", invalid.ValueOrZero()) 197 } 198 } 199 200 func assertFloat(t *testing.T, f Float, from string) { 201 if f.Float64 != 1.2345 { 202 t.Errorf("bad %s float: %f ≠ %f\n", from, f.Float64, 1.2345) 203 } 204 if !f.Valid { 205 t.Error(from, "is invalid, but should be valid") 206 } 207 } 208 209 func assertNullFloat(t *testing.T, f Float, from string) { 210 if f.Valid { 211 t.Error(from, "is valid, but should be invalid") 212 } 213 }