From 717ccd82be34e96bd820861dd7361558fd099cf4 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Wed, 16 Dec 2020 00:06:46 +0300 Subject: [PATCH] add config tests Signed-off-by: Vasiliy Tolstov --- config/default_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 config/default_test.go diff --git a/config/default_test.go b/config/default_test.go new file mode 100644 index 0000000..35b4d4b --- /dev/null +++ b/config/default_test.go @@ -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) +}