c3b007a4e7
- ConfigError y ConfigValidation como tipos producto con sus .md en types/infra/ - config_validate: validacion con tags required/format/min/max/oneof via reflection - config_merge: merge no-mutante de map[string]string con precedencia de override - config_dump: serializacion de structs a map con mascara *** para campos secret - 17 tests unitarios, todos PASS Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
68 lines
1.6 KiB
Go
68 lines
1.6 KiB
Go
package infra
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestConfigDump(t *testing.T) {
|
|
t.Run("campos normales se incluyen como string", func(t *testing.T) {
|
|
type Cfg struct {
|
|
Host string
|
|
Port int
|
|
}
|
|
got := ConfigDump(Cfg{Host: "localhost", Port: 8080})
|
|
if got["Host"] != "localhost" {
|
|
t.Errorf("expected Host=localhost, got %q", got["Host"])
|
|
}
|
|
if got["Port"] != "8080" {
|
|
t.Errorf("expected Port=8080, got %q", got["Port"])
|
|
}
|
|
})
|
|
|
|
t.Run("campos secret aparecen como tres asteriscos", func(t *testing.T) {
|
|
type Cfg struct {
|
|
APIKey string `secret:"true"`
|
|
}
|
|
got := ConfigDump(Cfg{APIKey: "super-secret-key"})
|
|
if got["APIKey"] != "***" {
|
|
t.Errorf("expected ***, got %q", got["APIKey"])
|
|
}
|
|
})
|
|
|
|
t.Run("campos no exportados no aparecen", func(t *testing.T) {
|
|
type Cfg struct {
|
|
Public string
|
|
private string //nolint:structcheck
|
|
}
|
|
_ = Cfg{} // ensure it compiles
|
|
got := ConfigDump(Cfg{Public: "yes"})
|
|
if _, ok := got["private"]; ok {
|
|
t.Error("private field should not be in dump")
|
|
}
|
|
})
|
|
|
|
t.Run("puntero a struct funciona", func(t *testing.T) {
|
|
type Cfg struct {
|
|
Name string
|
|
}
|
|
got := ConfigDump(&Cfg{Name: "test"})
|
|
if got["Name"] != "test" {
|
|
t.Errorf("expected Name=test, got %q", got["Name"])
|
|
}
|
|
})
|
|
|
|
t.Run("struct con bool y float se formatean correctamente", func(t *testing.T) {
|
|
type Cfg struct {
|
|
Debug bool
|
|
Timeout float64
|
|
}
|
|
got := ConfigDump(Cfg{Debug: true, Timeout: 3.14})
|
|
if got["Debug"] != "true" {
|
|
t.Errorf("expected Debug=true, got %q", got["Debug"])
|
|
}
|
|
if got["Timeout"] != "3.14" {
|
|
t.Errorf("expected Timeout=3.14, got %q", got["Timeout"])
|
|
}
|
|
})
|
|
}
|