-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathconfig_test.go
135 lines (108 loc) · 2.83 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
package gogo
import (
"io/ioutil"
"os"
"path"
"strconv"
"strings"
"sync/atomic"
"testing"
"github.com/golib/assert"
)
var (
fakePort uint64 = 9090
fakeConfig = func(name string) (config *AppConfig, err error) {
root, _ := os.Getwd()
filename := path.Join(root, "testdata", "config", name)
data, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
port := atomic.AddUint64(&fakePort, 1)
config, err = NewAppConfigFromString(strings.Replace(string(data), "9090", strconv.FormatUint(port, 10), -1))
if err == nil {
config.filename = filename
// try to load config of middlewares
config.LoadInterceptors()
}
return
}
)
func Test_NewConfig(t *testing.T) {
it := assert.New(t)
config, err := fakeConfig("application.yml")
if it.Nil(err) {
it.Implements((*Configer)(nil), config)
it.Equal(Test, config.Mode)
it.Equal("gogo", config.Name)
it.NotEmpty(config.Sections)
}
}
func Test_NewConfigWithoutMode(t *testing.T) {
it := assert.New(t)
config, err := NewAppConfigFromString(`{"name": "testing"}`)
if it.Nil(err) {
it.Equal(Development, config.Mode)
}
}
func Test_NewConfigWithoutName(t *testing.T) {
it := assert.New(t)
config, err := NewAppConfigFromString(`{"mode": "test"}`)
if it.Nil(err) {
it.Equal("GOGO", config.Name)
}
}
func Test_ConfigSetMode(t *testing.T) {
it := assert.New(t)
config, err := fakeConfig("application.yml")
if it.Nil(err) {
it.Equal(Test, config.Mode)
config.SetMode(Production)
it.Equal(Production, config.Mode)
}
}
func Test_ConfigSection(t *testing.T) {
it := assert.New(t)
config, err := fakeConfig("application.yml")
if it.Nil(err) {
section := config.Section()
it.NotNil(section.Server)
it.NotNil(section.Logger)
}
}
func Test_ConfigUnmarshalYAML(t *testing.T) {
it := assert.New(t)
var testConfig struct {
GettingStart struct {
Greeting string `yaml:"greeting"`
} `yaml:"getting_start"`
}
config, err := fakeConfig("application.yml")
if it.Nil(err) {
config.SetMode(Development)
err := config.UnmarshalYAML(&testConfig)
if it.Nil(err) {
it.Equal("Hello, gogo!", testConfig.GettingStart.Greeting)
}
}
}
func Test_ConfigWithDefaults(t *testing.T) {
it := assert.New(t)
config, err := NewAppConfigFromDefault()
if it.Nil(err) {
// it should work for development mode
testModes := []RunMode{
Development, Test, Production,
}
for _, mode := range testModes {
config.SetMode(mode)
section := config.Section()
it.Equal(DefaultServerConfig.Addr, section.Server.Addr)
it.Equal(DefaultServerConfig.Port, section.Server.Port)
it.Equal(DefaultServerConfig.Ssl, section.Server.Ssl)
it.Equal(DefaultLoggerConfig.Output, section.Logger.Output)
it.Equal(DefaultLoggerConfig.LevelName, section.Logger.LevelName)
it.Equal(DefaultLoggerConfig.FilterFields, section.Logger.FilterFields)
}
}
}