Skip to content

Commit 58b0be3

Browse files
committed
Test types.Float#UnmarshalJSON()
1 parent c0adc02 commit 58b0be3

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

types/float_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,34 @@ func TestFloat_UnmarshalText(t *testing.T) {
5757
})
5858
}
5959
}
60+
61+
func TestFloat_UnmarshalJSON(t *testing.T) {
62+
subtests := []struct {
63+
name string
64+
input string
65+
output sql.NullFloat64
66+
error bool
67+
}{
68+
{"null", `null`, sql.NullFloat64{}, false},
69+
{"bool", `false`, sql.NullFloat64{}, true},
70+
{"string", `"0"`, sql.NullFloat64{}, true},
71+
{"too_big", `1e309`, sql.NullFloat64{}, true},
72+
{"zero", `0`, sql.NullFloat64{Float64: 0, Valid: true}, false},
73+
{"negative", `-1`, sql.NullFloat64{Float64: -1, Valid: true}, false},
74+
{"fraction", `0.5`, sql.NullFloat64{Float64: 0.5, Valid: true}, false},
75+
{"exp", `2e1`, sql.NullFloat64{Float64: 20, Valid: true}, false},
76+
{"too_precise", `1e-1337`, sql.NullFloat64{Float64: 0, Valid: true}, false},
77+
}
78+
79+
for _, st := range subtests {
80+
t.Run(st.name, func(t *testing.T) {
81+
var actual Float
82+
if err := actual.UnmarshalJSON([]byte(st.input)); st.error {
83+
require.Error(t, err)
84+
} else {
85+
require.NoError(t, err)
86+
require.Equal(t, Float{NullFloat64: st.output}, actual)
87+
}
88+
})
89+
}
90+
}

0 commit comments

Comments
 (0)