github.com/seeker-insurance/kit@v0.0.13/db/null/zero/float_test.go (about) 1 package zero 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 valid, but should be invalid") 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 blank Float 53 err = json.Unmarshal(floatBlankJSON, &blank) 54 maybePanic(err) 55 assertNullFloat(t, blank, "null blank string json") 56 57 var zero Float 58 err = json.Unmarshal(zeroJSON, &zero) 59 maybePanic(err) 60 assertNullFloat(t, zero, "zero json") 61 62 var null Float 63 err = json.Unmarshal(nullJSON, &null) 64 maybePanic(err) 65 assertNullFloat(t, null, "null json") 66 67 var badType Float 68 err = json.Unmarshal(boolJSON, &badType) 69 if err == nil { 70 panic("err should not be nil") 71 } 72 assertNullFloat(t, badType, "wrong type json") 73 74 var invalid Float 75 err = invalid.UnmarshalJSON(invalidJSON) 76 if _, ok := err.(*json.SyntaxError); !ok { 77 t.Errorf("expected json.SyntaxError, not %T", err) 78 } 79 assertNullFloat(t, invalid, "invalid json") 80 } 81 82 func TestTextUnmarshalFloat(t *testing.T) { 83 var f Float 84 err := f.UnmarshalText([]byte("1.2345")) 85 maybePanic(err) 86 assertFloat(t, f, "UnmarshalText() float") 87 88 var zero Float 89 err = zero.UnmarshalText([]byte("0")) 90 maybePanic(err) 91 assertNullFloat(t, zero, "UnmarshalText() zero float") 92 93 var blank Float 94 err = blank.UnmarshalText([]byte("")) 95 maybePanic(err) 96 assertNullFloat(t, blank, "UnmarshalText() empty float") 97 98 var null Float 99 err = null.UnmarshalText([]byte("null")) 100 maybePanic(err) 101 assertNullFloat(t, null, `UnmarshalText() "null"`) 102 } 103 104 func TestMarshalFloat(t *testing.T) { 105 f := FloatFrom(1.2345) 106 data, err := json.Marshal(f) 107 maybePanic(err) 108 assertJSONEquals(t, data, "1.2345", "non-empty json marshal") 109 110 // invalid values should be encoded as 0 111 null := NewFloat(0, false) 112 data, err = json.Marshal(null) 113 maybePanic(err) 114 assertJSONEquals(t, data, "0", "null json marshal") 115 } 116 117 func TestMarshalFloatText(t *testing.T) { 118 f := FloatFrom(1.2345) 119 data, err := f.MarshalText() 120 maybePanic(err) 121 assertJSONEquals(t, data, "1.2345", "non-empty text marshal") 122 123 // invalid values should be encoded as zero 124 null := NewFloat(0, false) 125 data, err = null.MarshalText() 126 maybePanic(err) 127 assertJSONEquals(t, data, "0", "null text marshal") 128 } 129 130 func TestFloatPointer(t *testing.T) { 131 f := FloatFrom(1.2345) 132 ptr := f.Ptr() 133 if *ptr != 1.2345 { 134 t.Errorf("bad %s Float: %#v ≠ %v\n", "pointer", ptr, 1.2345) 135 } 136 137 null := NewFloat(0, false) 138 ptr = null.Ptr() 139 if ptr != nil { 140 t.Errorf("bad %s Float: %#v ≠ %s\n", "nil pointer", ptr, "nil") 141 } 142 } 143 144 func TestFloatIsZero(t *testing.T) { 145 f := FloatFrom(1.2345) 146 if f.IsZero() { 147 t.Errorf("IsZero() should be false") 148 } 149 150 null := NewFloat(0, false) 151 if !null.IsZero() { 152 t.Errorf("IsZero() should be true") 153 } 154 155 zero := NewFloat(0, true) 156 if !zero.IsZero() { 157 t.Errorf("IsZero() should be true") 158 } 159 } 160 161 func TestFloatSetValid(t *testing.T) { 162 change := NewFloat(0, false) 163 assertNullFloat(t, change, "SetValid()") 164 change.SetValid(1.2345) 165 assertFloat(t, change, "SetValid()") 166 } 167 168 func TestFloatScan(t *testing.T) { 169 var f Float 170 err := f.Scan(1.2345) 171 maybePanic(err) 172 assertFloat(t, f, "scanned float") 173 174 var null Float 175 err = null.Scan(nil) 176 maybePanic(err) 177 assertNullFloat(t, null, "scanned null") 178 } 179 180 func TestFloatInfNaN(t *testing.T) { 181 nan := NewFloat(math.NaN(), true) 182 _, err := nan.MarshalJSON() 183 if err == nil { 184 t.Error("expected error for NaN, got nil") 185 } 186 187 inf := NewFloat(math.Inf(1), true) 188 _, err = inf.MarshalJSON() 189 if err == nil { 190 t.Error("expected error for Inf, got nil") 191 } 192 } 193 194 func assertFloat(t *testing.T, f Float, from string) { 195 if f.Float64 != 1.2345 { 196 t.Errorf("bad %s float: %f ≠ %f\n", from, f.Float64, 1.2345) 197 } 198 if !f.Valid { 199 t.Error(from, "is invalid, but should be valid") 200 } 201 } 202 203 func assertNullFloat(t *testing.T, f Float, from string) { 204 if f.Valid { 205 t.Error(from, "is valid, but should be invalid") 206 } 207 }