2017-05-03 07:30:18 +02:00
|
|
|
// Package with the mostly static content (models) of this microservice
|
2017-04-04 19:28:46 +02:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2017-05-03 07:30:18 +02:00
|
|
|
// Function to test UnmarshalTOML()
|
2017-04-04 19:28:46 +02:00
|
|
|
func TestDuration(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
var tests = []struct {
|
2017-04-04 23:21:05 +02:00
|
|
|
input interface{}
|
2017-04-04 19:28:46 +02:00
|
|
|
err string
|
|
|
|
duration time.Duration
|
|
|
|
}{
|
2017-04-04 23:21:05 +02:00
|
|
|
{3, "invalid duration: \"%!s(int=3)\"", 0},
|
2017-04-04 19:28:46 +02:00
|
|
|
{"", "invalid duration: \"\"", 0},
|
2017-04-04 23:21:05 +02:00
|
|
|
{"am", "unable to parse duration am: strconv.Atoi: parsing \"a\": invalid syntax", 0},
|
2017-04-04 19:28:46 +02:00
|
|
|
{"1x", "invalid duration unit: x", 0},
|
|
|
|
{"1s", "", time.Second},
|
|
|
|
{"73s", "", time.Second * 73},
|
|
|
|
{"1m", "", time.Minute},
|
|
|
|
{"73m", "", time.Minute * 73},
|
|
|
|
{"1h", "", time.Hour},
|
|
|
|
{"43h", "", time.Hour * 43},
|
|
|
|
{"1d", "", time.Hour * 24},
|
|
|
|
{"8d", "", time.Hour * 24 * 8},
|
|
|
|
{"1w", "", time.Hour * 24 * 7},
|
|
|
|
{"52w", "", time.Hour * 24 * 7 * 52},
|
|
|
|
{"1y", "", time.Hour * 24 * 365},
|
|
|
|
{"3y", "", time.Hour * 24 * 365 * 3},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
|
|
|
|
d := Duration{}
|
|
|
|
err := d.UnmarshalTOML(test.input)
|
|
|
|
duration := d.Duration
|
|
|
|
|
|
|
|
if test.err == "" {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(test.duration, duration)
|
|
|
|
} else {
|
|
|
|
assert.EqualError(err, test.err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|