From 1679d00ba62710584d23017eb8469f8e9a5c90ab Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 5 Sep 2024 13:19:00 +0200 Subject: [PATCH 1/2] Test history.DowntimeEventTime#Value() --- pkg/icingadb/v1/history/downtime_test.go | 67 ++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 pkg/icingadb/v1/history/downtime_test.go diff --git a/pkg/icingadb/v1/history/downtime_test.go b/pkg/icingadb/v1/history/downtime_test.go new file mode 100644 index 000000000..e0f8c6f13 --- /dev/null +++ b/pkg/icingadb/v1/history/downtime_test.go @@ -0,0 +1,67 @@ +package history + +import ( + "database/sql/driver" + "github.com/icinga/icinga-go-library/types" + "github.com/stretchr/testify/require" + "testing" + "time" +) + +func TestDowntimeEventTime_Value(t *testing.T) { + start := types.UnixMilli(time.Unix(23, 320000000)) + cancel := types.UnixMilli(time.Unix(42, 240000000)) + end := types.UnixMilli(time.Unix(1337, 733100000)) + + f := types.Bool{Bool: false, Valid: true} + T := types.Bool{Bool: true, Valid: true} + + subtests := []struct { + name string + input *HistoryDowntime + output driver.Value + }{ + {name: "nil-history"}, + {name: "bad-event-type", input: &HistoryDowntime{ + HistoryMeta: HistoryMeta{EventType: "bad"}, + StartTime: start, CancelTime: cancel, EndTime: end, HasBeenCancelled: T, + }}, + {name: "start", output: int64(23320), input: &HistoryDowntime{ + HistoryMeta: HistoryMeta{EventType: "downtime_start"}, + StartTime: start, CancelTime: cancel, EndTime: end, HasBeenCancelled: T, + }}, + {name: "has-been-cancelled-nil", input: &HistoryDowntime{ + HistoryMeta: HistoryMeta{EventType: "downtime_end"}, + StartTime: start, CancelTime: cancel, EndTime: end, + }}, + {name: "has-been-cancelled", output: int64(42240), input: &HistoryDowntime{ + HistoryMeta: HistoryMeta{EventType: "downtime_end"}, + StartTime: start, CancelTime: cancel, EndTime: end, HasBeenCancelled: T, + }}, + {name: "end", output: int64(1337733), input: &HistoryDowntime{ + HistoryMeta: HistoryMeta{EventType: "downtime_end"}, + StartTime: start, CancelTime: cancel, EndTime: end, HasBeenCancelled: f, + }}, + {name: "start-nil", input: &HistoryDowntime{ + HistoryMeta: HistoryMeta{EventType: "downtime_start"}, + CancelTime: cancel, EndTime: end, HasBeenCancelled: T, + }}, + {name: "cancel-time-nil", input: &HistoryDowntime{ + HistoryMeta: HistoryMeta{EventType: "downtime_end"}, + StartTime: start, EndTime: end, HasBeenCancelled: T, + }}, + {name: "end-nil", input: &HistoryDowntime{ + HistoryMeta: HistoryMeta{EventType: "downtime_end"}, + StartTime: start, CancelTime: cancel, HasBeenCancelled: f, + }}, + } + + for _, st := range subtests { + t.Run(st.name, func(t *testing.T) { + v, err := (DowntimeEventTime{History: st.input}).Value() + + require.NoError(t, err) + require.Equal(t, st.output, v) + }) + } +} From 72283bc929abc8edc72e64249b56b4d7e52dbe56 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 5 Sep 2024 14:38:01 +0200 Subject: [PATCH 2/2] Test history.SlaDowntimeEndTime#Value() --- pkg/icingadb/v1/history/downtime_test.go | 36 ++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/pkg/icingadb/v1/history/downtime_test.go b/pkg/icingadb/v1/history/downtime_test.go index e0f8c6f13..2f4c3f9bc 100644 --- a/pkg/icingadb/v1/history/downtime_test.go +++ b/pkg/icingadb/v1/history/downtime_test.go @@ -65,3 +65,39 @@ func TestDowntimeEventTime_Value(t *testing.T) { }) } } + +func TestSlaDowntimeEndTime_Value(t *testing.T) { + cancel := types.UnixMilli(time.Unix(42, 240000000)) + end := types.UnixMilli(time.Unix(1337, 733100000)) + + f := types.Bool{Bool: false, Valid: true} + T := types.Bool{Bool: true, Valid: true} + + subtests := []struct { + name string + cancelled types.Bool + cancelTime types.UnixMilli + endTime types.UnixMilli + output driver.Value + }{ + {name: "nil", cancelTime: cancel, endTime: end, output: int64(1337733)}, + {name: "invalid", cancelled: types.Bool{Bool: true}, cancelTime: cancel, endTime: end, output: int64(1337733)}, + {name: "false", cancelled: f, cancelTime: cancel, endTime: end, output: int64(1337733)}, + {name: "true", cancelled: T, cancelTime: cancel, endTime: end, output: int64(42240)}, + {name: "false-nil", cancelled: f, cancelTime: cancel}, + {name: "true-nil", cancelled: T, endTime: end}, + } + + for _, st := range subtests { + t.Run(st.name, func(t *testing.T) { + v, err := (SlaDowntimeEndTime{History: &SlaHistoryDowntime{ + HasBeenCancelled: st.cancelled, + CancelTime: st.cancelTime, + EndTime: st.endTime, + }}).Value() + + require.NoError(t, err) + require.Equal(t, st.output, v) + }) + } +}