config updated to improve setting values, writing changes and test coverage
This commit is contained in:
1 parent
8f0e72821f
commit
169c6c6b64
8 files changed
+453
-59
No files matched your search
@@ -455,3 +455,70 @@ func TestLoadFromViperInvalidValues(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDatabaseSection(t *testing.T) {
|
||||
c := JobSearchConfigDatabase{File: "./jobsearch.db"}
|
||||
|
||||
if got, want := c.Keys(), []string{"file"}; !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("Keys = %v; want %v", got, want)
|
||||
}
|
||||
if got, want := c.Map(), map[string]any{"file": "./jobsearch.db"}; !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("Map = %v; want %v", got, want)
|
||||
}
|
||||
want := map[string]any{"database.file": "./jobsearch.db"}
|
||||
if got := c.Flatten(); !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("Flatten = %v; want %v", got, want)
|
||||
}
|
||||
if k, err := c.TypeOf("file"); err != nil || k != reflect.String {
|
||||
t.Errorf("TypeOf(file) = %v, %v", k, err)
|
||||
}
|
||||
if v, err := c.ValueOf("file"); err != nil || v != "./jobsearch.db" {
|
||||
t.Errorf("ValueOf(file) = %v, %v", v, err)
|
||||
}
|
||||
if _, err := c.TypeOf("nope"); err == nil {
|
||||
t.Error("TypeOf(nope) succeeded")
|
||||
}
|
||||
if _, err := c.ValueOf("nope"); err == nil {
|
||||
t.Error("ValueOf(nope) succeeded")
|
||||
}
|
||||
if err := c.SetValue("file", "/var/lib/js.db"); err != nil || c.File != "/var/lib/js.db" {
|
||||
t.Errorf("SetValue(file) = %v, File = %q", err, c.File)
|
||||
}
|
||||
if err := c.SetValue("file", 42); err == nil {
|
||||
t.Error("SetValue(file, 42) succeeded")
|
||||
}
|
||||
|
||||
// The root configuration reaches it as database.file.
|
||||
conf := defaultConfig(t)
|
||||
if err := conf.SetValue("database.file", "./other.db"); err != nil ||
|
||||
conf.Database.File != "./other.db" {
|
||||
t.Errorf("SetValue(database.file) = %v, File = %q", err, conf.Database.File)
|
||||
}
|
||||
}
|
||||
|
||||
func TestViperValue(t *testing.T) {
|
||||
resetViper(t)
|
||||
viper.Set("s", "text")
|
||||
viper.Set("i", "42") // strings are converted
|
||||
viper.Set("b", "true")
|
||||
|
||||
for _, tt := range []struct {
|
||||
key string
|
||||
kind reflect.Kind
|
||||
want any
|
||||
}{
|
||||
{"s", reflect.String, "text"},
|
||||
{"i", reflect.Int, 42},
|
||||
{"b", reflect.Bool, true},
|
||||
} {
|
||||
got, ok := viperValue(tt.key, tt.kind)
|
||||
if !ok || got != tt.want {
|
||||
t.Errorf("viperValue(%q, %v) = %#v, %v; want %#v", tt.key, tt.kind, got, ok, tt.want)
|
||||
}
|
||||
}
|
||||
for _, kind := range []reflect.Kind{reflect.Float64, reflect.Int8, reflect.Slice} {
|
||||
if got, ok := viperValue("i", kind); ok {
|
||||
t.Errorf("viperValue(i, %v) = %#v, true; want unhandled", kind, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user