micro-tests/config/default_test.go
Vasiliy Tolstov 717ccd82be add config tests
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2020-12-16 00:06:46 +03:00

41 lines
910 B
Go

package config
import (
"os"
"testing"
envconfig "github.com/unistack-org/micro-config-env"
"github.com/unistack-org/micro/v3"
"github.com/unistack-org/micro/v3/config"
)
type Config struct {
String string `env:"MICRO_TEST" default:"default"`
}
func TestMultiple(t *testing.T) {
cfg := &Config{}
svc := micro.NewService(micro.Configs(
config.NewConfig(config.Struct(cfg)),
envconfig.NewConfig(config.Struct(cfg)),
),
)
if err := svc.Init(); err != nil {
t.Fatal(err)
}
if cfg.String != "default" {
t.Fatalf("config not parsed by default source: %#+v\n", cfg)
}
os.Setenv("MICRO_TEST", "non_default")
if err := svc.Init(); err != nil {
t.Fatal(err)
}
if cfg.String == "default" {
t.Fatalf("config not parsed by default source: %#+v\n", cfg)
}
if cfg.String != "non_default" {
t.Fatalf("config not parsed by default source: %#+v\n", cfg)
}
t.Logf("config: %#+v\n", cfg)
}