configurator/configurator_test.go

114 lines
2.2 KiB
Go

package configurator
import (
"os"
"testing"
"git.belvedersky.ru/common/configurator/providers"
"git.belvedersky.ru/common/configurator/providers/file"
)
type TestConfig struct {
TestString string
TestBool bool
TestInt int
}
var (
FileConfig = providers.FileConfig{
FileName: "test",
}
defaultConfig = TestConfig{
TestString: "abcde",
TestBool: true,
TestInt: 123,
}
updateConfig = TestConfig{
TestString: "fghij",
TestBool: false,
TestInt: 456,
}
)
func init() {
file.WriteToFile("default_test", defaultConfig)
file.WriteToFile("test", defaultConfig)
cfg, err := New(providers.FILE, FileConfig, &defaultConfig)
if err != nil {
panic(err)
}
cfg.providerConfig = providers.FileConfig{
FileName: "default_test",
}
cfg.Update(defaultConfig)
}
// Тест создания конфигуратора
func TestNew(t *testing.T) {
// like test.yml
cfg, err := New(providers.FILE, FileConfig, &defaultConfig)
if err != nil {
t.Error(err)
}
// t.Log(cfg.Config)
config := cfg.Config.(*TestConfig)
if *config != defaultConfig {
t.Errorf("New().Config = %v, want %v", config, &defaultConfig)
}
}
// Тест изменения конфигуратора
func TestUpdate(t *testing.T) {
cfg, err := New(providers.FILE, FileConfig, TestConfig{})
if err != nil {
t.Error(err)
}
_, err = cfg.Update(updateConfig)
if err != nil {
t.Error(err)
}
if cfg.Config.(TestConfig) != updateConfig {
t.Errorf("Update().Config = %v, want %v", cfg.Config, updateConfig)
}
}
func TestGet(t *testing.T) {
want := updateConfig.TestString
cfg, err := New(providers.FILE, FileConfig, TestConfig{})
if err != nil {
t.Error(err)
}
testString, err := cfg.Get("TestString")
if err != nil {
t.Error(err)
}
if testString.(string) != want {
t.Errorf("cfg.Get(TestString) = %v, want %v", testString, want)
}
}
func TestSet(t *testing.T) {
cfg, err := New(providers.FILE, FileConfig, TestConfig{})
if err != nil {
t.Error(err)
}
cfg.Set("TestString", "test123")
_, err = cfg.Update(nil)
if err != nil {
t.Error(err)
}
if err := os.Remove("./default_test.yml"); err != nil {
t.Error(err)
}
if err := os.Remove("./test.yml"); err != nil {
t.Error(err)
}
}