1
0
mirror of https://github.com/rancher/steve.git synced 2025-07-17 08:21:41 +00:00
steve/pkg/resources/common/duration_test.go
Felipe Gehrke b3539616e0
#48673 - Added Timestamp Cache Handling to metadata.fields (#648)
* added timestamp convertion to metadata.fields

* fixed duration parsing

* fixed tests

* removed tags file

* added comments

* added better error handling

* changed ParseHumanDuration to use Fscanf

* added builtins handling

* adding mock updates

* fixing tests

* another try

* added timestamp convertion to metadata.fields

* addressing comments from @ericpromislow

* converting error to warning

* added template options
2025-06-16 15:33:28 -07:00

75 lines
1.4 KiB
Go

package common
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestParseHumanDuration(t *testing.T) {
testCases := []struct {
name string
input string
expected time.Duration
expectedErr bool
}{
{
name: "days + hours + mins + secs",
input: "1d23h45m56s",
expected: 24*time.Hour + 23*time.Hour + 45*time.Minute + 56*time.Second,
},
{
name: "hours + mins + secs",
input: "12h34m56s",
expected: 12*time.Hour + 34*time.Minute + 56*time.Second,
},
{
name: "days + hours",
input: "1d2h",
expected: 24*time.Hour + 2*time.Hour,
},
{
name: "hours + secs",
input: "1d2s",
expected: 24*time.Hour + 2*time.Second,
},
{
name: "mins + secs",
input: "1d2m",
expected: 24*time.Hour + 2*time.Minute,
},
{
name: "hours",
input: "1h",
expected: 1 * time.Hour,
},
{
name: "mins",
input: "1m",
expected: 1 * time.Minute,
},
{
name: "secs",
input: "0s",
expected: 0 * time.Second,
},
{
name: "invalid input",
input: "<invalid>",
expectedErr: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
output, err := ParseHumanReadableDuration(tc.input)
if tc.expectedErr {
assert.Error(t, err)
} else {
assert.Equal(t, tc.expected, output)
}
})
}
}