-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenv_test.go
187 lines (149 loc) · 4.75 KB
/
env_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
package goat
import (
"strings"
"testing"
"time"
"github.com/icrowley/fake"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
func Test_EnvString_Set(t *testing.T) {
setupEnvTest()
assertSet[string](t, EnvString, makeEnvKey(), "value", "default")
}
func Test_EnvString_NotSet(t *testing.T) {
setupEnvTest()
assertNotSet[string](t, EnvString, makeEnvKey(), "default")
}
func Test_EnvBool_Set(t *testing.T) {
setupEnvTest()
assertSet[bool](t, EnvBool, makeEnvKey(), false, true)
}
func Test_EnvBool_NotSet(t *testing.T) {
setupEnvTest()
assertNotSet[bool](t, EnvBool, makeEnvKey(), true)
}
func Test_EnvInt_Set(t *testing.T) {
setupEnvTest()
assertSet[int](t, EnvInt, makeEnvKey(), 1, 6)
}
func Test_EnvInt_NotSet(t *testing.T) {
setupEnvTest()
assertNotSet[int](t, EnvInt, makeEnvKey(), 6)
}
func Test_EnvInt32_Set(t *testing.T) {
setupEnvTest()
assertSet[int32](t, EnvInt32, makeEnvKey(), 1, 6)
}
func Test_EnvInt32_NotSet(t *testing.T) {
setupEnvTest()
assertNotSet[int32](t, EnvInt32, makeEnvKey(), 6)
}
func Test_EnvInt64_Set(t *testing.T) {
setupEnvTest()
assertSet[int64](t, EnvInt64, makeEnvKey(), 1, 6)
}
func Test_EnvInt64_NotSet(t *testing.T) {
setupEnvTest()
assertNotSet[int64](t, EnvInt64, makeEnvKey(), 6)
}
func Test_EnvUint16_Set(t *testing.T) {
setupEnvTest()
assertSet[uint16](t, EnvUint16, makeEnvKey(), 1, 6)
}
func Test_EnvUint16_NotSet(t *testing.T) {
setupEnvTest()
assertNotSet[uint16](t, EnvUint16, makeEnvKey(), 6)
}
func Test_EnvUint32_Set(t *testing.T) {
setupEnvTest()
assertSet[uint32](t, EnvUint32, makeEnvKey(), 1, 6)
}
func Test_EnvUint32_NotSet(t *testing.T) {
setupEnvTest()
assertNotSet[uint32](t, EnvUint32, makeEnvKey(), 6)
}
func Test_EnvUint64_Set(t *testing.T) {
setupEnvTest()
assertSet[uint64](t, EnvUint64, makeEnvKey(), 1, 6)
}
func Test_EnvUint64_NotSet(t *testing.T) {
setupEnvTest()
assertNotSet[uint64](t, EnvUint64, makeEnvKey(), 6)
}
func Test_EnvFloat64_Set(t *testing.T) {
setupEnvTest()
assertSet[float64](t, EnvFloat64, makeEnvKey(), 2.3, 7.8)
}
func Test_EnvFloat64_NotSet(t *testing.T) {
setupEnvTest()
assertNotSet[float64](t, EnvFloat64, makeEnvKey(), 7.8)
}
func Test_EnvTime_Set(t *testing.T) {
setupEnvTest()
assertSet[time.Time](t, EnvTime, makeEnvKey(), time.Now().AddDate(1, 0, 0), time.Now())
}
func Test_EnvTime_NotSet(t *testing.T) {
setupEnvTest()
assertNotSet[time.Time](t, EnvTime, makeEnvKey(), time.Now())
}
func Test_EnvDuration_Set(t *testing.T) {
setupEnvTest()
interval := time.Now().AddDate(1, 0, 0)
assertSet[time.Duration](t, EnvDuration, makeEnvKey(), time.Since(time.Now().AddDate(-1, 0, 0)), time.Now().Sub(interval))
}
func Test_EnvDuration_NotSet(t *testing.T) {
setupEnvTest()
interval := time.Now().AddDate(1, 0, 0)
assertNotSet[time.Duration](t, EnvDuration, makeEnvKey(), time.Now().Sub(interval))
}
func Test_EnvIntSlice_Set(t *testing.T) {
setupEnvTest()
assertSet[[]int](t, EnvIntSlice, makeEnvKey(), []int{1, 2, 3}, []int{5, 6, 7})
}
func Test_EnvIntSlice_NotSet(t *testing.T) {
setupEnvTest()
assertNotSet[[]int](t, EnvIntSlice, makeEnvKey(), []int{5, 6, 7})
}
func Test_EnvStringSlice_Set(t *testing.T) {
setupEnvTest()
assertSet[[]string](t, EnvStringSlice, makeEnvKey(), []string{"a", "b", "c"}, []string{"x", "y", "z"})
}
func Test_EnvStringSlice_NotSet(t *testing.T) {
setupEnvTest()
assertNotSet[[]string](t, EnvStringSlice, makeEnvKey(), []string{"x", "y", "z"})
}
func Test_EnvStringMap_Set(t *testing.T) {
setupEnvTest()
assertSet[map[string]any](t, EnvStringMap, makeEnvKey(), map[string]any{"a": 1, "b": "", "c": false}, map[string]any{"x": 0, "y": "qwerty", "z": []string{"asdf"}})
}
func Test_EnvStringMap_NotSet(t *testing.T) {
setupEnvTest()
assertNotSet[map[string]any](t, EnvStringMap, makeEnvKey(), map[string]any{"x": 0, "y": "qwerty", "z": []string{"asdf"}})
}
func Test_EnvStringMapString_Set(t *testing.T) {
setupEnvTest()
assertSet[map[string]string](t, EnvStringMapString, makeEnvKey(), map[string]string{"a": "1", "b": "2", "c": "3"}, map[string]string{"x": "4", "y": "5", "z": "6"})
}
func Test_EnvStringMapString_NotSet(t *testing.T) {
setupEnvTest()
assertNotSet[map[string]string](t, EnvStringMapString, makeEnvKey(), map[string]string{"x": "4", "y": "5", "z": "6"})
}
// Helpers
func setupEnvTest() {
viper.Reset()
viper.AutomaticEnv()
}
func makeEnvKey() string {
return strings.ToUpper(fake.Characters())
}
type envReader[T any] func(key string, defaultValue T) T
func assertSet[T any](t *testing.T, subject envReader[T], key string, value, defaultValue T) {
t.Helper()
viper.Set(key, value)
assert.Equal(t, value, subject(key, defaultValue))
}
func assertNotSet[T any](t *testing.T, subject envReader[T], key string, defaultValue T) {
t.Helper()
assert.Equal(t, defaultValue, subject(key, defaultValue))
}