-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadConfig_test.go
More file actions
126 lines (112 loc) · 3.1 KB
/
loadConfig_test.go
File metadata and controls
126 lines (112 loc) · 3.1 KB
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
package loadConfig_test
import (
"fmt"
"testing"
"github.com/dan0505/loadConfig"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
func TestLoadConfig(t *testing.T) {
v := viper.New()
v.AddConfigPath(".")
v.SetConfigName("test")
v.SetConfigType("json")
if err := v.ReadInConfig(); err != nil {
panic(fmt.Errorf("Error while reading config file %s", err))
}
t.Run("panic if cfg is not set", func(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Errorf("The code did not panic")
}
assert.Equal(t, r, "tag not set for TestString")
}()
type panicStruct struct {
TestString string
}
var testStruct panicStruct
loadConfig.LoadConfig(&testStruct, v)
t.Error("should panic")
})
t.Run("ignore if set -", func(t *testing.T) {
type stringStruct struct {
TestString string `cfg:"-"`
}
var testStruct stringStruct
loadConfig.LoadConfig(&testStruct, v)
assert.Equal(t, testStruct.TestString, "")
})
t.Run("load string", func(t *testing.T) {
type stringStruct struct {
TestString string `cfg:"load_string"`
}
var testStruct stringStruct
loadConfig.LoadConfig(&testStruct, v)
assert.Equal(t, testStruct.TestString, "test string")
})
t.Run("panic if no value", func(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Errorf("The code did not panic")
}
assert.Equal(t, r, "can't get config for fake_entry")
}()
type panicStruct struct {
TestString string `cfg:"fake_entry"`
}
var testStruct panicStruct
loadConfig.LoadConfig(&testStruct, v)
t.Error("should panic")
})
t.Run("load int", func(t *testing.T) {
type intStruct struct {
TestString int `cfg:"load_int"`
}
var testStruct intStruct
loadConfig.LoadConfig(&testStruct, v)
assert.Equal(t, testStruct.TestString, 100)
})
t.Run("load struct", func(t *testing.T) {
type structStruct struct {
TestStruct struct {
TestString string `cfg:"load_string"`
TestInt int `cfg:"load_int"`
} `cfg:"struct"`
}
var testStruct structStruct
loadConfig.LoadConfig(&testStruct, v)
assert.Equal(t, "test string 2", testStruct.TestStruct.TestString)
assert.Equal(t, 200, testStruct.TestStruct.TestInt)
})
t.Run("should ignore - struct", func(t *testing.T) {
type RandomInterface interface{}
type RandomStruct struct{}
type structStruct struct {
RandomPtr *RandomInterface `cfg:"-"`
AnotherPtr *structStruct `cfg:"-"`
AnotherStruct RandomStruct `cfg:"-"`
}
var testStruct structStruct
loadConfig.LoadConfig(&testStruct, v)
})
}
func TestLoadConfigWithEnvEntry(t *testing.T) {
v := viper.New()
v.AddConfigPath(".")
v.SetConfigName("test")
v.SetConfigType("json")
if err := v.ReadInConfig(); err != nil {
panic(fmt.Errorf("Error while reading config file %s", err))
}
t.Run("load with entry name", func(t *testing.T) {
type structStruct struct {
TestString string `cfg:"load_string"`
TestInt int `cfg:"load_int"`
}
var testStruct structStruct
loadConfig.LoadConfigWithEnvEntry(&testStruct, v, "struct")
assert.EqualValues(t, structStruct{"test string 2", 200}, testStruct)
})
}