2020-04-03 23:11:33 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
2020-04-05 12:37:21 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-04-03 23:11:33 +00:00
|
|
|
)
|
|
|
|
|
2022-03-02 06:40:26 +00:00
|
|
|
func TestParseDurationString_ShouldParseDurationString(t *testing.T) {
|
2020-04-03 23:11:33 +00:00
|
|
|
duration, err := ParseDurationString("1h")
|
2022-03-02 06:40:26 +00:00
|
|
|
|
2020-04-03 23:11:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 60*time.Minute, duration)
|
|
|
|
}
|
|
|
|
|
2022-03-02 06:40:26 +00:00
|
|
|
func TestParseDurationString_ShouldParseBlankString(t *testing.T) {
|
|
|
|
duration, err := ParseDurationString("")
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, time.Second*0, duration)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseDurationString_ShouldParseDurationStringAllUnits(t *testing.T) {
|
2020-04-03 23:11:33 +00:00
|
|
|
duration, err := ParseDurationString("1y")
|
2022-03-02 06:40:26 +00:00
|
|
|
|
2020-04-03 23:11:33 +00:00
|
|
|
assert.NoError(t, err)
|
2022-03-02 06:40:26 +00:00
|
|
|
assert.Equal(t, time.Hour*24*365, duration)
|
2020-04-03 23:11:33 +00:00
|
|
|
|
|
|
|
duration, err = ParseDurationString("1M")
|
2022-03-02 06:40:26 +00:00
|
|
|
|
2020-04-03 23:11:33 +00:00
|
|
|
assert.NoError(t, err)
|
2022-03-02 06:40:26 +00:00
|
|
|
assert.Equal(t, time.Hour*24*30, duration)
|
2020-04-03 23:11:33 +00:00
|
|
|
|
|
|
|
duration, err = ParseDurationString("1w")
|
2022-03-02 06:40:26 +00:00
|
|
|
|
2020-04-03 23:11:33 +00:00
|
|
|
assert.NoError(t, err)
|
2022-03-02 06:40:26 +00:00
|
|
|
assert.Equal(t, time.Hour*24*7, duration)
|
2020-04-03 23:11:33 +00:00
|
|
|
|
|
|
|
duration, err = ParseDurationString("1d")
|
2022-03-02 06:40:26 +00:00
|
|
|
|
2020-04-03 23:11:33 +00:00
|
|
|
assert.NoError(t, err)
|
2022-03-02 06:40:26 +00:00
|
|
|
assert.Equal(t, time.Hour*24, duration)
|
2020-04-03 23:11:33 +00:00
|
|
|
|
|
|
|
duration, err = ParseDurationString("1h")
|
2022-03-02 06:40:26 +00:00
|
|
|
|
2020-04-03 23:11:33 +00:00
|
|
|
assert.NoError(t, err)
|
2022-03-02 06:40:26 +00:00
|
|
|
assert.Equal(t, time.Hour, duration)
|
2020-04-03 23:11:33 +00:00
|
|
|
|
|
|
|
duration, err = ParseDurationString("1s")
|
2022-03-02 06:40:26 +00:00
|
|
|
|
2020-04-03 23:11:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, time.Second, duration)
|
|
|
|
}
|
|
|
|
|
2022-03-02 06:40:26 +00:00
|
|
|
func TestParseDurationString_ShouldParseSecondsString(t *testing.T) {
|
2020-04-03 23:11:33 +00:00
|
|
|
duration, err := ParseDurationString("100")
|
2022-03-02 06:40:26 +00:00
|
|
|
|
2020-04-03 23:11:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 100*time.Second, duration)
|
|
|
|
}
|
|
|
|
|
2022-03-02 06:40:26 +00:00
|
|
|
func TestParseDurationString_ShouldNotParseDurationStringWithOutOfOrderQuantitiesAndUnits(t *testing.T) {
|
2020-04-03 23:11:33 +00:00
|
|
|
duration, err := ParseDurationString("h1")
|
2022-03-02 06:40:26 +00:00
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
assert.EqualError(t, err, "could not parse 'h1' as a duration")
|
2020-04-03 23:11:33 +00:00
|
|
|
assert.Equal(t, time.Duration(0), duration)
|
|
|
|
}
|
|
|
|
|
2022-03-02 06:40:26 +00:00
|
|
|
func TestParseDurationString_ShouldNotParseBadDurationString(t *testing.T) {
|
2020-04-03 23:11:33 +00:00
|
|
|
duration, err := ParseDurationString("10x")
|
2022-03-02 06:40:26 +00:00
|
|
|
|
|
|
|
assert.EqualError(t, err, "could not parse the units portion of '10x' in duration string '10x': the unit 'x' is not valid")
|
2020-04-03 23:11:33 +00:00
|
|
|
assert.Equal(t, time.Duration(0), duration)
|
|
|
|
}
|
|
|
|
|
2022-03-02 06:40:26 +00:00
|
|
|
func TestParseDurationString_ShouldParseDurationStringWithMultiValueUnits(t *testing.T) {
|
2020-04-03 23:11:33 +00:00
|
|
|
duration, err := ParseDurationString("10ms")
|
2022-03-02 06:40:26 +00:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, time.Duration(10)*time.Millisecond, duration)
|
2020-04-03 23:11:33 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 06:40:26 +00:00
|
|
|
func TestParseDurationString_ShouldParseDurationStringWithLeadingZero(t *testing.T) {
|
2020-04-03 23:11:33 +00:00
|
|
|
duration, err := ParseDurationString("005h")
|
2022-03-02 06:40:26 +00:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, time.Duration(5)*time.Hour, duration)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseDurationString_ShouldParseMultiUnitValues(t *testing.T) {
|
|
|
|
duration, err := ParseDurationString("1d3w10ms")
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t,
|
|
|
|
(time.Hour*time.Duration(24))+
|
|
|
|
(time.Hour*time.Duration(24)*time.Duration(7)*time.Duration(3))+
|
|
|
|
(time.Millisecond*time.Duration(10)), duration)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseDurationString_ShouldParseDuplicateUnitValues(t *testing.T) {
|
|
|
|
duration, err := ParseDurationString("1d4d2d")
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t,
|
|
|
|
(time.Hour*time.Duration(24))+
|
|
|
|
(time.Hour*time.Duration(24)*time.Duration(4))+
|
|
|
|
(time.Hour*time.Duration(24)*time.Duration(2)), duration)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStandardizeDurationString_ShouldParseStringWithSpaces(t *testing.T) {
|
|
|
|
result, err := StandardizeDurationString("1d 1h 20m")
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, result, "24h1h20m")
|
2020-04-03 23:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldTimeIntervalsMakeSense(t *testing.T) {
|
|
|
|
assert.Equal(t, Hour, time.Minute*60)
|
|
|
|
assert.Equal(t, Day, Hour*24)
|
|
|
|
assert.Equal(t, Week, Day*7)
|
|
|
|
assert.Equal(t, Year, Day*365)
|
|
|
|
assert.Equal(t, Month, Year/12)
|
|
|
|
}
|