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 60fad13 commit 45300cd
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions types/float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,34 @@ 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},
{"too_precise", `1e-1337`, sql.NullFloat64{Float64: 0, 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 45300cd

Please sign in to comment.