-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathloader_test.go
71 lines (56 loc) · 1.62 KB
/
loader_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
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package config
import (
"bytes"
"testing"
"github.com/stretchr/testify/require"
)
type testAppConfig struct {
Server struct {
Address string
}
}
func (c *testAppConfig) SetProviderDefaults(dp DataProvider) {
dp.SetDefault("server.addr", ":80")
}
func (c *testAppConfig) Set(dp DataProvider) error {
var err error
c.Server.Address, err = dp.GetString("server.addr")
return err
}
type testPersonConfig struct {
Name string
}
func (c *testPersonConfig) KeyPrefix() string {
return "person"
}
func (c *testPersonConfig) SetProviderDefaults(_ DataProvider) {}
func (c *testPersonConfig) Set(dp DataProvider) error {
var err error
c.Name, err = dp.GetString("name")
return err
}
func TestLoader_LoadFromReader(t *testing.T) {
cfgLoader := NewLoader(NewViperAdapter())
t.Run("load config, use defaults", func(t *testing.T) {
appCfg := &testAppConfig{}
err := cfgLoader.LoadFromReader(bytes.NewBufferString(`{}`), DataTypeJSON, appCfg)
require.NoError(t, err)
require.Equal(t, ":80", appCfg.Server.Address)
})
t.Run("load config", func(t *testing.T) {
appCfg := &testAppConfig{}
err := cfgLoader.LoadFromReader(bytes.NewBufferString(`{"server":{"addr":":777"}}`), DataTypeJSON, appCfg)
require.NoError(t, err)
require.Equal(t, ":777", appCfg.Server.Address)
})
t.Run("load config, use key prefix", func(t *testing.T) {
personCfg := &testPersonConfig{}
err := cfgLoader.LoadFromReader(bytes.NewBufferString(testPersonConfigJSON), DataTypeJSON, personCfg)
require.NoError(t, err)
require.Equal(t, "Steve", personCfg.Name)
})
}