package test import ( "testing" . "github.com/smartystreets/goconvey/convey" 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 TestIsStructPointer(t *testing.T) { Convey("IsStructPointer", t, func() { Convey("returns false for struct value", func() { So(cfg.IsStructPointer(MyConfig{}), ShouldBeFalse) }) Convey("returns false for string", func() { So(cfg.IsStructPointer("Bar"), ShouldBeFalse) }) Convey("returns false for simple array", func() { So(cfg.IsStructPointer([2]int{292, 2}), ShouldBeFalse) }) Convey("returns false for int", func() { So(cfg.IsStructPointer(12), ShouldBeFalse) }) Convey("returns false for bool", func() { So(cfg.IsStructPointer(false), ShouldBeFalse) }) Convey("returns true for struct pointer", func() { So(cfg.IsStructPointer(&MyConfig{}), ShouldBeTrue) }) Convey("returns false for struct pointer array", func() { cfg1 := &MyConfig{Value: "Foo"} cfg2 := &MyConfig{Value: "Bar"} array := [2]*MyConfig{cfg1, cfg2} So(cfg.IsStructPointer(array), ShouldBeFalse) }) }) } func TestLoadConfigurationFromFile(t *testing.T) { Convey("LoadConfigurationFromFile", t, func() { Convey("returns error for non-pointer type", func() { err := cfg.LoadConfigurationFromFile(MyConfig{}, "testdata/config.yml") So(err, ShouldNotBeNil) }) Convey("loads config from file successfully", func() { myConfig := &MyConfig{} err := cfg.LoadConfigurationFromFile(myConfig, "testdata/config.yml") So(err, ShouldBeNil) So(myConfig.Value, ShouldEqual, "Foo") So(myConfig.Value1, ShouldBeTrue) So(myConfig.Value2, ShouldEqual, 8080) }) Convey("returns error when config file does not exist", func() { myConfig := &MyConfig{} err := cfg.LoadConfigurationFromFile(myConfig, "testdata/config1.yml") So(err, ShouldNotBeNil) }) }) } func TestGetConfigurationFiles(t *testing.T) { Convey("GetConfigurationFiles returns 4 candidate paths", t, func() { fileNames := cfg.GetConfigurationFiles("myapp") So(len(fileNames), ShouldEqual, 4) }) } func TestGetConfigurationFile(t *testing.T) { Convey("GetConfigurationFile", t, func() { Convey("returns empty string when no matching config file exists", func() { result := cfg.GetConfigurationFile("nonexistentapp_xyz_123") So(result, ShouldEqual, "") }) }) } func TestLoadConfiguration(t *testing.T) { Convey("LoadConfiguration", t, func() { Convey("returns error for non-pointer type", func() { err := cfg.LoadConfiguration(MyConfig{}) So(err, ShouldNotBeNil) }) Convey("returns nil for struct pointer with no config file found", func() { myConfig := &MyConfig{} err := cfg.LoadConfiguration(myConfig) So(err, ShouldBeNil) So(myConfig.Value, ShouldEqual, "Bar") }) }) }