-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdurations_test.go
72 lines (66 loc) · 1.5 KB
/
durations_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
package pgq
import (
"database/sql/driver"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestDurationsValue(t *testing.T) {
tt := []struct {
desc string
durations Durations
expectedVal driver.Value
expectedErr error
}{
{
desc: "a few durations",
durations: Durations{time.Minute, time.Minute * 60 * 48, 10},
expectedVal: "{1m0s,48h0m0s,10ns}",
},
}
for _, tc := range tt {
t.Run(tc.desc, func(t *testing.T) {
val, err := tc.durations.Value()
assert.Equal(t, tc.expectedVal, val)
assert.Equal(t, tc.expectedErr, err)
})
}
}
func TestDurationsScan(t *testing.T) {
tt := []struct {
desc string
src interface{}
hasErr bool
expectedDurations Durations
}{
{
desc: "valid src",
src: []byte("{1m0s,48h0m0s,10ns}"),
expectedDurations: Durations{time.Minute, time.Minute * 60 * 48, 10},
},
{
desc: "nil src",
expectedDurations: Durations{},
},
{
desc: "non byte src",
src: "blah blah",
expectedDurations: Durations{},
hasErr: true,
},
{
desc: "invalid durations",
src: []byte("{blerg}"),
expectedDurations: Durations{},
hasErr: true,
},
}
for _, tc := range tt {
t.Run(tc.desc, func(t *testing.T) {
ds := Durations{}
err := ds.Scan(tc.src)
assert.Equal(t, tc.expectedDurations, ds)
assert.Equal(t, tc.hasErr, err != nil)
})
}
}