-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
93 lines (79 loc) · 1.82 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
package boot
import (
"testing"
"github.com/spf13/viper"
)
func TestInitDefaultConfig(t *testing.T) {
keyStr := "str"
keyInt := "int"
keyBool := "bool"
testDefaultConfig := map[string]interface{}{
keyStr: "abc",
keyInt: 123,
keyBool: true,
}
testConfigPrefix := "demo.test."
InitDefaultConfig(testConfigPrefix, testDefaultConfig)
if testDefaultConfig[keyStr] != viper.GetString(testConfigPrefix+keyStr) {
t.Fail()
}
if testDefaultConfig[keyInt] != viper.GetInt(testConfigPrefix+keyInt) {
t.Fail()
}
if testDefaultConfig[keyBool] != viper.GetBool(testConfigPrefix+keyBool) {
t.Fail()
}
}
func TestInitDefault(t *testing.T) {
_ = initConfig()
if viper.GetString(configPrefix+"file") != defaultConfig["file"] {
t.Fail()
}
if viper.GetString(configPrefix+"path") != defaultConfig["path"] {
t.Fail()
}
if viper.GetBool(configPrefix+"enabled") != defaultConfig["enabled"] {
t.Fail()
}
}
func TestInitLocalConfigFailed(t *testing.T) {
// load bad local file
viper.Set("boot.config.file", "bad_config_test.yaml")
if initLocalConfig() == nil {
t.Fail()
}
}
func TestInitLocalConfigFaile1(t *testing.T) {
// not found file
viper.Set("boot.config.file", "not_found.yaml")
if initLocalConfig() != nil {
t.Fail()
}
}
func TestInitLocalConfig(t *testing.T) {
viper.Set("boot.config.file", "application_test.yaml")
if initLocalConfig() != nil {
t.Fail()
}
if viper.GetString("test.str") != "foo" {
t.Fail()
}
if viper.GetInt("test.int") != 2233 {
t.Fail()
}
if viper.GetBool("test.bool") != false {
t.Fail()
}
}
func TestInitConfig(t *testing.T) {
viper.Set("boot.config.file", "application_test.yaml")
if initConfig() != nil {
t.Fail()
}
viper.Set("boot.config.enabled", false)
if initConfig() != nil {
t.Fail()
}
// restore the default values
viper.Set("boot.config.enabled", true)
}