Skip to content

Commit 17cedc4

Browse files
authored
Merge pull request #27 from Icinga/test-Float
Test types.Float
2 parents b05aeed + 58b0be3 commit 17cedc4

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

types/float_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
}
31+
32+
func TestFloat_UnmarshalText(t *testing.T) {
33+
subtests := []struct {
34+
name string
35+
input string
36+
output sql.NullFloat64
37+
error bool
38+
}{
39+
{"empty", "", sql.NullFloat64{}, true},
40+
{"too_big", "1e309", sql.NullFloat64{}, true},
41+
{"zero", "0", sql.NullFloat64{Float64: 0, Valid: true}, false},
42+
{"negative", "-1", sql.NullFloat64{Float64: -1, Valid: true}, false},
43+
{"fraction", "0.5", sql.NullFloat64{Float64: 0.5, Valid: true}, false},
44+
{"exp", "2e1", sql.NullFloat64{Float64: 20, Valid: true}, false},
45+
{"too_precise", "1e-1337", sql.NullFloat64{Float64: 0, Valid: true}, false},
46+
}
47+
48+
for _, st := range subtests {
49+
t.Run(st.name, func(t *testing.T) {
50+
var actual Float
51+
if err := actual.UnmarshalText([]byte(st.input)); st.error {
52+
require.Error(t, err)
53+
} else {
54+
require.NoError(t, err)
55+
require.Equal(t, Float{NullFloat64: st.output}, actual)
56+
}
57+
})
58+
}
59+
}
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)