forked from heetch/confita
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
228 lines (194 loc) · 5.22 KB
/
config_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
package confita_test
import (
"context"
"encoding/json"
"math"
"strconv"
"testing"
"time"
"github.com/heetch/confita"
"github.com/heetch/confita/backend"
"github.com/stretchr/testify/require"
)
type store map[string]string
func (s store) Get(ctx context.Context, key string) ([]byte, error) {
data, ok := s[key]
if !ok {
return nil, backend.ErrNotFound
}
return []byte(data), nil
}
type longRunningStore time.Duration
func (s longRunningStore) Get(ctx context.Context, key string) ([]byte, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(time.Duration(s)):
return []byte(time.Now().String()), nil
}
}
type valueUnmarshaler store
func (k valueUnmarshaler) Get(ctx context.Context, key string) ([]byte, error) {
return store(k).Get(ctx, key)
}
func (k valueUnmarshaler) UnmarshalValue(ctx context.Context, key string, to interface{}) error {
data, err := store(k).Get(ctx, key)
if err != nil {
return err
}
return json.Unmarshal(data, to)
}
func TestLoad(t *testing.T) {
type nested struct {
Int int `config:"int"`
String string `config:"string"`
}
type testStruct struct {
Bool bool `config:"bool"`
Int int `config:"int"`
Int8 int8 `config:"int8"`
Int16 int16 `config:"int16"`
Int32 int32 `config:"int32"`
Int64 int64 `config:"int64"`
Uint uint `config:"uint"`
Uint8 uint8 `config:"uint8"`
Uint16 uint16 `config:"uint16"`
Uint32 uint32 `config:"uint32"`
Uint64 uint64 `config:"uint64"`
Float32 float32 `config:"float32"`
Float64 float64 `config:"float64"`
Ptr *string `config:"ptr"`
String string `config:"string"`
Duration time.Duration `config:"duration"`
Struct nested
StructPtrNil *nested
StructPtrNotNil *nested
Ignored string
}
var s testStruct
s.StructPtrNotNil = new(nested)
boolStore := store{
"bool": "true",
}
intStore := store{
"int": strconv.FormatInt(math.MaxInt64, 10),
"int8": strconv.FormatInt(math.MaxInt8, 10),
"int16": strconv.FormatInt(math.MaxInt16, 10),
"int32": strconv.FormatInt(math.MaxInt32, 10),
"int64": strconv.FormatInt(math.MaxInt64, 10),
}
uintStore := store{
"uint": strconv.FormatUint(math.MaxUint64, 10),
"uint8": strconv.FormatUint(math.MaxUint8, 10),
"uint16": strconv.FormatUint(math.MaxUint16, 10),
"uint32": strconv.FormatUint(math.MaxUint32, 10),
"uint64": strconv.FormatUint(math.MaxUint64, 10),
}
floatStore := store{
"float32": strconv.FormatFloat(math.MaxFloat32, 'f', 6, 32),
"float64": strconv.FormatFloat(math.MaxFloat64, 'f', 6, 64),
}
otherStore := store{
"ptr": "ptr",
"string": "string",
"duration": "10s",
}
loader := confita.NewLoader(
boolStore,
intStore,
uintStore,
floatStore,
otherStore)
err := loader.Load(context.Background(), &s)
require.NoError(t, err)
ptr := "ptr"
require.EqualValues(t, testStruct{
Bool: true,
Int: math.MaxInt64,
Int8: math.MaxInt8,
Int16: math.MaxInt16,
Int32: math.MaxInt32,
Int64: math.MaxInt64,
Uint: math.MaxUint64,
Uint8: math.MaxUint8,
Uint16: math.MaxUint16,
Uint32: math.MaxUint32,
Uint64: math.MaxUint64,
Float32: math.MaxFloat32,
Float64: math.MaxFloat64,
Ptr: &ptr,
String: "string",
Duration: 10 * time.Second,
Struct: nested{
Int: math.MaxInt64,
String: "string",
},
StructPtrNotNil: &nested{
Int: math.MaxInt64,
String: "string",
},
}, s)
}
func TestLoadRequired(t *testing.T) {
s := struct {
Name string `config:"name,required"`
}{}
st := make(store)
err := confita.NewLoader(st).Load(context.Background(), &s)
require.Error(t, err)
}
func TestLoadIgnored(t *testing.T) {
s := struct {
Name string `config:"-"`
Age int `config:"age"`
}{}
st := store{
"name": "name",
"age": "10",
}
err := confita.NewLoader(st).Load(context.Background(), &s)
require.NoError(t, err)
require.Equal(t, 10, s.Age)
require.Zero(t, s.Name)
}
func TestLoadContextCancel(t *testing.T) {
s := struct {
Name string `config:"-"`
Age int `config:"age"`
}{}
st := store{
"name": "name",
"age": "10",
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := confita.NewLoader(st).Load(ctx, &s)
require.Equal(t, context.Canceled, err)
}
func TestLoadContextTimeout(t *testing.T) {
s := struct {
Name string `config:"-"`
Age int `config:"age"`
}{}
st := longRunningStore(10 * time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
err := confita.NewLoader(st).Load(ctx, &s)
require.Equal(t, context.DeadlineExceeded, err)
}
func TestLoadFromValueUnmarshaler(t *testing.T) {
s := struct {
Name string `config:"name"`
Age int `config:"age"`
Ignored string `config:"-"`
}{}
st := valueUnmarshaler{
"name": `"name"`,
"age": "10",
}
err := confita.NewLoader(st).Load(context.Background(), &s)
require.NoError(t, err)
require.Equal(t, "name", s.Name)
require.Equal(t, 10, s.Age)
require.Zero(t, s.Ignored)
}