-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnvViper_test.go
116 lines (105 loc) · 2.82 KB
/
EnvViper_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
package envviper
import (
"github.com/stretchr/testify/suite"
"os"
"strings"
"testing"
)
const (
DbUriPgsql = "pgsql:https://test:test@localhost:5432/test?sslmode=disabled"
DbUriMongo = "mongo:https://test:test@localhost:27017/test?authMechanism=scram-sha-1"
DbUriNats = "nats:https://localhost:4222"
DbUriPgsql2 = "pgsql:https://test:test@localhost:5433/test"
)
type Configuration struct {
Http HttpConfig
Db map[string]DBConf
}
type HttpConfig struct {
Port string
Timeout int
}
type DBConf struct {
Uri string
Maxlifetime int
}
type viperTestSuite struct {
suite.Suite
}
func TestViper(t *testing.T) {
suite.Run(t, new(viperTestSuite))
}
func (s *viperTestSuite) SetupSuite() {
_ = os.Setenv("VIPER_DB_PGSQL_URI", DbUriPgsql)
_ = os.Setenv("VIPER_DB_NATS_URI", DbUriNats)
_ = os.Setenv("VIPER_DB_MONGO_URI", DbUriMongo)
_ = os.Setenv("VIPER_DB_PGSQL2_URI", DbUriPgsql2)
}
func (s *viperTestSuite) TestUnmarshal() {
vp := NewEnvViper()
vp.SetConfigType("json")
vp.SetConfigFile("config.json")
err := vp.ReadInConfig()
s.Nil(err)
vp.SetEnvParamsSimple("viper")
var cfg Configuration
err = vp.Unmarshal(&cfg)
s.Nil(err)
s.Equal("8080", cfg.Http.Port)
s.Equal(30, cfg.Http.Timeout)
s.Contains(cfg.Db, "pgsql")
s.Contains(cfg.Db, "nats")
s.Contains(cfg.Db, "mongo")
s.Contains(cfg.Db, "pgsql2")
s.Contains(cfg.Db, "mysql")
s.Equal(60, cfg.Db["pgsql"].Maxlifetime)
s.Equal(0, cfg.Db["pgsql2"].Maxlifetime)
s.Equal(5, cfg.Db["nats"].Maxlifetime)
s.Equal(5, cfg.Db["mongo"].Maxlifetime)
s.Equal(10, cfg.Db["mysql"].Maxlifetime)
s.Equal(DbUriPgsql, cfg.Db["pgsql"].Uri)
s.Equal(DbUriPgsql2, cfg.Db["pgsql2"].Uri)
s.Equal(DbUriNats, cfg.Db["nats"].Uri)
s.Equal(DbUriMongo, cfg.Db["mongo"].Uri)
s.Equal("", cfg.Db["mysql"].Uri)
}
func (s *viperTestSuite) TestPanics() {
vp := NewEnvViper()
s.Panics(func() {
vp.AutomaticEnv()
})
s.Panics(func() {
vp.SetEnvPrefix("viper")
})
s.Panics(func() {
vp.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
})
}
func (s *viperTestSuite) TestUnmarshalGlobal() {
vp := DefEnvViper()
vp.SetConfigType("json")
vp.SetConfigFile("config.json")
err := vp.ReadInConfig()
s.Nil(err)
vp.SetEnvParamsSimple("viper")
var cfg Configuration
err = vp.Unmarshal(&cfg)
s.Nil(err)
s.Equal("8080", cfg.Http.Port)
s.Equal(30, cfg.Http.Timeout)
s.Contains(cfg.Db, "pgsql")
s.Contains(cfg.Db, "nats")
s.Contains(cfg.Db, "mongo")
s.Contains(cfg.Db, "pgsql2")
s.Contains(cfg.Db, "mysql")
s.Equal(60, cfg.Db["pgsql"].Maxlifetime)
s.Equal(0, cfg.Db["pgsql2"].Maxlifetime)
s.Equal(5, cfg.Db["nats"].Maxlifetime)
s.Equal(5, cfg.Db["mongo"].Maxlifetime)
s.Equal(10, cfg.Db["mysql"].Maxlifetime)
s.Equal(DbUriPgsql, cfg.Db["pgsql"].Uri)
s.Equal(DbUriPgsql2, cfg.Db["pgsql2"].Uri)
s.Equal(DbUriNats, cfg.Db["nats"].Uri)
s.Equal(DbUriMongo, cfg.Db["mongo"].Uri)
s.Equal("", cfg.Db["mysql"].Uri)
}