forked from voi-oss/svc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
51 lines (43 loc) · 1.35 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
package svc
import (
"os"
"reflect"
"strings"
"testing"
"github.com/caarlos0/env/v6"
"github.com/stretchr/testify/require"
)
func TestLoadFromEnv(t *testing.T) {
err := LoadFromEnv(map[string]string{})
require.Error(t, err)
require.Equal(t, env.ErrNotAStructPtr, err)
test := struct {
StrVal string `env:"strVal"`
IntVal int `env:"intVal"`
EmptyRequiredVal string `env:"emptyVal" validate:"required"`
}{}
err = os.Setenv("strVal", "testStrVal")
require.NoError(t, err)
err = os.Setenv("intVal", "123")
require.NoError(t, err)
err = LoadFromEnv(&test)
require.Error(t, err)
require.Equal(t, "Key: 'EmptyRequiredVal' Error:Field validation for 'EmptyRequiredVal' failed on the 'required' tag", err.Error())
require.Equal(t, "testStrVal", test.StrVal)
require.Equal(t, 123, test.IntVal)
}
func TestLoadFromEnvWithParsers(t *testing.T) {
test := struct {
MapVal map[string]string `env:"mapVal" envDefault:""`
}{}
err := os.Setenv("mapVal", "testKey:testVal")
require.NoError(t, err)
err = LoadFromEnvWithParsers(&test, map[reflect.Type]env.ParserFunc{
reflect.TypeOf(map[string]string{}): func(v string) (interface{}, error) {
items := strings.Split(v, ":")
return map[string]string{items[0]: items[1]}, nil
},
})
require.NoError(t, err)
require.Equal(t, map[string]string{"testKey": "testVal"}, test.MapVal)
}