style(alerting): align quiet hours code and tests with Go style guide
This commit is contained in:
1 parent
ee3769f578
commit
1c31003baf
4 files changed
+162
-94
No files matched your search
@@ -7,22 +7,23 @@ import (
|
||||
)
|
||||
|
||||
func TestValidateQuietHours(t *testing.T) {
|
||||
got, code := validateQuietHours([]quietHourRequest{
|
||||
in := []quietHourRequest{
|
||||
{Weekdays: flexInts{5, 1, 5, 3}, Start: "22:00", End: "07:00", TimeZone: "Europe/Berlin"},
|
||||
{Weekdays: nil, Start: "00:00", End: "24:00", TimeZone: "UTC"},
|
||||
{Weekdays: flexInts{0, 6}, Start: "00:00", End: "00:00", TimeZone: "UTC"},
|
||||
})
|
||||
}
|
||||
got, code := validateQuietHours(in)
|
||||
if code != "" {
|
||||
t.Fatalf("unexpected error code %q", code)
|
||||
t.Fatalf("validateQuietHours(%+v) error code = %q, want %q", in, code, "")
|
||||
}
|
||||
if len(got) != 3 {
|
||||
t.Fatalf("len = %d, want 3", len(got))
|
||||
t.Fatalf("len(validateQuietHours(...)) = %d, want 3", len(got))
|
||||
}
|
||||
if !slices.Equal(got[0].Weekdays, []int{1, 3, 5}) {
|
||||
t.Errorf("weekdays not sorted/deduped: %v", got[0].Weekdays)
|
||||
if want := []int{1, 3, 5}; !slices.Equal(got[0].Weekdays, want) {
|
||||
t.Errorf("got[0].Weekdays = %v, want %v", got[0].Weekdays, want)
|
||||
}
|
||||
if got[1].Weekdays == nil || len(got[1].Weekdays) != 0 {
|
||||
t.Errorf("nil weekdays should normalize to empty slice: %#v", got[1].Weekdays)
|
||||
t.Errorf("got[1].Weekdays = %#v, want non-nil empty []int{}", got[1].Weekdays)
|
||||
}
|
||||
|
||||
invalid := []struct {
|
||||
@@ -30,33 +31,73 @@ func TestValidateQuietHours(t *testing.T) {
|
||||
req quietHourRequest
|
||||
want string
|
||||
}{
|
||||
{"empty tz", quietHourRequest{Start: "22:00", End: "07:00", TimeZone: ""}, "invalid_time_zone"},
|
||||
{"local tz", quietHourRequest{Start: "22:00", End: "07:00", TimeZone: "Local"}, "invalid_time_zone"},
|
||||
{"unknown tz", quietHourRequest{Start: "22:00", End: "07:00", TimeZone: "Mars/Olympus"}, "invalid_time_zone"},
|
||||
{"bad start", quietHourRequest{Start: "24:00", End: "07:00", TimeZone: "UTC"}, "invalid_quiet_hours"},
|
||||
{"bad end", quietHourRequest{Start: "22:00", End: "24:01", TimeZone: "UTC"}, "invalid_quiet_hours"},
|
||||
{"short format", quietHourRequest{Start: "9:00", End: "17:00", TimeZone: "UTC"}, "invalid_quiet_hours"},
|
||||
{"equal non-zero", quietHourRequest{Start: "12:00", End: "12:00", TimeZone: "UTC"}, "invalid_quiet_hours"},
|
||||
{"weekday negative", quietHourRequest{Weekdays: flexInts{-1}, Start: "22:00", End: "07:00", TimeZone: "UTC"}, "invalid_quiet_hours"},
|
||||
{"weekday > 6", quietHourRequest{Weekdays: flexInts{7}, Start: "22:00", End: "07:00", TimeZone: "UTC"}, "invalid_quiet_hours"},
|
||||
{
|
||||
name: "empty time zone",
|
||||
req: quietHourRequest{Start: "22:00", End: "07:00", TimeZone: ""},
|
||||
want: "invalid_time_zone",
|
||||
},
|
||||
{
|
||||
name: "local time zone",
|
||||
req: quietHourRequest{Start: "22:00", End: "07:00", TimeZone: "Local"},
|
||||
want: "invalid_time_zone",
|
||||
},
|
||||
{
|
||||
name: "unknown time zone",
|
||||
req: quietHourRequest{Start: "22:00", End: "07:00", TimeZone: "Mars/Olympus"},
|
||||
want: "invalid_time_zone",
|
||||
},
|
||||
{
|
||||
name: "invalid start hour",
|
||||
req: quietHourRequest{Start: "24:00", End: "07:00", TimeZone: "UTC"},
|
||||
want: "invalid_quiet_hours",
|
||||
},
|
||||
{
|
||||
name: "invalid end minute",
|
||||
req: quietHourRequest{Start: "22:00", End: "24:01", TimeZone: "UTC"},
|
||||
want: "invalid_quiet_hours",
|
||||
},
|
||||
{
|
||||
name: "short clock format",
|
||||
req: quietHourRequest{Start: "9:00", End: "17:00", TimeZone: "UTC"},
|
||||
want: "invalid_quiet_hours",
|
||||
},
|
||||
{
|
||||
name: "equal non-zero start and end",
|
||||
req: quietHourRequest{Start: "12:00", End: "12:00", TimeZone: "UTC"},
|
||||
want: "invalid_quiet_hours",
|
||||
},
|
||||
{
|
||||
name: "negative weekday",
|
||||
req: quietHourRequest{Weekdays: flexInts{-1}, Start: "22:00", End: "07:00", TimeZone: "UTC"},
|
||||
want: "invalid_quiet_hours",
|
||||
},
|
||||
{
|
||||
name: "weekday out of range",
|
||||
req: quietHourRequest{Weekdays: flexInts{7}, Start: "22:00", End: "07:00", TimeZone: "UTC"},
|
||||
want: "invalid_quiet_hours",
|
||||
},
|
||||
}
|
||||
for _, tc := range invalid {
|
||||
if _, code := validateQuietHours([]quietHourRequest{tc.req}); code != tc.want {
|
||||
t.Errorf("%s: code = %q, want %q", tc.name, code, tc.want)
|
||||
}
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
_, got := validateQuietHours([]quietHourRequest{tc.req})
|
||||
if got != tc.want {
|
||||
t.Errorf("validateQuietHours(%+v) error code = %q, want %q", tc.req, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestQuietHoursContains(t *testing.T) {
|
||||
berlin, err := time.LoadLocation("Europe/Berlin")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
t.Fatalf("time.LoadLocation(%q) error = %v", "Europe/Berlin", err)
|
||||
}
|
||||
at := func(y int, m time.Month, d, hh, mm int, loc *time.Location) time.Time {
|
||||
return time.Date(y, m, d, hh, mm, 0, 0, loc)
|
||||
}
|
||||
|
||||
// 2026-11-23 is Monday, 2026-11-27 is Friday, 2026-11-28 is Saturday, 2026-11-29 is Sunday.
|
||||
// 2026-11-23 is Monday, 2026-11-27 is Friday, 2026-11-28 is Saturday,
|
||||
// and 2026-11-29 is Sunday.
|
||||
daytime := quietHourView{Weekdays: []int{1, 2, 3, 4, 5}, Start: "09:00", End: "17:00", TimeZone: "Europe/Berlin"}
|
||||
overnight := quietHourView{Weekdays: []int{1, 2, 3, 4, 5}, Start: "22:00", End: "07:00", TimeZone: "Europe/Berlin"}
|
||||
weekendZero := quietHourView{Weekdays: []int{0, 6}, Start: "00:00", End: "00:00", TimeZone: "Europe/Berlin"}
|
||||
@@ -65,45 +106,47 @@ func TestQuietHoursContains(t *testing.T) {
|
||||
dailyNight := quietHourView{Weekdays: []int{}, Start: "23:00", End: "06:00", TimeZone: "Europe/Berlin"}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
w quietHourView
|
||||
ts time.Time
|
||||
want bool
|
||||
name string
|
||||
window quietHourView
|
||||
ts time.Time
|
||||
want bool
|
||||
}{
|
||||
{"daytime before start", daytime, at(2026, 11, 23, 8, 59, berlin), false},
|
||||
{"daytime at start", daytime, at(2026, 11, 23, 9, 0, berlin), true},
|
||||
{"daytime inside", daytime, at(2026, 11, 23, 16, 59, berlin), true},
|
||||
{"daytime at end", daytime, at(2026, 11, 23, 17, 0, berlin), false},
|
||||
{"daytime wrong day", daytime, at(2026, 11, 28, 12, 0, berlin), false},
|
||||
{name: "daytime before start", window: daytime, ts: at(2026, 11, 23, 8, 59, berlin), want: false},
|
||||
{name: "daytime at start", window: daytime, ts: at(2026, 11, 23, 9, 0, berlin), want: true},
|
||||
{name: "daytime inside", window: daytime, ts: at(2026, 11, 23, 16, 59, berlin), want: true},
|
||||
{name: "daytime at end", window: daytime, ts: at(2026, 11, 23, 17, 0, berlin), want: false},
|
||||
{name: "daytime wrong day", window: daytime, ts: at(2026, 11, 28, 12, 0, berlin), want: false},
|
||||
|
||||
{"overnight mon before", overnight, at(2026, 11, 23, 21, 59, berlin), false},
|
||||
{"overnight mon start", overnight, at(2026, 11, 23, 22, 0, berlin), true},
|
||||
{"overnight mon early morning (from sun)", overnight, at(2026, 11, 23, 3, 0, berlin), false},
|
||||
{"overnight tue early morning (from mon)", overnight, at(2026, 11, 24, 6, 59, berlin), true},
|
||||
{"overnight tue at end", overnight, at(2026, 11, 24, 7, 0, berlin), false},
|
||||
{"overnight sat early morning (from fri)", overnight, at(2026, 11, 28, 3, 0, berlin), true},
|
||||
{"overnight sat late evening", overnight, at(2026, 11, 28, 23, 0, berlin), false},
|
||||
{"overnight sun early morning", overnight, at(2026, 11, 29, 3, 0, berlin), false},
|
||||
{name: "overnight mon before", window: overnight, ts: at(2026, 11, 23, 21, 59, berlin), want: false},
|
||||
{name: "overnight mon start", window: overnight, ts: at(2026, 11, 23, 22, 0, berlin), want: true},
|
||||
{name: "overnight mon early from sun", window: overnight, ts: at(2026, 11, 23, 3, 0, berlin), want: false},
|
||||
{name: "overnight tue early from mon", window: overnight, ts: at(2026, 11, 24, 6, 59, berlin), want: true},
|
||||
{name: "overnight tue at end", window: overnight, ts: at(2026, 11, 24, 7, 0, berlin), want: false},
|
||||
{name: "overnight sat early from fri", window: overnight, ts: at(2026, 11, 28, 3, 0, berlin), want: true},
|
||||
{name: "overnight sat late", window: overnight, ts: at(2026, 11, 28, 23, 0, berlin), want: false},
|
||||
{name: "overnight sun early", window: overnight, ts: at(2026, 11, 29, 3, 0, berlin), want: false},
|
||||
|
||||
{"weekend 00-00 sat", weekendZero, at(2026, 11, 28, 0, 0, berlin), true},
|
||||
{"weekend 00-00 sun late", weekendZero, at(2026, 11, 29, 23, 59, berlin), true},
|
||||
{"weekend 00-00 mon", weekendZero, at(2026, 11, 30, 0, 0, berlin), false},
|
||||
{"weekend 00-24 sat", weekend24, at(2026, 11, 28, 15, 0, berlin), true},
|
||||
{"weekend 00-24 mon", weekend24, at(2026, 11, 30, 0, 0, berlin), false},
|
||||
{name: "weekend 00-00 sat", window: weekendZero, ts: at(2026, 11, 28, 0, 0, berlin), want: true},
|
||||
{name: "weekend 00-00 sun late", window: weekendZero, ts: at(2026, 11, 29, 23, 59, berlin), want: true},
|
||||
{name: "weekend 00-00 mon", window: weekendZero, ts: at(2026, 11, 30, 0, 0, berlin), want: false},
|
||||
{name: "weekend 00-24 sat", window: weekend24, ts: at(2026, 11, 28, 15, 0, berlin), want: true},
|
||||
{name: "weekend 00-24 mon", window: weekend24, ts: at(2026, 11, 30, 0, 0, berlin), want: false},
|
||||
|
||||
{"evening 18-00 fri inside", eveningEndZero, at(2026, 11, 27, 23, 59, time.UTC), true},
|
||||
{"evening 18-00 sat midnight", eveningEndZero, at(2026, 11, 28, 0, 0, time.UTC), false},
|
||||
{name: "evening 18-00 fri inside", window: eveningEndZero, ts: at(2026, 11, 27, 23, 59, time.UTC), want: true},
|
||||
{name: "evening 18-00 sat midnight", window: eveningEndZero, ts: at(2026, 11, 28, 0, 0, time.UTC), want: false},
|
||||
|
||||
// Winter (CET = UTC+1) vs summer (CEST = UTC+2) in Europe/Berlin.
|
||||
{"daily winter 22:30 UTC is 23:30 CET", dailyNight, at(2026, 1, 15, 22, 30, time.UTC), true},
|
||||
{"daily winter 21:30 UTC is 22:30 CET", dailyNight, at(2026, 1, 15, 21, 30, time.UTC), false},
|
||||
{"daily summer 21:30 UTC is 23:30 CEST", dailyNight, at(2026, 7, 15, 21, 30, time.UTC), true},
|
||||
{"daily summer 03:30 UTC is 05:30 CEST", dailyNight, at(2026, 7, 15, 3, 30, time.UTC), true},
|
||||
{"daily summer 04:00 UTC is 06:00 CEST", dailyNight, at(2026, 7, 15, 4, 0, time.UTC), false},
|
||||
{name: "daily winter 22:30 UTC is 23:30 CET", window: dailyNight, ts: at(2026, 1, 15, 22, 30, time.UTC), want: true},
|
||||
{name: "daily winter 21:30 UTC is 22:30 CET", window: dailyNight, ts: at(2026, 1, 15, 21, 30, time.UTC), want: false},
|
||||
{name: "daily summer 21:30 UTC is 23:30 CEST", window: dailyNight, ts: at(2026, 7, 15, 21, 30, time.UTC), want: true},
|
||||
{name: "daily summer 03:30 UTC is 05:30 CEST", window: dailyNight, ts: at(2026, 7, 15, 3, 30, time.UTC), want: true},
|
||||
{name: "daily summer 04:00 UTC is 06:00 CEST", window: dailyNight, ts: at(2026, 7, 15, 4, 0, time.UTC), want: false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
if got := tc.w.contains(tc.ts); got != tc.want {
|
||||
t.Errorf("%s: contains(%s) = %t, want %t", tc.name, tc.ts.Format(time.RFC3339), got, tc.want)
|
||||
}
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := tc.window.contains(tc.ts); got != tc.want {
|
||||
t.Errorf("contains(%s) = %t, want %t", tc.ts.Format(time.RFC3339), got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user