-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigKey_test.go
142 lines (127 loc) · 2.99 KB
/
ConfigKey_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package verdeter
import (
"errors"
"testing"
"github.com/ditrit/verdeter/models"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
func TestConfigKeyComputedValue(t *testing.T) {
viper.Reset()
cf := &ConfigKey{
Name: "test.ConfigKey",
computedValue: func() (dynamicDefault interface{}) {
return 2
},
}
cf.ComputeDefaultValue()
assert.Equal(t, 2, viper.GetInt("test.ConfigKey"))
viper.Set("test.ConfigKey", 1)
cf.ComputeDefaultValue()
assert.Equal(t, 1, viper.GetInt("test.ConfigKey"))
}
func TestConfigKeyCheckRequired(t *testing.T) {
viper.Reset()
cf := &ConfigKey{
Name: "test.ConfigKey",
required: false,
}
assert.NoError(t, cf.CheckRequired())
cf.required = true
assert.Error(t, cf.CheckRequired())
viper.Set("test.ConfigKey", 1)
assert.NoError(t, cf.CheckRequired())
}
func TestConfigKeyNormalize(t *testing.T) {
viper.Reset()
cf := &ConfigKey{
Name: "test.ConfigKey",
normalizeFunc: func(input interface{}) (output interface{}) {
intVal := input.(int)
return intVal + 1
},
}
viper.Set("test.ConfigKey", 1)
assert.Equal(t, 1, viper.GetInt("test.ConfigKey"))
cf.Normalize()
assert.Equal(t, 2, viper.GetInt("test.ConfigKey"))
}
func TestConfigKeyValidators(t *testing.T) {
viper.Reset()
cf := &ConfigKey{
Name: "test.ConfigKey",
validators: []models.Validator{
{
Name: "greater than 1",
Func: func(input interface{}) error {
intVal := input.(int)
if intVal < 1 {
return errors.New("greater than 1")
}
return nil
},
}, {
Name: "greater than 5",
Func: func(input interface{}) error {
intVal := input.(int)
if intVal < 5 {
return errors.New("greater than 5")
}
return nil
},
},
},
}
viper.Set("test.ConfigKey", 0)
assert.Len(t, cf.CheckValidators(), 2)
viper.Set("test.ConfigKey", 2)
assert.Len(t, cf.CheckValidators(), 1)
viper.Set("test.ConfigKey", 12)
assert.Len(t, cf.CheckValidators(), 0)
}
func TestConfigKeyValidate(t *testing.T) {
viper.Reset()
cf := &ConfigKey{
Name: "test.ConfigKey",
validators: []models.Validator{
{
Name: "greater than 1",
Func: func(input interface{}) error {
intVal := input.(int)
if intVal <= 1 {
return errors.New("greater than 1")
}
return nil
},
}, {
Name: "greater than 5",
Func: func(input interface{}) error {
intVal := input.(int)
if intVal <= 5 {
return errors.New("greater than 5")
}
return nil
},
},
},
required: false,
computedValue: func() (dynamicDefault interface{}) {
return 50
},
normalizeFunc: func(input interface{}) (output interface{}) {
intVal := input.(int)
return intVal + 1
},
}
viper.Reset()
assert.Len(t, cf.Validate(), 0)
assert.Equal(t, 51, viper.GetInt("test.ConfigKey"))
viper.Reset()
viper.Set("test.ConfigKey", -1)
assert.Len(t, cf.Validate(), 2)
viper.Reset()
viper.Set("test.ConfigKey", 1)
assert.Len(t, cf.Validate(), 1)
viper.Set("test.ConfigKey", 11)
assert.Len(t, cf.Validate(), 0)
}