Skip to content

Commit

Permalink
Test types.Float#UnmarshalJSON()
Browse files Browse the repository at this point in the history
  • Loading branch information
Al2Klimov committed Aug 5, 2024
1 parent 33f2c7d commit 48c672a
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions types/float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}

0 comments on commit 48c672a

Please sign in to comment.