add config tests

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-12-16 00:06:46 +03:00
parent 0b3005f71e
commit 717ccd82be

40
config/default_test.go Normal file
View File

@ -0,0 +1,40 @@
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)
}