-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
148 lines (135 loc) · 5.02 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
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
143
144
145
146
147
148
package config
import (
// "os"
"reflect"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func TestConfig(t *testing.T) {
Convey("Config basic functionality", t, func() {
mainConfig := New()
slice := make([]interface{}, 3)
slice[0] = "1"
slice[1] = 2
slice[2] = true
slice2 := make([]interface{}, 4)
slice2[0] = "foo"
slice2[1] = 2
slice2[2] = true
slice2[3] = map[string]interface{}{
"val": 1,
}
configData := map[string]interface{}{
"foo": 1,
"slice": slice,
"slice2": slice2,
"stringSlice": []string{"foo"},
"intSlice": []int64{1},
"floatSlice": []float64{1},
"mapString": map[string]string{
"key1": "val1",
"key2": "val2",
},
"floatData": 1.1,
"bar": "baz",
"bool": "true",
"duration": time.Duration(123),
"durationString": "1h2m3s",
"durationInt": int(123),
"durationIntString": "123",
"durationInvalidString": "baz",
"db": map[string]interface{}{
"username": "user",
},
"db2": map[interface{}]interface{}{
"1": "test",
},
"deep": map[string]interface{}{
"notvalid": map[int]interface{}{
1: "bar",
},
},
"deep2": map[interface{}]interface{}{
"notvalid": map[int]interface{}{
1: "bar",
},
},
}
config2Data := map[string]interface{}{
"db": map[string]interface{}{
"password": "pass",
},
}
mainConfig.AddMap(configData)
mainConfig.AddMap(config2Data)
Convey("Sub Config", func() {
dbConfig := mainConfig.GetConfig("db")
notConfig := mainConfig.GetConfig("bar")
notFoundConfig := mainConfig.GetConfig("notfound")
So(dbConfig, ShouldNotBeNil)
So(notConfig, ShouldBeNil)
So(notFoundConfig, ShouldBeNil)
So(dbConfig.GetString("username"), ShouldEqual, "user")
})
Convey("Get variable", func() {
So(mainConfig.GetString("bar"), ShouldEqual, "baz")
So(mainConfig.GetInt("foo"), ShouldEqual, 1)
So(mainConfig.GetFloat32("floatData"), ShouldEqual, 1.1)
So(mainConfig.GetFloat64("floatData"), ShouldEqual, 1.1)
So(mainConfig.GetBool("bool"), ShouldEqual, true)
duration, _ := time.ParseDuration("1h2m3s")
So(mainConfig.GetDuration("duration"), ShouldEqual, 123)
So(mainConfig.GetDuration("durationString"), ShouldEqual, duration)
So(mainConfig.GetDuration("durationIntString"), ShouldEqual, 123)
So(mainConfig.GetDuration("durationInt"), ShouldEqual, 123)
So(reflect.DeepEqual(mainConfig.GetStringSlice("slice"), []string{"1", "2", "true"}), ShouldBeTrue)
So(reflect.DeepEqual(mainConfig.GetIntSlice("slice"), []int64{1, 2, 1}), ShouldBeTrue)
So(reflect.DeepEqual(mainConfig.GetFloatSlice("slice"), []float64{1, 2, 1}), ShouldBeTrue)
So(reflect.DeepEqual(mainConfig.GetStringSlice("stringSlice"), []string{"foo"}), ShouldBeTrue)
So(reflect.DeepEqual(mainConfig.GetIntSlice("intSlice"), []int64{1}), ShouldBeTrue)
So(reflect.DeepEqual(mainConfig.GetFloatSlice("floatSlice"), []float64{1}), ShouldBeTrue)
So(reflect.DeepEqual(mainConfig.GetStringSlice("intSlice"), []string{"1"}), ShouldBeTrue)
})
Convey("Get not found variable", func() {
So(mainConfig.GetString(""), ShouldEqual, "")
So(mainConfig.GetString("notfound"), ShouldEqual, "")
So(mainConfig.GetInt("notfound"), ShouldEqual, 0)
So(mainConfig.GetInt64("notfound"), ShouldEqual, 0)
So(mainConfig.GetFloat32("notfound"), ShouldEqual, 0)
So(mainConfig.GetFloat64("notfound"), ShouldEqual, 0)
So(mainConfig.GetBool("notfound"), ShouldEqual, false)
So(mainConfig.GetStringSlice("notfound"), ShouldBeNil)
So(mainConfig.GetIntSlice("notfound"), ShouldBeNil)
So(mainConfig.GetFloatSlice("notfound"), ShouldBeNil)
So(mainConfig.GetDuration("notfound"), ShouldEqual, 0)
So(mainConfig.GetDuration("db2.bar"), ShouldEqual, 0)
})
Convey("Get not valid variable", func() {
So(mainConfig.GetString("slice"), ShouldEqual, "")
So(mainConfig.GetInt("slice"), ShouldEqual, 0)
So(mainConfig.GetInt64("slice"), ShouldEqual, 0)
So(mainConfig.GetFloat32("slice"), ShouldEqual, 0)
So(mainConfig.GetFloat64("slice"), ShouldEqual, 0)
So(mainConfig.GetBool("slice"), ShouldEqual, false)
So(mainConfig.GetDuration("slice"), ShouldEqual, 0)
So(mainConfig.GetDuration("durationInvalidString"), ShouldEqual, 0)
So(mainConfig.GetStringSlice("slice2"), ShouldBeNil)
So(mainConfig.GetIntSlice("stringSlice"), ShouldBeNil)
So(mainConfig.GetIntSlice("slice2"), ShouldBeNil)
So(mainConfig.GetFloatSlice("stringSlice"), ShouldBeNil)
So(mainConfig.GetFloatSlice("slice2"), ShouldBeNil)
})
Convey("Test Delimeter", func() {
mainConfig.SetDelimiter("")
So(mainConfig.GetDelimiter(), ShouldEqual, DefaultDelimiter)
mainConfig.SetDelimiter(",")
So(mainConfig.GetString("db,username"), ShouldEqual, "user")
So(mainConfig.GetString("db2,1"), ShouldEqual, "test")
})
Convey("Test Not valid key type", func() {
So(mainConfig.GetString("deep.notvalid.1"), ShouldEqual, "")
So(mainConfig.GetString("deep2.notvalid.1"), ShouldEqual, "")
})
})
}