-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigurator_test.go
More file actions
292 lines (251 loc) · 7.63 KB
/
configurator_test.go
File metadata and controls
292 lines (251 loc) · 7.63 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
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
package config
import (
"os"
"strings"
"testing"
)
// Setup dummy config object. Good example of how to do in a real project
type TestConfig struct {
DB_HOST string
DB_PORT int
PROD bool
COOLDOWN float64
}
var config_struct = &TestConfig{
DB_HOST: "host",
DB_PORT: 1,
PROD: true,
COOLDOWN: 0.5,
}
var config_iface Configurator = config_struct
func (c TestConfig) GetEnvString(field string) string {
//in real project would be config.GetEnvString(...)
return GetEnvString(c, field)
}
func (c TestConfig) GetEnvBool(field string) bool {
//in real project would be config.GetEnvBool(...)
return GetEnvBool(c, field)
}
func (c TestConfig) GetEnvInt(field string) int {
//in real project would be config.GetEnvInt(...)
return GetEnvInt(c, field)
}
func (c TestConfig) GetEnvFloat(field string) float64 {
//in real project would be config.GetEnvFloat(...)
return GetEnvFloat(c, field)
}
// End setup
func TestReadValues(t *testing.T) {
host := getString(config_iface, "DB_HOST")
port := getInt(config_iface, "DB_PORT")
prod := getBool(config_iface, "PROD")
cooldown := getFloat(config_iface, "COOLDOWN")
if host != config_struct.DB_HOST {
t.Fatalf("Getting string value failed. Got '%v'. want '%v'", host, config_struct.DB_HOST)
}
if port != config_struct.DB_PORT {
t.Fatalf("Getting int value failed. Got '%v'. want '%v'", port, config_struct.DB_PORT)
}
if prod != config_struct.PROD {
t.Fatalf("Getting bool value failed. Got '%v'. want '%v'", port, config_struct.PROD)
}
if cooldown != config_struct.COOLDOWN {
t.Fatalf("Getting float value failed. Got '%v'. want '%v'", port, config_struct.COOLDOWN)
}
}
func TestEnvOverridesConfig(t *testing.T) {
os.Setenv("DB_HOST", "newhost")
os.Setenv("DB_PORT", "2")
os.Setenv("PROD", "false")
os.Setenv("COOLDOWN", "0.6")
host := config_iface.GetEnvString("DB_HOST")
port := config_iface.GetEnvInt("DB_PORT")
prod := config_iface.GetEnvBool("PROD")
cooldown := config_iface.GetEnvFloat("COOLDOWN")
if host != "newhost" {
t.Fatalf("Getting string value failed. Got '%v'. want '%v'", host, "newhost")
}
if port != 2 {
t.Fatalf("Getting int value failed. Got '%v'. want '%v'", port, 2)
}
if prod != false {
t.Fatalf("Getting bool value failed. Got '%v'. want '%v'", prod, false)
}
if cooldown != 0.6 {
t.Fatalf("Getting float value failed. Got '%v'. want '%v'", cooldown, 0.6)
}
}
func TestConfigFallbackToEnv(t *testing.T) {
os.Unsetenv("DB_HOST")
os.Unsetenv("DB_PORT")
os.Unsetenv("PROD")
os.Unsetenv("COOLDOWN")
host := config_iface.GetEnvString("DB_HOST")
port := config_iface.GetEnvInt("DB_PORT")
prod := config_iface.GetEnvBool("PROD")
cooldown := config_iface.GetEnvFloat("COOLDOWN")
if host != config_struct.DB_HOST {
t.Fatalf("Getting string value failed. Got '%v'. want '%v'", host, config_struct.DB_HOST)
}
if port != config_struct.DB_PORT {
t.Fatalf("Getting int value failed. Got '%v'. want '%v'", port, config_struct.DB_PORT)
}
if prod != config_struct.PROD {
t.Fatalf("Getting bool value failed. Got '%v'. want '%v'", port, config_struct.DB_PORT)
}
if cooldown != config_struct.COOLDOWN {
t.Fatalf("Getting float value failed. Got '%v'. want '%v'", port, config_struct.COOLDOWN)
}
}
func TestWrongIntFieldCausesPanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Reading non existant field did not cause panic")
}
}()
value := getInt(config_iface, "DOES_NOT_EXIST")
use(value)
}
func TestWrongStringFieldCausesPanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Reading non existant field did not cause panic")
}
}()
value := getString(config_iface, "DOES_NOT_EXIST")
use(value)
}
func TestWrongBoolFieldCausesPanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Reading non existant field did not cause panic")
}
}()
value := getBool(config_iface, "DOES_NOT_EXIST")
use(value)
}
func TestWrongFloatFieldCausesPanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Reading non existant field did not cause panic")
}
}()
value := getFloat(config_iface, "DOES_NOT_EXIST")
use(value)
}
func TestBoolWithVariousEnvStrings(t *testing.T) {
falses := []string{
"0", "f", "F", "FALSE", "false", "False",
}
trues := []string{
"1", "t", "T", "TRUE", "true", "True",
}
config_struct.PROD = true
for _, elem := range falses {
os.Setenv("PROD", elem)
if config_iface.GetEnvBool("PROD") {
t.Fatalf("setting boolean env to '%v' should be interpreted bool as false", elem)
}
}
config_struct.PROD = false
for _, elem := range trues {
os.Setenv("PROD", elem)
if !config_iface.GetEnvBool("PROD") {
t.Fatalf("setting boolean env to '%v' should be interpreted bool as true", elem)
}
}
}
func TestBoolWithBadEnvStrings(t *testing.T) {
invalid_bool_strings := []string{
"off", "on", "yes", "no", "y", "n", "nope", "nada", "anything", "",
}
config_struct.PROD = true
for _, elem := range invalid_bool_strings {
os.Setenv("PROD", elem)
if !config_iface.GetEnvBool("PROD") {
t.Fatalf("setting boolean env to '%v' should fallback to default value", elem)
}
}
config_struct.PROD = false
for _, elem := range invalid_bool_strings {
os.Setenv("PROD", elem)
if config_iface.GetEnvBool("PROD") {
t.Fatalf("setting boolean env to '%v' should fallback to default value", elem)
}
}
}
func TestPanicMessageIncludesFieldName(t *testing.T) {
panicCheck := func(f func()) {
field_name := "DOES_NOT_EXIST"
defer func() {
r := recover()
if r == nil {
t.Errorf("Reading non existant field did not cause panic")
} else {
switch v := r.(type) {
case string:
if !strings.Contains(v, field_name) {
t.Errorf("Panic msg should contain field name '%s'. Got '%v'", field_name, r)
}
case error:
if !strings.Contains(v.Error(), "DOES_NOT_EXIST") {
t.Errorf("Panic msg should contain field name '%s'. Got '%v'", field_name, r)
}
default:
t.Errorf("Panic's type is unkown. Cannot check error msg. Type is '%T'", r)
}
}
}()
f()
}
panicCheck(func() { config_iface.GetEnvInt("DOES_NOT_EXIST") })
panicCheck(func() { config_iface.GetEnvFloat("DOES_NOT_EXIST") })
panicCheck(func() { config_iface.GetEnvString("DOES_NOT_EXIST") })
panicCheck(func() { config_iface.GetEnvBool("DOES_NOT_EXIST") })
}
func TestPanicOnExistingFieldWrongType(t *testing.T) {
var field_name, field_type string
panicCheck := func(f func()) {
defer func() {
r := recover()
if r == nil {
t.Errorf("Reading field using wrong type did not cause panic")
} else {
switch v := r.(type) {
case string:
if !strings.Contains(v, field_name) {
t.Errorf("Panic msg should contain field name '%s'. Got '%v'", field_name, r)
}
if !strings.Contains(v, field_type) {
t.Errorf("Panic msg should contain field type. Got '%v'", r)
}
case error:
if !strings.Contains(v.Error(), "DOES_NOT_EXIST") {
t.Errorf("Panic msg should contain field name '%s'. Got '%v'", field_name, r)
}
if !strings.Contains(v.Error(), field_type) {
t.Errorf("Panic msg should contain field type. Got '%v'", r)
}
default:
t.Errorf("Panic's type is unkown. Cannot check error msg. Type is '%T'", r)
}
}
}()
f()
}
field_name = "DB_HOST"
field_type = "int"
panicCheck(func() { config_iface.GetEnvInt("DB_HOST") })
field_type = "float"
panicCheck(func() { config_iface.GetEnvFloat("DB_HOST") })
field_type = "bool"
panicCheck(func() { config_iface.GetEnvBool("DB_HOST") })
field_name = "DB_PORT"
field_type = "string"
panicCheck(func() { config_iface.GetEnvString("DB_PORT") })
}
func use(vals ...any) {
for _, val := range vals {
_ = val
}
}