-
Notifications
You must be signed in to change notification settings - Fork 28
/
gonfig_test.go
307 lines (258 loc) · 6.12 KB
/
gonfig_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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package gonfig
import (
"io"
"io/ioutil"
"os"
"strings"
"testing"
)
func Test_GetFromYAML_Filename_Empty_Should_Not_Panic(t *testing.T) {
type Conf struct {
}
conf := Conf{}
err := getFromYAML("", &conf)
if err != nil {
t.Error("getFromYAML should not panic", err)
}
}
func tmpFileWithContent(content string, t *testing.T) string {
file, err := ioutil.TempFile("", "gonfig_test_data_")
if err != nil {
t.Error("Error creating file with test data", err)
}
_, err = io.Copy(file, strings.NewReader("{}"))
if err != nil {
t.Error("Error writing test data", err)
}
return file.Name()
}
func Test_GetFromYAML_Filename_Should_Not_be_Panic(t *testing.T) {
filename := tmpFileWithContent("{}", t)
defer os.Remove(filename)
type Conf struct {
}
conf := Conf{}
err := getFromYAML(filename, &conf)
if err != nil {
t.Error("getFromYAML file not found", err)
}
}
func Test_getFromEnvVariables_should_not_panic_if_wrong_data(t *testing.T) {
type Conf struct {
Id int
}
os.Setenv("Id", "abc")
conf := Conf{}
getFromEnvVariables(&conf)
if conf.Id != 0 {
t.Error("Id should be 0", conf.Id)
}
}
func Test_getFromEnvVariables_should_find_and_parse_int(t *testing.T) {
type Conf struct {
Id int
}
os.Setenv("Id", "123")
conf := Conf{}
getFromEnvVariables(&conf)
if conf.Id != 123 {
t.Error("Id should be 123", conf.Id)
}
}
func Test_getFromEnvVariables_should_find_and_parse_int16(t *testing.T) {
type Conf struct {
Id int16
}
os.Setenv("Id", "123")
conf := Conf{}
getFromEnvVariables(&conf)
if conf.Id != 123 {
t.Error("Id should be 123", conf.Id)
}
}
func Test_getFromEnvVariables_should_find_and_parse_int32(t *testing.T) {
type Conf struct {
Id int32
}
os.Setenv("Id", "123")
conf := Conf{}
getFromEnvVariables(&conf)
if conf.Id != 123 {
t.Error("Id should be 123", conf.Id)
}
}
func Test_getFromEnvVariables_should_find_and_parse_int64(t *testing.T) {
type Conf struct {
Id int64
}
os.Setenv("Id", "123")
conf := Conf{}
getFromEnvVariables(&conf)
if conf.Id != 123 {
t.Error("Id should be 123", conf.Id)
}
}
func Test_getFromEnvVariables_should_find_and_parse_uint(t *testing.T) {
type Conf struct {
Id uint
}
os.Setenv("Id", "123")
conf := Conf{}
getFromEnvVariables(&conf)
if conf.Id != 123 {
t.Error("Id should be 123", conf.Id)
}
}
func Test_getFromEnvVariables_should_find_and_parse_uint16(t *testing.T) {
type Conf struct {
Id uint16
}
os.Setenv("Id", "123")
conf := Conf{}
getFromEnvVariables(&conf)
if conf.Id != 123 {
t.Error("Id should be 123", conf.Id)
}
}
func Test_getFromEnvVariables_should_find_and_parse_uint32(t *testing.T) {
type Conf struct {
Id uint32
}
os.Setenv("Id", "123")
conf := Conf{}
getFromEnvVariables(&conf)
if conf.Id != 123 {
t.Error("Id should be 123", conf.Id)
}
}
func Test_getFromEnvVariables_should_find_and_parse_uint64(t *testing.T) {
type Conf struct {
Id uint64
}
os.Setenv("Id", "123")
conf := Conf{}
getFromEnvVariables(&conf)
if conf.Id != 123 {
t.Error("Id should be 123", conf.Id)
}
}
func Test_getFromEnvVariables_should_find_and_parse_bool(t *testing.T) {
type Conf struct {
Id bool
}
os.Setenv("Id", "true")
conf := Conf{}
getFromEnvVariables(&conf)
if conf.Id != true {
t.Error("Id should be true", conf.Id)
}
}
func Test_getFromEnvVariables_should_find_and_parse_float32(t *testing.T) {
type Conf struct {
Id float32
}
os.Setenv("Id", "123.123")
conf := Conf{}
getFromEnvVariables(&conf)
if conf.Id != 123.123 {
t.Error("Id should be 123.123", conf.Id)
}
}
func Test_getFromEnvVariables_should_find_and_parse_float64(t *testing.T) {
type Conf struct {
Id float64
}
os.Setenv("Id", "123.123")
conf := Conf{}
getFromEnvVariables(&conf)
if conf.Id != 123.123 {
t.Error("Id should be 123.123", conf.Id)
}
}
func Test_getFromEnvVariables_should_find_and_parse_string(t *testing.T) {
type Conf struct {
Id string
}
os.Setenv("Id", "abc")
conf := Conf{}
getFromEnvVariables(&conf)
if conf.Id != "abc" {
t.Error("Id should be abc", conf.Id)
}
}
func Test_getFromCustomEnvVariables_should_find_and_parse_string(t *testing.T) {
type Conf struct {
Id string `env:"CONF_ID"`
}
os.Setenv("CONF_ID", "abc")
conf := Conf{}
getFromEnvVariables(&conf)
if conf.Id != "abc" {
t.Error("Id should be abc", conf.Id)
}
}
func Test_getFromCustomEnvVariables_should_find_and_parse_object(t *testing.T) {
type SubConf struct {
ID string
Number int
}
type Conf struct {
Subconf SubConf `env:"SUB_CONF"`
}
os.Setenv("SUB_CONF", "{\"ID\":\"abc\", \"Number\": 123}")
conf := Conf{}
getFromEnvVariables(&conf)
if conf.Subconf.ID != "abc" {
t.Errorf("ID should be abc %s", conf.Subconf.ID)
}
if conf.Subconf.Number != 123 {
t.Errorf("Number should be 123 %d", conf.Subconf.Number)
}
}
func Test_getFromCustomEnvVariables_should_find_and_parse_JSONObjectArray(t *testing.T) {
type SubConf struct {
ID string
Number int
}
type Conf struct {
Subconfs []SubConf `env:"SUB_CONFS"`
}
os.Setenv("SUB_CONFS", "[{\"ID\":\"abc\", \"Number\": 123},{\"ID\":\"def\", \"Number\": 456}]")
conf := Conf{}
getFromEnvVariables(&conf)
if len(conf.Subconfs) != 2 {
t.Errorf("Conf should have 2 Subconfs items %d", len(conf.Subconfs))
}
if conf.Subconfs[0].ID != "abc" {
t.Errorf("ID should be abc %s", conf.Subconfs[0].ID)
}
if conf.Subconfs[0].Number != 123 {
t.Errorf("Number should be 123 %d", conf.Subconfs[0].Number)
}
if conf.Subconfs[1].ID != "def" {
t.Errorf("ID should be def %s", conf.Subconfs[1].ID)
}
if conf.Subconfs[1].Number != 456 {
t.Errorf("Number should be 456 %d", conf.Subconfs[1].Number)
}
}
func Test_getFromCustomEnvVariables_should_find_and_parse_JSONArray(t *testing.T) {
type Conf struct {
Values []int `env:"SUB_VALUES"`
}
os.Setenv("SUB_VALUES", "[1,2,3]")
conf := Conf{}
getFromEnvVariables(&conf)
if len(conf.Values) != 3 {
t.Errorf("Conf should have 3 items %d", len(conf.Values))
}
if conf.Values[0] != 1 {
t.Errorf("Values[0] should be 1 %d", conf.Values[0])
}
if conf.Values[1] != 2 {
t.Errorf("Values[1] should be 2 %d", conf.Values[1])
}
if conf.Values[2] != 3 {
t.Errorf("Values[2] should be 3 %d", conf.Values[2])
}
}