-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconf_json_test.go
159 lines (135 loc) · 4.34 KB
/
conf_json_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
package conf
import (
"encoding/json"
"io/ioutil"
"os"
"testing"
)
const (
testJSONTmpConfPath = "/tmp/nxs-go-conf_test_json.conf"
testJSONValString = "Test String"
testJSONValString1 = "Test String1"
testJSONValString2 = "Test String2"
testJSONValString3 = "Test String3"
testJSONValInt = 123
testJSONValMapKey1 = "map_key1"
testJSONValMapKey2 = "map_key2"
testJSONValMapKey3 = "map_key3"
testJSONValStringEnvVar = "TEST_JSON_CONF_STRING"
)
type tConfJSONIn struct {
StringTest string `json:"string_test,omitempty"`
IntTest int `json:"int_test,omitempty"`
StructsTest StructJSONTest `json:"struct_test,omitempty"`
StructsSliceTest []StructJSONTest `json:"struct_slice_test,omitempty"`
StructsMapTest map[string]StructJSONTest `json:"struct_map_test,omitempty"`
StringsSliceTest []string `json:"strings_slice_test"`
}
type StructJSONTest struct {
StringTest string `json:"string_test,omitempty"`
}
func TestJSONFormat(t *testing.T) {
type tConfOut struct {
StringTest string `conf:"string_test" conf_extraopts:"required"`
IntTest int `conf:"int_test" conf_extraopts:"default=18"`
StructsTest struct {
StringTest string `conf:"string_test" conf_extraopts:"required"`
} `conf:"struct_test" conf_extraopts:"required"`
StructsSliceTest []struct {
StringTest string `conf:"string_test" conf_extraopts:"default=Test String"`
} `conf:"struct_slice_test" conf_extraopts:"required"`
StructsMapTest map[string]struct {
StringTest string `conf:"string_test" conf_extraopts:"default=Test String"`
} `conf:"struct_map_test" conf_extraopts:"required"`
StringsSliceTest []string `conf:"strings_slice_test"`
}
var c tConfOut
// Prepare test config file and fill it with testing data
testPrepareJSONConfig(t)
if err := Load(&c, Settings{
ConfPath: testJSONTmpConfPath,
ConfType: ConfigTypeJSON,
WeaklyTypes: false,
UnknownDeny: true,
}); err != nil {
t.Fatal("Config load error:", err)
}
// Remove test config file
os.Remove(testJSONTmpConfPath)
// Check loaded data
// Check specified string data
if c.StringTest != testJSONValString {
t.Fatal("Incorrect loaded data: StringTest")
}
// Check default int value
if c.IntTest != testJSONValInt {
t.Fatal("Incorrect loaded data: IntTest")
}
// Check substruct field
if c.StructsTest.StringTest != testJSONValString {
t.Fatal("Incorrect loaded data: StructsTest.StringTest")
}
// Check substructs slice size
if len(c.StructsSliceTest) != 3 {
t.Fatal("Incorrect loaded data: StructsSliceTest")
}
// Check substruct map string field
if c.StructsMapTest[testJSONValMapKey1].StringTest != testJSONValString1 {
t.Fatal("Incorrect loaded data: StructsMapTest[map_key1].StringTest")
}
// Check substruct map string field ENV data
if c.StructsMapTest[testJSONValMapKey2].StringTest != testJSONValString2 {
t.Fatal("Incorrect loaded data: StructsMapTest[map_key2].StringTest")
}
// Check substruct map string field default data
if c.StructsMapTest[testJSONValMapKey3].StringTest != testJSONValString {
t.Fatal("Incorrect loaded data: StructsMapTest[map_key3].StringTest")
}
// Check string slice size
if len(c.StringsSliceTest) != 3 {
t.Fatal("Incorrect loaded data: StringsSliceTest")
}
}
func testPrepareJSONConfig(t *testing.T) {
c := tConfJSONIn{
StringTest: testJSONValString,
IntTest: testJSONValInt,
StructsTest: StructJSONTest{
StringTest: testJSONValString,
},
StructsSliceTest: []StructJSONTest{
{
StringTest: testJSONValString1,
},
{
StringTest: testJSONValString2,
},
{
StringTest: testJSONValString3,
},
},
StructsMapTest: map[string]StructJSONTest{
testJSONValMapKey1: StructJSONTest{
StringTest: testJSONValString1,
},
testJSONValMapKey2: StructJSONTest{
StringTest: "ENV:" + testJSONValStringEnvVar,
},
testJSONValMapKey3: StructJSONTest{},
},
StringsSliceTest: []string{
testJSONValString1,
testJSONValString2,
testJSONValString3,
},
}
s, err := json.Marshal(&c)
if err != nil {
t.Fatal("Json encode error:", err)
}
if err := ioutil.WriteFile(testJSONTmpConfPath, s, 0644); err != nil {
t.Fatal("Config file prepare error:", err)
}
// Set ENV variables
os.Setenv(testJSONValStringEnvVar, testJSONValString2)
}