Refactor timeperiod tests

parent dd4d270d
......@@ -31,6 +31,34 @@ func newTimePeriods(t *testing.T) (time.Time, *TimePeriod) {
return now, timePeriods
}
func TestInPeriod(t *testing.T) {
now, timePeriods := newTimePeriods(t)
timePeriods.GetCurrentTime = func() time.Time {
return now
}
assert.True(t, timePeriods.InPeriod())
}
func TestPeriodOut(t *testing.T) {
now, timePeriods := newTimePeriods(t)
timePeriods.GetCurrentTime = func() time.Time {
return now.Add(time.Hour * 48)
}
assert.False(t, timePeriods.InPeriod())
now, timePeriods = newTimePeriods(t)
timePeriods.GetCurrentTime = func() time.Time {
return now.Add(time.Hour * 4)
}
assert.False(t, timePeriods.InPeriod())
now, timePeriods = newTimePeriods(t)
timePeriods.GetCurrentTime = func() time.Time {
return now.Add(time.Minute * 4)
}
assert.False(t, timePeriods.InPeriod())
}
func TestInvalidTimezone(t *testing.T) {
_, err := TimePeriods([]string{}, "InvalidTimezone/String")
assert.Error(t, err)
......@@ -53,44 +81,19 @@ func testTimeperiodsWithTimezone(t *testing.T, period, timezone string, month ti
}
}
func TestOffPeakPeriod(t *testing.T) {
func TestTimeperiodsWithTimezone(t *testing.T) {
period := "* * 10-17 * * * *"
timezone := "Europe/Berlin"
// inside or outside of the timeperiod, basing on DST status
testTimeperiodsWithTimezone(t, "* * 10-17 * * * *", "Europe/Berlin", time.January, 1, 16, 30, true)
testTimeperiodsWithTimezone(t, "* * 10-17 * * * *", "Europe/Berlin", time.July, 1, 16, 30, false)
testTimeperiodsWithTimezone(t, period, timezone, time.January, 1, 16, 30, true)
testTimeperiodsWithTimezone(t, period, timezone, time.July, 1, 16, 30, false)
// always inside of the timeperiod
testTimeperiodsWithTimezone(t, "* * 10-17 * * * *", "Europe/Berlin", time.January, 1, 14, 30, true)
testTimeperiodsWithTimezone(t, "* * 10-17 * * * *", "Europe/Berlin", time.July, 1, 14, 30, true)
testTimeperiodsWithTimezone(t, period, timezone, time.January, 1, 14, 30, true)
testTimeperiodsWithTimezone(t, period, timezone, time.July, 1, 14, 30, true)
// always outside of the timeperiod
testTimeperiodsWithTimezone(t, "* * 10-17 * * * *", "Europe/Berlin", time.January, 1, 20, 30, false)
testTimeperiodsWithTimezone(t, "* * 10-17 * * * *", "Europe/Berlin", time.July, 1, 20, 30, false)
}
func TestInPeriod(t *testing.T) {
now, timePeriods := newTimePeriods(t)
timePeriods.GetCurrentTime = func() time.Time {
return now
}
assert.True(t, timePeriods.InPeriod())
}
func TestPeriodOut(t *testing.T) {
now, timePeriods := newTimePeriods(t)
timePeriods.GetCurrentTime = func() time.Time {
return now.Add(time.Hour * 48)
}
assert.False(t, timePeriods.InPeriod())
now, timePeriods = newTimePeriods(t)
timePeriods.GetCurrentTime = func() time.Time {
return now.Add(time.Hour * 4)
}
assert.False(t, timePeriods.InPeriod())
now, timePeriods = newTimePeriods(t)
timePeriods.GetCurrentTime = func() time.Time {
return now.Add(time.Minute * 4)
}
assert.False(t, timePeriods.InPeriod())
testTimeperiodsWithTimezone(t, period, timezone, time.January, 1, 20, 30, false)
testTimeperiodsWithTimezone(t, period, timezone, time.July, 1, 20, 30, false)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment