Skip to content

Commit c10871d

Browse files
committed
tests: Use sql NullFloat64 for sla testing
1 parent 9705ecc commit c10871d

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

tests/sql/sla_test.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sql_test
22

33
import (
44
"crypto/rand"
5+
"database/sql"
56
"database/sql/driver"
67
"fmt"
78
"github.com/go-sql-driver/mysql"
@@ -22,7 +23,7 @@ func TestSla(t *testing.T) {
2223
Events []SlaHistoryEvent
2324
Start uint64
2425
End uint64
25-
Expected float64
26+
Expected sql.NullFloat64
2627
}
2728

2829
tests := []TestData{{
@@ -31,7 +32,7 @@ func TestSla(t *testing.T) {
3132
Events: nil,
3233
Start: 1000,
3334
End: 2000,
34-
Expected: 100.0,
35+
Expected: sql.NullFloat64{},
3536
}, {
3637
Name: "MultipleStateChanges",
3738
// Some flapping, test that all changes are considered.
@@ -46,7 +47,7 @@ func TestSla(t *testing.T) {
4647
},
4748
Start: 1000,
4849
End: 2000,
49-
Expected: 60.0,
50+
Expected: sql.NullFloat64{Float64: 60.0},
5051
}, {
5152
Name: "OverlappingDowntimesAndProblems",
5253
// SLA should be 90%:
@@ -65,7 +66,7 @@ func TestSla(t *testing.T) {
6566
},
6667
Start: 1000,
6768
End: 2000,
68-
Expected: 90.0,
69+
Expected: sql.NullFloat64{Float64: 90.0},
6970
}, {
7071
Name: "CriticalBeforeInterval",
7172
// If there is no event within the SLA interval, the last state from before the interval should be used.
@@ -74,7 +75,7 @@ func TestSla(t *testing.T) {
7475
},
7576
Start: 1000,
7677
End: 2000,
77-
Expected: 0.0,
78+
Expected: sql.NullFloat64{Float64: 0.0},
7879
}, {
7980
Name: "CriticalBeforeIntervalWithDowntime",
8081
// State change and downtime start from before the SLA interval should be considered if still relevant.
@@ -84,7 +85,7 @@ func TestSla(t *testing.T) {
8485
},
8586
Start: 1000,
8687
End: 2000,
87-
Expected: 80.0,
88+
Expected: sql.NullFloat64{Float64: 80.0},
8889
}, {
8990
Name: "CriticalBeforeIntervalWithOverlappingDowntimes",
9091
// Test that overlapping downtimes are properly accounted for.
@@ -99,7 +100,7 @@ func TestSla(t *testing.T) {
99100
},
100101
Start: 1000,
101102
End: 2000,
102-
Expected: 80.0,
103+
Expected: sql.NullFloat64{Float64: 80.0},
103104
}, {
104105
Name: "FallbackToPreviousState",
105106
// If there is no state event from before the SLA interval, the previous hard state from the first event
@@ -109,7 +110,7 @@ func TestSla(t *testing.T) {
109110
},
110111
Start: 1000,
111112
End: 2000,
112-
Expected: 80.0,
113+
Expected: sql.NullFloat64{Float64: 80.0},
113114
}, {
114115
Name: "FallbackToCurrentState",
115116
// If there are no state history events, the current state of the checkable should be used.
@@ -118,7 +119,7 @@ func TestSla(t *testing.T) {
118119
},
119120
Start: 1000,
120121
End: 2000,
121-
Expected: 0.0,
122+
Expected: sql.NullFloat64{Float64: 0.0},
122123
}, {
123124
Name: "PreferInitialStateFromBeforeOverLaterState",
124125
// The previous_hard_state should only be used as a fallback when there is no event from before the
@@ -129,7 +130,7 @@ func TestSla(t *testing.T) {
129130
},
130131
Start: 1000,
131132
End: 2000,
132-
Expected: 80.0,
133+
Expected: sql.NullFloat64{Float64: 80.0},
133134
}, {
134135
Name: "PreferInitialStateFromBeforeOverCurrentState",
135136
// The current state should only be used as a fallback when there is no state history event.
@@ -140,7 +141,7 @@ func TestSla(t *testing.T) {
140141
},
141142
Start: 1000,
142143
End: 2000,
143-
Expected: 0.0,
144+
Expected: sql.NullFloat64{Float64: 0.0},
144145
}, {
145146
Name: "PreferLaterStateOverCurrentState",
146147
// The current state should only be used as a fallback when there is no state history event.
@@ -151,7 +152,7 @@ func TestSla(t *testing.T) {
151152
},
152153
Start: 1000,
153154
End: 2000,
154-
Expected: 80.0,
155+
Expected: sql.NullFloat64{Float64: 80.0},
155156
}, {
156157
Name: "InitialUnknownReducesTotalTime",
157158
Events: []SlaHistoryEvent{
@@ -161,7 +162,7 @@ func TestSla(t *testing.T) {
161162
},
162163
Start: 1000,
163164
End: 2000,
164-
Expected: 60,
165+
Expected: sql.NullFloat64{Float64: 60},
165166
}, {
166167
Name: "IntermediateUnknownReducesTotalTime",
167168
Events: []SlaHistoryEvent{
@@ -173,7 +174,7 @@ func TestSla(t *testing.T) {
173174
},
174175
Start: 1000,
175176
End: 2000,
176-
Expected: 60,
177+
Expected: sql.NullFloat64{Float64: 60},
177178
}}
178179

179180
for _, test := range tests {
@@ -226,14 +227,14 @@ func TestSla(t *testing.T) {
226227
})
227228
}
228229

229-
func execSqlSlaFunc(db *sqlx.DB, m *SlaHistoryMeta, start uint64, end uint64) (float64, error) {
230-
var result float64
230+
func execSqlSlaFunc(db *sqlx.DB, m *SlaHistoryMeta, start uint64, end uint64) (sql.NullFloat64, error) {
231+
var result sql.NullFloat64
231232
err := db.Get(&result, db.Rebind("SELECT get_sla_ok_percent(?, ?, ?, ?)"),
232233
m.HostId, m.ServiceId, start, end)
233234
return result, err
234235
}
235236

236-
func testSla(t *testing.T, db *sqlx.DB, events []SlaHistoryEvent, start uint64, end uint64, expected float64, msg string) {
237+
func testSla(t *testing.T, db *sqlx.DB, events []SlaHistoryEvent, start uint64, end uint64, expected sql.NullFloat64, msg string) {
237238
t.Run("Host", func(t *testing.T) {
238239
testSlaWithObjectType(t, db, events, false, start, end, expected, msg)
239240
})
@@ -243,7 +244,7 @@ func testSla(t *testing.T, db *sqlx.DB, events []SlaHistoryEvent, start uint64,
243244
}
244245

245246
func testSlaWithObjectType(t *testing.T, db *sqlx.DB,
246-
events []SlaHistoryEvent, service bool, start uint64, end uint64, expected float64, msg string,
247+
events []SlaHistoryEvent, service bool, start uint64, end uint64, expected sql.NullFloat64, msg string,
247248
) {
248249
makeId := func() []byte {
249250
id := make([]byte, 20)
@@ -271,7 +272,7 @@ func testSlaWithObjectType(t *testing.T, db *sqlx.DB,
271272

272273
r, err := execSqlSlaFunc(db, &meta, start, end)
273274
require.NoError(t, err, "SLA query should not fail")
274-
assert.Equal(t, expected, r, msg)
275+
assert.Equal(t, expected.Float64, r.Float64, msg)
275276
}
276277

277278
type SlaHistoryMeta struct {

0 commit comments

Comments
 (0)