Skip to content

Commit e44f615

Browse files
committed
Test types.Float#MarshalJSON()
1 parent 715b56e commit e44f615

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

types/float_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package types
2+
3+
import (
4+
"database/sql"
5+
"github.com/stretchr/testify/require"
6+
"testing"
7+
)
8+
9+
func TestFloat_MarshalJSON(t *testing.T) {
10+
subtests := []struct {
11+
name string
12+
input sql.NullFloat64
13+
output string
14+
}{
15+
{"null", sql.NullFloat64{}, `null`},
16+
{"invalid", sql.NullFloat64{Float64: 42}, `null`},
17+
{"zero", sql.NullFloat64{Float64: 0, Valid: true}, `0`},
18+
{"negative", sql.NullFloat64{Float64: -1, Valid: true}, `-1`},
19+
{"fraction", sql.NullFloat64{Float64: 0.5, Valid: true}, `0.5`},
20+
}
21+
22+
for _, st := range subtests {
23+
t.Run(st.name, func(t *testing.T) {
24+
actual, err := Float{st.input}.MarshalJSON()
25+
26+
require.NoError(t, err)
27+
require.Equal(t, st.output, string(actual))
28+
})
29+
}
30+
}

0 commit comments

Comments
 (0)