-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
230 lines (187 loc) · 4.45 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"io/ioutil"
"os"
"testing"
)
type dbCfgMock struct {
Conf, Valid string
ValidInterval int
ValidLimit float64
}
var t_toml = []dbCfgMock{
dbCfgMock{
Conf: `
# Some comment
[database]
type = "mysql"
user = "any"
name = "dbname"
host = "some.host"
[score]
interval = 77
`,
Valid: "any@tcp(some.host:3306)/dbname",
ValidInterval: 77,
ValidLimit: 0.22,
},
dbCfgMock{
Conf: `
# Some comment
[database]
type = "mysql"
name = "dbname"
user = "any"
password = "good"
port = 3336
host = "some.host"
[database.parameters]
charset = "utf8"
loc = "US/Pacific"
parseTime = "true"
[score]
limit = 0.13
`,
Valid: "any:good@tcp(some.host:3336)/dbname?charset=utf8&loc=US%2FPacific&parseTime=true",
ValidInterval: 60,
ValidLimit: 0.13,
},
}
// Helper: create temporary file
func getTempFile() (fpath string, err error) {
var (
file *os.File
)
if file, err = ioutil.TempFile("", "test_config.toml"); err != nil {
return
}
fpath = file.Name()
file.Close()
return
}
// Helper: Write temporary content
func setTempConf(fpath, content string) (err error) {
var file *os.File
if file, err = os.OpenFile(fpath, os.O_WRONLY, 0666); err != nil {
return err
}
defer file.Close()
_, err = file.WriteString(content)
return
}
func TestConfig_FileNotexists(t *testing.T) {
var (
err error
)
if _, err = NewConfig("/this/is/not/valid/File/check"); err == nil {
t.Fatal("Expected error on none valid file, but got nothing")
}
}
func TestConfig_DirInsteadFile(t *testing.T) {
var (
dir string
err error
)
if dir, err = ioutil.TempDir("", ""); err != nil {
t.Fatalf("Can't get temporary dir, error: %s", err.Error())
}
if _, err = NewConfig(dir); err == nil {
t.Fatal("Expected error message, but got nothing")
}
}
func TestConfig_LoadFile(t *testing.T) {
var (
fpath string
err error
cfg *Config
)
if fpath, err = getTempFile(); err != nil {
t.Fatalf("Cannot create temporary file: %s", err.Error())
}
defer os.Remove(fpath)
for _, v := range t_toml {
if err = setTempConf(fpath, v.Conf); err != nil {
t.Fatalf("Cannot write to temporary file: %s", err.Error())
}
if cfg, err = NewConfig(fpath); err != nil {
t.Fatalf("Expected to open file %s, but got error: %s", fpath, err.Error())
}
if err = cfg.Parse(); err != nil {
t.Errorf("Unexpected error %s", err.Error())
}
if dsn := cfg.Database.DSN(); v.Valid != dsn {
t.Errorf("Expected %s, but got %s", v.Valid, dsn)
}
os.Truncate(fpath, 0)
}
}
func Test_GetEmptyScoreInterval(t *testing.T) {
var (
cfg = &Config{}
interval int = 60
)
if v := cfg.GetScoreInterval(); v != interval {
t.Errorf("Expected score interval %d, but got %d", interval, v)
}
}
func Test_GetEmptyScoreLinit(t *testing.T) {
var (
cfg = &Config{}
limit float64 = 0.22
)
if v := cfg.GetScoreLimit(); v != limit {
t.Errorf("Expected score interval %f, but got %f", limit, v)
}
}
func Test_GetScoreInterval(t *testing.T) {
var (
fpath string
err error
cfg *Config
)
if fpath, err = getTempFile(); err != nil {
t.Fatalf("Cannot create temporary file: %s", err.Error())
}
defer os.Remove(fpath)
for _, v := range t_toml {
if err = setTempConf(fpath, v.Conf); err != nil {
t.Fatalf("Cannot write to temporary file: %s", err.Error())
}
if cfg, err = NewConfig(fpath); err != nil {
t.Fatalf("Expected to open file %s, but got error: %s", fpath, err.Error())
}
if err = cfg.Parse(); err != nil {
t.Errorf("Unexpected error %s", err.Error())
}
if interval := cfg.GetScoreInterval(); v.ValidInterval != interval {
t.Errorf("Expected score interval %d, but got %d", v.ValidInterval, interval)
}
os.Truncate(fpath, 0)
}
}
func Test_GetScoreLimit(t *testing.T) {
var (
fpath string
err error
cfg *Config
)
if fpath, err = getTempFile(); err != nil {
t.Fatalf("Cannot create temporary file: %s", err.Error())
}
defer os.Remove(fpath)
for _, v := range t_toml {
if err = setTempConf(fpath, v.Conf); err != nil {
t.Fatalf("Cannot write to temporary file: %s", err.Error())
}
if cfg, err = NewConfig(fpath); err != nil {
t.Fatalf("Expected to open file %s, but got error: %s", fpath, err.Error())
}
if err = cfg.Parse(); err != nil {
t.Errorf("Unexpected error %s", err.Error())
}
if limit := cfg.GetScoreLimit(); v.ValidLimit != limit {
t.Errorf("Expected score interval %f, but got %f", v.ValidLimit, limit)
}
os.Truncate(fpath, 0)
}
}