-
Notifications
You must be signed in to change notification settings - Fork 0
/
duration_test.go
87 lines (76 loc) · 2.15 KB
/
duration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package gosql
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestDuration(t *testing.T) {
tests := []struct {
value string
target Duration
}{
{"1ns", Nanosecond},
{"1us", Microsecond},
{"1ms", Millisecond},
{"1s", Second},
{"1m", Minute},
{"1h", Hour},
{"1d", Duration(24 * time.Hour)},
{"1w", Duration(7 * 24 * time.Hour)},
}
for _, test := range tests {
t.Run("parse:"+test.value, func(t *testing.T) {
d, err := ParseDuration(test.value)
if assert.NoError(t, err) {
assert.Equal(t, test.target, d)
}
})
t.Run("scan:"+test.value, func(t *testing.T) {
var d Duration
if err := d.Scan(test.value); assert.NoError(t, err) {
assert.Equal(t, test.target, d)
}
if err := d.Scan([]byte(test.value)); assert.NoError(t, err) {
assert.Equal(t, test.target, d)
}
if err := d.Scan(int64(d)); assert.NoError(t, err) {
assert.Equal(t, test.target, d)
}
})
}
t.Run("scan:invalid", func(t *testing.T) {
var d Duration
assert.Error(t, d.Scan("invalid"))
assert.Error(t, d.Scan([]byte("invalid")))
assert.Error(t, d.Scan("w-error"))
assert.Error(t, d.Scan(any(nil)))
})
t.Run("string", func(t *testing.T) {
assert.Equal(t, "1ns", Nanosecond.String())
assert.Equal(t, "1µs", Microsecond.String())
})
t.Run("values", func(t *testing.T) {
assert.Equal(t, int64(1), Nanosecond.Nanoseconds())
assert.Equal(t, int64(1), Microsecond.Microseconds())
assert.Equal(t, int64(1), Millisecond.Milliseconds())
assert.Equal(t, float64(1), Second.Seconds())
assert.Equal(t, float64(1), Minute.Minutes())
assert.Equal(t, float64(1), Hour.Hours())
assert.Equal(t, Hour, Hour.Truncate(Minute))
assert.Equal(t, Hour, Hour.Round(Minute))
assert.Equal(t, Hour, Hour.Abs())
v, err := Hour.Value()
assert.NoError(t, err)
assert.Equal(t, "1h0m0s", v)
})
t.Run("json", func(t *testing.T) {
var d Duration
assert.NoError(t, d.UnmarshalJSON([]byte(`"1h"`)))
assert.Equal(t, Hour, d)
v, err := d.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, []byte(`"1h0m0s"`), v)
assert.Error(t, d.UnmarshalJSON([]byte(`1h`)))
assert.Error(t, d.UnmarshalJSON([]byte(``)))
})
}