Ensure that tested hour never goes negative

This commit is contained in:
TwinProduction 2021-10-03 21:50:58 -04:00
parent babe7b0be9
commit d19f564e4e

View File

@ -171,7 +171,7 @@ func TestConfig_IsUnderMaintenance(t *testing.T) {
{ {
name: "under-maintenance-starting-4h-ago-for-8h", name: "under-maintenance-starting-4h-ago-for-8h",
cfg: &Config{ cfg: &Config{
Start: fmt.Sprintf("%02d:00", now.Hour()-4), Start: fmt.Sprintf("%02d:00", normalizeHour(now.Hour()-4)),
Duration: 8 * time.Hour, Duration: 8 * time.Hour,
}, },
expected: true, expected: true,
@ -179,7 +179,7 @@ func TestConfig_IsUnderMaintenance(t *testing.T) {
{ {
name: "under-maintenance-starting-4h-ago-for-3h", name: "under-maintenance-starting-4h-ago-for-3h",
cfg: &Config{ cfg: &Config{
Start: fmt.Sprintf("%02d:00", now.Hour()-4), Start: fmt.Sprintf("%02d:00", normalizeHour(now.Hour()-4)),
Duration: 3 * time.Hour, Duration: 3 * time.Hour,
}, },
expected: false, expected: false,
@ -187,7 +187,7 @@ func TestConfig_IsUnderMaintenance(t *testing.T) {
{ {
name: "under-maintenance-starting-5h-ago-for-1h", name: "under-maintenance-starting-5h-ago-for-1h",
cfg: &Config{ cfg: &Config{
Start: fmt.Sprintf("%02d:00", now.Hour()-5), Start: fmt.Sprintf("%02d:00", normalizeHour(now.Hour()-5)),
Duration: time.Hour, Duration: time.Hour,
}, },
expected: false, expected: false,
@ -204,8 +204,10 @@ func TestConfig_IsUnderMaintenance(t *testing.T) {
} }
for _, scenario := range scenarios { for _, scenario := range scenarios {
t.Run(scenario.name, func(t *testing.T) { t.Run(scenario.name, func(t *testing.T) {
if scenario.cfg.ValidateAndSetDefaults() != nil { t.Log(scenario.cfg.Start)
t.Fatal("validation shouldn't have returned an error") t.Log(now)
if err := scenario.cfg.ValidateAndSetDefaults(); err != nil {
t.Fatal("validation shouldn't have returned an error, got", err)
} }
isUnderMaintenance := scenario.cfg.IsUnderMaintenance() isUnderMaintenance := scenario.cfg.IsUnderMaintenance()
if isUnderMaintenance != scenario.expected { if isUnderMaintenance != scenario.expected {
@ -215,3 +217,10 @@ func TestConfig_IsUnderMaintenance(t *testing.T) {
}) })
} }
} }
func normalizeHour(hour int) int {
if hour < 0 {
return hour + 24
}
return hour
}