83 lines
2.0 KiB
Go
83 lines
2.0 KiB
Go
package test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
cfg "scm.yoorie.de/go-lib/micro/config"
|
|
)
|
|
|
|
type MyConfig struct {
|
|
Value string `default:"Bar" yaml:"value"`
|
|
Value1 bool `default:"false" yaml:"value1"`
|
|
Value2 int `default:"8080" yaml:"value2"`
|
|
}
|
|
|
|
func (config *MyConfig) appName() string {
|
|
return "myconfig"
|
|
}
|
|
|
|
func TestIsStructPointerStruct(t *testing.T) {
|
|
assert.False(t, cfg.IsStructPointer(MyConfig{}))
|
|
}
|
|
func TestIsStructPointerString(t *testing.T) {
|
|
assert.False(t, cfg.IsStructPointer("Bar"))
|
|
}
|
|
|
|
func TestIsStructPointerSimpleArray(t *testing.T) {
|
|
assert.False(t, cfg.IsStructPointer([2]int{292, 2}))
|
|
}
|
|
|
|
func TestIsStructPointerInt(t *testing.T) {
|
|
assert.False(t, cfg.IsStructPointer(12))
|
|
}
|
|
|
|
func TestIsStructPointerBool(t *testing.T) {
|
|
assert.False(t, cfg.IsStructPointer(false))
|
|
}
|
|
|
|
func TestIsStructPointerStructPointer(t *testing.T) {
|
|
assert.True(t, cfg.IsStructPointer(&MyConfig{}))
|
|
}
|
|
|
|
func TestIsStructPointerStructPointerArray(t *testing.T) {
|
|
cfg1 := &MyConfig{
|
|
Value: "Foo",
|
|
}
|
|
cfg2 := &MyConfig{
|
|
Value: "Bar",
|
|
}
|
|
array := [2]*MyConfig{cfg1, cfg2}
|
|
assert.False(t, cfg.IsStructPointer(array))
|
|
}
|
|
|
|
func TestLoadConfigurationFromFileWithNoPointerInvalidType(t *testing.T) {
|
|
myConfig := MyConfig{}
|
|
err := cfg.LoadConfigurationFromFile(myConfig, "testdata/config.yml")
|
|
assert.NotNil(t, err)
|
|
}
|
|
|
|
func TestLoadConfigurationFromFile(t *testing.T) {
|
|
myConfig := &MyConfig{}
|
|
assert.Equal(t, "myconfig", myConfig.appName())
|
|
cfg.LoadConfigurationFromFile(myConfig, "testdata/config.yml")
|
|
assert.Equal(t, "Foo", myConfig.Value)
|
|
assert.Equal(t, true, myConfig.Value1)
|
|
assert.Equal(t, 8080, myConfig.Value2)
|
|
}
|
|
func TestLoadConfigurationFromFileNotExist(t *testing.T) {
|
|
myConfig := &MyConfig{}
|
|
assert.Equal(t, "myconfig", myConfig.appName())
|
|
err := cfg.LoadConfigurationFromFile(myConfig, "testdata/config1.yml")
|
|
assert.NotNil(t, err)
|
|
}
|
|
|
|
func TestGetConfigurationFiles(t *testing.T) {
|
|
fileNames := cfg.GetConfigurationFiles("myapp")
|
|
assert.Equal(t, 4, len(fileNames))
|
|
|
|
for _, fileName := range fileNames {
|
|
t.Log(fileName)
|
|
}
|
|
}
|