Skip to content

Commit

Permalink
Test types.Float#UnmarshalText()
Browse files Browse the repository at this point in the history
  • Loading branch information
Al2Klimov committed Sep 17, 2024
1 parent e44f615 commit c0adc02
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions types/float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,32 @@ func TestFloat_MarshalJSON(t *testing.T) {
})
}
}

func TestFloat_UnmarshalText(t *testing.T) {
subtests := []struct {
name string
input string
output sql.NullFloat64
error bool
}{
{"empty", "", 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.UnmarshalText([]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 c0adc02

Please sign in to comment.