-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
75 lines (69 loc) · 1.75 KB
/
config_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
package google_pubsub_test
import (
"testing"
"time"
gp "github.com/indaband/google-pubsub"
"github.com/stretchr/testify/assert"
)
func TestConfigEvent(t *testing.T) {
t.Run("StringEnvSuiteCase", StringEnvSuiteCase)
}
func StringEnvSuiteCase(t *testing.T) {
t.Run("should return time.Duration type from string value", func(t *testing.T) {
duration := gp.String().ParseToDuration("3s", time.Second)
assert.Equal(t, time.Second*3, duration)
})
t.Run("should return default value from it fails to parse given value", func(t *testing.T) {
duration := gp.String().ParseToDuration("invalid-value", time.Second)
assert.Equal(t, time.Second, duration)
})
t.Run("should return default value from it if value is blank given value", func(t *testing.T) {
duration := gp.String().ParseToDuration("", time.Second)
assert.Equal(t, time.Second, duration)
})
}
func TestStringEnv_ParseToInt(t *testing.T) {
type args struct {
value string
defaultValue int
}
tests := []struct {
name string
s gp.StringEnv
args args
want int
}{
{
name: "it should convert to given number",
s: gp.String(),
args: args{
value: "10",
defaultValue: 1,
},
want: 10,
},
{
name: "it should grab default value in case of invalid one",
s: gp.String(),
args: args{
value: "fake",
defaultValue: 1,
},
want: 1,
},
{
name: "it should grab default value in case empty value",
s: gp.String(),
args: args{
value: "",
defaultValue: 21,
},
want: 21,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.s.ParseToInt(tt.args.value, tt.args.defaultValue), "ParseToInt(%v, %v)", tt.args.value, tt.args.defaultValue)
})
}
}