diff --git a/types/float_test.go b/types/float_test.go index 74707aad..a2296a27 100644 --- a/types/float_test.go +++ b/types/float_test.go @@ -56,3 +56,33 @@ func TestFloat_UnmarshalText(t *testing.T) { }) } } + +func TestFloat_UnmarshalJSON(t *testing.T) { + subtests := []struct { + name string + input string + output sql.NullFloat64 + error bool + }{ + {"null", `null`, sql.NullFloat64{}, false}, + {"bool", `false`, sql.NullFloat64{}, true}, + {"string", `"0"`, sql.NullFloat64{}, true}, + {"too_big", `1e309`, sql.NullFloat64{}, true}, + {"zero", `0`, sql.NullFloat64{Float64: 0, Valid: true}, false}, + {"negative", `-1`, sql.NullFloat64{Float64: -1, Valid: true}, false}, + {"fraction", `0.5`, sql.NullFloat64{Float64: 0.5, Valid: true}, false}, + {"exp", `2e1`, sql.NullFloat64{Float64: 20, Valid: true}, false}, + } + + for _, st := range subtests { + t.Run(st.name, func(t *testing.T) { + var actual Float + if err := actual.UnmarshalJSON([]byte(st.input)); st.error { + require.Error(t, err) + } else { + require.NoError(t, err) + require.Equal(t, Float{NullFloat64: st.output}, actual) + } + }) + } +}